Debian Codegraph @

chart2/source/controller/chartapiwrapper/TitleWrapper.cxx

raw
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 "TitleWrapper.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <ControllerLockGuard.hxx>
23
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
30 #include <CharacterProperties.hxx>
31 #include <LinePropertiesHelper.hxx>
32 #include <FillProperties.hxx>
33 #include <UserDefinedProperties.hxx>
34 #include "WrappedCharacterHeightProperty.hxx"
35 #include "WrappedTextRotationProperty.hxx"
36 #include "WrappedAutomaticPositionProperties.hxx"
37 #include "WrappedScaleTextProperties.hxx"
38
39 #include <algorithm>
40 #include <rtl/ustrbuf.hxx>
41 #include <cppuhelper/propshlp.hxx>
42 #include <utility>
43
44 using namespace ::com::sun::star;
45 using ::com::sun::star::beans::Property;
46 using ::com::sun::star::uno::Any;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
49
50 namespace chart
51 {
52 namespace {
53
54 class WrappedTitleStringProperty : public WrappedProperty
55 {
56 public:
57 explicit WrappedTitleStringProperty( const Reference< uno::XComponentContext >& xContext );
58
59 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
60 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
61 virtual Any getPropertyDefault( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
62
63 protected:
64 Reference< uno::XComponentContext > m_xContext;
65 };
66
67 }
68
69 WrappedTitleStringProperty::WrappedTitleStringProperty( const Reference< uno::XComponentContext >& xContext )
70 : ::chart::WrappedProperty( u"String"_ustr, OUString() )
71 , m_xContext( xContext )
72 {
73 }
74
75 void WrappedTitleStringProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
76 {
77 Title* pTitle = dynamic_cast<Title*>(xInnerPropertySet.get());
78 if(pTitle)
79 {
80 OUString aString;
81 rOuterValue >>= aString;
82 TitleHelper::setCompleteString( aString, pTitle, m_xContext );
83 }
84 }
85 Any WrappedTitleStringProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
86 {
87 Any aRet( getPropertyDefault( Reference< beans::XPropertyState >( xInnerPropertySet, uno::UNO_QUERY ) ) );
88 Reference< chart2::XTitle > xTitle(xInnerPropertySet,uno::UNO_QUERY);
89 if(xTitle.is())
90 {
91 const Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
92
93 OUStringBuffer aBuf;
94 for( Reference< chart2::XFormattedString > const & formattedStr : aStrings )
95 {
96 aBuf.append( formattedStr->getString());
97 }
98 aRet <<= aBuf.makeStringAndClear();
99 }
100 return aRet;
101 }
102 Any WrappedTitleStringProperty::getPropertyDefault( const Reference< beans::XPropertyState >& /*xInnerPropertyState*/ ) const
103 {
104 return uno::Any( OUString() );//default title is an empty String
105 }
106
107 namespace {
108
109 class WrappedTitleFormStringsProperty : public WrappedProperty
110 {
111 public:
112 explicit WrappedTitleFormStringsProperty();
113
114 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
115 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
116 virtual Any getPropertyDefault( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
117 };
118
119 }
120
121 WrappedTitleFormStringsProperty::WrappedTitleFormStringsProperty()
122 : ::chart::WrappedProperty( u"FormattedStrings"_ustr, OUString() )
123 {
124 }
125
126 void WrappedTitleFormStringsProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
127 {
128 Title* pTitle = dynamic_cast<Title*>(xInnerPropertySet.get());
129 if (pTitle)
130 {
131 Sequence< Reference< chart2::XFormattedString >> xFormattedStrings;
132 rOuterValue >>= xFormattedStrings;
133 TitleHelper::setFormattedString(pTitle, xFormattedStrings);
134 }
135 }
136 Any WrappedTitleFormStringsProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
137 {
138 Any aRet(getPropertyDefault(Reference< beans::XPropertyState >(xInnerPropertySet, uno::UNO_QUERY)));
139 Reference< chart2::XTitle > xTitle(xInnerPropertySet, uno::UNO_QUERY);
140 if (xTitle.is())
141 {
142 const Sequence< Reference< chart2::XFormattedString > > aStrings(xTitle->getText());
143 aRet <<= aStrings;
144 }
145 return aRet;
146 }
147 Any WrappedTitleFormStringsProperty::getPropertyDefault( const Reference< beans::XPropertyState >& /*xInnerPropertyState*/ ) const
148 {
149 return uno::Any(Sequence< Reference< chart2::XFormattedString > >()); //default title is an empty Sequence of XFormattedStrings
150 }
151
152 namespace {
153
154 class WrappedStackedTextProperty : public WrappedProperty
155 {
156 public:
157 WrappedStackedTextProperty();
158 };
159
160 }
161
162 WrappedStackedTextProperty::WrappedStackedTextProperty()
163 : ::chart::WrappedProperty( u"StackedText"_ustr, u"StackCharacters"_ustr )
164 {
165 }
166
167 }// end namespace chart
168
169 namespace
170 {
171
172 enum
173 {
174 PROP_TITLE_STRING,
175 PROP_TITLE_FORMATTED_STRINGS,
176 PROP_TITLE_VISIBLE,
177 PROP_TITLE_TEXT_ROTATION,
178 PROP_TITLE_TEXT_STACKED
179 };
180
181 void lcl_AddPropertiesToVector(
182 std::vector< Property > & rOutProperties )
183 {
184 rOutProperties.emplace_back( "String",
185 PROP_TITLE_STRING,
186 cppu::UnoType<OUString>::get(),
187 beans::PropertyAttribute::BOUND
188 | beans::PropertyAttribute::MAYBEVOID );
189
190 rOutProperties.emplace_back( "FormattedStrings",
191 PROP_TITLE_FORMATTED_STRINGS,
192 cppu::UnoType< Sequence< Reference< chart2::XFormattedString >>>::get(),
193 beans::PropertyAttribute::BOUND
194 | beans::PropertyAttribute::MAYBEVOID );
195
196 rOutProperties.emplace_back( "Visible",
197 PROP_TITLE_VISIBLE,
198 cppu::UnoType<OUString>::get(),
199 beans::PropertyAttribute::BOUND
200 | beans::PropertyAttribute::MAYBEVOID );
201
202 rOutProperties.emplace_back( "TextRotation",
203 PROP_TITLE_TEXT_ROTATION,
204 cppu::UnoType<sal_Int32>::get(),
205 beans::PropertyAttribute::BOUND
206 | beans::PropertyAttribute::MAYBEDEFAULT );
207 rOutProperties.emplace_back( "StackedText",
208 PROP_TITLE_TEXT_STACKED,
209 cppu::UnoType<bool>::get(),
210 beans::PropertyAttribute::BOUND
211 | beans::PropertyAttribute::MAYBEDEFAULT );
212 }
213
214 const Sequence< Property > & StaticTitleWrapperPropertyArray()
215 {
216 static Sequence< Property > aPropSeq = []()
217 {
218 std::vector< beans::Property > aProperties;
219 lcl_AddPropertiesToVector( aProperties );
220 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
221 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
222 ::chart::FillProperties::AddPropertiesToVector( aProperties );
223 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
224 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
225 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
226
227 std::sort( aProperties.begin(), aProperties.end(),
228 ::chart::PropertyNameLess() );
229
230 return comphelper::containerToSequence( aProperties );
231 }();
232 return aPropSeq;
233 };
234
235
236 } // anonymous namespace
237
238 namespace chart::wrapper
239 {
240
241 TitleWrapper::TitleWrapper( ::chart::TitleHelper::eTitleType eTitleType,
242 std::shared_ptr<Chart2ModelContact> spChart2ModelContact ) :
243 m_spChart2ModelContact(std::move( spChart2ModelContact )),
244 m_eTitleType(eTitleType)
245 {
246 ControllerLockGuardUNO aCtrlLockGuard( m_spChart2ModelContact->getDocumentModel() );
247 if( !getTitleObject().is() ) //#i83831# create an empty title at the model, thus references to properties can be mapped correctly
248 TitleHelper::createTitle( m_eTitleType, OUString(), m_spChart2ModelContact->getDocumentModel(), m_spChart2ModelContact->m_xContext );
249 }
250
251 TitleWrapper::~TitleWrapper()
252 {
253 }
254
255 // ____ XShape ____
256 awt::Point SAL_CALL TitleWrapper::getPosition()
257 {
258 return m_spChart2ModelContact->GetTitlePosition( getTitleObject() );
259 }
260
261 void SAL_CALL TitleWrapper::setPosition( const awt::Point& aPosition )
262 {
263 Reference< beans::XPropertySet > xPropertySet( getInnerPropertySet() );
264 if(xPropertySet.is())
265 {
266 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
267
268 chart2::RelativePosition aRelativePosition;
269 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
270 aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
271 aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
272 xPropertySet->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
273 }
274 }
275
276 awt::Size SAL_CALL TitleWrapper::getSize()
277 {
278 return m_spChart2ModelContact->GetTitleSize( getTitleObject() );
279 }
280
281 void SAL_CALL TitleWrapper::setSize( const awt::Size& /*aSize*/ )
282 {
283 OSL_FAIL( "trying to set size of title" );
284 }
285
286 // ____ XShapeDescriptor (base of XShape) ____
287 OUString SAL_CALL TitleWrapper::getShapeType()
288 {
289 return u"com.sun.star.chart.ChartTitle"_ustr;
290 }
291
292 // ____ XComponent ____
293 void SAL_CALL TitleWrapper::dispose()
294 {
295 std::unique_lock g(m_aMutex);
296 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
297 m_aEventListenerContainer.disposeAndClear( g, lang::EventObject( xSource ) );
298
299 clearWrappedPropertySet();
300 }
301
302 void SAL_CALL TitleWrapper::addEventListener(
303 const Reference< lang::XEventListener >& xListener )
304 {
305 std::unique_lock g(m_aMutex);
306 m_aEventListenerContainer.addInterface( g, xListener );
307 }
308
309 void SAL_CALL TitleWrapper::removeEventListener(
310 const Reference< lang::XEventListener >& aListener )
311 {
312 std::unique_lock g(m_aMutex);
313 m_aEventListenerContainer.removeInterface( g, aListener );
314 }
315
316 void TitleWrapper::getFastCharacterPropertyValue( sal_Int32 nHandle, Any& rValue )
317 {
318 OSL_ASSERT( FAST_PROPERTY_ID_START_CHAR_PROP <= nHandle &&
319 nHandle < CharacterProperties::FAST_PROPERTY_ID_END_CHAR_PROP );
320
321 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
322 Reference< beans::XFastPropertySet > xFastProp( xProp, uno::UNO_QUERY );
323 if(xProp.is())
324 {
325 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
326 if( pWrappedProperty )
327 {
328 rValue = pWrappedProperty->getPropertyValue( xProp );
329 }
330 else if( xFastProp.is() )
331 {
332 rValue = xFastProp->getFastPropertyValue( nHandle );
333 }
334 }
335
336 }
337
338 void TitleWrapper::setFastCharacterPropertyValue(
339 sal_Int32 nHandle, const Any& rValue )
340 {
341 OSL_ASSERT( FAST_PROPERTY_ID_START_CHAR_PROP <= nHandle &&
342 nHandle < CharacterProperties::FAST_PROPERTY_ID_END_CHAR_PROP );
343
344 Reference< chart2::XTitle > xTitle( getTitleObject() );
345 if( !xTitle.is())
346 return;
347
348 const Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
349 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
350
351 for( Reference< chart2::XFormattedString > const & formattedStr : aStrings )
352 {
353 Reference< beans::XFastPropertySet > xFastPropertySet( formattedStr, uno::UNO_QUERY );
354 Reference< beans::XPropertySet > xPropSet( xFastPropertySet, uno::UNO_QUERY );
355
356 if( pWrappedProperty )
357 pWrappedProperty->setPropertyValue( rValue, xPropSet );
358 else if( xFastPropertySet.is() )
359 xFastPropertySet->setFastPropertyValue( nHandle, rValue );
360 }
361
362 Reference< beans::XPropertySet > xInnerProp = getInnerPropertySet();
363 Reference< beans::XFastPropertySet > xFastInnerProp( xInnerProp, uno::UNO_QUERY );
364 if (xInnerProp.is())
365 {
366 if (pWrappedProperty)
367 pWrappedProperty->setPropertyValue(rValue, xInnerProp);
368 else if (xFastInnerProp.is())
369 xFastInnerProp->setFastPropertyValue(nHandle, rValue);
370 }
371 }
372
373 // WrappedPropertySet
374
375 void SAL_CALL TitleWrapper::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
376 {
377 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
378 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
379 {
380 setFastCharacterPropertyValue( nHandle, rValue );
381 }
382 else
383 WrappedPropertySet::setPropertyValue( rPropertyName, rValue );
384 }
385
386 Any SAL_CALL TitleWrapper::getPropertyValue( const OUString& rPropertyName )
387 {
388 Any aRet;
389 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
390 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
391 getFastCharacterPropertyValue( nHandle, aRet );
392 else
393 aRet = WrappedPropertySet::getPropertyValue( rPropertyName );
394 return aRet;
395 }
396
397 beans::PropertyState SAL_CALL TitleWrapper::getPropertyState( const OUString& rPropertyName )
398 {
399 beans::PropertyState aState( beans::PropertyState_DIRECT_VALUE );
400
401 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
402 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
403 {
404 Reference< beans::XPropertyState > xPropState( getInnerPropertySet(), uno::UNO_QUERY);
405 if( xPropState.is() )
406 {
407 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
408 if( pWrappedProperty )
409 aState = pWrappedProperty->getPropertyState( xPropState );
410 else
411 aState = xPropState->getPropertyState( rPropertyName );
412 }
413 }
414 else
415 aState = WrappedPropertySet::getPropertyState( rPropertyName );
416
417 return aState;
418 }
419 void SAL_CALL TitleWrapper::setPropertyToDefault( const OUString& rPropertyName )
420 {
421 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
422 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
423 {
424 Any aDefault = getPropertyDefault( rPropertyName );
425 setFastCharacterPropertyValue( nHandle, aDefault );
426 }
427 else
428 WrappedPropertySet::setPropertyToDefault( rPropertyName );
429 }
430 Any SAL_CALL TitleWrapper::getPropertyDefault( const OUString& rPropertyName )
431 {
432 Any aRet;
433
434 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
435 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
436 {
437 Reference< beans::XPropertyState > xPropState( getInnerPropertySet(), uno::UNO_QUERY );
438 if( xPropState.is() )
439 {
440 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
441 if( pWrappedProperty )
442 aRet = pWrappedProperty->getPropertyDefault(xPropState);
443 else
444 aRet = xPropState->getPropertyDefault( rPropertyName );
445 }
446 }
447 else
448 aRet = WrappedPropertySet::getPropertyDefault( rPropertyName );
449
450 return aRet;
451 }
452
453 void SAL_CALL TitleWrapper::addPropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
454 {
455 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
456 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
457 {
458 Reference< beans::XPropertySet > xPropSet = getInnerPropertySet();
459 if( xPropSet.is() )
460 xPropSet->addPropertyChangeListener( rPropertyName, xListener );
461 }
462 else
463 WrappedPropertySet::addPropertyChangeListener( rPropertyName, xListener );
464 }
465 void SAL_CALL TitleWrapper::removePropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
466 {
467 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
468 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
469 {
470 Reference< beans::XPropertySet > xPropSet = getInnerPropertySet();
471 if( xPropSet.is() )
472 xPropSet->removePropertyChangeListener( rPropertyName, xListener );
473 }
474 else
475 WrappedPropertySet::removePropertyChangeListener( rPropertyName, xListener );
476 }
477
478 //ReferenceSizePropertyProvider
479 void TitleWrapper::updateReferenceSize()
480 {
481 Reference< beans::XPropertySet > xProp( getTitleObject(), uno::UNO_QUERY );
482 if( xProp.is() )
483 {
484 if( xProp->getPropertyValue( u"ReferencePageSize"_ustr ).hasValue() )
485 xProp->setPropertyValue( u"ReferencePageSize"_ustr, uno::Any(
486 m_spChart2ModelContact->GetPageSize() ));
487 }
488 }
489 Any TitleWrapper::getReferenceSize()
490 {
491 Any aRet;
492 Reference< beans::XPropertySet > xProp( getTitleObject(), uno::UNO_QUERY );
493 if( xProp.is() )
494 aRet = xProp->getPropertyValue( u"ReferencePageSize"_ustr );
495
496 return aRet;
497 }
498 awt::Size TitleWrapper::getCurrentSizeForReference()
499 {
500 return m_spChart2ModelContact->GetPageSize();
501 }
502
503 Reference< chart2::XTitle > TitleWrapper::getTitleObject()
504 {
505 return TitleHelper::getTitle( m_eTitleType, m_spChart2ModelContact->getDocumentModel() );
506 }
507
508 // WrappedPropertySet
509
510 Reference< beans::XPropertySet > TitleWrapper::getInnerPropertySet()
511 {
512 return Reference< beans::XPropertySet >( getTitleObject(), uno::UNO_QUERY );
513 }
514
515 const Sequence< beans::Property >& TitleWrapper::getPropertySequence()
516 {
517 return StaticTitleWrapperPropertyArray();
518 }
519
520 std::vector< std::unique_ptr<WrappedProperty> > TitleWrapper::createWrappedProperties()
521 {
522 std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
523
524 aWrappedProperties.emplace_back( new WrappedTitleStringProperty( m_spChart2ModelContact->m_xContext ) );
525 aWrappedProperties.emplace_back( new WrappedTitleFormStringsProperty() );
526 aWrappedProperties.emplace_back( new WrappedTextRotationProperty( true ) );
527 aWrappedProperties.emplace_back( new WrappedStackedTextProperty() );
528 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
529 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
530 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
531
532 return aWrappedProperties;
533 }
534
535 OUString SAL_CALL TitleWrapper::getImplementationName()
536 {
537 return u"com.sun.star.comp.chart.Title"_ustr;
538 }
539
540 sal_Bool SAL_CALL TitleWrapper::supportsService( const OUString& rServiceName )
541 {
542 return cppu::supportsService(this, rServiceName);
543 }
544
545 css::uno::Sequence< OUString > SAL_CALL TitleWrapper::getSupportedServiceNames()
546 {
547 return {
548 u"com.sun.star.chart.ChartTitle"_ustr,
549 u"com.sun.star.drawing.Shape"_ustr,
550 u"com.sun.star.xml.UserDefinedAttributesSupplier"_ustr,
551 u"com.sun.star.style.CharacterProperties"_ustr
552 };
553 }
554
555 } // namespace chart
556
557 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */