Line data Source code
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 :
20 :
21 : #include "ChartDropTargetHelper.hxx"
22 : #include "DiagramHelper.hxx"
23 : #include "DataSourceHelper.hxx"
24 :
25 : #include <com/sun/star/chart2/XChartDocument.hpp>
26 : #include <com/sun/star/chart2/data/XDataProvider.hpp>
27 : #include <com/sun/star/container/XChild.hpp>
28 :
29 : #include <sot/formats.hxx>
30 : #include <vector>
31 :
32 : using namespace ::com::sun::star;
33 :
34 : using ::com::sun::star::uno::Reference;
35 : using ::com::sun::star::uno::Sequence;
36 : using ::rtl::OUString;
37 :
38 : namespace
39 : {
40 :
41 0 : ::std::vector< OUString > lcl_getStringsFromByteSequence(
42 : const Sequence< sal_Int8 > & aByteSequence )
43 : {
44 0 : ::std::vector< OUString > aResult;
45 0 : const sal_Int32 nLength = aByteSequence.getLength();
46 0 : const sal_Char * pBytes( reinterpret_cast< const sal_Char* >( aByteSequence.getConstArray()));
47 0 : sal_Int32 nStartPos = 0;
48 0 : for( sal_Int32 nPos=0; nPos<nLength; ++nPos )
49 : {
50 0 : if( pBytes[nPos] == '\0' )
51 : {
52 0 : aResult.push_back( OUString( pBytes + nStartPos, (nPos - nStartPos), RTL_TEXTENCODING_ASCII_US ));
53 0 : nStartPos = nPos + 1;
54 : }
55 : }
56 0 : return aResult;
57 : }
58 :
59 : } // anonymous namespace
60 :
61 : namespace chart
62 : {
63 :
64 0 : ChartDropTargetHelper::ChartDropTargetHelper(
65 : const Reference< datatransfer::dnd::XDropTarget >& rxDropTarget,
66 : const Reference< chart2::XChartDocument > & xChartDocument ) :
67 : DropTargetHelper( rxDropTarget ),
68 0 : m_xChartDocument( xChartDocument )
69 0 : {}
70 :
71 0 : ChartDropTargetHelper::~ChartDropTargetHelper()
72 0 : {}
73 :
74 0 : bool ChartDropTargetHelper::satisfiesPrerequisites() const
75 : {
76 0 : return ( m_xChartDocument.is() &&
77 0 : ! m_xChartDocument->hasInternalDataProvider());
78 : }
79 :
80 0 : sal_Int8 ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent& rEvt )
81 : {
82 0 : sal_Int8 nResult = DND_ACTION_NONE;
83 :
84 0 : if( ( rEvt.mnAction == DND_ACTION_COPY ||
85 : rEvt.mnAction == DND_ACTION_MOVE ) &&
86 0 : satisfiesPrerequisites() &&
87 0 : IsDropFormatSupported( SOT_FORMATSTR_ID_LINK ) )
88 : {
89 : // @todo: check if the data is suitable. Is this possible without XTransferable?
90 0 : nResult = rEvt.mnAction;
91 : }
92 :
93 0 : return nResult;
94 : }
95 :
96 0 : sal_Int8 ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent& rEvt )
97 : {
98 0 : sal_Int8 nResult = DND_ACTION_NONE;
99 :
100 0 : if( ( rEvt.mnAction == DND_ACTION_COPY ||
101 : rEvt.mnAction == DND_ACTION_MOVE ) &&
102 0 : rEvt.maDropEvent.Transferable.is() &&
103 0 : satisfiesPrerequisites())
104 : {
105 0 : TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
106 0 : if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_LINK ))
107 : {
108 0 : Sequence< sal_Int8 > aBytes;
109 0 : if( aDataHelper.GetSequence( SOT_FORMATSTR_ID_LINK, aBytes ))
110 : {
111 0 : ::std::vector< OUString > aStrings( lcl_getStringsFromByteSequence( aBytes ));
112 0 : if( aStrings.size() >= 3 && aStrings[0] == "soffice" )
113 : {
114 0 : OUString aDocName( aStrings[1] );
115 0 : OUString aRangeString( aStrings[2] );
116 0 : Reference< container::XChild > xChild( m_xChartDocument, uno::UNO_QUERY );
117 0 : if( xChild.is())
118 : {
119 0 : Reference< frame::XModel > xParentModel( xChild->getParent(), uno::UNO_QUERY );
120 0 : if( xParentModel.is() &&
121 0 : m_xChartDocument.is())
122 : {
123 0 : bool bDataComesFromParent = true;
124 : // @todo: get the title somehow and compare it to
125 : // aDocName if successful (the document is the
126 : // parent)
127 0 : if( bDataComesFromParent )
128 : {
129 0 : Reference< chart2::XDiagram > xDiagram( m_xChartDocument->getFirstDiagram() );
130 0 : Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
131 0 : if( xDataProvider.is() && xDiagram.is() &&
132 0 : DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument ))
133 : {
134 : Reference< chart2::data::XDataSource > xDataSource(
135 0 : DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument ));
136 : Sequence< beans::PropertyValue > aArguments(
137 0 : xDataProvider->detectArguments( xDataSource ));
138 :
139 0 : OUString aOldRange;
140 0 : beans::PropertyValue * pCellRange = 0;
141 0 : for( sal_Int32 i=0; i<aArguments.getLength(); ++i )
142 : {
143 0 : if ( aArguments[i].Name == "CellRangeRepresentation" )
144 : {
145 0 : pCellRange = (aArguments.getArray() + i);
146 0 : aArguments[i].Value >>= aOldRange;
147 0 : break;
148 : }
149 : }
150 0 : if( pCellRange )
151 : {
152 : // copy means add ranges, move means replace
153 0 : if( rEvt.mnAction == DND_ACTION_COPY )
154 : {
155 : // @todo: using implcit knowledge that ranges can be
156 : // merged with ";". This should be done more general
157 0 : pCellRange->Value <<= (aOldRange + OUString( sal_Unicode(';')) + aRangeString );
158 : }
159 : // move means replace range
160 : else
161 : {
162 0 : pCellRange->Value <<= aRangeString;
163 : }
164 :
165 0 : xDataSource.set( xDataProvider->createDataSource( aArguments ));
166 0 : xDiagram->setDiagramData( xDataSource, aArguments );
167 :
168 : // always return copy state to avoid deletion of the dragged range
169 0 : nResult = DND_ACTION_COPY;
170 0 : }
171 0 : }
172 : }
173 0 : }
174 0 : }
175 0 : }
176 0 : }
177 0 : }
178 : }
179 0 : return nResult;
180 : }
181 :
182 : } // namespace chart
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|