Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/chart2/source/controller/dialogs/DialogModel.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 "DialogModel.hxx"
21
#include <RangeSelectionHelper.hxx>
22
#include <DataInterpreter.hxx>
23
#include <DataSeries.hxx>
24
#include <DataSeriesHelper.hxx>
25
#include <DataSourceHelper.hxx>
26
#include <Diagram.hxx>
27
#include <strings.hrc>
28
#include <ResId.hxx>
29
#include <ControllerLockGuard.hxx>
30
#include <ChartType.hxx>
31
#include <ChartTypeTemplate.hxx>
32
#include <ThreeDHelper.hxx>
33
#include <ChartModel.hxx>
34
#include <BaseCoordinateSystem.hxx>
35
#include <LabeledDataSequence.hxx>
36
37
#include <com/sun/star/chart2/AxisType.hpp>
38
#include <com/sun/star/chart2/XDataSeriesContainer.hpp>
39
#include <comphelper/diagnose_ex.hxx>
40
41
#include <rtl/ustring.hxx>
42
43
#include <utility>
44
#include <algorithm>
45
#include <cstddef>
46
#include <iterator>
47
#include <numeric>
48
49
using namespace ::com::sun::star;
50
using namespace ::com::sun::star::chart2;
51
52
using ::com::sun::star::uno::Reference;
53
using ::com::sun::star::uno::Sequence;
54
55
namespace
56
{
57
constexpr OUString lcl_aLabelRole( u"label"_ustr );
58
59
60
OUString lcl_ConvertRole( const OUString & rRoleString )
61
0
{
62
0
    OUString aResult( rRoleString );
63
64
0
    typedef std::map< OUString, OUString > tTranslationMap;
65
0
    static const tTranslationMap aTranslationMap =
66
0
    {
67
0
        { "categories", ::chart::SchResId( STR_DATA_ROLE_CATEGORIES ) },
68
0
        { "error-bars-x", ::chart::SchResId( STR_DATA_ROLE_X_ERROR ) },
69
0
        { "error-bars-x-positive", ::chart::SchResId( STR_DATA_ROLE_X_ERROR_POSITIVE ) },
70
0
        { "error-bars-x-negative", ::chart::SchResId( STR_DATA_ROLE_X_ERROR_NEGATIVE ) },
71
0
        { "error-bars-y", ::chart::SchResId( STR_DATA_ROLE_Y_ERROR ) },
72
0
        { "error-bars-y-positive", ::chart::SchResId( STR_DATA_ROLE_Y_ERROR_POSITIVE ) },
73
0
        { "error-bars-y-negative", ::chart::SchResId( STR_DATA_ROLE_Y_ERROR_NEGATIVE ) },
74
0
        { "label",        ::chart::SchResId( STR_DATA_ROLE_LABEL ) },
75
0
        { "values-first", ::chart::SchResId( STR_DATA_ROLE_FIRST ) },
76
0
        { "values-last",  ::chart::SchResId( STR_DATA_ROLE_LAST ) },
77
0
        { "values-max",   ::chart::SchResId( STR_DATA_ROLE_MAX ) },
78
0
        { "values-min",   ::chart::SchResId( STR_DATA_ROLE_MIN ) },
79
0
        { "values-x",     ::chart::SchResId( STR_DATA_ROLE_X ) },
80
0
        { "values-y",     ::chart::SchResId( STR_DATA_ROLE_Y ) },
81
0
        { "values-size",  ::chart::SchResId( STR_DATA_ROLE_SIZE ) },
82
0
        { "FillColor",    ::chart::SchResId( STR_PROPERTY_ROLE_FILLCOLOR ) },
83
0
        { "BorderColor",  ::chart::SchResId( STR_PROPERTY_ROLE_BORDERCOLOR ) },
84
0
    };
85
86
0
    tTranslationMap::const_iterator aIt( aTranslationMap.find( rRoleString ));
87
0
    if( aIt != aTranslationMap.end())
88
0
    {
89
0
        aResult = (*aIt).second;
90
0
    }
91
0
    return aResult;
92
0
}
93
94
typedef std::map< OUString, sal_Int32 > lcl_tRoleIndexMap;
95
96
lcl_tRoleIndexMap lcl_createRoleIndexMap()
97
0
{
98
0
    lcl_tRoleIndexMap aMap;
99
0
    sal_Int32 nIndex = 0;
100
101
0
    aMap[ u"label"_ustr ] =                 ++nIndex;
102
0
    aMap[ u"categories"_ustr ] =            ++nIndex;
103
0
    aMap[ u"values-x"_ustr ] =              ++nIndex;
104
0
    aMap[ u"values-y"_ustr ] =              ++nIndex;
105
0
    aMap[ u"error-bars-x"_ustr ] =          ++nIndex;
106
0
    aMap[ u"error-bars-x-positive"_ustr ] = ++nIndex;
107
0
    aMap[ u"error-bars-x-negative"_ustr ] = ++nIndex;
108
0
    aMap[ u"error-bars-y"_ustr ] =          ++nIndex;
109
0
    aMap[ u"error-bars-y-positive"_ustr ] = ++nIndex;
110
0
    aMap[ u"error-bars-y-negative"_ustr ] = ++nIndex;
111
0
    aMap[ u"values-first"_ustr ] =          ++nIndex;
112
0
    aMap[ u"values-min"_ustr ] =            ++nIndex;
113
0
    aMap[ u"values-max"_ustr ] =            ++nIndex;
114
0
    aMap[ u"values-last"_ustr ] =           ++nIndex;
115
0
    aMap[ u"values-size"_ustr ] =           ++nIndex;
116
117
0
    return aMap;
118
0
}
119
120
121
struct lcl_RolesWithRangeAppend
122
{
123
    typedef Reference< data::XLabeledDataSequence > value_type;
124
    typedef ::chart::DialogModel::tRolesWithRanges tContainerType;
125
126
    explicit lcl_RolesWithRangeAppend( tContainerType * rCnt,
127
                                       OUString aLabelRole )
128
0
            : m_rDestCnt( rCnt ),
129
0
              m_aRoleForLabelSeq(std::move( aLabelRole ))
130
0
    {}
131
132
    lcl_RolesWithRangeAppend & operator= ( const value_type & xVal )
133
0
    {
134
0
        try
135
0
        {
136
0
            if( xVal.is())
137
0
            {
138
                // data sequence
139
0
                Reference< data::XDataSequence > xSeq( xVal->getValues());
140
0
                if( xSeq.is())
141
0
                {
142
0
                    OUString aRole;
143
0
                    Reference< beans::XPropertySet > xProp( xSeq, uno::UNO_QUERY_THROW );
144
0
                    if( xProp->getPropertyValue( u"Role"_ustr ) >>= aRole )
145
0
                    {
146
0
                        m_rDestCnt->emplace(aRole, xSeq->getSourceRangeRepresentation());
147
                        // label
148
0
                        if( aRole == m_aRoleForLabelSeq )
149
0
                        {
150
0
                            Reference< data::XDataSequence > xLabelSeq( xVal->getLabel());
151
0
                            if( xLabelSeq.is())
152
0
                            {
153
0
                                m_rDestCnt->emplace(
154
0
                                        lcl_aLabelRole, xLabelSeq->getSourceRangeRepresentation());
155
0
                            }
156
0
                        }
157
0
                    }
158
0
                }
159
0
            }
160
0
        }
161
0
        catch( const uno::Exception & )
162
0
        {
163
0
            DBG_UNHANDLED_EXCEPTION("chart2");
164
0
        }
165
0
        return *this;
166
0
    }
167
168
    // Implement output operator requirements as required by std::copy (and
169
    // implement prefix increment in terms of postfix increment to avoid unused
170
    // member function warnings for the latter in the common case where
171
    // std::copy would not actually need it):
172
0
    lcl_RolesWithRangeAppend & operator* ()     { return *this; }
173
0
    lcl_RolesWithRangeAppend & operator++ ()    { return operator++(0); }
174
0
    lcl_RolesWithRangeAppend & operator++ (int) { return *this; }
175
176
private:
177
    tContainerType * m_rDestCnt;
178
    OUString m_aRoleForLabelSeq;
179
};
180
181
}
182
183
namespace std
184
{
185
    template<> struct iterator_traits<lcl_RolesWithRangeAppend>
186
    {
187
        typedef std::output_iterator_tag iterator_category;
188
        typedef Reference< data::XLabeledDataSequence > value_type;
189
        typedef value_type& reference;
190
    };
191
}
192
193
namespace {
194
195
void lcl_SetSequenceRole(
196
    const Reference< data::XDataSequence > & xSeq,
197
    const OUString & rRole )
198
0
{
199
0
    Reference< beans::XPropertySet > xProp( xSeq, uno::UNO_QUERY );
200
0
    if( xProp.is())
201
0
        xProp->setPropertyValue( u"Role"_ustr , uno::Any( rRole ));
202
0
}
203
204
Sequence< OUString > lcl_CopyExcludingValuesFirst(
205
    Sequence< OUString > const & i_aInput )
206
0
{
207
0
    Sequence< OUString > aOutput( i_aInput.getLength());
208
0
    auto pOutput = aOutput.getArray();
209
0
    int nSourceIndex, nDestIndex;
210
0
    for( nSourceIndex = nDestIndex = 0; nSourceIndex < i_aInput.getLength(); nSourceIndex++ )
211
0
    {
212
0
        if( i_aInput[nSourceIndex] == "values-first" )
213
0
        {
214
0
            aOutput.realloc( aOutput.getLength() - 1 );
215
0
            pOutput = aOutput.getArray();
216
0
        }
217
0
        else
218
0
        {
219
0
            pOutput[nDestIndex] = i_aInput[nSourceIndex];
220
0
            nDestIndex++;
221
0
        }
222
0
    }
223
0
    return aOutput;
224
0
}
225
226
rtl::Reference< ::chart::DataSeries > lcl_CreateNewSeries(
227
    const rtl::Reference< ::chart::ChartType > & xChartType,
228
    sal_Int32 nNewSeriesIndex,
229
    sal_Int32 nTotalNumberOfSeriesInCTGroup,
230
    const rtl::Reference< ::chart::Diagram > & xDiagram,
231
    const rtl::Reference< ::chart::ChartTypeTemplate > & xTemplate,
232
    bool bCreateDataCachedSequences )
233
0
{
234
    // create plain series
235
0
    rtl::Reference< ::chart::DataSeries > xResult = new ::chart::DataSeries();
236
0
    if( xTemplate.is())
237
0
    {
238
        // @deprecated: correct default color should be found by view
239
        // without setting it as hard attribute
240
0
        Reference< XColorScheme > xColorScheme( xDiagram->getDefaultColorScheme());
241
0
        if( xColorScheme.is())
242
0
            xResult->setPropertyValue( u"Color"_ustr , uno::Any( xColorScheme->getColorByIndex( nNewSeriesIndex )));
243
0
        std::size_t nGroupIndex=0;
244
0
        if( xChartType.is())
245
0
        {
246
0
            std::vector< rtl::Reference< ::chart::ChartType > > aCTs =
247
0
                xDiagram->getChartTypes();
248
0
            for( ; nGroupIndex < aCTs.size(); ++nGroupIndex)
249
0
                if( aCTs[nGroupIndex] == xChartType )
250
0
                    break;
251
0
            if( nGroupIndex == aCTs.size())
252
0
                nGroupIndex = 0;
253
0
        }
254
0
        xTemplate->applyStyle2( xResult, nGroupIndex, nNewSeriesIndex, nTotalNumberOfSeriesInCTGroup );
255
0
    }
256
257
0
    if( bCreateDataCachedSequences )
258
0
    {
259
        // set chart type specific roles
260
0
        if( xChartType.is() )
261
0
        {
262
0
            std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewSequences;
263
0
            const OUString aRoleOfSeqForSeriesLabel = xChartType->getRoleOfSequenceForSeriesLabel();
264
0
            const OUString aLabel(::chart::SchResId(STR_DATA_UNNAMED_SERIES));
265
0
            Sequence< OUString > aPossibleRoles( xChartType->getSupportedMandatoryRoles());
266
0
            Sequence< OUString > aPossibleOptRoles( xChartType->getSupportedOptionalRoles());
267
268
            //special handling for candlestick type
269
0
            if( xTemplate.is())
270
0
            {
271
0
                rtl::Reference< ::chart::DataInterpreter > xInterpreter( xTemplate->getDataInterpreter2());
272
0
                if( xInterpreter.is())
273
0
                {
274
0
                    sal_Int32 nStockVariant;
275
0
                    if( xInterpreter->getChartTypeSpecificData(u"stock variant"_ustr) >>= nStockVariant )
276
0
                    {
277
0
                        if( nStockVariant == 0 || nStockVariant == 2) {
278
                            //delete "values-first" role
279
0
                            aPossibleRoles = lcl_CopyExcludingValuesFirst(aPossibleRoles);
280
0
                            aPossibleOptRoles = lcl_CopyExcludingValuesFirst(aPossibleOptRoles);
281
0
                        }
282
0
                    }
283
0
                }
284
0
            }
285
286
0
            const Sequence< OUString > aRoles( aPossibleRoles );
287
0
            const Sequence< OUString > aOptRoles( aPossibleOptRoles );
288
289
0
            for(OUString const & role : aRoles)
290
0
            {
291
0
                if( role == lcl_aLabelRole )
292
0
                    continue;
293
0
                Reference< data::XDataSequence > xSeq( ::chart::DataSourceHelper::createCachedDataSequence() );
294
0
                lcl_SetSequenceRole( xSeq, role );
295
                // assert that aRoleOfSeqForSeriesLabel is part of the mandatory roles
296
0
                if( role == aRoleOfSeqForSeriesLabel )
297
0
                {
298
0
                    Reference< data::XDataSequence > xLabel( ::chart::DataSourceHelper::createCachedDataSequence( aLabel ));
299
0
                    lcl_SetSequenceRole( xLabel, lcl_aLabelRole );
300
0
                    aNewSequences.push_back( ::chart::DataSourceHelper::createLabeledDataSequence( xSeq, xLabel ));
301
0
                }
302
0
                else
303
0
                    aNewSequences.push_back( ::chart::DataSourceHelper::createLabeledDataSequence( xSeq ));
304
0
            }
305
306
0
            for(OUString const & role : aOptRoles)
307
0
            {
308
0
                if( role == lcl_aLabelRole )
309
0
                    continue;
310
0
                Reference< data::XDataSequence > xSeq( ::chart::DataSourceHelper::createCachedDataSequence());
311
0
                lcl_SetSequenceRole( xSeq, role );
312
0
                aNewSequences.push_back( ::chart::DataSourceHelper::createLabeledDataSequence( xSeq ));
313
0
            }
314
315
0
            xResult->setData( aNewSequences );
316
0
        }
317
0
    }
318
319
0
    return xResult;
320
0
}
321
322
struct lcl_addSeriesNumber
323
{
324
    sal_Int32 operator() ( sal_Int32 nCurrentNumber, const Reference< XDataSeriesContainer > & xCnt ) const
325
0
    {
326
0
        if( xCnt.is())
327
0
            return nCurrentNumber + (xCnt->getDataSeries().getLength());
328
0
        return nCurrentNumber;
329
0
    }
330
};
331
332
} // anonymous namespace
333
334
namespace chart
335
{
336
337
DialogModelTimeBasedInfo::DialogModelTimeBasedInfo():
338
0
    bTimeBased(false),
339
0
    nStart(0),
340
0
    nEnd(0)
341
0
{
342
0
}
343
344
DialogModel::DialogModel(
345
    rtl::Reference<::chart::ChartModel> xChartDocument ) :
346
0
        m_xChartDocument(std::move( xChartDocument )),
347
0
        m_aTimerTriggeredControllerLock( m_xChartDocument )
348
0
{
349
0
}
350
351
DialogModel::~DialogModel()
352
0
{
353
0
    if(maTimeBasedInfo.bTimeBased)
354
0
    {
355
0
        getModel().setTimeBasedRange(maTimeBasedInfo.nStart, maTimeBasedInfo.nEnd);
356
0
    }
357
0
}
358
359
void DialogModel::setTemplate(
360
    const rtl::Reference< ChartTypeTemplate > & xTemplate )
361
0
{
362
0
    m_xTemplate = xTemplate;
363
0
}
364
365
std::shared_ptr< RangeSelectionHelper > const &
366
    DialogModel::getRangeSelectionHelper() const
367
0
{
368
0
    if( ! m_spRangeSelectionHelper)
369
0
        m_spRangeSelectionHelper =
370
0
            std::make_shared<RangeSelectionHelper>( m_xChartDocument );
371
372
0
    return m_spRangeSelectionHelper;
373
0
}
374
375
const rtl::Reference<::chart::ChartModel> & DialogModel::getChartModel() const
376
0
{
377
0
    return m_xChartDocument;
378
0
}
379
380
Reference< data::XDataProvider > DialogModel::getDataProvider() const
381
0
{
382
0
    Reference< data::XDataProvider > xResult;
383
0
    if( m_xChartDocument.is())
384
0
        xResult.set( m_xChartDocument->getDataProvider());
385
0
    return xResult;
386
0
}
387
388
std::vector< rtl::Reference< ChartType > >
389
    DialogModel::getAllDataSeriesContainers() const
390
0
{
391
0
    std::vector< rtl::Reference< ChartType > > aResult;
392
393
0
    try
394
0
    {
395
0
        if( !m_xChartDocument )
396
0
            return {};
397
0
        rtl::Reference< Diagram > xDiagram = m_xChartDocument->getFirstChartDiagram();
398
0
        if( xDiagram.is())
399
0
        {
400
0
            const std::vector< rtl::Reference< BaseCoordinateSystem > > aCooSysSeq(
401
0
                xDiagram->getBaseCoordinateSystems());
402
0
            for( rtl::Reference< BaseCoordinateSystem > const & coords : aCooSysSeq )
403
0
            {
404
0
                aResult.insert(aResult.end(),
405
0
                               coords->getChartTypes2().begin(),
406
0
                               coords->getChartTypes2().end());
407
408
0
            }
409
0
        }
410
0
    }
411
0
    catch( const uno::Exception & )
412
0
    {
413
0
        DBG_UNHANDLED_EXCEPTION("chart2");
414
0
    }
415
416
0
    return aResult;
417
0
}
418
419
std::vector< DialogModel::tSeriesWithChartTypeByName >
420
    DialogModel::getAllDataSeriesWithLabel() const
421
0
{
422
0
    std::vector< tSeriesWithChartTypeByName > aResult;
423
0
    std::vector< rtl::Reference< ChartType > > aContainers(
424
0
        getAllDataSeriesContainers());
425
426
0
    for (const auto & rxChartType : aContainers )
427
0
    {
428
0
        try
429
0
        {
430
0
            const std::vector< rtl::Reference< DataSeries > > & aSeq = rxChartType->getDataSeries2();
431
0
            OUString aRole = rxChartType->getRoleOfSequenceForSeriesLabel();
432
0
            for( rtl::Reference< DataSeries > const & dataSeries : aSeq )
433
0
            {
434
0
                aResult.push_back(
435
0
                    ::chart::DialogModel::tSeriesWithChartTypeByName(
436
0
                        dataSeries->getLabelForRole( aRole ),
437
0
                        std::make_pair( dataSeries, rxChartType )));
438
0
            }
439
0
        }
440
0
        catch( const uno::Exception & )
441
0
        {
442
0
            DBG_UNHANDLED_EXCEPTION("chart2");
443
0
        }
444
0
    }
445
446
0
    return aResult;
447
0
}
448
449
namespace {
450
451
void addMissingRoles(DialogModel::tRolesWithRanges& rResult, const uno::Sequence<OUString>& rRoles)
452
0
{
453
0
    for(OUString const & role : rRoles)
454
0
    {
455
0
        if(rResult.find(role) == rResult.end())
456
0
            rResult.emplace(role, OUString());
457
0
    }
458
0
}
459
460
/**
461
 * Insert a new data series to chart type at position after specified series
462
 * position.
463
 *
464
 * @param xChartType chart type that contains data series.
465
 * @param xSeries insertion position.  The new series will be inserted after
466
 *                this one.
467
 * @param xNewSeries new data series to insert.
468
 */
469
void addNewSeriesToContainer(
470
    const rtl::Reference<ChartType>& xChartType,
471
    const rtl::Reference<DataSeries>& xSeries,
472
    const rtl::Reference<DataSeries>& xNewSeries )
473
0
{
474
0
    auto aSeries = xChartType->getDataSeries2();
475
476
0
    auto aIt = std::find( aSeries.begin(), aSeries.end(), xSeries);
477
478
0
    if( aIt == aSeries.end())
479
        // if we have no series we insert at the first position.
480
0
        aIt = aSeries.begin();
481
0
    else
482
        // vector::insert inserts before, so we have to advance
483
0
        ++aIt;
484
485
0
    aSeries.insert(aIt, xNewSeries);
486
0
    xChartType->setDataSeries(aSeries);
487
0
}
488
489
}
490
491
DialogModel::tRolesWithRanges DialogModel::getRolesWithRanges(
492
    const Reference< XDataSeries > & xSeries,
493
    const OUString & aRoleOfSequenceForLabel,
494
    const rtl::Reference< ::chart::ChartType > & xChartType )
495
0
{
496
0
    DialogModel::tRolesWithRanges aResult;
497
0
    try
498
0
    {
499
0
        Reference< data::XDataSource > xSource( xSeries, uno::UNO_QUERY_THROW );
500
0
        const Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSource->getDataSequences());
501
0
        std::copy( aSeq.begin(), aSeq.end(),
502
0
                     lcl_RolesWithRangeAppend( &aResult, aRoleOfSequenceForLabel ));
503
0
        if( xChartType.is())
504
0
        {
505
            // add missing mandatory roles
506
0
            Sequence< OUString > aRoles( xChartType->getSupportedMandatoryRoles());
507
0
            addMissingRoles(aResult, aRoles);
508
509
            // add missing optional roles
510
0
            aRoles = xChartType->getSupportedOptionalRoles();
511
0
            addMissingRoles(aResult, aRoles);
512
513
            // add missing property roles
514
0
            aRoles = xChartType->getSupportedPropertyRoles();
515
0
            addMissingRoles(aResult, aRoles);
516
0
        }
517
0
    }
518
0
    catch( const uno::Exception & )
519
0
    {
520
0
        DBG_UNHANDLED_EXCEPTION("chart2");
521
0
    }
522
0
    return aResult;
523
0
}
524
525
void DialogModel::moveSeries(
526
    const rtl::Reference< DataSeries > & xSeries,
527
    MoveDirection eDirection )
