Coverage Report

Created: 2025-11-16 09:57

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