bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / items / customshapeitem.cxx
blob4b51ac9becb9fa2c6967f1144bbc98bd44f91a89
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 <svx/sdasitm.hxx>
21 #include <svx/svdattr.hxx>
23 using namespace ::std;
24 using namespace com::sun::star;
26 SdrCustomShapeEngineItem::SdrCustomShapeEngineItem()
27 : SfxStringItem( SDRATTR_CUSTOMSHAPE_ENGINE, "" )
30 SdrCustomShapeDataItem::SdrCustomShapeDataItem()
31 : SfxStringItem( SDRATTR_CUSTOMSHAPE_DATA, "" )
34 TYPEINIT1_FACTORY( SdrCustomShapeGeometryItem, SfxPoolItem , new SdrCustomShapeGeometryItem);
36 SdrCustomShapeGeometryItem::SdrCustomShapeGeometryItem()
37 : SfxPoolItem( SDRATTR_CUSTOMSHAPE_GEOMETRY )
40 SdrCustomShapeGeometryItem::SdrCustomShapeGeometryItem( const uno::Sequence< beans::PropertyValue >& rVal )
41 : SfxPoolItem( SDRATTR_CUSTOMSHAPE_GEOMETRY )
43 sal_Int32 i, j;
44 aPropSeq = rVal;
46 for ( i = 0; i < aPropSeq.getLength(); i++ )
48 beans::PropertyValue& rPropVal = aPropSeq[ i ];
49 std::pair<PropertyHashMap::iterator, bool> const ret(
50 aPropHashMap.insert(std::make_pair(rPropVal.Name, i)));
51 assert(ret.second); // serious bug: duplicate xml attribute exported
52 if (!ret.second)
54 throw uno::RuntimeException(
55 "CustomShapeGeometry has duplicate property " + rPropVal.Name);
57 if ( rPropVal.Value.getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
59 uno::Sequence< beans::PropertyValue > const & rPropSeq = *static_cast<uno::Sequence< beans::PropertyValue > const *>(rPropVal.Value.getValue());
60 for ( j = 0; j < rPropSeq.getLength(); j++ )
62 beans::PropertyValue const & rPropVal2 = rPropSeq[ j ];
63 aPropPairHashMap[ PropertyPair( rPropVal.Name, rPropVal2.Name ) ] = j;
69 com::sun::star::uno::Any* SdrCustomShapeGeometryItem::GetPropertyValueByName( const OUString& rPropName )
71 com::sun::star::uno::Any* pRet = NULL;
72 PropertyHashMap::iterator aHashIter( aPropHashMap.find( rPropName ) );
73 if ( aHashIter != aPropHashMap.end() )
74 pRet = &aPropSeq[ (*aHashIter).second ].Value;
75 return pRet;
78 const com::sun::star::uno::Any* SdrCustomShapeGeometryItem::GetPropertyValueByName( const OUString& rPropName ) const
80 const com::sun::star::uno::Any* pRet = NULL;
81 PropertyHashMap::const_iterator aHashIter( aPropHashMap.find( rPropName ) );
82 if ( aHashIter != aPropHashMap.end() )
83 pRet = &aPropSeq[ (*aHashIter).second ].Value;
84 return pRet;
87 com::sun::star::uno::Any* SdrCustomShapeGeometryItem::GetPropertyValueByName( const OUString& rSequenceName, const OUString& rPropName )
89 com::sun::star::uno::Any* pRet = NULL;
90 com::sun::star::uno::Any* pSeqAny = GetPropertyValueByName( rSequenceName );
91 if ( pSeqAny )
93 if ( pSeqAny->getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
95 PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rSequenceName, rPropName ) ) );
96 if ( aHashIter != aPropPairHashMap.end() )
98 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
99 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pSeqAny->getValue());
100 pRet = const_cast<css::uno::Any *>(&rSecSequence[ (*aHashIter).second ].Value);
104 return pRet;
107 const com::sun::star::uno::Any* SdrCustomShapeGeometryItem::GetPropertyValueByName( const OUString& rSequenceName, const OUString& rPropName ) const
109 const com::sun::star::uno::Any* pRet = NULL;
110 const com::sun::star::uno::Any* pSeqAny = GetPropertyValueByName( rSequenceName );
111 if ( pSeqAny )
113 if ( pSeqAny->getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
115 PropertyPairHashMap::const_iterator aHashIter( aPropPairHashMap.find( PropertyPair( rSequenceName, rPropName ) ) );
116 if ( aHashIter != aPropPairHashMap.end() )
118 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
119 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pSeqAny->getValue());
120 pRet = &rSecSequence[ (*aHashIter).second ].Value;
124 return pRet;
127 void SdrCustomShapeGeometryItem::SetPropertyValue( const com::sun::star::beans::PropertyValue& rPropVal )
129 com::sun::star::uno::Any* pAny = GetPropertyValueByName( rPropVal.Name );
130 if ( pAny )
131 { // property is already available
132 sal_Int32 i;
133 if ( pAny->getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
134 { // old property is a sequence->each entry has to be removed from the HashPairMap
135 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
136 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pAny->getValue());
137 for ( i = 0; i < rSecSequence.getLength(); i++ )
139 PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rPropVal.Name, rSecSequence[ i ].Name ) ) );
140 if ( aHashIter != aPropPairHashMap.end() )
141 aPropPairHashMap.erase( aHashIter );
144 *pAny = rPropVal.Value;
145 if ( rPropVal.Value.getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
146 { // the new property is a sequence->each entry has to be inserted into the HashPairMap
147 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
148 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pAny->getValue());
149 for ( i = 0; i < rSecSequence.getLength(); i++ )
151 beans::PropertyValue const & rPropVal2 = rSecSequence[ i ];
152 aPropPairHashMap[ PropertyPair( rPropVal.Name, rPropVal2.Name ) ] = i;
156 else
157 { // it's a new property
158 assert(aPropSeq.end() == std::find_if(aPropSeq.begin(), aPropSeq.end(),
159 [&rPropVal](beans::PropertyValue const& rVal)
160 { return rVal.Name == rPropVal.Name; } ));
161 sal_uInt32 nIndex = aPropSeq.getLength();
162 aPropSeq.realloc( nIndex + 1 );
163 aPropSeq[ nIndex ] = rPropVal ;
165 aPropHashMap[ rPropVal.Name ] = nIndex;
169 void SdrCustomShapeGeometryItem::SetPropertyValue( const OUString& rSequenceName, const com::sun::star::beans::PropertyValue& rPropVal )
171 com::sun::star::uno::Any* pAny = GetPropertyValueByName( rSequenceName, rPropVal.Name );
172 if ( pAny ) // just replacing
173 *pAny = rPropVal.Value;
174 else
176 com::sun::star::uno::Any* pSeqAny = GetPropertyValueByName( rSequenceName );
177 if( pSeqAny == NULL )
179 ::com::sun::star::uno::Sequence < beans::PropertyValue > aSeq;
180 beans::PropertyValue aValue;
181 aValue.Name = rSequenceName;
182 aValue.Value = ::com::sun::star::uno::makeAny( aSeq );
184 assert(aPropSeq.end() == std::find_if(aPropSeq.begin(), aPropSeq.end(),
185 [&rSequenceName](beans::PropertyValue const& rV)
186 { return rV.Name == rSequenceName; } ));
187 sal_uInt32 nIndex = aPropSeq.getLength();
188 aPropSeq.realloc( nIndex + 1 );
189 aPropSeq[ nIndex ] = aValue;
190 aPropHashMap[ rSequenceName ] = nIndex;
192 pSeqAny = &aPropSeq[ nIndex ].Value;
195 DBG_ASSERT( pSeqAny, "SdrCustomShapeGeometryItem::SetPropertyValue() - No Value??" );
197 if( pSeqAny )
199 if ( pSeqAny->getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
201 PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rSequenceName, rPropVal.Name ) ) );
202 if ( aHashIter != aPropPairHashMap.end() )
204 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
205 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pSeqAny->getValue());
206 const_cast<css::uno::Sequence<css::beans::PropertyValue> &>(rSecSequence)[ (*aHashIter).second ].Value = rPropVal.Value;
208 else
210 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
211 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pSeqAny->getValue());
213 sal_Int32 nCount = rSecSequence.getLength();
214 const_cast<css::uno::Sequence<css::beans::PropertyValue> &>(rSecSequence).realloc( nCount + 1 );
215 const_cast<css::uno::Sequence<css::beans::PropertyValue> &>(rSecSequence)[ nCount ] = rPropVal;
217 aPropPairHashMap[ PropertyPair( rSequenceName, rPropVal.Name ) ] = nCount;
224 void SdrCustomShapeGeometryItem::ClearPropertyValue( const OUString& rPropName )
226 if ( aPropSeq.getLength() )
228 PropertyHashMap::iterator aHashIter( aPropHashMap.find( rPropName ) );
229 if ( aHashIter != aPropHashMap.end() )
231 com::sun::star::uno::Any* pSeqAny = &aPropSeq[ (*aHashIter).second ].Value;
232 if ( pSeqAny )
234 if ( pSeqAny->getValueType() == cppu::UnoType<css::uno::Sequence < beans::PropertyValue >>::get() )
236 ::com::sun::star::uno::Sequence < beans::PropertyValue > const & rSecSequence =
237 *static_cast<css::uno::Sequence < beans::PropertyValue > const *>(pSeqAny->getValue());
239 sal_Int32 i;
240 for ( i = 0; i < rSecSequence.getLength(); i++ )
242 PropertyPairHashMap::iterator _aHashIter( aPropPairHashMap.find( PropertyPair( rPropName, rSecSequence[ i ].Name ) ) );
243 if ( _aHashIter != aPropPairHashMap.end() )
244 aPropPairHashMap.erase( _aHashIter ); // removing property from pair hashmap
248 sal_Int32 nLength = aPropSeq.getLength();
249 if ( nLength )
251 sal_Int32 nIndex = (*aHashIter).second;
252 if ( nIndex != ( nLength - 1 ) ) // resizing sequence
254 PropertyHashMap::iterator aHashIter2( aPropHashMap.find( aPropSeq[ nLength - 1 ].Name ) );
255 (*aHashIter2).second = nIndex;
256 aPropSeq[ (*aHashIter).second ] = aPropSeq[ aPropSeq.getLength() - 1 ];
258 aPropSeq.realloc( aPropSeq.getLength() - 1 );
260 aPropHashMap.erase( aHashIter ); // removing property from hashmap
265 SdrCustomShapeGeometryItem::~SdrCustomShapeGeometryItem()
268 SdrCustomShapeGeometryItem::SdrCustomShapeGeometryItem( SvStream& /*rIn*/, sal_uInt16 nVersion ):
269 SfxPoolItem( SDRATTR_CUSTOMSHAPE_GEOMETRY )
271 if ( nVersion )
276 bool SdrCustomShapeGeometryItem::operator==( const SfxPoolItem& rCmp ) const
278 bool bRet = SfxPoolItem::operator==( rCmp );
279 if ( bRet )
280 bRet = static_cast<const SdrCustomShapeGeometryItem&>(rCmp).aPropSeq == aPropSeq;
281 return bRet;
284 bool SdrCustomShapeGeometryItem::GetPresentation(
285 SfxItemPresentation ePresentation, SfxMapUnit /*eCoreMetric*/,
286 SfxMapUnit /*ePresentationMetric*/, OUString &rText, const IntlWrapper *) const
288 rText += " ";
289 if ( ePresentation == SFX_ITEM_PRESENTATION_COMPLETE )
291 rText = " " + rText;
292 return true;
294 else if ( ePresentation == SFX_ITEM_PRESENTATION_NAMELESS )
295 return true;
296 return false;
299 SfxPoolItem* SdrCustomShapeGeometryItem::Create( SvStream& rIn, sal_uInt16 nItemVersion ) const
301 return new SdrCustomShapeGeometryItem( rIn, nItemVersion );
304 SvStream& SdrCustomShapeGeometryItem::Store( SvStream& rOut, sal_uInt16 nItemVersion ) const
306 if ( nItemVersion )
310 return rOut;
313 SfxPoolItem* SdrCustomShapeGeometryItem::Clone( SfxItemPool * /*pPool*/ ) const
315 return new SdrCustomShapeGeometryItem( GetGeometry() );
318 sal_uInt16 SdrCustomShapeGeometryItem::GetVersion( sal_uInt16 /*nFileFormatVersion*/ ) const
320 return 1;
322 bool SdrCustomShapeGeometryItem::QueryValue( uno::Any& rVal, sal_uInt8 /*nMemberId*/ ) const
324 rVal <<= aPropSeq;
325 return true;
327 bool SdrCustomShapeGeometryItem::PutValue( const uno::Any& rVal, sal_uInt8 /*nMemberId*/ )
329 if ( ! ( rVal >>= aPropSeq ) )
330 return false;
331 else
333 for (sal_Int32 i = 0; i < aPropSeq.getLength(); ++i)
335 for (sal_Int32 j = i+1; j < aPropSeq.getLength(); ++j)
337 if (aPropSeq[i].Name == aPropSeq[j].Name)
339 assert(false); // serious bug: duplicate xml attribute exported
340 OUString const name(aPropSeq[i].Name);
341 aPropSeq.realloc(0);
342 throw uno::RuntimeException(
343 "CustomShapeGeometry has duplicate property " + name);
347 return true;
351 SdrCustomShapeReplacementURLItem::SdrCustomShapeReplacementURLItem()
352 : SfxStringItem( SDRATTR_CUSTOMSHAPE_REPLACEMENT_URL, "" )
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */