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