Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / WrappedSymbolProperties.cxx
blob79c45ea1d888f053ffc4c1908663fd170a5150c9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "WrappedSymbolProperties.hxx"
21 #include "WrappedSeriesOrDiagramProperty.hxx"
22 #include <FastPropertyIdRanges.hxx>
23 #include <ChartType.hxx>
24 #include <ChartTypeHelper.hxx>
25 #include <com/sun/star/chart2/Symbol.hpp>
26 #include <com/sun/star/chart2/SymbolStyle.hpp>
27 #include <com/sun/star/awt/Size.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <com/sun/star/beans/XPropertyState.hpp>
30 #include <com/sun/star/chart/ChartSymbolType.hpp>
31 #include <com/sun/star/drawing/LineStyle.hpp>
32 #include <vcl/GraphicLoader.hxx>
34 #include <vcl/graph.hxx>
35 #include <comphelper/diagnose_ex.hxx>
37 using namespace ::com::sun::star;
38 using ::com::sun::star::uno::Any;
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::beans::Property;
42 namespace chart::wrapper
45 namespace
48 class WrappedSymbolTypeProperty : public WrappedSeriesOrDiagramProperty< sal_Int32 >
50 public:
51 virtual sal_Int32 getValueFromSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet ) const override;
52 virtual void setValueToSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet, const sal_Int32& aNewValue ) const override;
54 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
55 virtual beans::PropertyState getPropertyState( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
57 explicit WrappedSymbolTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
58 tSeriesOrDiagramPropertyType ePropertyType);
61 class WrappedSymbolBitmapURLProperty : public WrappedSeriesOrDiagramProperty<OUString>
63 public:
64 virtual OUString getValueFromSeries(const Reference<beans::XPropertySet>& xSeriesPropertySet) const override;
65 virtual void setValueToSeries(const Reference<beans::XPropertySet> & xSeriesPropertySet, OUString const & xNewGraphicURL) const override;
67 explicit WrappedSymbolBitmapURLProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
68 tSeriesOrDiagramPropertyType ePropertyType);
71 class WrappedSymbolBitmapProperty : public WrappedSeriesOrDiagramProperty<uno::Reference<graphic::XGraphic>>
73 public:
74 virtual uno::Reference<graphic::XGraphic> getValueFromSeries(const Reference<beans::XPropertySet>& xSeriesPropertySet) const override;
75 virtual void setValueToSeries(const Reference<beans::XPropertySet> & xSeriesPropertySet, uno::Reference<graphic::XGraphic> const & xNewGraphic) const override;
77 explicit WrappedSymbolBitmapProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
78 tSeriesOrDiagramPropertyType ePropertyType);
81 class WrappedSymbolSizeProperty : public WrappedSeriesOrDiagramProperty< awt::Size >
83 public:
84 virtual awt::Size getValueFromSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet ) const override;
85 virtual void setValueToSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet, const awt::Size& aNewSize ) const override;
86 virtual beans::PropertyState getPropertyState( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
88 explicit WrappedSymbolSizeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
89 tSeriesOrDiagramPropertyType ePropertyType);
92 class WrappedSymbolAndLinesProperty : public WrappedSeriesOrDiagramProperty< bool >
94 public:
95 virtual bool getValueFromSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet ) const override;
96 virtual void setValueToSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet, const bool& bDrawLines ) const override;
97 virtual beans::PropertyState getPropertyState( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
99 explicit WrappedSymbolAndLinesProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
100 tSeriesOrDiagramPropertyType ePropertyType);
103 enum
105 //symbol properties
106 PROP_CHART_SYMBOL_TYPE = FAST_PROPERTY_ID_START_CHART_SYMBOL_PROP,
107 PROP_CHART_SYMBOL_BITMAP_URL,
108 PROP_CHART_SYMBOL_BITMAP,
109 PROP_CHART_SYMBOL_SIZE,
110 PROP_CHART_SYMBOL_AND_LINES
113 sal_Int32 lcl_getSymbolType( const css::chart2::Symbol& rSymbol )
115 sal_Int32 nSymbol = css::chart::ChartSymbolType::NONE;
116 switch( rSymbol.Style )
118 case chart2::SymbolStyle_NONE:
119 break;
120 case chart2::SymbolStyle_AUTO:
121 nSymbol = css::chart::ChartSymbolType::AUTO;
122 break;
123 case chart2::SymbolStyle_STANDARD:
124 nSymbol = rSymbol.StandardSymbol%15;
125 break;
126 case chart2::SymbolStyle_POLYGON://new feature
127 nSymbol = css::chart::ChartSymbolType::AUTO;
128 break;
129 case chart2::SymbolStyle_GRAPHIC:
130 nSymbol = css::chart::ChartSymbolType::BITMAPURL;
131 break;
132 default:
133 nSymbol = css::chart::ChartSymbolType::AUTO;
134 break;
136 return nSymbol;
138 void lcl_setSymbolTypeToSymbol( sal_Int32 nSymbolType, chart2::Symbol& rSymbol )
140 switch( nSymbolType )
142 case css::chart::ChartSymbolType::NONE:
143 rSymbol.Style = chart2::SymbolStyle_NONE;
144 break;
145 case css::chart::ChartSymbolType::AUTO:
146 rSymbol.Style = chart2::SymbolStyle_AUTO;
147 break;
148 case css::chart::ChartSymbolType::BITMAPURL:
149 rSymbol.Style = chart2::SymbolStyle_GRAPHIC;
150 break;
151 default:
152 rSymbol.Style = chart2::SymbolStyle_STANDARD;
153 rSymbol.StandardSymbol = nSymbolType;
154 break;
158 void lcl_addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
159 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact
160 , tSeriesOrDiagramPropertyType ePropertyType )
162 rList.emplace_back( new WrappedSymbolTypeProperty( spChart2ModelContact, ePropertyType ) );
163 rList.emplace_back( new WrappedSymbolBitmapURLProperty( spChart2ModelContact, ePropertyType ) );
164 rList.emplace_back( new WrappedSymbolBitmapProperty( spChart2ModelContact, ePropertyType ) );
165 rList.emplace_back( new WrappedSymbolSizeProperty( spChart2ModelContact, ePropertyType ) );
166 rList.emplace_back( new WrappedSymbolAndLinesProperty( spChart2ModelContact, ePropertyType ) );
169 }//anonymous namespace
171 void WrappedSymbolProperties::addProperties( std::vector< Property > & rOutProperties )
173 rOutProperties.emplace_back( "SymbolType",
174 PROP_CHART_SYMBOL_TYPE,
175 cppu::UnoType<sal_Int32>::get(),
176 beans::PropertyAttribute::BOUND
177 | beans::PropertyAttribute::MAYBEDEFAULT );
179 rOutProperties.emplace_back( "SymbolBitmapURL",
180 PROP_CHART_SYMBOL_BITMAP_URL,
181 cppu::UnoType<OUString>::get(),
182 beans::PropertyAttribute::BOUND
183 | beans::PropertyAttribute::MAYBEDEFAULT );
185 rOutProperties.emplace_back( "SymbolBitmap",
186 PROP_CHART_SYMBOL_BITMAP,
187 cppu::UnoType<graphic::XGraphic>::get(),
188 beans::PropertyAttribute::BOUND
189 | beans::PropertyAttribute::MAYBEDEFAULT );
191 rOutProperties.emplace_back( "SymbolSize",
192 PROP_CHART_SYMBOL_SIZE,
193 cppu::UnoType<awt::Size>::get(),
194 beans::PropertyAttribute::BOUND
195 | beans::PropertyAttribute::MAYBEDEFAULT );
197 rOutProperties.emplace_back( "Lines",
198 PROP_CHART_SYMBOL_AND_LINES,
199 cppu::UnoType<bool>::get(),
200 beans::PropertyAttribute::BOUND
201 | beans::PropertyAttribute::MAYBEDEFAULT );
204 void WrappedSymbolProperties::addWrappedPropertiesForSeries( std::vector< std::unique_ptr<WrappedProperty> >& rList
205 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
207 lcl_addWrappedProperties( rList, spChart2ModelContact, DATA_SERIES );
210 void WrappedSymbolProperties::addWrappedPropertiesForDiagram( std::vector< std::unique_ptr<WrappedProperty> >& rList
211 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
213 lcl_addWrappedProperties( rList, spChart2ModelContact, DIAGRAM );
216 WrappedSymbolTypeProperty::WrappedSymbolTypeProperty(
217 const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
218 tSeriesOrDiagramPropertyType ePropertyType )
219 : WrappedSeriesOrDiagramProperty< sal_Int32 >( "SymbolType"
220 , uno::Any( css::chart::ChartSymbolType::NONE )
221 , spChart2ModelContact
222 , ePropertyType )
226 sal_Int32 WrappedSymbolTypeProperty::getValueFromSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet ) const
228 sal_Int32 aRet = 0;
229 m_aDefaultValue >>= aRet;
230 chart2::Symbol aSymbol;
231 if( xSeriesPropertySet.is() && ( xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol ) )
232 aRet = lcl_getSymbolType( aSymbol );
233 return aRet;
236 void WrappedSymbolTypeProperty::setValueToSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet, const sal_Int32& nSymbolType ) const
238 if(!xSeriesPropertySet.is())
239 return;
241 chart2::Symbol aSymbol;
242 xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol;
244 lcl_setSymbolTypeToSymbol( nSymbolType, aSymbol );
245 xSeriesPropertySet->setPropertyValue( "Symbol", uno::Any( aSymbol ) );
248 Any WrappedSymbolTypeProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
250 //the old chart (< OOo 2.3) needs symbol-type="automatic" at the plot-area if any of the series should be able to have symbols
251 if( m_ePropertyType == DIAGRAM )
253 bool bHasAmbiguousValue = false;
254 sal_Int32 aValue = 0;
255 if( detectInnerValue( aValue, bHasAmbiguousValue ) )
257 if(bHasAmbiguousValue)
259 m_aOuterValue <<= css::chart::ChartSymbolType::AUTO;
261 else
263 if( aValue == css::chart::ChartSymbolType::NONE )
264 m_aOuterValue <<= css::chart::ChartSymbolType::NONE;
265 else
266 m_aOuterValue <<= css::chart::ChartSymbolType::AUTO;
269 return m_aOuterValue;
271 else
273 css::uno::Any aRet( m_aDefaultValue );
274 aRet <<= getValueFromSeries( xInnerPropertySet );
275 return aRet;
279 beans::PropertyState WrappedSymbolTypeProperty::getPropertyState( const Reference< beans::XPropertyState >& xInnerPropertyState ) const
281 //the special situation for this property here is that the diagram default can be
282 //different from the normal default and different from all singles series values
283 //so we need to return PropertyState_DIRECT_VALUE for more cases
285 if( m_ePropertyType == DATA_SERIES && //single series or point
286 m_spChart2ModelContact)
288 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
289 rtl::Reference< ::chart::DataSeries > xSeries( dynamic_cast<DataSeries*>(xInnerPropertyState.get()) );
290 rtl::Reference< ChartType > xChartType( xDiagram->getChartTypeOfSeries( xSeries ) );
291 if( ChartTypeHelper::isSupportingSymbolProperties( xChartType, 2 ) )
292 return beans::PropertyState_DIRECT_VALUE;
294 return WrappedProperty::getPropertyState( xInnerPropertyState );
297 WrappedSymbolBitmapURLProperty::WrappedSymbolBitmapURLProperty(
298 const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
299 tSeriesOrDiagramPropertyType ePropertyType )
300 : WrappedSeriesOrDiagramProperty<OUString>("SymbolBitmapURL",
301 uno::Any(OUString()), spChart2ModelContact, ePropertyType)
305 OUString WrappedSymbolBitmapURLProperty::getValueFromSeries(const Reference< beans::XPropertySet >& /*xSeriesPropertySet*/) const
307 return OUString();
310 void WrappedSymbolBitmapURLProperty::setValueToSeries(
311 const Reference< beans::XPropertySet >& xSeriesPropertySet,
312 OUString const & xNewGraphicURL) const
314 if (!xSeriesPropertySet.is())
315 return;
317 chart2::Symbol aSymbol;
318 if (xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol)
320 if (!xNewGraphicURL.isEmpty())
322 Graphic aGraphic = vcl::graphic::loadFromURL(xNewGraphicURL);
323 aSymbol.Graphic.set(aGraphic.GetXGraphic());
324 xSeriesPropertySet->setPropertyValue("Symbol", uno::Any(aSymbol));
329 WrappedSymbolBitmapProperty::WrappedSymbolBitmapProperty(
330 const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
331 tSeriesOrDiagramPropertyType ePropertyType )
332 : WrappedSeriesOrDiagramProperty<uno::Reference<graphic::XGraphic>>("SymbolBitmap",
333 uno::Any(uno::Reference<graphic::XGraphic>()), spChart2ModelContact, ePropertyType)
337 uno::Reference<graphic::XGraphic> WrappedSymbolBitmapProperty::getValueFromSeries(const Reference< beans::XPropertySet >& xSeriesPropertySet) const
339 uno::Reference<graphic::XGraphic> xGraphic;
340 m_aDefaultValue >>= xGraphic;
342 chart2::Symbol aSymbol;
343 if (xSeriesPropertySet.is() && (xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol)
344 && aSymbol.Graphic.is())
346 xGraphic = aSymbol.Graphic;
348 return xGraphic;
351 void WrappedSymbolBitmapProperty::setValueToSeries(
352 const Reference< beans::XPropertySet >& xSeriesPropertySet,
353 uno::Reference<graphic::XGraphic> const & xNewGraphic) const
355 if (!xSeriesPropertySet.is())
356 return;
358 chart2::Symbol aSymbol;
359 if (xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol)
361 if (xNewGraphic.is())
363 aSymbol.Graphic.set(xNewGraphic);
364 xSeriesPropertySet->setPropertyValue("Symbol", uno::Any(aSymbol));
369 namespace
372 void lcl_correctSymbolSizeForBitmaps( chart2::Symbol& rSymbol )
374 if( rSymbol.Style != chart2::SymbolStyle_GRAPHIC )
375 return;
376 if( rSymbol.Size.Width != -1 )
377 return;
378 if( rSymbol.Size.Height != -1 )
379 return;
381 //find a good automatic size
384 const awt::Size aDefaultSize(250,250);
385 awt::Size aSize = aDefaultSize;
386 uno::Reference< beans::XPropertySet > xProp( rSymbol.Graphic, uno::UNO_QUERY );
387 if( xProp.is() )
389 bool bFoundSize = false;
392 if( xProp->getPropertyValue( "Size100thMM" ) >>= aSize )
394 if( aSize.Width == 0 && aSize.Height == 0 )
395 aSize = aDefaultSize;
396 else
397 bFoundSize = true;
400 catch( const uno::Exception& )
402 TOOLS_WARN_EXCEPTION("chart2", "" );
405 if(!bFoundSize)
407 awt::Size aAWTPixelSize(10,10);
408 if( xProp->getPropertyValue( "SizePixel" ) >>= aAWTPixelSize )
410 Size aPixelSize(aAWTPixelSize.Width,aAWTPixelSize.Height);
411 Size aNewSize = o3tl::convert(aPixelSize, o3tl::Length::pt, o3tl::Length::mm100);
413 aSize = awt::Size( aNewSize.Width(), aNewSize.Height() );
415 if( aSize.Width == 0 && aSize.Height == 0 )
416 aSize = aDefaultSize;
420 rSymbol.Size = aSize;
422 catch( const uno::Exception& )
424 TOOLS_WARN_EXCEPTION("chart2", "" );
428 }//end anonymous namespace
430 WrappedSymbolSizeProperty::WrappedSymbolSizeProperty(
431 const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
432 tSeriesOrDiagramPropertyType ePropertyType )
433 : WrappedSeriesOrDiagramProperty< awt::Size >( "SymbolSize"
434 , uno::Any( awt::Size(250,250) ), spChart2ModelContact, ePropertyType )
438 awt::Size WrappedSymbolSizeProperty::getValueFromSeries( const Reference< beans::XPropertySet >& xSeriesPropertySet ) const
440 awt::Size aRet;
441 m_aDefaultValue >>= aRet;
442 chart2::Symbol aSymbol;
443 if( xSeriesPropertySet.is() && ( xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol ))
444 aRet = aSymbol.Size;
445 return aRet;
448 void WrappedSymbolSizeProperty::setValueToSeries(
449 const Reference< beans::XPropertySet >& xSeriesPropertySet,
450 const awt::Size& aNewSize ) const
452 if(!xSeriesPropertySet.is())
453 return;
455 chart2::Symbol aSymbol;
456 if( xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol )
458 aSymbol.Size = aNewSize;
459 lcl_correctSymbolSizeForBitmaps(aSymbol);
460 xSeriesPropertySet->setPropertyValue( "Symbol", uno::Any( aSymbol ) );
464 beans::PropertyState WrappedSymbolSizeProperty::getPropertyState( const Reference< beans::XPropertyState >& xInnerPropertyState ) const
466 //only export symbol size if necessary
467 if( m_ePropertyType == DIAGRAM )
468 return beans::PropertyState_DEFAULT_VALUE;
472 chart2::Symbol aSymbol;
473 Reference< beans::XPropertySet > xSeriesPropertySet( xInnerPropertyState, uno::UNO_QUERY );
474 if( xSeriesPropertySet.is() && ( xSeriesPropertySet->getPropertyValue("Symbol") >>= aSymbol ))
476 if( aSymbol.Style != chart2::SymbolStyle_NONE )
477 return beans::PropertyState_DIRECT_VALUE;
480 catch( const uno::Exception & )
482 DBG_UNHANDLED_EXCEPTION("chart2");
484 return beans::PropertyState_DEFAULT_VALUE;
487 WrappedSymbolAndLinesProperty::WrappedSymbolAndLinesProperty(
488 const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact,
489 tSeriesOrDiagramPropertyType ePropertyType )
490 : WrappedSeriesOrDiagramProperty< bool >( "Lines"
491 , uno::Any( true ), spChart2ModelContact, ePropertyType )
495 bool WrappedSymbolAndLinesProperty::getValueFromSeries( const Reference< beans::XPropertySet >& /*xSeriesPropertySet*/ ) const
497 //do not export this property anymore, instead use a linestyle none for no lines
498 return true;
501 void WrappedSymbolAndLinesProperty::setValueToSeries(
502 const Reference< beans::XPropertySet >& xSeriesPropertySet,
503 const bool& bDrawLines ) const
505 if(!xSeriesPropertySet.is())
506 return;
508 drawing::LineStyle eOldLineStyle( drawing::LineStyle_SOLID );
509 xSeriesPropertySet->getPropertyValue( "LineStyle" ) >>= eOldLineStyle;
510 if( bDrawLines )
512 //#i114298# don't overwrite dashed lines with solid lines here
513 if( eOldLineStyle == drawing::LineStyle_NONE )
514 xSeriesPropertySet->setPropertyValue( "LineStyle", uno::Any( drawing::LineStyle_SOLID ) );
516 else
518 if( eOldLineStyle != drawing::LineStyle_NONE )
519 xSeriesPropertySet->setPropertyValue( "LineStyle", uno::Any( drawing::LineStyle_NONE ) );
523 beans::PropertyState WrappedSymbolAndLinesProperty::getPropertyState( const Reference< beans::XPropertyState >& /*xInnerPropertyState*/ ) const
525 //do not export this property anymore, instead use a linestyle none for no lines
526 return beans::PropertyState_DEFAULT_VALUE;
529 } //namespace chart
531 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */