Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / OPropertySet.cxx
blob859dbcfdf757b5da418c83d0bfc739ba9987b16d
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 <OPropertySet.hxx>
21 #include "ImplOPropertySet.hxx"
22 #include <cppuhelper/queryinterface.hxx>
24 #include <vector>
25 #include <memory>
27 using namespace ::com::sun::star;
29 using ::com::sun::star::uno::Reference;
30 using ::com::sun::star::uno::Sequence;
31 using ::com::sun::star::uno::Any;
32 using ::osl::MutexGuard;
34 // needed for MS compiler
35 using ::cppu::OBroadcastHelper;
36 using ::cppu::OPropertySetHelper;
38 namespace property
41 OPropertySet::OPropertySet( ::osl::Mutex & par_rMutex ) :
42 OBroadcastHelper( par_rMutex ),
43 // the following causes a warning; there seems to be no way to avoid it
44 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
45 m_rMutex( par_rMutex ),
46 m_pImplProperties( new impl::ImplOPropertySet() ),
47 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
51 OPropertySet::OPropertySet( const OPropertySet & rOther, ::osl::Mutex & par_rMutex ) :
52 OBroadcastHelper( par_rMutex ),
53 // the following causes a warning; there seems to be no way to avoid it
54 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
55 m_rMutex( par_rMutex ),
56 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
58 MutexGuard aGuard( m_rMutex );
59 if (rOther.m_pImplProperties)
60 m_pImplProperties.reset(new impl::ImplOPropertySet(*rOther.m_pImplProperties));
63 void OPropertySet::SetNewValuesExplicitlyEvenIfTheyEqualDefault()
65 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault = true;
68 OPropertySet::~OPropertySet()
71 Any SAL_CALL OPropertySet::queryInterface( const uno::Type& aType )
73 return ::cppu::queryInterface(
74 aType,
75 static_cast< lang::XTypeProvider * >( this ),
76 static_cast< beans::XPropertySet * >( this ),
77 static_cast< beans::XMultiPropertySet * >( this ),
78 static_cast< beans::XFastPropertySet * >( this ),
79 static_cast< beans::XPropertyState * >( this ),
80 static_cast< beans::XMultiPropertyStates * >( this ),
81 static_cast< style::XStyleSupplier * >( this ) );
84 // ____ XTypeProvider ____
85 Sequence< uno::Type > SAL_CALL
86 OPropertySet::getTypes()
88 static const Sequence< uno::Type > aTypeList{
89 cppu::UnoType<lang::XTypeProvider>::get(),
90 cppu::UnoType<beans::XPropertySet>::get(),
91 cppu::UnoType<beans::XMultiPropertySet>::get(),
92 cppu::UnoType<beans::XFastPropertySet>::get(),
93 cppu::UnoType<beans::XPropertyState>::get(),
94 cppu::UnoType<beans::XMultiPropertyStates>::get(),
95 cppu::UnoType<style::XStyleSupplier>::get() };
97 return aTypeList;
100 Sequence< sal_Int8 > SAL_CALL
101 OPropertySet::getImplementationId()
103 return css::uno::Sequence<sal_Int8>();
106 // ____ XPropertyState ____
107 beans::PropertyState SAL_CALL
108 OPropertySet::getPropertyState( const OUString& PropertyName )
110 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
112 return m_pImplProperties->GetPropertyStateByHandle(
113 rPH.getHandleByName( PropertyName ));
116 Sequence< beans::PropertyState > SAL_CALL
117 OPropertySet::getPropertyStates( const Sequence< OUString >& aPropertyName )
119 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
121 std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[ aPropertyName.getLength() ]);
122 rPH.fillHandles( pHandles.get(), aPropertyName );
124 std::vector< sal_Int32 > aHandles( pHandles.get(), pHandles.get() + aPropertyName.getLength());
125 pHandles.reset();
127 return m_pImplProperties->GetPropertyStatesByHandle( aHandles );
130 void SAL_CALL
131 OPropertySet::setPropertyToDefault( const OUString& PropertyName )
133 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
135 m_pImplProperties->SetPropertyToDefault( rPH.getHandleByName( PropertyName ));
136 firePropertyChangeEvent();
139 Any SAL_CALL
140 OPropertySet::getPropertyDefault( const OUString& aPropertyName )
142 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
144 return GetDefaultValue( rPH.getHandleByName( aPropertyName ) );
147 // ____ XMultiPropertyStates ____
149 // Note: getPropertyStates() is already implemented in XPropertyState with the
150 // same signature
152 void SAL_CALL
153 OPropertySet::setAllPropertiesToDefault()
155 m_pImplProperties->SetAllPropertiesToDefault();
156 firePropertyChangeEvent();
159 void SAL_CALL
160 OPropertySet::setPropertiesToDefault( const Sequence< OUString >& aPropertyNames )
162 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
164 std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[ aPropertyNames.getLength() ]);
165 rPH.fillHandles( pHandles.get(), aPropertyNames );
167 std::vector< sal_Int32 > aHandles( pHandles.get(), pHandles.get() + aPropertyNames.getLength());
168 pHandles.reset();
170 m_pImplProperties->SetPropertiesToDefault( aHandles );
173 Sequence< Any > SAL_CALL
174 OPropertySet::getPropertyDefaults( const Sequence< OUString >& aPropertyNames )
176 ::cppu::IPropertyArrayHelper & rPH = getInfoHelper();
177 const sal_Int32 nElements = aPropertyNames.getLength();
179 Sequence< Any > aResult( nElements );
180 Any * pResultArray = aResult.getArray();
181 sal_Int32 nI = 0;
183 for( ; nI < nElements; ++nI )
185 pResultArray[ nI ] = GetDefaultValue(
186 rPH.getHandleByName( aPropertyNames[ nI ] ));
189 return aResult;
192 sal_Bool SAL_CALL OPropertySet::convertFastPropertyValue
193 ( Any & rConvertedValue,
194 Any & rOldValue,
195 sal_Int32 nHandle,
196 const Any& rValue )
198 getFastPropertyValue( rOldValue, nHandle );
199 //accept longs also for short values
201 sal_Int16 nValue;
202 if( (rOldValue>>=nValue) && !(rValue>>=nValue) )
204 sal_Int32 n32Value = 0;
205 if( rValue>>=n32Value )
207 rConvertedValue <<= static_cast<sal_Int16>(n32Value);
208 return true;
211 sal_Int64 n64Value = 0;
212 if( rValue>>=n64Value )
214 rConvertedValue <<= static_cast<sal_Int16>(n64Value);
215 return true;
219 rConvertedValue = rValue;
220 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && rOldValue == rConvertedValue )
221 return false;//no change necessary
222 return true;
225 void SAL_CALL OPropertySet::setFastPropertyValue_NoBroadcast
226 ( sal_Int32 nHandle,
227 const Any& rValue )
229 #if OSL_DEBUG_LEVEL > 0
230 if( rValue.hasValue())
232 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
233 OUString aName;
234 rPH.fillPropertyMembersByHandle( &aName, nullptr, nHandle );
235 OSL_ENSURE( rValue.isExtractableTo( rPH.getPropertyByName( aName ).Type ),
236 "Property type is wrong" );
238 #endif
240 Any aDefault;
243 aDefault = GetDefaultValue( nHandle );
245 catch( const beans::UnknownPropertyException& )
247 aDefault.clear();
249 m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
250 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && aDefault.hasValue() && aDefault == rValue ) //#i98893# don't export defaults to file
251 m_pImplProperties->SetPropertyToDefault( nHandle );
252 else
253 m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
256 void SAL_CALL OPropertySet::getFastPropertyValue
257 ( Any& rValue,
258 sal_Int32 nHandle ) const
260 if( ! m_pImplProperties->GetPropertyValueByHandle( rValue, nHandle ))
262 // property was not set -> try style
263 uno::Reference< beans::XFastPropertySet > xStylePropSet( m_pImplProperties->GetStyle(), uno::UNO_QUERY );
264 if( xStylePropSet.is() )
266 #ifdef DBG_UTIL
268 // check if the handle of the style points to the same property
269 // name as the handle in this property set
270 uno::Reference< beans::XPropertySet > xPropSet( xStylePropSet, uno::UNO_QUERY );
271 if( xPropSet.is())
273 uno::Reference< beans::XPropertySetInfo > xInfo = xPropSet->getPropertySetInfo();
274 if( xInfo.is() )
276 // for some reason the virtual method getInfoHelper() is
277 // not const
278 ::cppu::IPropertyArrayHelper & rPH =
279 const_cast< OPropertySet * >( this )->getInfoHelper();
281 // find the Property with Handle nHandle in Style
282 Sequence< beans::Property > aProps( xInfo->getProperties() );
283 sal_Int32 nI = aProps.getLength() - 1;
284 while( ( nI >= 0 ) && nHandle != aProps[ nI ].Handle )
285 --nI;
287 if( nI >= 0 ) // => nHandle == aProps[nI].Handle
289 // check whether the handle in this property set is
290 // the same as the one in the style
291 beans::Property aProp( rPH.getPropertyByName( aProps[ nI ].Name ) );
292 OSL_ENSURE( nHandle == aProp.Handle,
293 "HandleCheck: Handles for same property differ!" );
295 if( nHandle == aProp.Handle )
297 OSL_ENSURE( aProp.Type == aProps[nI].Type,
298 "HandleCheck: Types differ!" );
299 OSL_ENSURE( aProp.Attributes == aProps[nI].Attributes,
300 "HandleCheck: Attributes differ!" );
303 else
305 OSL_FAIL( "HandleCheck: Handle not found in Style" );
308 else
309 OSL_FAIL( "HandleCheck: Invalid XPropertySetInfo returned" );
311 else
312 OSL_FAIL( "HandleCheck: XPropertySet not supported" );
314 #endif
315 rValue = xStylePropSet->getFastPropertyValue( nHandle );
317 else
319 // there is no style (or the style does not support XFastPropertySet)
320 // => take the default value
323 rValue = GetDefaultValue( nHandle );
325 catch( const beans::UnknownPropertyException& )
327 rValue.clear();
333 void OPropertySet::firePropertyChangeEvent()
335 // nothing in base class
338 // ____ XStyleSupplier ____
339 Reference< style::XStyle > SAL_CALL OPropertySet::getStyle()
341 return m_pImplProperties->GetStyle();
344 void SAL_CALL OPropertySet::setStyle( const Reference< style::XStyle >& xStyle )
346 if( ! m_pImplProperties->SetStyle( xStyle ))
347 throw lang::IllegalArgumentException(
348 "Empty Style",
349 static_cast< beans::XPropertySet * >( this ),
350 0 );
353 // ____ XMultiPropertySet ____
354 void SAL_CALL OPropertySet::setPropertyValues(
355 const Sequence< OUString >& PropertyNames, const Sequence< Any >& Values )
357 ::cppu::OPropertySetHelper::setPropertyValues( PropertyNames, Values );
359 firePropertyChangeEvent();
362 // ____ XFastPropertySet ____
363 void SAL_CALL OPropertySet::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
365 ::cppu::OPropertySetHelper::setFastPropertyValue( nHandle, rValue );
367 firePropertyChangeEvent();
370 } // namespace property
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */