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 .
21 #include "RangeHighlighter.hxx"
22 #include "WeakListenerAdapter.hxx"
23 #include "ChartModelHelper.hxx"
24 #include "DataSourceHelper.hxx"
25 #include "ContainerHelper.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
;
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
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())
91 Reference
< frame::XController
> xController( m_xSelectionSupplier
, uno::UNO_QUERY
);
92 Reference
< frame::XModel
> xChartModel
;
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
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
)
124 fillRangesForDataPoint( xDataSeries
, nIndex
);
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
);
136 else if( xDataSeries
.is() )
139 fillRangesForDataSeries( xDataSeries
);
142 else if( OBJECTTYPE_AXIS
== eObjectType
)
145 Reference
< chart2::XAxis
> xAxis( ObjectIdentifier::getObjectPropertySet( aCID
, xChartModel
), uno::UNO_QUERY
);
148 fillRangesForCategories( xAxis
);
152 else if( OBJECTTYPE_PAGE
== eObjectType
153 || OBJECTTYPE_DIAGRAM
== eObjectType
154 || OBJECTTYPE_DIAGRAM_WALL
== eObjectType
155 || OBJECTTYPE_DIAGRAM_FLOOR
== eObjectType
159 Reference
< chart2::XDiagram
> xDia( ObjectIdentifier::getDiagramForCID( aCID
, xChartModel
) );
162 fillRangesForDiagram( xDia
);
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
;
180 //if nothing is selected select all ranges
181 Reference
< chart2::XChartDocument
> xChartDoc( xChartModel
, uno::UNO_QUERY_THROW
);
182 fillRangesForDiagram( xChartDoc
->getFirstDiagram() );
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
);
212 sal_Int32 nPreferredColor
= PREFERED_DEFAULT_COLOR
;
213 lcl_fillRanges( m_aSelectedRanges
,
214 ::chart::DataSourceHelper::getRangesFromDataSource( xSource
),
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
=
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
);
243 sal_Int32 nPreferredColor
= PREFERED_DEFAULT_COLOR
;
244 lcl_fillRanges( m_aSelectedRanges
,
245 ::chart::DataSourceHelper::getRangesFromDataSource( xSource
),
251 fillRangesForDataSeries( xSeries
);
255 void RangeHighlighter::fillRangesForCategories( const Reference
< chart2::XAxis
> & xAxis
)
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
);
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());
280 aHilightedRanges
.push_back(
281 chart2::data::HighlightedRange(
282 xLabel
->getSourceRangeRepresentation(),
287 sal_Int32 nUnhiddenIndex
= DataSeriesHelper::translateIndexFromHiddenToFullSequence( nIndex
, xValues
, !m_bIncludeHiddenCells
);
289 aHilightedRanges
.push_back(
290 chart2::data::HighlightedRange(
291 xValues
->getSourceRangeRepresentation(),
296 m_aSelectedRanges
= ContainerHelper::ContainerToSequence( aHilightedRanges
);
301 void SAL_CALL
RangeHighlighter::addSelectionChangeListener( const Reference
< view::XSelectionChangeListener
>& xListener
)
302 throw (uno::RuntimeException
)
307 if( m_nAddedListenerCount
== 0 )
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 )
326 // ____ XSelectionChangeListener ____
327 void SAL_CALL
RangeHighlighter::selectionChanged( const lang::EventObject
& /*aEvent*/ )
328 throw (uno::RuntimeException
)
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) );
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
);
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 ));
374 m_xSelectionSupplier
->addSelectionChangeListener( m_xListener
);
378 void RangeHighlighter::stopListening()
380 if( m_xSelectionSupplier
.is() && m_xListener
.is())
382 m_xSelectionSupplier
->removeSelectionChangeListener( m_xListener
);
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
396 m_xSelectionSupplier
.clear();
397 m_nAddedListenerCount
= 0;
398 m_aSelectedRanges
.realloc( 0 );
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */