Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / main / ChartDropTargetHelper.cxx
blob6ef4ff484539835a2ceca7690fbc816628c3c6a7
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 <DataSourceHelper.hxx>
23 #include <com/sun/star/chart2/XChartDocument.hpp>
24 #include <com/sun/star/chart2/data/XDataProvider.hpp>
25 #include <com/sun/star/container/XChild.hpp>
27 #include <sot/formats.hxx>
28 #include <vector>
30 using namespace ::com::sun::star;
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::Sequence;
35 namespace
38 std::vector< OUString > lcl_getStringsFromByteSequence(
39 const Sequence< sal_Int8 > & aByteSequence )
41 std::vector< OUString > aResult;
42 const sal_Int32 nLength = aByteSequence.getLength();
43 const sal_Char * pBytes( reinterpret_cast< const sal_Char* >( aByteSequence.getConstArray()));
44 sal_Int32 nStartPos = 0;
45 for( sal_Int32 nPos=0; nPos<nLength; ++nPos )
47 if( pBytes[nPos] == '\0' )
49 aResult.emplace_back( pBytes + nStartPos, (nPos - nStartPos), RTL_TEXTENCODING_ASCII_US );
50 nStartPos = nPos + 1;
53 return aResult;
56 } // anonymous namespace
58 namespace chart
61 ChartDropTargetHelper::ChartDropTargetHelper(
62 const Reference< datatransfer::dnd::XDropTarget >& rxDropTarget,
63 const Reference< chart2::XChartDocument > & xChartDocument ) :
64 DropTargetHelper( rxDropTarget ),
65 m_xChartDocument( xChartDocument )
68 ChartDropTargetHelper::~ChartDropTargetHelper()
71 bool ChartDropTargetHelper::satisfiesPrerequisites() const
73 return ( m_xChartDocument.is() &&
74 ! m_xChartDocument->hasInternalDataProvider());
77 sal_Int8 ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent& rEvt )
79 sal_Int8 nResult = DND_ACTION_NONE;
81 if( ( rEvt.mnAction == DND_ACTION_COPY ||
82 rEvt.mnAction == DND_ACTION_MOVE ) &&
83 satisfiesPrerequisites() &&
84 IsDropFormatSupported( SotClipboardFormatId::LINK ) )
86 // @todo: check if the data is suitable. Is this possible without XTransferable?
87 nResult = rEvt.mnAction;
90 return nResult;
93 sal_Int8 ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent& rEvt )
95 sal_Int8 nResult = DND_ACTION_NONE;
97 if( ( rEvt.mnAction == DND_ACTION_COPY ||
98 rEvt.mnAction == DND_ACTION_MOVE ) &&
99 rEvt.maDropEvent.Transferable.is() &&
100 satisfiesPrerequisites())
102 TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
103 if( aDataHelper.HasFormat( SotClipboardFormatId::LINK ))
105 Sequence<sal_Int8> aBytes = aDataHelper.GetSequence(SotClipboardFormatId::LINK, OUString());
106 if (aBytes.hasElements())
108 std::vector< OUString > aStrings( lcl_getStringsFromByteSequence( aBytes ));
109 if( aStrings.size() >= 3 && aStrings[0] == "soffice" )
111 OUString aRangeString( aStrings[2] );
112 Reference< container::XChild > xChild( m_xChartDocument, uno::UNO_QUERY );
113 if( xChild.is())
115 Reference< frame::XModel > xParentModel( xChild->getParent(), uno::UNO_QUERY );
116 if( xParentModel.is() &&
117 m_xChartDocument.is())
119 // @todo: get the title somehow and compare it to
120 // aDocName if successful (the document is the
121 // parent)
122 Reference< chart2::XDiagram > xDiagram( m_xChartDocument->getFirstDiagram() );
123 Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
124 if( xDataProvider.is() && xDiagram.is() &&
125 DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument ))
127 Reference< chart2::data::XDataSource > xDataSource(
128 DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument ));
129 Sequence< beans::PropertyValue > aArguments(
130 xDataProvider->detectArguments( xDataSource ));
132 OUString aOldRange;
133 beans::PropertyValue * pCellRange = nullptr;
134 for( sal_Int32 i=0; i<aArguments.getLength(); ++i )
136 if ( aArguments[i].Name == "CellRangeRepresentation" )
138 pCellRange = (aArguments.getArray() + i);
139 aArguments[i].Value >>= aOldRange;
140 break;
143 if( pCellRange )
145 // copy means add ranges, move means replace
146 if( rEvt.mnAction == DND_ACTION_COPY )
148 // @todo: using implicit knowledge that ranges can be
149 // merged with ";". This should be done more general
150 pCellRange->Value <<= aOldRange + ";" + aRangeString;
152 // move means replace range
153 else
155 pCellRange->Value <<= aRangeString;
158 xDataSource.set( xDataProvider->createDataSource( aArguments ));
159 xDiagram->setDiagramData( xDataSource, aArguments );
161 // always return copy state to avoid deletion of the dragged range
162 nResult = DND_ACTION_COPY;
171 return nResult;
174 } // namespace chart
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */