Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / TitleWrapper.cxx
blob8a30633ac935cbcfd1b1a4bad8f8f23bf5a95f1b
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 "TitleWrapper.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <ControllerLockGuard.hxx>
24 #include <comphelper/sequence.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/chart2/RelativePosition.hpp>
28 #include <com/sun/star/chart2/XTitle.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
31 #include <CharacterProperties.hxx>
32 #include <LinePropertiesHelper.hxx>
33 #include <FillProperties.hxx>
34 #include <UserDefinedProperties.hxx>
35 #include "WrappedCharacterHeightProperty.hxx"
36 #include "WrappedTextRotationProperty.hxx"
37 #include "WrappedAutomaticPositionProperties.hxx"
38 #include "WrappedScaleTextProperties.hxx"
40 #include <algorithm>
41 #include <rtl/ustrbuf.hxx>
42 #include <cppuhelper/propshlp.hxx>
44 using namespace ::com::sun::star;
45 using ::com::sun::star::beans::Property;
46 using ::osl::MutexGuard;
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Sequence;
51 namespace chart
53 class WrappedTitleStringProperty : public WrappedProperty
55 public:
56 explicit WrappedTitleStringProperty( const Reference< uno::XComponentContext >& xContext );
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;
60 virtual Any getPropertyDefault( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
62 protected:
63 Reference< uno::XComponentContext > m_xContext;
66 WrappedTitleStringProperty::WrappedTitleStringProperty( const Reference< uno::XComponentContext >& xContext )
67 : ::chart::WrappedProperty( "String", OUString() )
68 , m_xContext( xContext )
72 void WrappedTitleStringProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
74 Reference< chart2::XTitle > xTitle(xInnerPropertySet,uno::UNO_QUERY);
75 if(xTitle.is())
77 OUString aString;
78 rOuterValue >>= aString;
79 TitleHelper::setCompleteString( aString, xTitle, m_xContext );
82 Any WrappedTitleStringProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
84 Any aRet( getPropertyDefault( Reference< beans::XPropertyState >( xInnerPropertySet, uno::UNO_QUERY ) ) );
85 Reference< chart2::XTitle > xTitle(xInnerPropertySet,uno::UNO_QUERY);
86 if(xTitle.is())
88 Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
90 OUStringBuffer aBuf;
91 for( sal_Int32 i = 0; i < aStrings.getLength(); ++i )
93 aBuf.append( aStrings[ i ]->getString());
95 aRet <<= aBuf.makeStringAndClear();
97 return aRet;
99 Any WrappedTitleStringProperty::getPropertyDefault( const Reference< beans::XPropertyState >& /*xInnerPropertyState*/ ) const
101 return uno::Any( OUString() );//default title is an empty String
104 class WrappedStackedTextProperty : public WrappedProperty
106 public:
107 WrappedStackedTextProperty();
110 WrappedStackedTextProperty::WrappedStackedTextProperty()
111 : ::chart::WrappedProperty( "StackedText", "StackCharacters" )
115 }// end namespace chart
117 namespace
120 enum
122 PROP_TITLE_STRING,
123 PROP_TITLE_TEXT_ROTATION,
124 PROP_TITLE_TEXT_STACKED
127 void lcl_AddPropertiesToVector(
128 std::vector< Property > & rOutProperties )
130 rOutProperties.emplace_back( "String",
131 PROP_TITLE_STRING,
132 cppu::UnoType<OUString>::get(),
133 beans::PropertyAttribute::BOUND
134 | beans::PropertyAttribute::MAYBEVOID );
136 rOutProperties.emplace_back( "TextRotation",
137 PROP_TITLE_TEXT_ROTATION,
138 cppu::UnoType<sal_Int32>::get(),
139 beans::PropertyAttribute::BOUND
140 | beans::PropertyAttribute::MAYBEDEFAULT );
141 rOutProperties.emplace_back( "StackedText",
142 PROP_TITLE_TEXT_STACKED,
143 cppu::UnoType<bool>::get(),
144 beans::PropertyAttribute::BOUND
145 | beans::PropertyAttribute::MAYBEDEFAULT );
148 struct StaticTitleWrapperPropertyArray_Initializer
150 Sequence< Property >* operator()()
152 static Sequence< Property > aPropSeq( lcl_GetPropertySequence() );
153 return &aPropSeq;
156 private:
157 static Sequence< Property > lcl_GetPropertySequence()
159 std::vector< beans::Property > aProperties;
160 lcl_AddPropertiesToVector( aProperties );
161 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
162 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
163 ::chart::FillProperties::AddPropertiesToVector( aProperties );
164 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
165 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
166 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
168 std::sort( aProperties.begin(), aProperties.end(),
169 ::chart::PropertyNameLess() );
171 return comphelper::containerToSequence( aProperties );
175 struct StaticTitleWrapperPropertyArray : public rtl::StaticAggregate< Sequence< Property >, StaticTitleWrapperPropertyArray_Initializer >
179 } // anonymous namespace
181 namespace chart
183 namespace wrapper
186 TitleWrapper::TitleWrapper( ::chart::TitleHelper::eTitleType eTitleType,
187 const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact ) :
188 m_spChart2ModelContact( spChart2ModelContact ),
189 m_aEventListenerContainer( m_aMutex ),
190 m_eTitleType(eTitleType)
192 ControllerLockGuardUNO aCtrlLockGuard( Reference< frame::XModel >( m_spChart2ModelContact->getChart2Document(), uno::UNO_QUERY ));
193 if( !getTitleObject().is() ) //#i83831# create an empty title at the model, thus references to properties can be mapped correctly
194 TitleHelper::createTitle( m_eTitleType, OUString(), m_spChart2ModelContact->getChartModel(), m_spChart2ModelContact->m_xContext );
197 TitleWrapper::~TitleWrapper()
201 // ____ XShape ____
202 awt::Point SAL_CALL TitleWrapper::getPosition()
204 return m_spChart2ModelContact->GetTitlePosition( getTitleObject() );
207 void SAL_CALL TitleWrapper::setPosition( const awt::Point& aPosition )
209 Reference< beans::XPropertySet > xPropertySet( getInnerPropertySet() );
210 if(xPropertySet.is())
212 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
214 chart2::RelativePosition aRelativePosition;
215 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
216 aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
217 aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
218 xPropertySet->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
222 awt::Size SAL_CALL TitleWrapper::getSize()
224 return m_spChart2ModelContact->GetTitleSize( getTitleObject() );
227 void SAL_CALL TitleWrapper::setSize( const awt::Size& /*aSize*/ )
229 OSL_FAIL( "trying to set size of title" );
232 // ____ XShapeDescriptor (base of XShape) ____
233 OUString SAL_CALL TitleWrapper::getShapeType()
235 return "com.sun.star.chart.ChartTitle";
238 // ____ XComponent ____
239 void SAL_CALL TitleWrapper::dispose()
241 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
242 m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
244 MutexGuard aGuard( m_aMutex);
245 clearWrappedPropertySet();
248 void SAL_CALL TitleWrapper::addEventListener(
249 const Reference< lang::XEventListener >& xListener )
251 m_aEventListenerContainer.addInterface( xListener );
254 void SAL_CALL TitleWrapper::removeEventListener(
255 const Reference< lang::XEventListener >& aListener )
257 m_aEventListenerContainer.removeInterface( aListener );
260 Reference< beans::XPropertySet > TitleWrapper::getFirstCharacterPropertySet()
262 Reference< beans::XPropertySet > xProp;
264 Reference< chart2::XTitle > xTitle( getTitleObject() );
265 if( xTitle.is())
267 Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
268 if( aStrings.hasElements() )
269 xProp.set( aStrings[0], uno::UNO_QUERY );
272 return xProp;
275 void TitleWrapper::getFastCharacterPropertyValue( sal_Int32 nHandle, Any& rValue )
277 OSL_ASSERT( FAST_PROPERTY_ID_START_CHAR_PROP <= nHandle &&
278 nHandle < CharacterProperties::FAST_PROPERTY_ID_END_CHAR_PROP );
280 Reference< beans::XPropertySet > xProp = getFirstCharacterPropertySet();
281 Reference< beans::XFastPropertySet > xFastProp( xProp, uno::UNO_QUERY );
282 if(xProp.is())
284 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
285 if( pWrappedProperty )
287 rValue = pWrappedProperty->getPropertyValue( xProp );
289 else if( xFastProp.is() )
291 rValue = xFastProp->getFastPropertyValue( nHandle );
297 void TitleWrapper::setFastCharacterPropertyValue(
298 sal_Int32 nHandle, const Any& rValue )
300 OSL_ASSERT( FAST_PROPERTY_ID_START_CHAR_PROP <= nHandle &&
301 nHandle < CharacterProperties::FAST_PROPERTY_ID_END_CHAR_PROP );
303 Reference< chart2::XTitle > xTitle( getTitleObject() );
304 if( xTitle.is())
306 Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
307 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
309 for( sal_Int32 i = 0; i < aStrings.getLength(); ++i )
311 Reference< beans::XFastPropertySet > xFastPropertySet( aStrings[ i ], uno::UNO_QUERY );
312 Reference< beans::XPropertySet > xPropSet( xFastPropertySet, uno::UNO_QUERY );
314 if( pWrappedProperty )
315 pWrappedProperty->setPropertyValue( rValue, xPropSet );
316 else if( xFastPropertySet.is() )
317 xFastPropertySet->setFastPropertyValue( nHandle, rValue );
322 // WrappedPropertySet
324 void SAL_CALL TitleWrapper::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
326 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
327 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
329 setFastCharacterPropertyValue( nHandle, rValue );
331 else
332 WrappedPropertySet::setPropertyValue( rPropertyName, rValue );
335 Any SAL_CALL TitleWrapper::getPropertyValue( const OUString& rPropertyName )
337 Any aRet;
338 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
339 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
340 getFastCharacterPropertyValue( nHandle, aRet );
341 else
342 aRet = WrappedPropertySet::getPropertyValue( rPropertyName );
343 return aRet;
346 beans::PropertyState SAL_CALL TitleWrapper::getPropertyState( const OUString& rPropertyName )
348 beans::PropertyState aState( beans::PropertyState_DIRECT_VALUE );
350 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
351 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
353 Reference< beans::XPropertyState > xPropState( getFirstCharacterPropertySet(), uno::UNO_QUERY );
354 if( xPropState.is() )
356 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
357 if( pWrappedProperty )
358 aState = pWrappedProperty->getPropertyState( xPropState );
359 else
360 aState = xPropState->getPropertyState( rPropertyName );
363 else
364 aState = WrappedPropertySet::getPropertyState( rPropertyName );
366 return aState;
368 void SAL_CALL TitleWrapper::setPropertyToDefault( const OUString& rPropertyName )
370 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
371 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
373 Any aDefault = getPropertyDefault( rPropertyName );
374 setFastCharacterPropertyValue( nHandle, aDefault );
376 else
377 WrappedPropertySet::setPropertyToDefault( rPropertyName );
379 Any SAL_CALL TitleWrapper::getPropertyDefault( const OUString& rPropertyName )
381 Any aRet;
383 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
384 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
386 Reference< beans::XPropertyState > xPropState( getFirstCharacterPropertySet(), uno::UNO_QUERY );
387 if( xPropState.is() )
389 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
390 if( pWrappedProperty )
391 aRet = pWrappedProperty->getPropertyDefault(xPropState);
392 else
393 aRet = xPropState->getPropertyDefault( rPropertyName );
396 else
397 aRet = WrappedPropertySet::getPropertyDefault( rPropertyName );
399 return aRet;
402 void SAL_CALL TitleWrapper::addPropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
404 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
405 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
407 Reference< beans::XPropertySet > xPropSet = getFirstCharacterPropertySet();
408 if( xPropSet.is() )
409 xPropSet->addPropertyChangeListener( rPropertyName, xListener );
411 else
412 WrappedPropertySet::addPropertyChangeListener( rPropertyName, xListener );
414 void SAL_CALL TitleWrapper::removePropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
416 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
417 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
419 Reference< beans::XPropertySet > xPropSet = getFirstCharacterPropertySet();
420 if( xPropSet.is() )
421 xPropSet->removePropertyChangeListener( rPropertyName, xListener );
423 else
424 WrappedPropertySet::removePropertyChangeListener( rPropertyName, xListener );
427 //ReferenceSizePropertyProvider
428 void TitleWrapper::updateReferenceSize()
430 Reference< beans::XPropertySet > xProp( getTitleObject(), uno::UNO_QUERY );
431 if( xProp.is() )
433 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
434 xProp->setPropertyValue( "ReferencePageSize", uno::Any(
435 m_spChart2ModelContact->GetPageSize() ));
438 Any TitleWrapper::getReferenceSize()
440 Any aRet;
441 Reference< beans::XPropertySet > xProp( getTitleObject(), uno::UNO_QUERY );
442 if( xProp.is() )
443 aRet = xProp->getPropertyValue( "ReferencePageSize" );
445 return aRet;
447 awt::Size TitleWrapper::getCurrentSizeForReference()
449 return m_spChart2ModelContact->GetPageSize();
452 Reference< chart2::XTitle > TitleWrapper::getTitleObject()
454 return TitleHelper::getTitle( m_eTitleType, m_spChart2ModelContact->getChartModel() );
457 // WrappedPropertySet
459 Reference< beans::XPropertySet > TitleWrapper::getInnerPropertySet()
461 return Reference< beans::XPropertySet >( getTitleObject(), uno::UNO_QUERY );
464 const Sequence< beans::Property >& TitleWrapper::getPropertySequence()
466 return *StaticTitleWrapperPropertyArray::get();
469 std::vector< std::unique_ptr<WrappedProperty> > TitleWrapper::createWrappedProperties()
471 std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
473 aWrappedProperties.emplace_back( new WrappedTitleStringProperty( m_spChart2ModelContact->m_xContext ) );
474 aWrappedProperties.emplace_back( new WrappedTextRotationProperty( true ) );
475 aWrappedProperties.emplace_back( new WrappedStackedTextProperty() );
476 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
477 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
478 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
480 return aWrappedProperties;
483 OUString SAL_CALL TitleWrapper::getImplementationName()
485 return "com.sun.star.comp.chart.Title";
488 sal_Bool SAL_CALL TitleWrapper::supportsService( const OUString& rServiceName )
490 return cppu::supportsService(this, rServiceName);
493 css::uno::Sequence< OUString > SAL_CALL TitleWrapper::getSupportedServiceNames()
495 return {
496 "com.sun.star.chart.ChartTitle",
497 "com.sun.star.drawing.Shape",
498 "com.sun.star.xml.UserDefinedAttributesSupplier",
499 "com.sun.star.style.CharacterProperties"
503 } // namespace wrapper
504 } // namespace chart
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */