| 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 |
#pragma once |
| 20 |
|
| 21 |
#include <WrappedProperty.hxx> |
| 22 |
#include "Chart2ModelContact.hxx" |
| 23 |
#include <DataSeries.hxx> |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
#include <utility> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
namespace chart::wrapper |
| 30 |
{ |
| 31 |
|
| 32 |
enum tSeriesOrDiagramPropertyType |
| 33 |
{ |
| 34 |
DATA_SERIES, |
| 35 |
DIAGRAM |
| 36 |
}; |
| 37 |
|
| 38 |
//PROPERTYTYPE is the type of the outer property |
| 39 |
|
| 40 |
template< typename PROPERTYTYPE > |
| 41 |
class WrappedSeriesOrDiagramProperty : public WrappedProperty |
| 42 |
{ |
| 43 |
public: |
| 44 |
virtual PROPERTYTYPE getValueFromSeries( const css::uno::Reference< css::beans::XPropertySet >& xSeriesPropertySet ) const =0; |
| 45 |
virtual void setValueToSeries( const css::uno::Reference< css::beans::XPropertySet >& xSeriesPropertySet, const PROPERTYTYPE & aNewValue ) const =0; |
| 46 |
|
| 47 |
explicit WrappedSeriesOrDiagramProperty( const OUString& rName, const css::uno::Any& rDefaulValue |
| 48 |
, std::shared_ptr<Chart2ModelContact> spChart2ModelContact |
| 49 |
, tSeriesOrDiagramPropertyType ePropertyType ) |
| 50 |
: WrappedProperty(rName,OUString()) |
| 51 |
, m_spChart2ModelContact(std::move(spChart2ModelContact)) |
| 52 |
, m_aOuterValue(rDefaulValue) |
| 53 |
, m_aDefaultValue(rDefaulValue) |
| 54 |
, m_ePropertyType( ePropertyType ) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const |
| 59 |
{ |
| 60 |
rHasAmbiguousValue = false; |
| 61 |
if( m_ePropertyType != DIAGRAM || !m_spChart2ModelContact ) |
| 62 |
return false; |
| 63 |
bool bHasDetectableInnerValue = false; |
| 64 |
rtl::Reference<Diagram> xDiagram = m_spChart2ModelContact->getDiagram(); |
| 65 |
if (!xDiagram) |
| 66 |
return false; |
| 67 |
std::vector< rtl::Reference< DataSeries > > aSeriesVector = |
| 68 |
xDiagram->getDataSeries(); |
| 69 |
for (auto const& series : aSeriesVector) |
| 70 |
{ |
| 71 |
PROPERTYTYPE aCurValue = getValueFromSeries( series ); |
| 72 |
if( !bHasDetectableInnerValue ) |
| 73 |
rValue = aCurValue; |
| 74 |
else |
| 75 |
{ |
| 76 |
if( rValue != aCurValue ) |
| 77 |
{ |
| 78 |
rHasAmbiguousValue = true; |
| 79 |
break; |
| 80 |
} |
| 81 |
else |
| 82 |
rValue = aCurValue; |
| 83 |
} |
| 84 |
bHasDetectableInnerValue = true; |
| 85 |
} |
| 86 |
return bHasDetectableInnerValue; |
| 87 |
} |
| 88 |
void setInnerValue( PROPERTYTYPE aNewValue ) const |
| 89 |
{ |
| 90 |
if( m_ePropertyType == DIAGRAM && |
| 91 |
m_spChart2ModelContact ) |
| 92 |
{ |
| 93 |
std::vector< rtl::Reference< DataSeries > > aSeriesVector = |
| 94 |
m_spChart2ModelContact->getDiagram()->getDataSeries(); |
| 95 |
for (auto const& series : aSeriesVector) |
| 96 |
{ |
| 97 |
setValueToSeries( series, aNewValue ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
virtual void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override |
| 102 |
{ |
| 103 |
PROPERTYTYPE aNewValue = PROPERTYTYPE(); |
| 104 |
if( ! (rOuterValue >>= aNewValue) ) |
| 105 |
throw css::lang::IllegalArgumentException( u"statistic property requires different type"_ustr, nullptr, 0 ); |
| 106 |
|
| 107 |
if( m_ePropertyType == DIAGRAM ) |
| 108 |
{ |
| 109 |
m_aOuterValue = rOuterValue; |
| 110 |
|
| 111 |
bool bHasAmbiguousValue = false; |
| 112 |
PROPERTYTYPE aOldValue = PROPERTYTYPE(); |
| 113 |
if( detectInnerValue( aOldValue, bHasAmbiguousValue ) ) |
| 114 |
{ |
| 115 |
if( bHasAmbiguousValue || aNewValue != aOldValue ) |
| 116 |
setInnerValue( aNewValue ); |
| 117 |
} |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
setValueToSeries( xInnerPropertySet, aNewValue ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
virtual css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override |
| 126 |
{ |
| 127 |
if( m_ePropertyType == DIAGRAM ) |
| 128 |
{ |
| 129 |
bool bHasAmbiguousValue = false; |
| 130 |
PROPERTYTYPE aValue = PROPERTYTYPE(); |
| 131 |
if( detectInnerValue( aValue, bHasAmbiguousValue ) ) |
| 132 |
{ |
| 133 |
if(bHasAmbiguousValue) |
| 134 |
m_aOuterValue = m_aDefaultValue; |
| 135 |
else |
| 136 |
m_aOuterValue <<= aValue; |
| 137 |
} |
| 138 |
return m_aOuterValue; |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
css::uno::Any aRet( m_aDefaultValue ); |
| 143 |
aRet <<= getValueFromSeries( xInnerPropertySet ); |
| 144 |
return aRet; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
virtual css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /* xInnerPropertyState */ ) const override |
| 149 |
{ |
| 150 |
return m_aDefaultValue; |
| 151 |
} |
| 152 |
|
| 153 |
protected: |
| 154 |
std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact; |
| 155 |
mutable css::uno::Any m_aOuterValue; |
| 156 |
css::uno::Any m_aDefaultValue; |
| 157 |
tSeriesOrDiagramPropertyType m_ePropertyType; |
| 158 |
}; |
| 159 |
|
| 160 |
} //namespace chart::wrapper |
| 161 |
|
| 162 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |