LibreOffice Module chart2 (master) 1
OPropertySet.cxx
Go to the documentation of this file.
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 <OPropertySet.hxx>
21#include <CloneHelper.hxx>
23#include <com/sun/star/uno/Sequence.hxx>
24#include <com/sun/star/style/XStyle.hpp>
25
26#include <algorithm>
27#include <memory>
28
29using namespace ::com::sun::star;
30
31using ::com::sun::star::uno::Reference;
32using ::com::sun::star::uno::Sequence;
33using ::com::sun::star::uno::Any;
34using ::osl::MutexGuard;
35
36// needed for MS compiler
37using ::cppu::OBroadcastHelper;
38using ::cppu::OPropertySetHelper;
39
40namespace property
41{
42
45 // the following causes a warning; there seems to be no way to avoid it
46 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
47 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
48{
49}
50
53 // the following causes a warning; there seems to be no way to avoid it
54 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
55 css::lang::XTypeProvider(),
56 css::beans::XPropertyState(),
57 css::beans::XMultiPropertyStates(),
58 css::style::XStyleSupplier(),
59 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
60{
61 MutexGuard aGuard( m_aMutex );
62
64
65 // clone interface properties
66 for(auto& rProp : m_aProperties)
67 {
68 if( rProp.second.hasValue() &&
69 rProp.second.getValueType().getTypeClass() == uno::TypeClass_INTERFACE )
70 {
71 Reference< util::XCloneable > xCloneable;
72 if( rProp.second >>= xCloneable )
73 rProp.second <<= xCloneable->createClone();
74 }
75 }
76
78}
79
81{
83}
84
86{}
87
89{
90 return ::cppu::queryInterface(
91 aType,
92 static_cast< lang::XTypeProvider * >( this ),
93 static_cast< beans::XPropertySet * >( this ),
94 static_cast< beans::XMultiPropertySet * >( this ),
95 static_cast< beans::XFastPropertySet * >( this ),
96 static_cast< beans::XPropertyState * >( this ),
97 static_cast< beans::XMultiPropertyStates * >( this ),
98 static_cast< style::XStyleSupplier * >( this ) );
99}
100
101// ____ XTypeProvider ____
102Sequence< uno::Type > SAL_CALL
104{
105 static const Sequence< uno::Type > aTypeList{
113
114 return aTypeList;
115}
116
119{
120 return css::uno::Sequence<sal_Int8>();
121}
122
123// ____ XPropertyState ____
124beans::PropertyState SAL_CALL
125 OPropertySet::getPropertyState( const OUString& PropertyName )
126{
128
130 rPH.getHandleByName( PropertyName ));
131}
132
133Sequence< beans::PropertyState > SAL_CALL
134 OPropertySet::getPropertyStates( const Sequence< OUString >& aPropertyName )
135{
137
138 std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[ aPropertyName.getLength() ]);
139 rPH.fillHandles( pHandles.get(), aPropertyName );
140
141 std::vector< sal_Int32 > aHandles( pHandles.get(), pHandles.get() + aPropertyName.getLength());
142 pHandles.reset();
143
144 return GetPropertyStatesByHandle( aHandles );
145}
146
147void SAL_CALL
148 OPropertySet::setPropertyToDefault( const OUString& PropertyName )
149{
151
152 SetPropertyToDefault( rPH.getHandleByName( PropertyName ));
154}
155
156Any SAL_CALL
157 OPropertySet::getPropertyDefault( const OUString& aPropertyName )
158{
160
161 Any any;
162 GetDefaultValue( rPH.getHandleByName( aPropertyName ), any );
163 return any;
164}
165
166// ____ XMultiPropertyStates ____
167
168// Note: getPropertyStates() is already implemented in XPropertyState with the
169// same signature
170
171void SAL_CALL
173{
176}
177
178void SAL_CALL
179 OPropertySet::setPropertiesToDefault( const Sequence< OUString >& aPropertyNames )
180{
182
183 std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[ aPropertyNames.getLength() ]);
184 rPH.fillHandles( pHandles.get(), aPropertyNames );
185
186 std::vector< sal_Int32 > aHandles( pHandles.get(), pHandles.get() + aPropertyNames.getLength());
187 pHandles.reset();
188
189 SetPropertiesToDefault( aHandles );
190}
191
192Sequence< Any > SAL_CALL
193 OPropertySet::getPropertyDefaults( const Sequence< OUString >& aPropertyNames )
194{
196 const sal_Int32 nElements = aPropertyNames.getLength();
197
198 Sequence< Any > aResult( nElements );
199 Any * pResultArray = aResult.getArray();
200 sal_Int32 nI = 0;
201
202 for( ; nI < nElements; ++nI )
203 {
205 rPH.getHandleByName( aPropertyNames[ nI ] ),
206 pResultArray[ nI ]);
207 }
208
209 return aResult;
210}
211
213 ( Any & rConvertedValue,
214 Any & rOldValue,
215 sal_Int32 nHandle,
216 const Any& rValue )
217{
218 getFastPropertyValue( rOldValue, nHandle );
219 //accept longs also for short values
220 {
221 sal_Int16 nValue;
222 if( (rOldValue>>=nValue) && !(rValue>>=nValue) )
223 {
224 sal_Int32 n32Value = 0;
225 if( rValue>>=n32Value )
226 {
227 rConvertedValue <<= static_cast<sal_Int16>(n32Value);
228 return true;
229 }
230
231 sal_Int64 n64Value = 0;
232 if( rValue>>=n64Value )
233 {
234 rConvertedValue <<= static_cast<sal_Int16>(n64Value);
235 return true;
236 }
237 }
238 }
239 rConvertedValue = rValue;
240 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && rOldValue == rConvertedValue )
241 return false;//no change necessary
242 return true;
243}
244
246 ( sal_Int32 nHandle,
247 const Any& rValue )
248{
249#if OSL_DEBUG_LEVEL > 0
250 if( rValue.hasValue())
251 {
253 OUString aName;
254 rPH.fillPropertyMembersByHandle( &aName, nullptr, nHandle );
255 OSL_ENSURE( rValue.isExtractableTo( rPH.getPropertyByName( aName ).Type ),
256 "Property type is wrong" );
257 }
258#endif
259
260 Any aDefault;
261 try
262 {
263 GetDefaultValue( nHandle, aDefault );
264 }
265 catch( const beans::UnknownPropertyException& )
266 {
267 aDefault.clear();
268 }
270 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && aDefault.hasValue() && aDefault == rValue ) //#i98893# don't export defaults to file
272 else
274}
275
277 ( Any& rValue,
278 sal_Int32 nHandle ) const
279{
280 if( GetPropertyValueByHandle( rValue, nHandle ))
281 return;
282
283 // property was not set -> try style
284 uno::Reference< beans::XFastPropertySet > xStylePropSet( m_xStyle, uno::UNO_QUERY );
285 if( xStylePropSet.is() )
286 {
287#ifdef DBG_UTIL
288 {
289 // check if the handle of the style points to the same property
290 // name as the handle in this property set
291 uno::Reference< beans::XPropertySet > xPropSet( xStylePropSet, uno::UNO_QUERY );
292 if( xPropSet.is())
293 {
294 uno::Reference< beans::XPropertySetInfo > xInfo = xPropSet->getPropertySetInfo();
295 if( xInfo.is() )
296 {
297 // for some reason the virtual method getInfoHelper() is
298 // not const
300 const_cast< OPropertySet * >( this )->getInfoHelper();
301
302 // find the Property with Handle nHandle in Style
303 Sequence< beans::Property > aProps( xInfo->getProperties() );
304 sal_Int32 nI = aProps.getLength() - 1;
305 while( ( nI >= 0 ) && nHandle != aProps[ nI ].Handle )
306 --nI;
307
308 if( nI >= 0 ) // => nHandle == aProps[nI].Handle
309 {
310 // check whether the handle in this property set is
311 // the same as the one in the style
312 beans::Property aProp( rPH.getPropertyByName( aProps[ nI ].Name ) );
313 OSL_ENSURE( nHandle == aProp.Handle,
314 "HandleCheck: Handles for same property differ!" );
315
316 if( nHandle == aProp.Handle )
317 {
318 OSL_ENSURE( aProp.Type == aProps[nI].Type,
319 "HandleCheck: Types differ!" );
320 OSL_ENSURE( aProp.Attributes == aProps[nI].Attributes,
321 "HandleCheck: Attributes differ!" );
322 }
323 }
324 else
325 {
326 OSL_FAIL( "HandleCheck: Handle not found in Style" );
327 }
328 }
329 else
330 OSL_FAIL( "HandleCheck: Invalid XPropertySetInfo returned" );
331 }
332 else
333 OSL_FAIL( "HandleCheck: XPropertySet not supported" );
334 }
335#endif
336 rValue = xStylePropSet->getFastPropertyValue( nHandle );
337 }
338 else
339 {
340 // there is no style (or the style does not support XFastPropertySet)
341 // => take the default value
342 try
343 {
344 GetDefaultValue( nHandle, rValue );
345 }
346 catch( const beans::UnknownPropertyException& )
347 {
348 rValue.clear();
349 }
350 }
351}
352
354{
355 // nothing in base class
356}
357
358// ____ XStyleSupplier ____
359Reference< style::XStyle > SAL_CALL OPropertySet::getStyle()
360{
361 return m_xStyle;
362}
363
364void SAL_CALL OPropertySet::setStyle( const Reference< style::XStyle >& xStyle )
365{
366 if( ! SetStyle( xStyle ))
367 throw lang::IllegalArgumentException(
368 "Empty Style",
369 static_cast< beans::XPropertySet * >( this ),
370 0 );
371}
372
373// ____ XMultiPropertySet ____
375 const Sequence< OUString >& PropertyNames, const Sequence< Any >& Values )
376{
378
380}
381
382// ____ XFastPropertySet ____
383void SAL_CALL OPropertySet::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
384{
386
388}
389
390beans::PropertyState OPropertySet::GetPropertyStateByHandle( sal_Int32 nHandle ) const
391{
392 if( m_aProperties.end() == m_aProperties.find( nHandle ))
393 return beans::PropertyState_DEFAULT_VALUE;
394 return beans::PropertyState_DIRECT_VALUE;
395}
396
397Sequence< beans::PropertyState > OPropertySet::GetPropertyStatesByHandle(
398 const std::vector< sal_Int32 > & aHandles ) const
399{
400 Sequence< beans::PropertyState > aResult( aHandles.size());
401
402 std::transform( aHandles.begin(), aHandles.end(),
403 aResult.getArray(),
404 [this](sal_Int32 nHandle) { return GetPropertyStateByHandle(nHandle); });
405
406 return aResult;
407}
408
409void OPropertySet::SetPropertyToDefault( sal_Int32 nHandle )
410{
411 auto aFoundIter( m_aProperties.find( nHandle ) );
412
413 if( m_aProperties.end() != aFoundIter )
414 {
415 m_aProperties.erase( aFoundIter );
416 }
417}
418
420 const std::vector< sal_Int32 > & aHandles )
421{
422 for(auto nHandle : aHandles)
423 m_aProperties.erase(nHandle);
424}
425
427{
428 m_aProperties.clear();
429}
430
432 Any & rValue,
433 sal_Int32 nHandle ) const
434{
435 bool bResult = false;
436
437 auto aFoundIter( m_aProperties.find( nHandle ) );
438
439 if( m_aProperties.end() != aFoundIter )
440 {
441 rValue = (*aFoundIter).second;
442 bResult = true;
443 }
444
445 return bResult;
446}
447
449 sal_Int32 nHandle, const Any & rValue )
450{
451 m_aProperties[ nHandle ] = rValue;
452}
453
454bool OPropertySet::SetStyle( const Reference< style::XStyle > & xStyle )
455{
456 if( ! xStyle.is())
457 return false;
458
459 m_xStyle = xStyle;
460 return true;
461}
462
463
464} // namespace property
465
466/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Any & any
mutable::osl::Mutex m_aMutex
virtual sal_Bool SAL_CALL fillPropertyMembersByHandle(::rtl::OUString *pPropName, sal_Int16 *pAttributes, sal_Int32 nHandle)=0
virtual sal_Int32 SAL_CALL fillHandles(sal_Int32 *pHandles, const css::uno::Sequence< ::rtl::OUString > &rPropNames)=0
virtual sal_Int32 SAL_CALL getHandleByName(const ::rtl::OUString &rPropertyName)=0
virtual css::beans::Property SAL_CALL getPropertyByName(const ::rtl::OUString &rPropertyName)=0
virtual void SAL_CALL setFastPropertyValue(sal_Int32 nHandle, const css::uno::Any &rValue) SAL_OVERRIDE
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< ::rtl::OUString > &PropertyNames, const css::uno::Sequence< css::uno::Any > &Values) SAL_OVERRIDE
css::uno::Type const & get()
virtual void SAL_CALL setPropertyToDefault(const OUString &PropertyName) override final
virtual css::beans::PropertyState SAL_CALL getPropertyState(const OUString &PropertyName) override final
css::uno::Reference< css::style::XStyle > m_xStyle
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
The same as getFastPropertyValue, but return the value through rValue and nHandle is always valid.
css::uno::Sequence< css::beans::PropertyState > GetPropertyStatesByHandle(const std::vector< sal_Int32 > &aHandles) const
void SetNewValuesExplicitlyEvenIfTheyEqualDefault()
bool GetPropertyValueByHandle(css::uno::Any &rValue, sal_Int32 nHandle) const
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override final
Try to convert the value rValue to the type required by the property associated with nHandle.
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override=0
The InfoHelper table contains all property names and types of this object.
virtual css::uno::Reference< css::style::XStyle > SAL_CALL getStyle() override final
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
The same as setFastPropertyValue; nHandle is always valid.
virtual void SAL_CALL setAllPropertiesToDefault() override final
css::beans::PropertyState GetPropertyStateByHandle(sal_Int32 nHandle) const
supports states DIRECT_VALUE and DEFAULT_VALUE
virtual void firePropertyChangeEvent()
implement this method in derived classes to get called when properties change.
virtual css::uno::Any SAL_CALL getPropertyDefault(const OUString &aPropertyName) override final
void SetPropertyValueByHandle(sal_Int32 nHandle, const css::uno::Any &rValue)
virtual void SAL_CALL setPropertiesToDefault(const css::uno::Sequence< OUString > &aPropertyNames) override final
virtual void GetDefaultValue(sal_Int32 nHandle, css::uno::Any &rAny) const =0
implement this method to provide default values for all properties supporting defaults.
std::unordered_map< sal_Int32, css::uno::Any > m_aProperties
void SetPropertiesToDefault(const std::vector< sal_Int32 > &aHandles)
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
virtual void SAL_CALL setFastPropertyValue(sal_Int32 nHandle, const css::uno::Any &rValue) override final
void SetPropertyToDefault(sal_Int32 nHandle)
virtual css::uno::Sequence< css::beans::PropertyState > SAL_CALL getPropertyStates(const css::uno::Sequence< OUString > &aPropertyName) override final
bool SetStyle(const css::uno::Reference< css::style::XStyle > &xStyle)
bool m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
virtual void SAL_CALL setStyle(const css::uno::Reference< css::style::XStyle > &xStyle) override final
virtual css::uno::Sequence< css::uno::Any > SAL_CALL getPropertyDefaults(const css::uno::Sequence< OUString > &aPropertyNames) override final
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< OUString > &PropertyNames, const css::uno::Sequence< css::uno::Any > &Values) override final
sal_Int32 nElements
sal_Int16 nValue
std::mutex m_aMutex
OUString aName
OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper
sal_Int32 nHandle
functor that clones a UNO-Reference
Definition: CloneHelper.hxx:33
unsigned char sal_Bool