Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / main / ChartDropTargetHelper.cxx
blob5205d9620753171f544e4c4e0da6b4e25eddda98
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ChartDropTargetHelper.hxx"
21 #include <DiagramHelper.hxx>
22 #include <DataSourceHelper.hxx>
24 #include <com/sun/star/chart2/XChartDocument.hpp>
25 #include <com/sun/star/chart2/data/XDataProvider.hpp>
26 #include <com/sun/star/container/XChild.hpp>
28 #include <sot/formats.hxx>
29 #include <vector>
31 using namespace ::com::sun::star;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::Sequence;
36 namespace
39 std::vector< OUString > lcl_getStringsFromByteSequence(
40 const Sequence< sal_Int8 > & aByteSequence )
42 std::vector< OUString > aResult;
43 const sal_Int32 nLength = aByteSequence.getLength();
44 const sal_Char * pBytes( reinterpret_cast< const sal_Char* >( aByteSequence.getConstArray()));
45 sal_Int32 nStartPos = 0;
46 for( sal_Int32 nPos=0; nPos<nLength; ++nPos )
48 if( pBytes[nPos] == '\0' )
50 aResult.emplace_back( pBytes + nStartPos, (nPos - nStartPos), RTL_TEXTENCODING_ASCII_US );
51 nStartPos = nPos + 1;
54 return aResult;
57 } // anonymous namespace
59 namespace chart
62 ChartDropTargetHelper::ChartDropTargetHelper(
63 const Reference< datatransfer::dnd::XDropTarget >& rxDropTarget,
64 const Reference< chart2::XChartDocument > & xChartDocument ) :
65 DropTargetHelper( rxDropTarget ),
66 m_xChartDocument( xChartDocument )
69 ChartDropTargetHelper::~ChartDropTargetHelper()
72 bool ChartDropTargetHelper::satisfiesPrerequisites() const
74 return ( m_xChartDocument.is() &&
75 ! m_xChartDocument->hasInternalDataProvider());
78 sal_Int8 ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent& rEvt )
80 sal_Int8 nResult = DND_ACTION_NONE;
82 if( ( rEvt.mnAction == DND_ACTION_COPY ||
83 rEvt.mnAction == DND_ACTION_MOVE ) &&
84 satisfiesPrerequisites() &&
85 IsDropFormatSupported( SotClipboardFormatId::LINK ) )
87 // @todo: check if the data is suitable. Is this possible without XTransferable?
88 nResult = rEvt.mnAction;
91 return nResult;
94 sal_Int8 ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent& rEvt )
96 sal_Int8 nResult = DND_ACTION_NONE;
98 if( ( rEvt.mnAction == DND_ACTION_COPY ||
99 rEvt.mnAction == DND_ACTION_MOVE ) &&
100 rEvt.maDropEvent.Transferable.is() &&
101 satisfiesPrerequisites())
103 TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
104 if( aDataHelper.HasFormat( SotClipboardFormatId::LINK ))
106 Sequence<sal_Int8> aBytes = aDataHelper.GetSequence(SotClipboardFormatId::LINK, OUString());
107 if (aBytes.getLength())
109 std::vector< OUString > aStrings( lcl_getStringsFromByteSequence( aBytes ));
110 if( aStrings.size() >= 3 && aStrings[0] == "soffice" )
112 OUString aRangeString( aStrings[2] );
113 Reference< container::XChild > xChild( m_xChartDocument, uno::UNO_QUERY );
114 if( xChild.is())
116 Reference< frame::XModel > xParentModel( xChild->getParent(), uno::UNO_QUERY );
117 if( xParentModel.is() &&
118 m_xChartDocument.is())
120 // @todo: get the title somehow and compare it to
121 // aDocName if successful (the document is the
122 // parent)
123 Reference< chart2::XDiagram > xDiagram( m_xChartDocument->getFirstDiagram() );
124 Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
125 if( xDataProvider.is() && xDiagram.is() &&
126 DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument ))
128 Reference< chart2::data::XDataSource > xDataSource(
129 DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument ));
130 Sequence< beans::PropertyValue > aArguments(
131 xDataProvider->detectArguments( xDataSource ));
133 OUString aOldRange;
134 beans::PropertyValue * pCellRange = nullptr;
135 for( sal_Int32 i=0; i<aArguments.getLength(); ++i )
137 if ( aArguments[i].Name == "CellRangeRepresentation" )
139 pCellRange = (aArguments.getArray() + i);
140 aArguments[i].Value >>= aOldRange;
141 break;
144 if( pCellRange )
146 // copy means add ranges, move means replace
147 if( rEvt.mnAction == DND_ACTION_COPY )
149 // @todo: using implicit knowledge that ranges can be
150 // merged with ";". This should be done more general
151 pCellRange->Value <<= (aOldRange + ";" + aRangeString );
153 // move means replace range
154 else
156 pCellRange->Value <<= aRangeString;
159 xDataSource.set( xDataProvider->createDataSource( aArguments ));
160 xDiagram->setDiagramData( xDataSource, aArguments );
162 // always return copy state to avoid deletion of the dragged range
163 nResult = DND_ACTION_COPY;
172 return nResult;
175 } // namespace chart
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */