LibreOffice Module chart2 (master) 1
ChartDropTargetHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include <DataSource.hxx>
22#include <DataSourceHelper.hxx>
23#include <ChartModel.hxx>
24#include <Diagram.hxx>
25
26#include <com/sun/star/chart2/data/XDataProvider.hpp>
27
28#include <sot/formats.hxx>
29#include <utility>
30#include <vector>
31
32using namespace ::com::sun::star;
33
34using ::com::sun::star::uno::Reference;
35using ::com::sun::star::uno::Sequence;
36
37namespace
38{
39
40std::vector< OUString > lcl_getStringsFromByteSequence(
41 const Sequence< sal_Int8 > & aByteSequence )
42{
43 std::vector< OUString > aResult;
44 const sal_Int32 nLength = aByteSequence.getLength();
45 const char * pBytes( reinterpret_cast< const char* >( aByteSequence.getConstArray()));
46 sal_Int32 nStartPos = 0;
47 for( sal_Int32 nPos=0; nPos<nLength; ++nPos )
48 {
49 if( pBytes[nPos] == '\0' )
50 {
51 aResult.emplace_back( pBytes + nStartPos, (nPos - nStartPos), RTL_TEXTENCODING_ASCII_US );
52 nStartPos = nPos + 1;
53 }
54 }
55 return aResult;
56}
57
58} // anonymous namespace
59
60namespace chart
61{
62
64 const Reference< datatransfer::dnd::XDropTarget >& rxDropTarget,
66 DropTargetHelper( rxDropTarget ),
67 m_xChartDocument(std::move( xChartDocument ))
68{}
69
70ChartDropTargetHelper::~ChartDropTargetHelper()
71{}
72
73bool ChartDropTargetHelper::satisfiesPrerequisites() const
74{
75 return ( m_xChartDocument.is() &&
76 ! m_xChartDocument->hasInternalDataProvider());
77}
78
79sal_Int8 ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent& rEvt )
80{
81 sal_Int8 nResult = DND_ACTION_NONE;
82
83 if( ( rEvt.mnAction == DND_ACTION_COPY ||
84 rEvt.mnAction == DND_ACTION_MOVE ) &&
85 satisfiesPrerequisites() &&
86 IsDropFormatSupported( SotClipboardFormatId::LINK ) )
87 {
88 // @todo: check if the data is suitable. Is this possible without XTransferable?
89 nResult = rEvt.mnAction;
90 }
91
92 return nResult;
93}
94
95sal_Int8 ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent& rEvt )
96{
97 sal_Int8 nResult = DND_ACTION_NONE;
98
99 if( ( rEvt.mnAction == DND_ACTION_COPY ||
100 rEvt.mnAction == DND_ACTION_MOVE ) &&
101 rEvt.maDropEvent.Transferable.is() &&
102 satisfiesPrerequisites())
103 {
104 TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
105 if( aDataHelper.HasFormat( SotClipboardFormatId::LINK ))
106 {
107 Sequence<sal_Int8> aBytes = aDataHelper.GetSequence(SotClipboardFormatId::LINK, OUString());
108 if (aBytes.hasElements())
109 {
110 std::vector< OUString > aStrings( lcl_getStringsFromByteSequence( aBytes ));
111 if( aStrings.size() >= 3 && aStrings[0] == "soffice" )
112 {
113 OUString aRangeString( aStrings[2] );
114 if( m_xChartDocument.is())
115 {
116 Reference< frame::XModel > xParentModel( m_xChartDocument->getParent(), uno::UNO_QUERY );
117 if( xParentModel.is() &&
118 m_xChartDocument.is())
119 {
120 // @todo: get the title somehow and compare it to
121 // aDocName if successful (the document is the
122 // parent)
123 rtl::Reference< Diagram > xDiagram = m_xChartDocument->getFirstChartDiagram();
124 Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
125 if( xDataProvider.is() && xDiagram.is() &&
126 DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument ))
127 {
128 rtl::Reference< DataSource > xDataSource1 =
129 DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument );
131 xDataProvider->detectArguments( xDataSource1 ));
132
133 OUString aOldRange;
134 beans::PropertyValue * pCellRange = nullptr;
135 for( sal_Int32 i=0; i<aArguments.getLength(); ++i )
136 {
137 if ( aArguments[i].Name == "CellRangeRepresentation" )
138 {
139 pCellRange = (aArguments.getArray() + i);
140 aArguments[i].Value >>= aOldRange;
141 break;
142 }
143 }
144 if( pCellRange )
145 {
146 // copy means add ranges, move means replace
147 if( rEvt.mnAction == DND_ACTION_COPY )
148 {
149 // @todo: using implicit knowledge that ranges can be
150 // merged with ";". This should be done more general
151 pCellRange->Value <<= aOldRange + ";" + aRangeString;
152 }
153 // move means replace range
154 else
155 {
156 pCellRange->Value <<= aRangeString;
157 }
158
160 xDataProvider->createDataSource( aArguments );
161 xDiagram->setDiagramData( xDataSource2, aArguments );
162
163 // always return copy state to avoid deletion of the dragged range
164 nResult = DND_ACTION_COPY;
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172 }
173 return nResult;
174}
175
176} // namespace chart
177
178/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nPos
css::uno::Sequence< sal_Int8 > GetSequence(SotClipboardFormatId nFormat, const OUString &rDestDoc) const
bool HasFormat(SotClipboardFormatId nFormat) const
Sequence< PropertyValue > aArguments
int i
sal_Int8 mnAction
const css::datatransfer::dnd::DropTargetDropEvent maDropEvent
#define DND_ACTION_MOVE
#define DND_ACTION_COPY
#define DND_ACTION_NONE
OUString Name
signed char sal_Int8
sal_Int32 nLength