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