528
0
{
529
0
    m_aTimerTriggeredControllerLock.startTimer();
530
0
    ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
531
532
0
    rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
533
0
    xDiagram->moveSeries( xSeries, eDirection==MoveDirection::Down );
534
0
}
535
536
rtl::Reference< ::chart::DataSeries > DialogModel::insertSeriesAfter(
537
    const Reference< XDataSeries > & xUnoSeries,
538
    const rtl::Reference< ::chart::ChartType > & xChartType,
539
    bool bCreateDataCachedSequences /* = false */ )
540
0
{
541
0
    m_aTimerTriggeredControllerLock.startTimer();
542
0
    ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
543
0
    rtl::Reference< ::chart::DataSeries > xNewSeries;
544
0
    rtl::Reference<DataSeries> xSeries = dynamic_cast<DataSeries*>(xUnoSeries.get());
545
0
    assert(xSeries || !xUnoSeries);
546
547
0
    try
548
0
    {
549
0
        rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram() );
550
0
        ThreeDLookScheme e3DScheme = xDiagram->detectScheme();
551
552
0
        sal_Int32 nSeriesInChartType = 0;
553
0
        const sal_Int32 nTotalSeries = countSeries();
554
0
        if( xChartType.is())
555
0
        {
556
0
            nSeriesInChartType = xChartType->getDataSeries().getLength();
557
0
        }
558
559
        // create new series
560
0
        xNewSeries =
561
0
            lcl_CreateNewSeries(
562
0
                xChartType,
563
0
                nTotalSeries, // new series' index
564
0
                nSeriesInChartType,
565
0
                xDiagram,
566
0
                m_xTemplate,
567
0
                bCreateDataCachedSequences );
568
569
        // add new series to container
570
0
        if( xNewSeries.is())
571
0
            addNewSeriesToContainer(xChartType, xSeries, xNewSeries);
572
573
0
        xDiagram->setScheme( e3DScheme );
574
0
    }
