LCOV - code coverage report
Current view: top level - chart2/source/tools - RangeHighlighter.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 50 188 26.6 %
Date: 2014-04-11 Functions: 9 19 47.4 %
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 "RangeHighlighter.hxx"
      21             : #include "WeakListenerAdapter.hxx"
      22             : #include "ChartModelHelper.hxx"
      23             : #include "DataSourceHelper.hxx"
      24             : #include "ContainerHelper.hxx"
      25             : #include "macros.hxx"
      26             : #include "ObjectIdentifier.hxx"
      27             : #include "DataSeriesHelper.hxx"
      28             : 
      29             : #include <com/sun/star/chart2/XDataSeries.hpp>
      30             : #include <com/sun/star/chart/ErrorBarStyle.hpp>
      31             : #include <com/sun/star/drawing/XShape.hpp>
      32             : 
      33             : #define PREFERED_DEFAULT_COLOR 0x0000ff
      34             : 
      35             : using namespace ::com::sun::star;
      36             : 
      37             : using ::com::sun::star::uno::Reference;
      38             : using ::com::sun::star::uno::Sequence;
      39             : 
      40             : namespace
      41             : {
      42             : 
      43           0 : void lcl_fillRanges(
      44             :     Sequence< chart2::data::HighlightedRange > & rOutRanges,
      45             :     Sequence< OUString > aRangeStrings,
      46             :     sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR,
      47             :     sal_Int32 nIndex = -1 )
      48             : {
      49           0 :     rOutRanges.realloc( aRangeStrings.getLength());
      50           0 :     for( sal_Int32 i=0; i<aRangeStrings.getLength(); ++i )
      51             :     {
      52           0 :         rOutRanges[i].RangeRepresentation = aRangeStrings[i];
      53           0 :         rOutRanges[i].PreferredColor = nPreferredColor;
      54           0 :         rOutRanges[i].AllowMerginigWithOtherRanges = sal_False;
      55           0 :         rOutRanges[i].Index = nIndex;
      56             :     }
      57           0 : }
      58             : 
      59             : } // anonymous namespace
      60             : 
      61             : namespace chart
      62             : {
      63             : 
      64          17 : RangeHighlighter::RangeHighlighter(
      65             :     const Reference< view::XSelectionSupplier > & xSelectionSupplier ) :
      66             :         impl::RangeHighlighter_Base( m_aMutex ),
      67             :         m_xSelectionSupplier( xSelectionSupplier ),
      68             :         m_nAddedListenerCount( 0 ),
      69          17 :         m_bIncludeHiddenCells(true)
      70             : {
      71          17 : }
      72             : 
      73          34 : RangeHighlighter::~RangeHighlighter()
      74          34 : {}
      75             : 
      76             : // ____ XRangeHighlighter ____
      77           0 : Sequence< chart2::data::HighlightedRange > SAL_CALL RangeHighlighter::getSelectedRanges()
      78             :     throw (uno::RuntimeException, std::exception)
      79             : {
      80           0 :     return m_aSelectedRanges;
      81             : }
      82             : 
      83         360 : void RangeHighlighter::determineRanges()
      84             : {
      85         360 :     m_aSelectedRanges.realloc( 0 );
      86         360 :     if( m_xSelectionSupplier.is())
      87             :     {
      88             :         try
      89             :         {
      90         360 :             Reference< frame::XController > xController( m_xSelectionSupplier, uno::UNO_QUERY );
      91         360 :             Reference< frame::XModel > xChartModel;
      92         360 :             if( xController.is())
      93         360 :                 xChartModel.set( xController->getModel());
      94             : 
      95         360 :             m_bIncludeHiddenCells = ChartModelHelper::isIncludeHiddenCells( xChartModel );
      96             : 
      97         360 :             uno::Any aSelection( m_xSelectionSupplier->getSelection());
      98         360 :             const uno::Type& rType = aSelection.getValueType();
      99             : 
     100         360 :             if ( rType == ::getCppuType( static_cast< const OUString* >( 0 ) ) )
     101             :             {
     102             :                 // @todo??: maybe getSelection() should return a model object rather than a CID
     103             : 
     104           0 :                 OUString aCID;
     105           0 :                 aSelection >>= aCID;
     106           0 :                 if ( !aCID.isEmpty() )
     107             :                 {
     108           0 :                     ObjectType eObjectType = ObjectIdentifier::getObjectType( aCID );
     109           0 :                     sal_Int32 nIndex = ObjectIdentifier::getIndexFromParticleOrCID( aCID );
     110           0 :                     Reference< chart2::XDataSeries > xDataSeries( ObjectIdentifier::getDataSeriesForCID( aCID, xChartModel ) );
     111           0 :                     if( OBJECTTYPE_LEGEND_ENTRY == eObjectType )
     112             :                     {
     113           0 :                         OUString aParentParticel( ObjectIdentifier::getFullParentParticle( aCID ) );
     114           0 :                         ObjectType eParentObjectType = ObjectIdentifier::getObjectType( aParentParticel );
     115           0 :                         eObjectType = eParentObjectType;
     116           0 :                         if( OBJECTTYPE_DATA_POINT == eObjectType )
     117           0 :                             nIndex = ObjectIdentifier::getIndexFromParticleOrCID( aParentParticel );
     118             :                     }
     119             : 
     120           0 :                     if( OBJECTTYPE_DATA_POINT == eObjectType || OBJECTTYPE_DATA_LABEL == eObjectType )
     121             :                     {
     122             :                         // Data Point
     123           0 :                         fillRangesForDataPoint( xDataSeries, nIndex );
     124           0 :                         return;
     125             :                     }
     126           0 :                     else if( OBJECTTYPE_DATA_ERRORS_X == eObjectType ||
     127           0 :                              OBJECTTYPE_DATA_ERRORS_Y == eObjectType ||
     128             :                              OBJECTTYPE_DATA_ERRORS_Z == eObjectType )
     129             :                     {
     130             :                         // select error bar ranges, or data series, if the style is
     131             :                         // not set to FROM_DATA
     132           0 :                         fillRangesForErrorBars( ObjectIdentifier::getObjectPropertySet( aCID, xChartModel ), xDataSeries );
     133           0 :                         return;
     134             :                     }
     135           0 :                     else if( xDataSeries.is() )
     136             :                     {
     137             :                         // Data Series
     138           0 :                         fillRangesForDataSeries( xDataSeries );
     139           0 :                         return;
     140             :                     }
     141           0 :                     else if( OBJECTTYPE_AXIS == eObjectType )
     142             :                     {
     143             :                         // Axis (Categories)
     144           0 :                         Reference< chart2::XAxis > xAxis( ObjectIdentifier::getObjectPropertySet( aCID, xChartModel ), uno::UNO_QUERY );
     145           0 :                         if( xAxis.is())
     146             :                         {
     147           0 :                             fillRangesForCategories( xAxis );
     148           0 :                             return;
     149           0 :                         }
     150             :                     }
     151           0 :                     else if( OBJECTTYPE_PAGE == eObjectType
     152           0 :                              || OBJECTTYPE_DIAGRAM == eObjectType
     153           0 :                              || OBJECTTYPE_DIAGRAM_WALL == eObjectType
     154           0 :                              || OBJECTTYPE_DIAGRAM_FLOOR == eObjectType
     155             :                         )
     156             :                     {
     157             :                         // Diagram
     158           0 :                         Reference< chart2::XDiagram > xDia( ObjectIdentifier::getDiagramForCID( aCID, xChartModel ) );
     159           0 :                         if( xDia.is())
     160             :                         {
     161           0 :                             fillRangesForDiagram( xDia );
     162           0 :                             return;
     163           0 :                         }
     164           0 :                     }
     165           0 :                 }
     166             :             }
     167         360 :             else if ( rType == ::getCppuType( static_cast< const Reference< drawing::XShape >* >( 0 ) ) )
     168             :             {
     169             :                 // #i12587# support for shapes in chart
     170           0 :                 Reference< drawing::XShape > xShape;
     171           0 :                 aSelection >>= xShape;
     172           0 :                 if ( xShape.is() )
     173             :                 {
     174           0 :                     return;
     175           0 :                 }
     176             :             }
     177             :             else
     178             :             {
     179             :                 //if nothing is selected select all ranges
     180         360 :                 Reference< chart2::XChartDocument > xChartDoc( xChartModel, uno::UNO_QUERY_THROW );
     181         360 :                 fillRangesForDiagram( xChartDoc->getFirstDiagram() );
     182         360 :                 return;
     183           0 :             }
     184             :         }
     185           0 :         catch( const uno::Exception & ex )
     186             :         {
     187             :             ASSERT_EXCEPTION( ex );
     188             :         }
     189             :     }
     190             : }
     191             : 
     192         360 : void RangeHighlighter::fillRangesForDiagram( const Reference< chart2::XDiagram > & xDiagram )
     193             : {
     194         360 :     Sequence< OUString > aSelectedRanges( DataSourceHelper::getUsedDataRanges( xDiagram ));
     195         360 :     m_aSelectedRanges.realloc( aSelectedRanges.getLength());
     196             :     // @todo: merge ranges
     197        2829 :     for( sal_Int32 i=0; i<aSelectedRanges.getLength(); ++i )
     198             :     {
     199        2469 :         m_aSelectedRanges[i].RangeRepresentation = aSelectedRanges[i];
     200        2469 :         m_aSelectedRanges[i].Index = -1;
     201        2469 :         m_aSelectedRanges[i].PreferredColor = PREFERED_DEFAULT_COLOR;
     202        2469 :         m_aSelectedRanges[i].AllowMerginigWithOtherRanges = sal_True;
     203         360 :     }
     204         360 : }
     205             : 
     206           0 : void RangeHighlighter::fillRangesForDataSeries( const uno::Reference< chart2::XDataSeries > & xSeries )
     207             : {
     208           0 :     Reference< chart2::data::XDataSource > xSource( xSeries, uno::UNO_QUERY );
     209           0 :     if( xSource.is())
     210             :     {
     211           0 :         sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR;
     212             :         lcl_fillRanges( m_aSelectedRanges,
     213             :                         ::chart::DataSourceHelper::getRangesFromDataSource( xSource ),
     214           0 :                         nPreferredColor );
     215           0 :     }
     216           0 : }
     217             : 
     218           0 : void RangeHighlighter::fillRangesForErrorBars(
     219             :     const uno::Reference< beans::XPropertySet > & xErrorBar,
     220             :     const uno::Reference< chart2::XDataSeries > & xSeries )
     221             : {
     222             :     // only show error bar ranges, if the style is set to FROM_DATA
     223           0 :     bool bUsesRangesAsErrorBars = false;
     224             :     try
     225             :     {
     226           0 :         sal_Int32 nStyle = ::com::sun::star::chart::ErrorBarStyle::NONE;
     227             :         bUsesRangesAsErrorBars =
     228           0 :             ( xErrorBar.is() &&
     229           0 :               (xErrorBar->getPropertyValue( "ErrorBarStyle") >>= nStyle) &&
     230           0 :               nStyle == ::com::sun::star::chart::ErrorBarStyle::FROM_DATA );
     231             :     }
     232           0 :     catch( const uno::Exception & ex )
     233             :     {
     234             :         ASSERT_EXCEPTION( ex );
     235             :     }
     236             : 
     237           0 :     if( bUsesRangesAsErrorBars )
     238             :     {
     239           0 :         Reference< chart2::data::XDataSource > xSource( xErrorBar, uno::UNO_QUERY );
     240           0 :         if( xSource.is())
     241             :         {
     242           0 :             sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR;
     243             :             lcl_fillRanges( m_aSelectedRanges,
     244             :                             ::chart::DataSourceHelper::getRangesFromDataSource( xSource ),
     245           0 :                             nPreferredColor );
     246           0 :         }
     247             :     }
     248             :     else
     249             :     {
     250           0 :         fillRangesForDataSeries( xSeries );
     251             :     }
     252           0 : }
     253             : 
     254           0 : void RangeHighlighter::fillRangesForCategories( const Reference< chart2::XAxis > & xAxis )
     255             : {
     256           0 :     if( ! xAxis.is())
     257           0 :         return;
     258           0 :     chart2::ScaleData aData( xAxis->getScaleData());
     259             :     lcl_fillRanges( m_aSelectedRanges,
     260           0 :                     DataSourceHelper::getRangesFromLabeledDataSequence( aData.Categories ));
     261             : }
     262             : 
     263           0 : void RangeHighlighter::fillRangesForDataPoint( const Reference< uno::XInterface > & xDataSeries, sal_Int32 nIndex )
     264             : {
     265           0 :     if( xDataSeries.is())
     266             :     {
     267           0 :         Reference< chart2::data::XDataSource > xSource( xDataSeries, uno::UNO_QUERY );
     268           0 :         if( xSource.is() )
     269             :         {
     270           0 :             sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR;
     271           0 :             ::std::vector< chart2::data::HighlightedRange > aHilightedRanges;
     272           0 :             Sequence< Reference< chart2::data::XLabeledDataSequence > > aLSeqSeq( xSource->getDataSequences());
     273           0 :             for( sal_Int32 i=0; i<aLSeqSeq.getLength(); ++i )
     274             :             {
     275           0 :                 Reference< chart2::data::XDataSequence > xLabel( aLSeqSeq[i]->getLabel());
     276           0 :                 Reference< chart2::data::XDataSequence > xValues( aLSeqSeq[i]->getValues());
     277             : 
     278           0 :                 if( xLabel.is())
     279             :                     aHilightedRanges.push_back(
     280             :                         chart2::data::HighlightedRange(
     281           0 :                             xLabel->getSourceRangeRepresentation(),
     282             :                             -1,
     283             :                             nPreferredColor,
     284           0 :                             sal_False ));
     285             : 
     286           0 :                 sal_Int32 nUnhiddenIndex = DataSeriesHelper::translateIndexFromHiddenToFullSequence( nIndex, xValues, !m_bIncludeHiddenCells );
     287           0 :                 if( xValues.is())
     288             :                     aHilightedRanges.push_back(
     289             :                         chart2::data::HighlightedRange(
     290           0 :                             xValues->getSourceRangeRepresentation(),
     291             :                             nUnhiddenIndex,
     292             :                             nPreferredColor,
     293           0 :                             sal_False ));
     294           0 :             }
     295           0 :             m_aSelectedRanges = ContainerHelper::ContainerToSequence( aHilightedRanges );
     296           0 :         }
     297             :     }
     298           0 : }
     299             : 
     300           0 : void SAL_CALL RangeHighlighter::addSelectionChangeListener( const Reference< view::XSelectionChangeListener >& xListener )
     301             :     throw (uno::RuntimeException, std::exception)
     302             : {
     303           0 :     if(!xListener.is())
     304           0 :         return;
     305             : 
     306           0 :     if( m_nAddedListenerCount == 0 )
     307           0 :         startListening();
     308           0 :     rBHelper.addListener( ::getCppuType( & xListener ), xListener);
     309           0 :     ++m_nAddedListenerCount;
     310             : 
     311             :     //bring the new listener up to the current state
     312           0 :     lang::EventObject aEvent( static_cast< lang::XComponent* >( this ) );
     313           0 :     xListener->selectionChanged( aEvent );
     314             : }
     315             : 
     316           0 : void SAL_CALL RangeHighlighter::removeSelectionChangeListener( const Reference< view::XSelectionChangeListener >& xListener )
     317             :     throw (uno::RuntimeException, std::exception)
     318             : {
     319           0 :     rBHelper.removeListener( ::getCppuType( & xListener ), xListener );
     320           0 :     --m_nAddedListenerCount;
     321           0 :     if( m_nAddedListenerCount == 0 )
     322           0 :         stopListening();
     323           0 : }
     324             : 
     325             : // ____ XSelectionChangeListener ____
     326         360 : void SAL_CALL RangeHighlighter::selectionChanged( const lang::EventObject& /*aEvent*/ )
     327             :     throw (uno::RuntimeException, std::exception)
     328             : {
     329         360 :     determineRanges();
     330             : 
     331             :     // determine ranges of selected view objects
     332             :     // if changed, fire an event
     333         360 :     fireSelectionEvent();
     334         360 : }
     335             : 
     336         361 : void RangeHighlighter::fireSelectionEvent()
     337             : {
     338             :     ::cppu::OInterfaceContainerHelper* pIC = rBHelper.getContainer(
     339         361 :         ::getCppuType((const uno::Reference< view::XSelectionChangeListener >*)0) );
     340         361 :     if( pIC )
     341             :     {
     342           0 :         lang::EventObject aEvent( static_cast< lang::XComponent* >( this ) );
     343           0 :         ::cppu::OInterfaceIteratorHelper aIt( *pIC );
     344           0 :         while( aIt.hasMoreElements() )
     345             :         {
     346           0 :             uno::Reference< view::XSelectionChangeListener > xListener( aIt.next(), uno::UNO_QUERY );
     347           0 :             if( xListener.is() )
     348           0 :                 xListener->selectionChanged( aEvent );
     349           0 :         }
     350             :     }
     351         361 : }
     352             : 
     353           1 : void SAL_CALL RangeHighlighter::disposing( const lang::EventObject& Source )
     354             :     throw (uno::RuntimeException, std::exception)
     355             : {
     356           1 :     if( Source.Source == m_xSelectionSupplier )
     357             :     {
     358           1 :         m_xSelectionSupplier.clear();
     359           1 :         m_aSelectedRanges.realloc( 0 );
     360           1 :         fireSelectionEvent();
     361             :     }
     362           1 : }
     363             : 
     364           0 : void RangeHighlighter::startListening()
     365             : {
     366           0 :     if( m_xSelectionSupplier.is())
     367             :     {
     368           0 :         if( ! m_xListener.is())
     369             :         {
     370           0 :             m_xListener.set( new WeakSelectionChangeListenerAdapter( this ));
     371           0 :             determineRanges();
     372             :         }
     373           0 :         m_xSelectionSupplier->addSelectionChangeListener( m_xListener );
     374             :     }
     375           0 : }
     376             : 
     377           0 : void RangeHighlighter::stopListening()
     378             : {
     379           0 :     if( m_xSelectionSupplier.is() && m_xListener.is())
     380             :     {
     381           0 :         m_xSelectionSupplier->removeSelectionChangeListener( m_xListener );
     382           0 :         m_xListener.clear();
     383             :     }
     384           0 : }
     385             : 
     386             : // ____ WeakComponentImplHelperBase ____
     387             : // is called when dispose() is called at this component
     388          17 : void SAL_CALL RangeHighlighter::disposing()
     389             : {
     390             :     // @todo: remove listener. Currently the controller shows an assertion
     391             :     // because it is already disposed
     392             : //     stopListening();
     393          17 :     m_xListener.clear();
     394          17 :     m_xSelectionSupplier.clear();
     395          17 :     m_nAddedListenerCount =  0;
     396          17 :     m_aSelectedRanges.realloc( 0 );
     397          17 : }
     398             : 
     399             : } //  namespace chart
     400             : 
     401             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10