LCOV - code coverage report
Current view: top level - chart2/source/tools - ChartModelHelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 112 121 92.6 %
Date: 2014-04-11 Functions: 14 15 93.3 %
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             : #include "ChartModelHelper.hxx"
      21             : #include "macros.hxx"
      22             : #include "DiagramHelper.hxx"
      23             : #include "DataSourceHelper.hxx"
      24             : #include "ControllerLockGuard.hxx"
      25             : #include "RangeHighlighter.hxx"
      26             : #include "InternalDataProvider.hxx"
      27             : #include "ChartModel.hxx"
      28             : 
      29             : #include <com/sun/star/chart/ChartDataRowSource.hpp>
      30             : #include <com/sun/star/chart/XChartDocument.hpp>
      31             : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
      32             : #include <com/sun/star/chart2/XChartDocument.hpp>
      33             : #include <com/sun/star/chart2/XChartTypeContainer.hpp>
      34             : #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
      35             : #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
      36             : #include <com/sun/star/embed/Aspects.hpp>
      37             : #include <com/sun/star/embed/XVisualObject.hpp>
      38             : #include <com/sun/star/view/XSelectionChangeListener.hpp>
      39             : 
      40             : namespace chart
      41             : {
      42             : using namespace ::com::sun::star;
      43             : using namespace ::com::sun::star::chart2;
      44             : 
      45          17 : uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
      46             :         const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
      47             : {
      48          17 :     return new RangeHighlighter( xSelectionSupplier );
      49             : }
      50             : 
      51         212 : uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
      52             :     const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
      53             : {
      54         212 :     bool bDefaultDataInColumns(true);
      55             : 
      56             :     // #i120559# Try to access the current state of "DataRowSource" for the
      57             :     // chart data and use it as default for creating a new InternalDataProvider
      58         212 :     if(xChartDoc.is())
      59             :     {
      60          24 :         ::com::sun::star::uno::Reference< ::com::sun::star::chart::XChartDocument > xDoc(xChartDoc, uno::UNO_QUERY);
      61             : 
      62          24 :         if(xDoc.is())
      63             :         {
      64          24 :             ::com::sun::star::uno::Reference< ::com::sun::star::chart::XDiagram > aDiagram = xDoc->getDiagram();
      65             : 
      66          24 :             if(aDiagram.is())
      67             :             {
      68          24 :                 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xProp(aDiagram, uno::UNO_QUERY);
      69             : 
      70          24 :                 if(xProp.is())
      71             :                 {
      72          24 :                     ::com::sun::star::chart::ChartDataRowSource aDataRowSource(::com::sun::star::chart::ChartDataRowSource_COLUMNS);
      73             : 
      74          24 :                     xProp->getPropertyValue( OUString("DataRowSource") ) >>= aDataRowSource;
      75             : 
      76          24 :                     bDefaultDataInColumns = (::com::sun::star::chart::ChartDataRowSource_COLUMNS == aDataRowSource);
      77          24 :                 }
      78          24 :             }
      79          24 :         }
      80             :     }
      81             : 
      82         212 :     return new InternalDataProvider( xChartDoc, bConnectToModel, bDefaultDataInColumns );
      83             : }
      84             : 
      85      260107 : uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
      86             : {
      87      260107 :     uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
      88      260107 :     if( xChartDoc.is())
      89      260107 :         return ChartModelHelper::findDiagram( xChartDoc );
      90           0 :     return NULL;
      91             : }
      92             : 
      93      261915 : uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
      94             : {
      95             :     try
      96             :     {
      97      261915 :         if( xChartDoc.is())
      98      261727 :             return xChartDoc->getFirstDiagram();
      99             :     }
     100           0 :     catch( const uno::Exception & ex )
     101             :     {
     102             :         ASSERT_EXCEPTION( ex );
     103             :     }
     104         188 :     return NULL;
     105             : }
     106             : 
     107        5614 : uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( ChartModel& rModel )
     108             : {
     109        5614 :     uno::Reference< XCoordinateSystem > XCooSys;
     110       11228 :     uno::Reference< XCoordinateSystemContainer > xCooSysCnt( rModel.getFirstDiagram(), uno::UNO_QUERY );
     111        5614 :     if( xCooSysCnt.is() )
     112             :     {
     113        5614 :         uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
     114        5614 :         if( aCooSysSeq.getLength() )
     115        5614 :             XCooSys = aCooSysSeq[0];
     116             :     }
     117       11228 :     return XCooSys;
     118             : }
     119             : 
     120          25 : uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
     121             : {
     122          25 :     uno::Reference< XCoordinateSystem > XCooSys;
     123          50 :     uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
     124          25 :     if( xCooSysCnt.is() )
     125             :     {
     126          25 :         uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
     127          25 :         if( aCooSysSeq.getLength() )
     128          25 :             XCooSys = aCooSysSeq[0];
     129             :     }
     130          50 :     return XCooSys;
     131             : }
     132             : 
     133         323 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
     134             :     ChartModel& rModel )
     135             : {
     136         323 :     ::std::vector< uno::Reference< XDataSeries > > aResult;
     137             : 
     138         646 :     uno::Reference< XDiagram > xDiagram = rModel.getFirstDiagram();
     139         323 :     if( xDiagram.is())
     140         323 :         aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
     141             : 
     142         646 :     return aResult;
     143             : }
     144             : 
     145          26 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
     146             :     const uno::Reference< XChartDocument > & xChartDoc )
     147             : {
     148          26 :     ::std::vector< uno::Reference< XDataSeries > > aResult;
     149             : 
     150          52 :     uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
     151          26 :     if( xDiagram.is())
     152          26 :         aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
     153             : 
     154          52 :     return aResult;
     155             : }
     156             : 
     157           2 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
     158             :     const uno::Reference< frame::XModel > & xModel )
     159             : {
     160           2 :     return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
     161             : }
     162             : 
     163           0 : uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
     164             :                                 const uno::Reference< frame::XModel >& xModel
     165             :                               , const uno::Reference< XDataSeries >&   xGivenDataSeries )
     166             : {
     167           0 :     return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
     168             : }
     169             : 
     170        1654 : awt::Size ChartModelHelper::getDefaultPageSize()
     171             : {
     172        1654 :     return awt::Size( 16000, 9000 );
     173             : }
     174             : 
     175         218 : awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
     176             : {
     177         218 :     awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
     178         218 :     uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
     179             :     OSL_ENSURE(xVisualObject.is(),"need xVisualObject for page size");
     180         218 :     if( xVisualObject.is() )
     181         218 :         aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
     182         218 :     return aPageSize;
     183             : }
     184             : 
     185         360 : void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
     186             : {
     187         360 :     uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
     188         360 :     if( xDataReceiver.is() )
     189             :     {
     190         360 :         uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
     191             :         //trigger selection of cell range
     192         360 :         if( xSelectionChangeListener.is() )
     193             :         {
     194         360 :             lang::EventObject aEvent( xSelectionChangeListener );
     195         360 :             xSelectionChangeListener->selectionChanged( aEvent );
     196         360 :         }
     197         360 :     }
     198         360 : }
     199             : 
     200         450 : bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
     201             : {
     202         450 :     bool bIncluded = true;  // hidden cells are included by default.
     203             : 
     204         450 :     uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
     205         450 :     if (!xDiagram.is())
     206          34 :         return bIncluded;
     207             : 
     208         832 :     uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
     209         416 :     if (!xProp.is())
     210           0 :         return bIncluded;
     211             : 
     212             :     try
     213             :     {
     214         416 :         xProp->getPropertyValue("IncludeHiddenCells") >>= bIncluded;
     215             :     }
     216           0 :     catch( const beans::UnknownPropertyException& )
     217             :     {
     218             :     }
     219             : 
     220         866 :     return bIncluded;
     221             : }
     222             : 
     223         323 : bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, ChartModel& rModel )
     224             : {
     225         323 :     bool bChanged = false;
     226             :     try
     227             :     {
     228         323 :         ControllerLockGuard aLockedControllers( rModel );
     229             : 
     230         646 :         uno::Reference< beans::XPropertySet > xDiagramProperties( rModel.getFirstDiagram(), uno::UNO_QUERY );
     231         323 :         if (xDiagramProperties.is())
     232             :         {
     233         323 :             bool bOldValue = bIncludeHiddenCells;
     234         323 :             xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue;
     235         323 :             if( bOldValue == bIncludeHiddenCells )
     236           0 :                 bChanged = true;
     237             : 
     238             :             //set the property on all instances in all cases to get the different objects in sync!
     239             : 
     240         323 :             uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
     241             : 
     242             :             try
     243             :             {
     244         323 :                 uno::Reference< beans::XPropertySet > xDataProviderProperties( rModel.getDataProvider(), uno::UNO_QUERY );
     245         323 :                 if( xDataProviderProperties.is() )
     246          83 :                     xDataProviderProperties->setPropertyValue("IncludeHiddenCells", aNewValue );
     247             :             }
     248           0 :             catch( const beans::UnknownPropertyException& )
     249             :             {
     250             :                 //the property is optional!
     251             :             }
     252             : 
     253             :             try
     254             :             {
     255         323 :                 uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( rModel ) );
     256         323 :                 if( xUsedData.is() )
     257             :                 {
     258         323 :                     uno::Reference< beans::XPropertySet > xProp;
     259         646 :                     uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
     260         414 :                     for( sal_Int32 i=0; i<aData.getLength(); ++i )
     261             :                     {
     262         281 :                         xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
     263         281 :                         if(xProp.is())
     264         471 :                             xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
     265          91 :                         xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
     266          91 :                         if(xProp.is())
     267          45 :                             xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
     268         323 :                     }
     269         323 :                 }
     270             :             }
     271         190 :             catch( const beans::UnknownPropertyException& )
     272             :             {
     273             :                 //the property is optional!
     274             :             }
     275             : 
     276         323 :             xDiagramProperties->setPropertyValue( "IncludeHiddenCells", aNewValue);
     277         323 :         }
     278             :     }
     279           0 :     catch (const uno::Exception& e)
     280             :     {
     281             :         ASSERT_EXCEPTION(e);
     282             :     }
     283         323 :     return bChanged;
     284             : }
     285             : 
     286             : } //namespace chart
     287             : 
     288             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10