575
0
    catch( const uno::Exception & )
576
0
    {
577
0
        DBG_UNHANDLED_EXCEPTION("chart2");
578
0
    }
579
0
    return xNewSeries;
580
0
}
581
582
void DialogModel::deleteSeries(
583
    const rtl::Reference< DataSeries > & xSeries,
584
    const rtl::Reference< ChartType > & xChartType )
585
0
{
586
0
    m_aTimerTriggeredControllerLock.startTimer();
587
0
    ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
588
589
0
    xChartType->deleteSeries( xSeries );
590
0
}
591
592
uno::Reference< chart2::data::XLabeledDataSequence > DialogModel::getCategories() const
593
0
{
594
0
    uno::Reference< chart2::data::XLabeledDataSequence > xResult;
595
0
    try
596
0
    {
597
0
        if( m_xChartDocument.is())
598
0
        {
599
0
            rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
600
0
            if (xDiagram.is())
601
0
                xResult = xDiagram->getCategories();
602
0
        }
603
0
    }
604
0
    catch( const uno::Exception & )
605
0
    {
606
0
        DBG_UNHANDLED_EXCEPTION("chart2");
607
0
    }
608
0
    return xResult;
609
0
}
610
611
void DialogModel::setCategories( const Reference< chart2::data::XLabeledDataSequence > & xCategories )
612
0
{
613
0
    if( !m_xChartDocument.is())
614
0
        return;
615
616
0
    rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
617
0
    if( !xDiagram.is())
618
0
        return;
619
620
    // categories
621
0
    bool bSupportsCategories = true;
622
623
0
    rtl::Reference< ChartType > xFirstChartType( xDiagram->getChartTypeByIndex( 0 ) );
624
0
    if( xFirstChartType.is() )
625
0
    {
626
0
        sal_Int32 nAxisType = xFirstChartType->getAxisType(0); // x-axis
627
0
        bSupportsCategories = (nAxisType == AxisType::CATEGORY);
628
0
    }
629
0
    xDiagram->setCategories( xCategories, true, bSupportsCategories );
630
0
}
631
632
OUString DialogModel::getCategoriesRange() const
633
0
{
634
0
    OUString aRange;
635
0
    try
636
0
    {
637
0
        uno::Reference< chart2::data::XLabeledDataSequence > xLSeq( getCategories());
638
0
        if( xLSeq.is())
639
0
        {
640
0
            Reference< data::XDataSequence > xSeq( xLSeq->getValues());
641
0
            if( xSeq.is())
642
0
                aRange = xSeq->getSourceRangeRepresentation();
643
0
        }
644
0
    }
645
0
    catch (const lang::DisposedException&)
646
0
    {
647
0
        TOOLS_WARN_EXCEPTION( "chart2", "unexpected exception caught" );
648
0
    }
649
0
    return aRange;
650
0
}
651
652
bool DialogModel::isCategoryDiagram() const
653
0
{
654
0
    bool bRet = false;
655
0
    if( m_xChartDocument.is() && m_xChartDocument->getFirstChartDiagram())
656
0
        bRet = m_xChartDocument->getFirstChartDiagram()->isCategory();
657
0
    return bRet;
658
0
}
659
660
void DialogModel::detectArguments(
661
    OUString & rOutRangeString,
662
    bool & rOutUseColumns,
663
    bool & rOutFirstCellAsLabel,
664
    bool & rOutHasCategories ) const
