1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <RangeHighlighter.hxx>
21 #include <WeakListenerAdapter.hxx>
22 #include <ChartModel.hxx>
23 #include <ChartModelHelper.hxx>
24 #include <DataSourceHelper.hxx>
25 #include <ObjectIdentifier.hxx>
26 #include <DataSeries.hxx>
27 #include <DataSeriesHelper.hxx>
28 #include <Diagram.hxx>
30 #include <com/sun/star/chart2/ScaleData.hpp>
31 #include <com/sun/star/chart2/XAxis.hpp>
32 #include <com/sun/star/chart2/XDataSeries.hpp>
33 #include <com/sun/star/chart/ErrorBarStyle.hpp>
34 #include <com/sun/star/drawing/XShape.hpp>
35 #include <com/sun/star/view/XSelectionSupplier.hpp>
36 #include <comphelper/sequence.hxx>
37 #include <comphelper/diagnose_ex.hxx>
38 #include <tools/color.hxx>
40 using namespace ::com::sun::star
;
42 using ::com::sun::star::uno::Reference
;
43 using ::com::sun::star::uno::Sequence
;
48 const Color defaultPreferredColor
= COL_LIGHTBLUE
;
51 Sequence
< chart2::data::HighlightedRange
> & rOutRanges
,
52 const Sequence
< OUString
>& aRangeStrings
,
53 Color nPreferredColor
,
54 sal_Int32 nIndex
= -1 )
56 rOutRanges
.realloc( aRangeStrings
.getLength());
57 auto pOutRanges
= rOutRanges
.getArray();
58 for( sal_Int32 i
=0; i
<aRangeStrings
.getLength(); ++i
)
60 pOutRanges
[i
].RangeRepresentation
= aRangeStrings
[i
];
61 pOutRanges
[i
].PreferredColor
= sal_Int32(nPreferredColor
);
62 pOutRanges
[i
].AllowMerginigWithOtherRanges
= false;
63 pOutRanges
[i
].Index
= nIndex
;
67 } // anonymous namespace
72 RangeHighlighter::RangeHighlighter(
73 const rtl::Reference
< ChartModel
> & xChartModel
) :
74 m_xSelectionSupplier(xChartModel
->getCurrentController(), uno::UNO_QUERY
),
75 m_xChartModel( xChartModel
),
76 m_nAddedListenerCount( 0 ),
77 m_bIncludeHiddenCells(true)
81 RangeHighlighter::~RangeHighlighter()
84 // ____ XRangeHighlighter ____
85 Sequence
< chart2::data::HighlightedRange
> SAL_CALL
RangeHighlighter::getSelectedRanges()
87 return m_aSelectedRanges
;
90 void RangeHighlighter::determineRanges()
92 m_aSelectedRanges
.realloc( 0 );
93 if( !m_xChartModel
.is())
95 if( !m_xSelectionSupplier
.is())
100 m_bIncludeHiddenCells
= ChartModelHelper::isIncludeHiddenCells( m_xChartModel
);
102 uno::Any
aSelection( m_xSelectionSupplier
->getSelection());
103 const uno::Type
& rType
= aSelection
.getValueType();
105 if ( rType
== cppu::UnoType
<OUString
>::get() )
107 // @todo??: maybe getSelection() should return a model object rather than a CID
111 if ( !aCID
.isEmpty() )
113 ObjectType eObjectType
= ObjectIdentifier::getObjectType( aCID
);
114 sal_Int32 nIndex
= ObjectIdentifier::getIndexFromParticleOrCID( aCID
);
115 rtl::Reference
< DataSeries
> xDataSeries( ObjectIdentifier::getDataSeriesForCID( aCID
, m_xChartModel
) );
116 if( eObjectType
== OBJECTTYPE_LEGEND_ENTRY
)
118 OUString
aParentParticel( ObjectIdentifier::getFullParentParticle( aCID
) );
119 ObjectType eParentObjectType
= ObjectIdentifier::getObjectType( aParentParticel
);
120 eObjectType
= eParentObjectType
;
121 if( eObjectType
== OBJECTTYPE_DATA_POINT
)
122 nIndex
= ObjectIdentifier::getIndexFromParticleOrCID( aParentParticel
);
125 if( eObjectType
== OBJECTTYPE_DATA_POINT
|| eObjectType
== OBJECTTYPE_DATA_LABEL
)
128 fillRangesForDataPoint( xDataSeries
, nIndex
);
131 else if( eObjectType
== OBJECTTYPE_DATA_ERRORS_X
||
132 eObjectType
== OBJECTTYPE_DATA_ERRORS_Y
||
133 eObjectType
== OBJECTTYPE_DATA_ERRORS_Z
)
135 // select error bar ranges, or data series, if the style is
136 // not set to FROM_DATA
137 fillRangesForErrorBars( ObjectIdentifier::getObjectPropertySet( aCID
, m_xChartModel
), xDataSeries
);
140 else if( xDataSeries
.is() )
143 fillRangesForDataSeries( xDataSeries
);
146 else if( eObjectType
== OBJECTTYPE_AXIS
)
149 Reference
< chart2::XAxis
> xAxis( ObjectIdentifier::getObjectPropertySet( aCID
, m_xChartModel
), uno::UNO_QUERY
);
152 fillRangesForCategories( xAxis
);
156 else if( eObjectType
== OBJECTTYPE_PAGE
157 || eObjectType
== OBJECTTYPE_DIAGRAM
158 || eObjectType
== OBJECTTYPE_DIAGRAM_WALL
159 || eObjectType
== OBJECTTYPE_DIAGRAM_FLOOR
163 rtl::Reference
< ::chart::Diagram
> xDia( ObjectIdentifier::getDiagramForCID( aCID
, m_xChartModel
) );
166 fillRangesForDiagram( xDia
);
172 else if ( rType
== cppu::UnoType
< drawing::XShape
>::get() )
174 // #i12587# support for shapes in chart
175 Reference
< drawing::XShape
> xShape
;
176 aSelection
>>= xShape
;
184 //if nothing is selected select all ranges
185 fillRangesForDiagram( m_xChartModel
->getFirstChartDiagram() );
189 catch( const uno::Exception
& )
191 DBG_UNHANDLED_EXCEPTION("chart2");
195 void RangeHighlighter::fillRangesForDiagram( const rtl::Reference
< Diagram
> & xDiagram
)
197 Sequence
< OUString
> aSelectedRanges( DataSourceHelper::getUsedDataRanges( xDiagram
));
198 m_aSelectedRanges
.realloc( aSelectedRanges
.getLength());
199 auto pSelectedRanges
= m_aSelectedRanges
.getArray();
200 // @todo: merge ranges
201 for( sal_Int32 i
=0; i
<aSelectedRanges
.getLength(); ++i
)
203 pSelectedRanges
[i
].RangeRepresentation
= aSelectedRanges
[i
];
204 pSelectedRanges
[i
].Index
= -1;
205 pSelectedRanges
[i
].PreferredColor
= sal_Int32(defaultPreferredColor
);
206 pSelectedRanges
[i
].AllowMerginigWithOtherRanges
= true;
210 void RangeHighlighter::fillRangesForDataSeries( const uno::Reference
< chart2::XDataSeries
> & xSeries
)
212 Reference
< chart2::data::XDataSource
> xSource( xSeries
, uno::UNO_QUERY
);
215 lcl_fillRanges( m_aSelectedRanges
,
216 ::chart::DataSourceHelper::getRangesFromDataSource( xSource
),
217 defaultPreferredColor
);
221 void RangeHighlighter::fillRangesForErrorBars(
222 const uno::Reference
< beans::XPropertySet
> & xErrorBar
,
223 const uno::Reference
< chart2::XDataSeries
> & xSeries
)
225 // only show error bar ranges, if the style is set to FROM_DATA
226 bool bUsesRangesAsErrorBars
= false;
229 sal_Int32 nStyle
= css::chart::ErrorBarStyle::NONE
;
230 bUsesRangesAsErrorBars
=
232 (xErrorBar
->getPropertyValue( "ErrorBarStyle") >>= nStyle
) &&
233 nStyle
== css::chart::ErrorBarStyle::FROM_DATA
);
235 catch( const uno::Exception
& )
237 DBG_UNHANDLED_EXCEPTION("chart2");
240 if( bUsesRangesAsErrorBars
)
242 Reference
< chart2::data::XDataSource
> xSource( xErrorBar
, uno::UNO_QUERY
);
245 lcl_fillRanges( m_aSelectedRanges
,
246 ::chart::DataSourceHelper::getRangesFromDataSource( xSource
),
247 defaultPreferredColor
);
252 fillRangesForDataSeries( xSeries
);
256 void RangeHighlighter::fillRangesForCategories( const Reference
< chart2::XAxis
> & xAxis
)
260 chart2::ScaleData
aData( xAxis
->getScaleData());
261 lcl_fillRanges( m_aSelectedRanges
,
262 DataSourceHelper::getRangesFromLabeledDataSequence( aData
.Categories
),
263 defaultPreferredColor
);
266 void RangeHighlighter::fillRangesForDataPoint( const rtl::Reference
< DataSeries
> & xDataSeries
, sal_Int32 nIndex
)
268 if( !xDataSeries
.is())
271 Color nPreferredColor
= defaultPreferredColor
;
272 std::vector
< chart2::data::HighlightedRange
> aHilightedRanges
;
273 const std::vector
< uno::Reference
< chart2::data::XLabeledDataSequence
> > & aLSeqSeq( xDataSeries
->getDataSequences2());
274 for( uno::Reference
< chart2::data::XLabeledDataSequence
> const & labelDataSeq
: aLSeqSeq
)
276 Reference
< chart2::data::XDataSequence
> xLabel( labelDataSeq
->getLabel());
277 Reference
< chart2::data::XDataSequence
> xValues( labelDataSeq
->getValues());
280 aHilightedRanges
.emplace_back(
281 xLabel
->getSourceRangeRepresentation(),
283 sal_Int32(nPreferredColor
),
286 sal_Int32 nUnhiddenIndex
= DataSeriesHelper::translateIndexFromHiddenToFullSequence( nIndex
, xValues
, !m_bIncludeHiddenCells
);
288 aHilightedRanges
.emplace_back(
289 xValues
->getSourceRangeRepresentation(),
291 sal_Int32(nPreferredColor
),
294 m_aSelectedRanges
= comphelper::containerToSequence( aHilightedRanges
);
297 void SAL_CALL
RangeHighlighter::addSelectionChangeListener( const Reference
< view::XSelectionChangeListener
>& xListener
)
302 if( m_nAddedListenerCount
== 0 )
304 std::unique_lock
g(m_aMutex
);
305 maSelectionChangeListeners
.addInterface( g
, xListener
);
306 ++m_nAddedListenerCount
;
308 //bring the new listener up to the current state
309 lang::EventObject
aEvent( static_cast< lang::XComponent
* >( this ) );
310 xListener
->selectionChanged( aEvent
);
313 void SAL_CALL
RangeHighlighter::removeSelectionChangeListener( const Reference
< view::XSelectionChangeListener
>& xListener
)
315 std::unique_lock
g(m_aMutex
);
316 maSelectionChangeListeners
.removeInterface( g
, xListener
);
317 --m_nAddedListenerCount
;
318 if( m_nAddedListenerCount
== 0 )
322 // ____ XSelectionChangeListener ____
323 void SAL_CALL
RangeHighlighter::selectionChanged( const lang::EventObject
& /*aEvent*/ )
327 // determine ranges of selected view objects
328 // if changed, fire an event
329 fireSelectionEvent();
332 void RangeHighlighter::fireSelectionEvent()
334 std::unique_lock
g(m_aMutex
);
335 if( maSelectionChangeListeners
.getLength(g
) )
337 lang::EventObject
aEvent( static_cast< lang::XComponent
* >( this ) );
338 maSelectionChangeListeners
.forEach(g
,
339 [&aEvent
](const css::uno::Reference
<view::XSelectionChangeListener
>& xListener
)
341 xListener
->selectionChanged(aEvent
);
347 void SAL_CALL
RangeHighlighter::disposing( const lang::EventObject
& Source
)
349 if( Source
.Source
== m_xSelectionSupplier
)
351 m_xSelectionSupplier
.clear();
352 m_aSelectedRanges
.realloc( 0 );
353 fireSelectionEvent();
357 void RangeHighlighter::startListening()
359 if( m_xSelectionSupplier
.is())
361 if( ! m_xListener
.is())
363 m_xListener
.set( new WeakSelectionChangeListenerAdapter( this ));
366 m_xSelectionSupplier
->addSelectionChangeListener( m_xListener
);
370 void RangeHighlighter::stopListening()
372 if( m_xSelectionSupplier
.is() && m_xListener
.is())
374 m_xSelectionSupplier
->removeSelectionChangeListener( m_xListener
);
379 // ____ WeakComponentImplHelperBase ____
380 // is called when dispose() is called at this component
381 void RangeHighlighter::disposing(std::unique_lock
<std::mutex
>&)
383 // @todo: remove listener. Currently the controller shows an assertion
384 // because it is already disposed
387 m_xSelectionSupplier
.clear();
388 m_nAddedListenerCount
= 0;
389 m_aSelectedRanges
.realloc( 0 );
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */