Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / LegendWrapper.cxx
blob9ec8f02819efe213903d423b0fd4fbe83e4e20b8
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 "LegendWrapper.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <comphelper/sequence.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <com/sun/star/chart/ChartLegendPosition.hpp>
26 #include <com/sun/star/chart2/LegendPosition.hpp>
27 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
28 #include <com/sun/star/chart2/RelativePosition.hpp>
30 #include <CharacterProperties.hxx>
31 #include <LinePropertiesHelper.hxx>
32 #include <FillProperties.hxx>
33 #include <UserDefinedProperties.hxx>
34 #include "WrappedCharacterHeightProperty.hxx"
35 #include <PositionAndSizeHelper.hxx>
36 #include <WrappedDirectStateProperty.hxx>
37 #include "WrappedAutomaticPositionProperties.hxx"
38 #include "WrappedScaleTextProperties.hxx"
40 #include <algorithm>
41 #include <utility>
43 using namespace ::com::sun::star;
44 using ::com::sun::star::beans::Property;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
49 namespace chart
51 namespace {
53 class WrappedLegendAlignmentProperty : public WrappedProperty
55 public:
56 WrappedLegendAlignmentProperty();
58 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
59 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
61 protected:
62 virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const override;
63 virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const override;
68 WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
69 : ::chart::WrappedProperty( "Alignment", "AnchorPosition" )
73 Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
75 Any aRet;
76 if( xInnerPropertySet.is() )
78 bool bShowLegend = true;
79 xInnerPropertySet->getPropertyValue( "Show" ) >>= bShowLegend;
80 if(!bShowLegend)
82 aRet <<= css::chart::ChartLegendPosition_NONE;
84 else
86 aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
87 aRet = convertInnerToOuterValue( aRet );
90 return aRet;
93 void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
95 if(!xInnerPropertySet.is())
96 return;
98 bool bNewShowLegend = true;
99 bool bOldShowLegend = true;
101 css::chart::ChartLegendPosition eOuterPos(css::chart::ChartLegendPosition_NONE);
102 if( (rOuterValue >>= eOuterPos) && eOuterPos == css::chart::ChartLegendPosition_NONE )
103 bNewShowLegend = false;
104 xInnerPropertySet->getPropertyValue( "Show" ) >>= bOldShowLegend;
106 if(bNewShowLegend!=bOldShowLegend)
108 xInnerPropertySet->setPropertyValue( "Show", uno::Any(bNewShowLegend) );
110 if(!bNewShowLegend)
111 return;
113 //set corresponding LegendPosition
114 Any aInnerValue = convertOuterToInnerValue( rOuterValue );
115 xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
117 //correct LegendExpansion
118 chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
119 if( aInnerValue >>= eNewInnerPos )
121 css::chart::ChartLegendExpansion eNewExpansion =
122 ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
123 eNewInnerPos == chart2::LegendPosition_LINE_START )
124 ? css::chart::ChartLegendExpansion_HIGH
125 : css::chart::ChartLegendExpansion_WIDE;
127 css::chart::ChartLegendExpansion eOldExpansion( css::chart::ChartLegendExpansion_HIGH );
128 bool bExpansionWasSet(
129 xInnerPropertySet->getPropertyValue( "Expansion" ) >>= eOldExpansion );
131 if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
132 xInnerPropertySet->setPropertyValue( "Expansion", uno::Any( eNewExpansion ));
135 //correct RelativePosition
136 Any aRelativePosition( xInnerPropertySet->getPropertyValue("RelativePosition") );
137 if(aRelativePosition.hasValue())
139 xInnerPropertySet->setPropertyValue( "RelativePosition", Any() );
143 Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
145 css::chart::ChartLegendPosition ePos = css::chart::ChartLegendPosition_NONE;
147 chart2::LegendPosition eNewPos;
148 if( rInnerValue >>= eNewPos )
150 switch( eNewPos )
152 case chart2::LegendPosition_LINE_START:
153 ePos = css::chart::ChartLegendPosition_LEFT;
154 break;
155 case chart2::LegendPosition_LINE_END:
156 ePos = css::chart::ChartLegendPosition_RIGHT;
157 break;
158 case chart2::LegendPosition_PAGE_START:
159 ePos = css::chart::ChartLegendPosition_TOP;
160 break;
161 case chart2::LegendPosition_PAGE_END:
162 ePos = css::chart::ChartLegendPosition_BOTTOM;
163 break;
165 default:
166 ePos = css::chart::ChartLegendPosition_NONE;
167 break;
170 return uno::Any( ePos );
172 Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
174 chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
176 css::chart::ChartLegendPosition ePos;
177 if( rOuterValue >>= ePos )
179 switch( ePos )
181 case css::chart::ChartLegendPosition_LEFT:
182 eNewPos = chart2::LegendPosition_LINE_START;
183 break;
184 case css::chart::ChartLegendPosition_RIGHT:
185 eNewPos = chart2::LegendPosition_LINE_END;
186 break;
187 case css::chart::ChartLegendPosition_TOP:
188 eNewPos = chart2::LegendPosition_PAGE_START;
189 break;
190 case css::chart::ChartLegendPosition_BOTTOM:
191 eNewPos = chart2::LegendPosition_PAGE_END;
192 break;
193 default: // NONE
194 break;
198 return uno::Any( eNewPos );
202 namespace
205 enum
207 PROP_LEGEND_ALIGNMENT,
208 PROP_LEGEND_EXPANSION
211 void lcl_AddPropertiesToVector(
212 std::vector< Property > & rOutProperties )
214 rOutProperties.emplace_back( "Alignment",
215 PROP_LEGEND_ALIGNMENT,
216 cppu::UnoType<css::chart::ChartLegendPosition>::get(),
217 //#i111967# no PropertyChangeEvent is fired on change so far
218 beans::PropertyAttribute::MAYBEDEFAULT );
220 rOutProperties.emplace_back( "Expansion",
221 PROP_LEGEND_EXPANSION,
222 cppu::UnoType<css::chart::ChartLegendExpansion>::get(),
223 //#i111967# no PropertyChangeEvent is fired on change so far
224 beans::PropertyAttribute::MAYBEDEFAULT );
227 const Sequence< Property >& StaticLegendWrapperPropertyArray()
229 static Sequence< Property > aPropSeq = []()
231 std::vector< css::beans::Property > aProperties;
232 lcl_AddPropertiesToVector( aProperties );
233 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
234 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
235 ::chart::FillProperties::AddPropertiesToVector( aProperties );
236 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
237 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
238 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
240 std::sort( aProperties.begin(), aProperties.end(),
241 ::chart::PropertyNameLess() );
243 return comphelper::containerToSequence( aProperties );
244 }();
245 return aPropSeq;
248 } // anonymous namespace
250 namespace chart::wrapper
253 LegendWrapper::LegendWrapper(std::shared_ptr<Chart2ModelContact> spChart2ModelContact)
254 : m_spChart2ModelContact(std::move(spChart2ModelContact))
258 LegendWrapper::~LegendWrapper()
262 // ____ XShape ____
263 awt::Point SAL_CALL LegendWrapper::getPosition()
265 return m_spChart2ModelContact->GetLegendPosition();
268 void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
270 Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
271 if( xProp.is() )
273 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
275 chart2::RelativePosition aRelativePosition;
276 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
277 aRelativePosition.Primary = aPageSize.Width == 0 ? 0 : double(aPosition.X)/double(aPageSize.Width);
278 aRelativePosition.Secondary = aPageSize.Height == 0 ? 0 : double(aPosition.Y)/double(aPageSize.Height);
279 xProp->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
283 awt::Size SAL_CALL LegendWrapper::getSize()
285 return m_spChart2ModelContact->GetLegendSize();
288 void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
290 Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
291 if( xProp.is() )
293 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
294 awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
296 awt::Point aPos( getPosition() );
297 awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
299 PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
300 , xProp, aNewPositionAndSize, awt::Rectangle(), aPageRectangle );
304 // ____ XShapeDescriptor (base of XShape) ____
305 OUString SAL_CALL LegendWrapper::getShapeType()
307 return "com.sun.star.chart.ChartLegend";
310 // ____ XComponent ____
311 void SAL_CALL LegendWrapper::dispose()
313 std::unique_lock g(m_aMutex);
314 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
315 m_aEventListenerContainer.disposeAndClear( g, lang::EventObject( xSource ) );
317 clearWrappedPropertySet();
320 void SAL_CALL LegendWrapper::addEventListener(
321 const Reference< lang::XEventListener >& xListener )
323 std::unique_lock g(m_aMutex);
324 m_aEventListenerContainer.addInterface( g, xListener );
327 void SAL_CALL LegendWrapper::removeEventListener(
328 const Reference< lang::XEventListener >& aListener )
330 std::unique_lock g(m_aMutex);
331 m_aEventListenerContainer.removeInterface( g, aListener );
334 //ReferenceSizePropertyProvider
335 void LegendWrapper::updateReferenceSize()
337 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
338 if( xProp.is() )
340 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
341 xProp->setPropertyValue( "ReferencePageSize", uno::Any(
342 m_spChart2ModelContact->GetPageSize() ));
345 Any LegendWrapper::getReferenceSize()
347 Any aRet;
348 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
349 if( xProp.is() )
350 aRet = xProp->getPropertyValue( "ReferencePageSize" );
352 return aRet;
354 awt::Size LegendWrapper::getCurrentSizeForReference()
356 return m_spChart2ModelContact->GetPageSize();
359 // WrappedPropertySet
360 Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
362 Reference< beans::XPropertySet > xRet;
363 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
364 if( xDiagram.is() )
365 xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
366 OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
367 return xRet;
370 const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
372 return StaticLegendWrapperPropertyArray();
375 std::vector< std::unique_ptr<WrappedProperty> > LegendWrapper::createWrappedProperties()
377 std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
379 aWrappedProperties.emplace_back( new WrappedLegendAlignmentProperty() );
380 aWrappedProperties.emplace_back( new WrappedProperty( "Expansion", "Expansion"));
381 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
382 //same problem as for wall: the defaults in the old chart are different for different charttypes, so we need to export explicitly
383 aWrappedProperties.emplace_back( new WrappedDirectStateProperty("FillStyle", "FillStyle"));
384 aWrappedProperties.emplace_back( new WrappedDirectStateProperty("FillColor", "FillColor"));
385 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
386 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
388 return aWrappedProperties;
391 OUString SAL_CALL LegendWrapper::getImplementationName()
393 return "com.sun.star.comp.chart.Legend";
396 sal_Bool SAL_CALL LegendWrapper::supportsService( const OUString& rServiceName )
398 return cppu::supportsService(this, rServiceName);
401 css::uno::Sequence< OUString > SAL_CALL LegendWrapper::getSupportedServiceNames()
403 return {
404 "com.sun.star.chart.ChartLegend",
405 "com.sun.star.drawing.Shape",
406 "com.sun.star.xml.UserDefinedAttributesSupplier",
407 "com.sun.star.style.CharacterProperties"
411 } // namespace chart::wrapper
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */