LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/chart2/source/controller/main - ChartDropTargetHelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 5 74 6.8 %
Date: 2013-07-09 Functions: 3 7 42.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10