bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / RangeHighlighter.cxx
blob1d1a1d22edf8b1a3850fc2c4d63973c946d44d68
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 .
21 #include "RangeHighlighter.hxx"
22 #include "WeakListenerAdapter.hxx"
23 #include "ChartModelHelper.hxx"
24 #include "DataSourceHelper.hxx"
25 #include "ContainerHelper.hxx"
26 #include "macros.hxx"
27 #include "ObjectIdentifier.hxx"
28 #include "DataSeriesHelper.hxx"
30 #include <com/sun/star/chart2/XDataSeries.hpp>
31 #include <com/sun/star/chart/ErrorBarStyle.hpp>
32 #include <com/sun/star/drawing/XShape.hpp>
34 #define PREFERED_DEFAULT_COLOR 0x0000ff
36 using namespace ::com::sun::star;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Sequence;
41 namespace
44 void lcl_fillRanges(
45 Sequence< chart2::data::HighlightedRange > & rOutRanges,
46 Sequence< OUString > aRangeStrings,
47 sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR,
48 sal_Int32 nIndex = -1 )
50 rOutRanges.realloc( aRangeStrings.getLength());
51 for( sal_Int32 i=0; i<aRangeStrings.getLength(); ++i )
53 rOutRanges[i].RangeRepresentation = aRangeStrings[i];
54 rOutRanges[i].PreferredColor = nPreferredColor;
55 rOutRanges[i].AllowMerginigWithOtherRanges = sal_False;
56 rOutRanges[i].Index = nIndex;
60 } // anonymous namespace
62 namespace chart
65 RangeHighlighter::RangeHighlighter(
66 const Reference< view::XSelectionSupplier > & xSelectionSupplier ) :
67 impl::RangeHighlighter_Base( m_aMutex ),
68 m_xSelectionSupplier( xSelectionSupplier ),
69 m_nAddedListenerCount( 0 ),
70 m_bIncludeHiddenCells(true)
74 RangeHighlighter::~RangeHighlighter()
77 // ____ XRangeHighlighter ____
78 Sequence< chart2::data::HighlightedRange > SAL_CALL RangeHighlighter::getSelectedRanges()
79 throw (uno::RuntimeException)
81 return m_aSelectedRanges;
84 void RangeHighlighter::determineRanges()
86 m_aSelectedRanges.realloc( 0 );
87 if( m_xSelectionSupplier.is())
89 try
91 Reference< frame::XController > xController( m_xSelectionSupplier, uno::UNO_QUERY );
92 Reference< frame::XModel > xChartModel;
93 if( xController.is())
94 xChartModel.set( xController->getModel());
96 m_bIncludeHiddenCells = ChartModelHelper::isIncludeHiddenCells( xChartModel );
98 uno::Any aSelection( m_xSelectionSupplier->getSelection());
99 const uno::Type& rType = aSelection.getValueType();
101 if ( rType == ::getCppuType( static_cast< const OUString* >( 0 ) ) )
103 // @todo??: maybe getSelection() should return a model object rather than a CID
105 OUString aCID;
106 aSelection >>= aCID;
107 if ( !aCID.isEmpty() )
109 ObjectType eObjectType = ObjectIdentifier::getObjectType( aCID );
110 sal_Int32 nIndex = ObjectIdentifier::getIndexFromParticleOrCID( aCID );
111 Reference< chart2::XDataSeries > xDataSeries( ObjectIdentifier::getDataSeriesForCID( aCID, xChartModel ) );
112 if( OBJECTTYPE_LEGEND_ENTRY == eObjectType )
114 OUString aParentParticel( ObjectIdentifier::getFullParentParticle( aCID ) );
115 ObjectType eParentObjectType = ObjectIdentifier::getObjectType( aParentParticel );
116 eObjectType = eParentObjectType;
117 if( OBJECTTYPE_DATA_POINT == eObjectType )
118 nIndex = ObjectIdentifier::getIndexFromParticleOrCID( aParentParticel );
121 if( OBJECTTYPE_DATA_POINT == eObjectType || OBJECTTYPE_DATA_LABEL == eObjectType )
123 // Data Point
124 fillRangesForDataPoint( xDataSeries, nIndex );
125 return;
127 else if( OBJECTTYPE_DATA_ERRORS_X == eObjectType ||
128 OBJECTTYPE_DATA_ERRORS_Y == eObjectType ||
129 OBJECTTYPE_DATA_ERRORS_Z == eObjectType )
131 // select error bar ranges, or data series, if the style is
132 // not set to FROM_DATA
133 fillRangesForErrorBars( ObjectIdentifier::getObjectPropertySet( aCID, xChartModel ), xDataSeries );
134 return;
136 else if( xDataSeries.is() )
138 // Data Series
139 fillRangesForDataSeries( xDataSeries );
140 return;
142 else if( OBJECTTYPE_AXIS == eObjectType )
144 // Axis (Categories)
145 Reference< chart2::XAxis > xAxis( ObjectIdentifier::getObjectPropertySet( aCID, xChartModel ), uno::UNO_QUERY );
146 if( xAxis.is())
148 fillRangesForCategories( xAxis );
149 return;
152 else if( OBJECTTYPE_PAGE == eObjectType
153 || OBJECTTYPE_DIAGRAM == eObjectType
154 || OBJECTTYPE_DIAGRAM_WALL == eObjectType
155 || OBJECTTYPE_DIAGRAM_FLOOR == eObjectType
158 // Diagram
159 Reference< chart2::XDiagram > xDia( ObjectIdentifier::getDiagramForCID( aCID, xChartModel ) );
160 if( xDia.is())
162 fillRangesForDiagram( xDia );
163 return;
168 else if ( rType == ::getCppuType( static_cast< const Reference< drawing::XShape >* >( 0 ) ) )
170 // #i12587# support for shapes in chart
171 Reference< drawing::XShape > xShape;
172 aSelection >>= xShape;
173 if ( xShape.is() )
175 return;
178 else
180 //if nothing is selected select all ranges
181 Reference< chart2::XChartDocument > xChartDoc( xChartModel, uno::UNO_QUERY_THROW );
182 fillRangesForDiagram( xChartDoc->getFirstDiagram() );
183 return;
186 catch( const uno::Exception & ex )
188 ASSERT_EXCEPTION( ex );
193 void RangeHighlighter::fillRangesForDiagram( const Reference< chart2::XDiagram > & xDiagram )
195 Sequence< OUString > aSelectedRanges( DataSourceHelper::getUsedDataRanges( xDiagram ));
196 m_aSelectedRanges.realloc( aSelectedRanges.getLength());
197 // @todo: merge ranges
198 for( sal_Int32 i=0; i<aSelectedRanges.getLength(); ++i )
200 m_aSelectedRanges[i].RangeRepresentation = aSelectedRanges[i];
201 m_aSelectedRanges[i].Index = -1;
202 m_aSelectedRanges[i].PreferredColor = PREFERED_DEFAULT_COLOR;
203 m_aSelectedRanges[i].AllowMerginigWithOtherRanges = sal_True;
207 void RangeHighlighter::fillRangesForDataSeries( const uno::Reference< chart2::XDataSeries > & xSeries )
209 Reference< chart2::data::XDataSource > xSource( xSeries, uno::UNO_QUERY );
210 if( xSource.is())
212 sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR;
213 lcl_fillRanges( m_aSelectedRanges,
214 ::chart::DataSourceHelper::getRangesFromDataSource( xSource ),
215 nPreferredColor );
219 void RangeHighlighter::fillRangesForErrorBars(
220 const uno::Reference< beans::XPropertySet > & xErrorBar,
221 const uno::Reference< chart2::XDataSeries > & xSeries )
223 // only show error bar ranges, if the style is set to FROM_DATA
224 bool bUsesRangesAsErrorBars = false;
227 sal_Int32 nStyle = ::com::sun::star::chart::ErrorBarStyle::NONE;
228 bUsesRangesAsErrorBars =
229 ( xErrorBar.is() &&
230 (xErrorBar->getPropertyValue( "ErrorBarStyle") >>= nStyle) &&
231 nStyle == ::com::sun::star::chart::ErrorBarStyle::FROM_DATA );
233 catch( const uno::Exception & ex )
235 ASSERT_EXCEPTION( ex );
238 if( bUsesRangesAsErrorBars )
240 Reference< chart2::data::XDataSource > xSource( xErrorBar, uno::UNO_QUERY );
241 if( xSource.is())
243 sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR;
244 lcl_fillRanges( m_aSelectedRanges,
245 ::chart::DataSourceHelper::getRangesFromDataSource( xSource ),
246 nPreferredColor );
249 else
251 fillRangesForDataSeries( xSeries );
255 void RangeHighlighter::fillRangesForCategories( const Reference< chart2::XAxis > & xAxis )
257 if( ! xAxis.is())
258 return;
259 chart2::ScaleData aData( xAxis->getScaleData());
260 lcl_fillRanges( m_aSelectedRanges,
261 DataSourceHelper::getRangesFromLabeledDataSequence( aData.Categories ));
264 void RangeHighlighter::fillRangesForDataPoint( const Reference< uno::XInterface > & xDataSeries, sal_Int32 nIndex )
266 if( xDataSeries.is())
268 Reference< chart2::data::XDataSource > xSource( xDataSeries, uno::UNO_QUERY );
269 if( xSource.is() )
271 sal_Int32 nPreferredColor = PREFERED_DEFAULT_COLOR;
272 ::std::vector< chart2::data::HighlightedRange > aHilightedRanges;
273 Sequence< Reference< chart2::data::XLabeledDataSequence > > aLSeqSeq( xSource->getDataSequences());
274 for( sal_Int32 i=0; i<aLSeqSeq.getLength(); ++i )
276 Reference< chart2::data::XDataSequence > xLabel( aLSeqSeq[i]->getLabel());
277 Reference< chart2::data::XDataSequence > xValues( aLSeqSeq[i]->getValues());
279 if( xLabel.is())
280 aHilightedRanges.push_back(
281 chart2::data::HighlightedRange(
282 xLabel->getSourceRangeRepresentation(),
284 nPreferredColor,
285 sal_False ));
287 sal_Int32 nUnhiddenIndex = DataSeriesHelper::translateIndexFromHiddenToFullSequence( nIndex, xValues, !m_bIncludeHiddenCells );
288 if( xValues.is())
289 aHilightedRanges.push_back(
290 chart2::data::HighlightedRange(
291 xValues->getSourceRangeRepresentation(),
292 nUnhiddenIndex,
293 nPreferredColor,
294 sal_False ));
296 m_aSelectedRanges = ContainerHelper::ContainerToSequence( aHilightedRanges );
301 void SAL_CALL RangeHighlighter::addSelectionChangeListener( const Reference< view::XSelectionChangeListener >& xListener )
302 throw (uno::RuntimeException)
304 if(!xListener.is())
305 return;
307 if( m_nAddedListenerCount == 0 )
308 startListening();
309 rBHelper.addListener( ::getCppuType( & xListener ), xListener);
310 ++m_nAddedListenerCount;
312 //bring the new listener up to the current state
313 lang::EventObject aEvent( static_cast< lang::XComponent* >( this ) );
314 xListener->selectionChanged( aEvent );
317 void SAL_CALL RangeHighlighter::removeSelectionChangeListener( const Reference< view::XSelectionChangeListener >& xListener )
318 throw (uno::RuntimeException)
320 rBHelper.removeListener( ::getCppuType( & xListener ), xListener );
321 --m_nAddedListenerCount;
322 if( m_nAddedListenerCount == 0 )
323 stopListening();
326 // ____ XSelectionChangeListener ____
327 void SAL_CALL RangeHighlighter::selectionChanged( const lang::EventObject& /*aEvent*/ )
328 throw (uno::RuntimeException)
330 determineRanges();
332 // determine ranges of selected view objects
333 // if changed, fire an event
334 fireSelectionEvent();
337 void RangeHighlighter::fireSelectionEvent()
339 ::cppu::OInterfaceContainerHelper* pIC = rBHelper.getContainer(
340 ::getCppuType((const uno::Reference< view::XSelectionChangeListener >*)0) );
341 if( pIC )
343 lang::EventObject aEvent( static_cast< lang::XComponent* >( this ) );
344 ::cppu::OInterfaceIteratorHelper aIt( *pIC );
345 while( aIt.hasMoreElements() )
347 uno::Reference< view::XSelectionChangeListener > xListener( aIt.next(), uno::UNO_QUERY );
348 if( xListener.is() )
349 xListener->selectionChanged( aEvent );
354 void SAL_CALL RangeHighlighter::disposing( const lang::EventObject& Source )
355 throw (uno::RuntimeException)
357 if( Source.Source == m_xSelectionSupplier )
359 m_xSelectionSupplier.clear();
360 m_aSelectedRanges.realloc( 0 );
361 fireSelectionEvent();
365 void RangeHighlighter::startListening()
367 if( m_xSelectionSupplier.is())
369 if( ! m_xListener.is())
371 m_xListener.set( new WeakSelectionChangeListenerAdapter( this ));
372 determineRanges();
374 m_xSelectionSupplier->addSelectionChangeListener( m_xListener );
378 void RangeHighlighter::stopListening()
380 if( m_xSelectionSupplier.is() && m_xListener.is())
382 m_xSelectionSupplier->removeSelectionChangeListener( m_xListener );
383 m_xListener.clear();
388 // ____ WeakComponentImplHelperBase ____
389 // is called when dispose() is called at this component
390 void SAL_CALL RangeHighlighter::disposing()
392 // @todo: remove listener. Currently the controller shows an assertion
393 // because it is already disposed
394 // stopListening();
395 m_xListener.clear();
396 m_xSelectionSupplier.clear();
397 m_nAddedListenerCount = 0;
398 m_aSelectedRanges.realloc( 0 );
401 } // namespace chart
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */