| 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 "WrappedSplineProperties.hxx" |
| 21 |
#include "Chart2ModelContact.hxx" |
| 22 |
#include <FastPropertyIdRanges.hxx> |
| 23 |
#include <ChartType.hxx> |
| 24 |
#include <WrappedProperty.hxx> |
| 25 |
#include <unonames.hxx> |
| 26 |
|
| 27 |
#include <sal/log.hxx> |
| 28 |
|
| 29 |
#include <com/sun/star/chart2/CurveStyle.hpp> |
| 30 |
#include <com/sun/star/beans/PropertyAttribute.hpp> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
using namespace ::com::sun::star; |
| 34 |
using ::com::sun::star::uno::Any; |
| 35 |
using ::com::sun::star::beans::Property; |
| 36 |
|
| 37 |
namespace chart::wrapper |
| 38 |
{ |
| 39 |
|
| 40 |
namespace |
| 41 |
{ |
| 42 |
|
| 43 |
//PROPERTYTYPE is the type of the outer property |
| 44 |
|
| 45 |
template< typename PROPERTYTYPE > |
| 46 |
class WrappedSplineProperty : public WrappedProperty |
| 47 |
{ |
| 48 |
public: |
| 49 |
explicit WrappedSplineProperty( const OUString& rOuterName, OUString aInnerName |
| 50 |
, const css::uno::Any& rDefaulValue |
| 51 |
, std::shared_ptr<Chart2ModelContact> spChart2ModelContact ) |
| 52 |
: WrappedProperty(rOuterName,OUString()) |
| 53 |
, m_spChart2ModelContact(std::move(spChart2ModelContact)) |
| 54 |
, m_aOuterValue(rDefaulValue) |
| 55 |
, m_aDefaultValue(rDefaulValue) |
| 56 |
, m_aOwnInnerName(std::move(aInnerName)) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const |
| 61 |
{ |
| 62 |
rHasAmbiguousValue = false; |
| 63 |
rtl::Reference<Diagram> xDiagram = m_spChart2ModelContact->getDiagram(); |
| 64 |
if (!xDiagram) |
| 65 |
return false; |
| 66 |
bool bHasDetectableInnerValue = false; |
| 67 |
std::vector< rtl::Reference< ChartType > > aChartTypes = xDiagram->getChartTypes(); |
| 68 |
for( sal_Int32 nN = aChartTypes.size(); nN--; ) |
| 69 |
{ |
| 70 |
try |
| 71 |
{ |
| 72 |
Any aSingleValue = convertInnerToOuterValue( aChartTypes[nN]->getPropertyValue(m_aOwnInnerName) ); |
| 73 |
PROPERTYTYPE aCurValue = PROPERTYTYPE(); |
| 74 |
aSingleValue >>= aCurValue; |
| 75 |
if( !bHasDetectableInnerValue ) |
| 76 |
rValue = aCurValue; |
| 77 |
else |
| 78 |
{ |
| 79 |
if( rValue != aCurValue ) |
| 80 |
{ |
| 81 |
rHasAmbiguousValue = true; |
| 82 |
break; |
| 83 |
} |
| 84 |
else |
| 85 |
rValue = aCurValue; |
| 86 |
} |
| 87 |
bHasDetectableInnerValue = true; |
| 88 |
} |
| 89 |
catch( uno::Exception & ex ) |
| 90 |
{ |
| 91 |
//spline properties are not supported by all charttypes |
| 92 |
//in that cases this exception is ok |
| 93 |
ex.Context.is();//to have debug information without compilation warnings |
| 94 |
} |
| 95 |
} |
| 96 |
return bHasDetectableInnerValue; |
| 97 |
} |
| 98 |
void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const override |
| 99 |
{ |
| 100 |
PROPERTYTYPE aNewValue; |
| 101 |
if( ! (rOuterValue >>= aNewValue) ) |
| 102 |
throw css::lang::IllegalArgumentException( u"spline property requires different type"_ustr, nullptr, 0 ); |
| 103 |
|
| 104 |
m_aOuterValue = rOuterValue; |
| 105 |
|
| 106 |
bool bHasAmbiguousValue = false; |
| 107 |
PROPERTYTYPE aOldValue = PROPERTYTYPE(); |
| 108 |
if( !detectInnerValue( aOldValue, bHasAmbiguousValue ) ) |
| 109 |
return; |
| 110 |
|
| 111 |
if( !(bHasAmbiguousValue || aNewValue != aOldValue) ) |
| 112 |
return; |
| 113 |
|
| 114 |
std::vector< rtl::Reference< ChartType > > aChartTypes = |
| 115 |
m_spChart2ModelContact->getDiagram()->getChartTypes(); |
| 116 |
for( sal_Int32 nN = aChartTypes.size(); nN--; ) |
| 117 |
{ |
| 118 |
try |
| 119 |
{ |
| 120 |
aChartTypes[nN]->setPropertyValue(m_aOwnInnerName,convertOuterToInnerValue(uno::Any(aNewValue))); |
| 121 |
} |
| 122 |
catch( uno::Exception & ex ) |
| 123 |
{ |
| 124 |
//spline properties are not supported by all charttypes |
| 125 |
//in that cases this exception is ok |
| 126 |
ex.Context.is();//to have debug information without compilation warnings |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const override |
| 132 |
{ |
| 133 |
bool bHasAmbiguousValue = false; |
| 134 |
PROPERTYTYPE aValue; |
| 135 |
if( detectInnerValue( aValue, bHasAmbiguousValue ) ) |
| 136 |
{ |
| 137 |
m_aOuterValue <<= aValue; |
| 138 |
} |
| 139 |
return m_aOuterValue; |
| 140 |
} |
| 141 |
|
| 142 |
css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const override |
| 143 |
{ |
| 144 |
return m_aDefaultValue; |
| 145 |
} |
| 146 |
|
| 147 |
protected: |
| 148 |
std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact; |
| 149 |
mutable css::uno::Any m_aOuterValue; |
| 150 |
css::uno::Any m_aDefaultValue; |
| 151 |
// this inner name is not set as inner name at the base class |
| 152 |
const OUString m_aOwnInnerName; |
| 153 |
}; |
| 154 |
|
| 155 |
class WrappedSplineTypeProperty : public WrappedSplineProperty< sal_Int32 > |
| 156 |
{ |
| 157 |
public: |
| 158 |
explicit WrappedSplineTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact); |
| 159 |
|
| 160 |
virtual css::uno::Any convertInnerToOuterValue( const css::uno::Any& rInnerValue ) const override; |
| 161 |
virtual css::uno::Any convertOuterToInnerValue( const css::uno::Any& rOuterValue ) const override; |
| 162 |
}; |
| 163 |
|
| 164 |
enum |
| 165 |
{ |
| 166 |
//spline properties |
| 167 |
PROP_CHART_SPLINE_TYPE = FAST_PROPERTY_ID_START_CHART_SPLINE_PROP |
| 168 |
, PROP_CHART_SPLINE_ORDER |
| 169 |
, PROP_CHART_SPLINE_RESOLUTION |
| 170 |
}; |
| 171 |
|
| 172 |
}//anonymous namespace |
| 173 |
|
| 174 |
void WrappedSplineProperties::addProperties( std::vector< Property > & rOutProperties ) |
| 175 |
{ |
| 176 |
rOutProperties.emplace_back( CHART_UNONAME_SPLINE_TYPE, |
| 177 |
PROP_CHART_SPLINE_TYPE, |
| 178 |
cppu::UnoType<sal_Int32>::get(), |
| 179 |
beans::PropertyAttribute::BOUND |
| 180 |
| beans::PropertyAttribute::MAYBEDEFAULT |
| 181 |
| beans::PropertyAttribute::MAYBEVOID ); |
| 182 |
rOutProperties.emplace_back( CHART_UNONAME_SPLINE_ORDER, |
| 183 |
PROP_CHART_SPLINE_ORDER, |
| 184 |
cppu::UnoType<sal_Int32>::get(), |
| 185 |
beans::PropertyAttribute::BOUND |
| 186 |
| beans::PropertyAttribute::MAYBEDEFAULT |
| 187 |
| beans::PropertyAttribute::MAYBEVOID ); |
| 188 |
rOutProperties.emplace_back( CHART_UNONAME_SPLINE_RESOLUTION, |
| 189 |
PROP_CHART_SPLINE_RESOLUTION, |
| 190 |
cppu::UnoType<sal_Int32>::get(), |
| 191 |
beans::PropertyAttribute::BOUND |
| 192 |
| beans::PropertyAttribute::MAYBEDEFAULT |
| 193 |
| beans::PropertyAttribute::MAYBEVOID ); |
| 194 |
} |
| 195 |
|
| 196 |
void WrappedSplineProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList |
| 197 |
, const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact ) |
| 198 |
{ |
| 199 |
rList.emplace_back( new WrappedSplineTypeProperty( spChart2ModelContact ) ); |
| 200 |
rList.emplace_back( |
| 201 |
new WrappedSplineProperty<sal_Int32>( |
| 202 |
CHART_UNONAME_SPLINE_ORDER, CHART_UNONAME_SPLINE_ORDER, |
| 203 |
uno::Any(sal_Int32(3)), spChart2ModelContact)); |
| 204 |
rList.emplace_back( |
| 205 |
new WrappedSplineProperty<sal_Int32>( |
| 206 |
CHART_UNONAME_SPLINE_RESOLUTION, CHART_UNONAME_CURVE_RESOLUTION, |
| 207 |
uno::Any(sal_Int32(20)), spChart2ModelContact)); |
| 208 |
} |
| 209 |
|
| 210 |
WrappedSplineTypeProperty::WrappedSplineTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact) |
| 211 |
: WrappedSplineProperty<sal_Int32>(CHART_UNONAME_SPLINE_TYPE, CHART_UNONAME_CURVE_STYLE, uno::Any(sal_Int32(0)), spChart2ModelContact ) |
| 212 |
{ |
| 213 |
} |
| 214 |
|
| 215 |
Any WrappedSplineTypeProperty::convertInnerToOuterValue( const Any& rInnerValue ) const |
| 216 |
{ |
| 217 |
chart2::CurveStyle aInnerValue = chart2::CurveStyle_LINES; |
| 218 |
rInnerValue >>= aInnerValue; |
| 219 |
|
| 220 |
sal_Int32 nOuterValue; |
| 221 |
switch (aInnerValue) |
| 222 |
{ |
| 223 |
case chart2::CurveStyle_CUBIC_SPLINES: |
| 224 |
nOuterValue = 1; |
| 225 |
break; |
| 226 |
case chart2::CurveStyle_B_SPLINES: |
| 227 |
nOuterValue = 2; |
| 228 |
break; |
| 229 |
case chart2::CurveStyle_STEP_START: |
| 230 |
nOuterValue = 3; |
| 231 |
break; |
| 232 |
case chart2::CurveStyle_STEP_END: |
| 233 |
nOuterValue = 4; |
| 234 |
break; |
| 235 |
case chart2::CurveStyle_STEP_CENTER_X: |
| 236 |
nOuterValue = 5; |
| 237 |
break; |
| 238 |
case chart2::CurveStyle_STEP_CENTER_Y: |
| 239 |
nOuterValue = 6; |
| 240 |
break; |
| 241 |
default: |
| 242 |
nOuterValue = 0; |
| 243 |
} |
| 244 |
|
| 245 |
return uno::Any(nOuterValue); |
| 246 |
} |
| 247 |
Any WrappedSplineTypeProperty::convertOuterToInnerValue( const Any& rOuterValue ) const |
| 248 |
{ |
| 249 |
sal_Int32 nOuterValue=0; |
| 250 |
rOuterValue >>= nOuterValue; |
| 251 |
|
| 252 |
chart2::CurveStyle aInnerValue; |
| 253 |
|
| 254 |
switch (nOuterValue) |
| 255 |
{ |
| 256 |
case 1: |
| 257 |
aInnerValue = chart2::CurveStyle_CUBIC_SPLINES; |
| 258 |
break; |
| 259 |
case 2: |
| 260 |
aInnerValue = chart2::CurveStyle_B_SPLINES; |
| 261 |
break; |
| 262 |
case 3: |
| 263 |
aInnerValue = chart2::CurveStyle_STEP_START; |
| 264 |
break; |
| 265 |
case 4: |
| 266 |
aInnerValue = chart2::CurveStyle_STEP_END; |
| 267 |
break; |
| 268 |
case 5: |
| 269 |
aInnerValue = chart2::CurveStyle_STEP_CENTER_X; |
| 270 |
break; |
| 271 |
case 6: |
| 272 |
aInnerValue = chart2::CurveStyle_STEP_CENTER_Y; |
| 273 |
break; |
| 274 |
default: |
| 275 |
SAL_WARN_IF(nOuterValue != 0, "chart2", "Unknown line style"); |
| 276 |
aInnerValue = chart2::CurveStyle_LINES; |
| 277 |
} |
| 278 |
|
| 279 |
return uno::Any(aInnerValue); |
| 280 |
} |
| 281 |
|
| 282 |
} //namespace chart::wrapper |
| 283 |
|
| 284 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |