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