665
0
{
666
0
    try
667
0
    {
668
0
        uno::Sequence< sal_Int32 > aSequenceMapping;//todo YYYX
669
670
        // Note: unused data is currently not supported in being passed to detectRangeSegmentation
671
0
        if( m_xChartDocument.is())
672
0
        {
673
0
            (void)DataSourceHelper::detectRangeSegmentation(
674
0
                m_xChartDocument,
675
0
                rOutRangeString, aSequenceMapping, rOutUseColumns, rOutFirstCellAsLabel, rOutHasCategories );
676
0
        }
677
0
    }
678
0
    catch( const uno::Exception & )
679
0
    {
680
0
        DBG_UNHANDLED_EXCEPTION("chart2");
681
0
    }
682
0
}
683
684
bool DialogModel::allArgumentsForRectRangeDetected() const
685
0
{
686
0
    return DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument );
687
0
}
688
689
void DialogModel::startControllerLockTimer()
690
0
{
691
0
    m_aTimerTriggeredControllerLock.startTimer();
692
0
}
693
694
void DialogModel::setData(
695
    const Sequence< beans::PropertyValue > & rArguments )
696
0
{
697
0
    m_aTimerTriggeredControllerLock.startTimer();
698
0
    ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
699
700
0
    Reference< data::XDataProvider > xDataProvider( getDataProvider());
701
0
    if( ! xDataProvider.is() ||
702
0
        ! m_xTemplate.is() )
703
0
    {
704
0
        OSL_FAIL( "Model objects missing" );
705
0
        return;
706
0
    }
707
708
0
    try
709
0
    {
710
0
        Reference< chart2::data::XDataSource > xDataSource(
711
0
            xDataProvider->createDataSource( rArguments ) );
712
713
0
        rtl::Reference< ::chart::DataInterpreter > xInterpreter(
714
0
            m_xTemplate->getDataInterpreter2());
715
0
        if( xInterpreter.is())
716
0
        {
717
0
            rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram() );
718
0
            ThreeDLookScheme e3DScheme = xDiagram->detectScheme();
719
720
0
            std::vector< rtl::Reference< DataSeries > > aSeriesToReUse =
721
0
                xDiagram->getDataSeries();
722
0
            applyInterpretedData(
723
0
                xInterpreter->interpretDataSource(
724
0
                    xDataSource, rArguments,
725
0
                    aSeriesToReUse ),
726
0
                aSeriesToReUse);
727
728
0
            xDiagram->setScheme( e3DScheme );
729
0
        }
730
0
    }
731
0
    catch( const uno::Exception & )
732
0
    {
733
0
        DBG_UNHANDLED_EXCEPTION("chart2");
734
0
    }
735
0
}
736
737
void DialogModel::setTimeBasedRange( bool bTimeBased, sal_Int32 nStart, sal_Int32 nEnd) const
738
0
{
739
0
    maTimeBasedInfo.nStart = nStart;
740
0
    maTimeBasedInfo.nEnd = nEnd;
741
0
    maTimeBasedInfo.bTimeBased = bTimeBased;
742
0
}
743
744
OUString DialogModel::ConvertRoleFromInternalToUI( const OUString & rRoleString )
745
0
{
746
0
    return lcl_ConvertRole( rRoleString );
747
0
}
748
749
OUString DialogModel::GetRoleDataLabel()
750
0
{
751
0
    return ::chart::SchResId(STR_OBJECT_DATALABELS);
752
0
}
753
754
sal_Int32 DialogModel::GetRoleIndexForSorting( const OUString & rInternalRoleString )
755
0
{
756
0
    static lcl_tRoleIndexMap aRoleIndexMap = lcl_createRoleIndexMap();
757
758
0
    lcl_tRoleIndexMap::const_iterator aIt( aRoleIndexMap.find( rInternalRoleString ));
759
0
    if( aIt != aRoleIndexMap.end())
760
0
        return aIt->second;
761
762
0
    return 0;
763
0
}
764
765
// private methods
766
767
void DialogModel::applyInterpretedData(
768
    const InterpretedData & rNewData,
769
    const std::vector< rtl::Reference< DataSeries > > & rSeriesToReUse )
770
0
{
771
0
    if( ! m_xChartDocument.is())
772
0
        return;
773
774
0
    m_aTimerTriggeredControllerLock.startTimer();
775
0
    rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
776
0
    if( !xDiagram.is())
777
0
        return;
778
779
    // styles
780
0
    if( m_xTemplate.is() )
781
0
    {
782
0
        sal_Int32 nGroup = 0;
783
0
        sal_Int32 nSeriesCounter = 0;
784
0
        sal_Int32 nNewSeriesIndex = static_cast< sal_Int32 >( rSeriesToReUse.size());
785
0
        const sal_Int32 nOuterSize=rNewData.Series.size();
786
787
0
        for(; nGroup < nOuterSize; ++nGroup)
788
0
        {
789
0
            const std::vector< rtl::Reference< DataSeries > > & aSeries( rNewData.Series[ nGroup ] );
790
0
            const sal_Int32 nSeriesInGroup = aSeries.size();
791
0
            for( sal_Int32 nSeries=0; nSeries<nSeriesInGroup; ++nSeries, ++nSeriesCounter )
792
0
            {
793
0
                if( std::find( rSeriesToReUse.begin(), rSeriesToReUse.end(), aSeries[nSeries] )
794
0
                    == rSeriesToReUse.end())
795
0
                {
796
0
                    if( aSeries[nSeries].is())
797
0
                    {
798
                        // @deprecated: correct default color should be found by view
799
                        // without setting it as hard attribute
800
0
                        Reference< XColorScheme > xColorScheme( xDiagram->getDefaultColorScheme());
801
0
                        if( xColorScheme.is())
802
0
                            aSeries[nSeries]->setPropertyValue( u"Color"_ustr ,
803
0
                                uno::Any( xColorScheme->getColorByIndex( nSeriesCounter )));
804
0
                    }
805
0
                    m_xTemplate->applyStyle2( aSeries[nSeries], nGroup, nNewSeriesIndex++, nSeriesInGroup );
806
0
                }
807
0
            }
808
0
        }
809
0
    }
810
811
    // data series
812
0
    std::vector< rtl::Reference< ChartType > > aSeriesCnt = getAllDataSeriesContainers();
813
814
0
    OSL_ASSERT( aSeriesCnt.size() == rNewData.Series.size());
815
816
0
    auto aSrcIt = rNewData.Series.begin();
817
0
    auto aDestIt = aSeriesCnt.begin();
818
0
    for(; aSrcIt != rNewData.Series.end() && aDestIt != aSeriesCnt.end();
819
0
        ++aSrcIt, ++aDestIt )
820
0
    {
821
0
        try
822
0
        {
823
0
            OSL_ASSERT( (*aDestIt).is());
824
0
            (*aDestIt)->setDataSeries( *aSrcIt );
825
0
        }
826
0
        catch( const uno::Exception & )
827
0
        {
828
0
            DBG_UNHANDLED_EXCEPTION("chart2");
829
0
        }
830
0
    }
831
832
0
    DialogModel::setCategories(rNewData.Categories);
833
0
}
834
835
sal_Int32 DialogModel::countSeries() const
836
0
{
837
0
    std::vector< rtl::Reference< ChartType > > aCnt( getAllDataSeriesContainers());
838
0
    return std::accumulate( aCnt.begin(), aCnt.end(), 0, lcl_addSeriesNumber());
839
0
}
840
841
ChartModel& DialogModel::getModel() const
842
0
{
843
0
    return *m_xChartDocument;
844
0
}
845
846
} //  namespace chart
847
848
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */