xref: /trunk/main/svx/source/items/customshapeitem.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 #include <svx/sdasitm.hxx>
31 #include <svx/svdattr.hxx>
32 
33 using namespace ::std;
34 using namespace com::sun::star;
35 
36 SdrCustomShapeEngineItem::SdrCustomShapeEngineItem()
37 :   SfxStringItem( SDRATTR_CUSTOMSHAPE_ENGINE, String() )
38 {}
39 SdrCustomShapeEngineItem::SdrCustomShapeEngineItem( const String& rVal )
40 :   SfxStringItem( SDRATTR_CUSTOMSHAPE_ENGINE, rVal )
41 {}
42 
43 SdrCustomShapeDataItem::SdrCustomShapeDataItem()
44 :   SfxStringItem( SDRATTR_CUSTOMSHAPE_DATA, String() )
45 {}
46 SdrCustomShapeDataItem::SdrCustomShapeDataItem( const String& rVal )
47 :   SfxStringItem( SDRATTR_CUSTOMSHAPE_DATA, rVal )
48 {}
49 
50 bool SdrCustomShapeGeometryItem::PropertyEq::operator()( const rtl::OUString& r1, const rtl::OUString& r2 ) const
51 {
52     return r1.equals( r2 );
53 }
54 bool SdrCustomShapeGeometryItem::PropertyPairEq::operator()( const SdrCustomShapeGeometryItem::PropertyPair& r1, const SdrCustomShapeGeometryItem::PropertyPair& r2 ) const
55 {
56     return ( r1.first.equals( r2.first ) ) && ( r1.second.equals( r2.second ) );
57 }
58 size_t SdrCustomShapeGeometryItem::PropertyPairHash::operator()( const SdrCustomShapeGeometryItem::PropertyPair &r1 ) const
59 {
60     return (size_t)r1.first.hashCode() + r1.second.hashCode();
61 };
62 
63 TYPEINIT1_FACTORY( SdrCustomShapeGeometryItem, SfxPoolItem , new  SdrCustomShapeGeometryItem);
64 SdrCustomShapeGeometryItem::SdrCustomShapeGeometryItem()
65 :   SfxPoolItem( SDRATTR_CUSTOMSHAPE_GEOMETRY )
66 {}
67 SdrCustomShapeGeometryItem::SdrCustomShapeGeometryItem( const uno::Sequence< beans::PropertyValue >& rVal )
68 :   SfxPoolItem( SDRATTR_CUSTOMSHAPE_GEOMETRY )
69 {
70     sal_Int32 i, j;
71     aPropSeq = rVal;
72 
73     // hashing property values
74 //  beans::PropertyValue* pPropValues = aPropSeq.getArray();
75 //  const rtl::OUString* pPtr = NULL;
76     for ( i = 0; i < aPropSeq.getLength(); i++ )
77     {
78         beans::PropertyValue& rPropVal = aPropSeq[ i ];
79         aPropHashMap[ rPropVal.Name ] = i;
80         if ( rPropVal.Value.getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
81         {
82             uno::Sequence< beans::PropertyValue >& rPropSeq = *( uno::Sequence< beans::PropertyValue >*)rPropVal.Value.getValue();
83             for ( j = 0; j < rPropSeq.getLength(); j++ )
84             {
85                 beans::PropertyValue& rPropVal2 = rPropSeq[ j ];
86                 aPropPairHashMap[ PropertyPair( rPropVal.Name, rPropVal2.Name ) ] = j;
87             }
88         }
89     }
90 }
91 
92 com::sun::star::uno::Any* SdrCustomShapeGeometryItem::GetPropertyValueByName( const rtl::OUString& rPropName )
93 {
94     com::sun::star::uno::Any* pRet = NULL;
95     PropertyHashMap::iterator aHashIter( aPropHashMap.find( rPropName ) );
96     if ( aHashIter != aPropHashMap.end() )
97         pRet = &aPropSeq[ (*aHashIter).second ].Value;
98     return pRet;
99 }
100 
101 com::sun::star::uno::Any* SdrCustomShapeGeometryItem::GetPropertyValueByName( const rtl::OUString& rSequenceName, const rtl::OUString& rPropName )
102 {
103     com::sun::star::uno::Any* pRet = NULL;
104     com::sun::star::uno::Any* pSeqAny = GetPropertyValueByName( rSequenceName );
105     if ( pSeqAny )
106     {
107         if ( pSeqAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
108         {
109             PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rSequenceName, rPropName ) ) );
110             if ( aHashIter != aPropPairHashMap.end() )
111             {
112                 ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
113                     *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pSeqAny->getValue());
114                 pRet = &rSecSequence[ (*aHashIter).second ].Value;
115             }
116         }
117     }
118     return pRet;
119 }
120 
121 void SdrCustomShapeGeometryItem::SetPropertyValue( const com::sun::star::beans::PropertyValue& rPropVal )
122 {
123     com::sun::star::uno::Any* pAny = GetPropertyValueByName( rPropVal.Name );
124     if ( pAny )
125     {   // property is already available
126         sal_Int32 i;
127         if ( pAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
128         {   // old property is a sequence->each entry has to be removed from the HashPairMap
129             ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
130                 *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pAny->getValue());
131             for ( i = 0; i < rSecSequence.getLength(); i++ )
132             {
133                 PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rPropVal.Name, rSecSequence[ i ].Name ) ) );
134                 if ( aHashIter != aPropPairHashMap.end() )
135                     aPropPairHashMap.erase( aHashIter );
136             }
137         }
138         *pAny = rPropVal.Value;
139         if ( rPropVal.Value.getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
140         {   // the new property is a sequence->each entry has to be inserted into the HashPairMap
141             ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
142                 *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pAny->getValue());
143             for ( i = 0; i < rSecSequence.getLength(); i++ )
144             {
145                 beans::PropertyValue& rPropVal2 = rSecSequence[ i ];
146                 aPropPairHashMap[ PropertyPair( rPropVal.Name, rPropVal2.Name ) ] = i;
147             }
148         }
149     }
150     else
151     {   // its a new property
152         sal_uInt32 nIndex = aPropSeq.getLength();
153         aPropSeq.realloc( nIndex + 1 );
154         aPropSeq[ nIndex ] = rPropVal ;
155 
156         aPropHashMap[ rPropVal.Name ] = nIndex;
157     }
158 }
159 
160 void SdrCustomShapeGeometryItem::SetPropertyValue( const rtl::OUString& rSequenceName, const com::sun::star::beans::PropertyValue& rPropVal )
161 {
162     com::sun::star::uno::Any* pAny = GetPropertyValueByName( rSequenceName, rPropVal.Name );
163     if ( pAny ) // just replacing
164         *pAny = rPropVal.Value;
165     else
166     {
167         com::sun::star::uno::Any* pSeqAny = GetPropertyValueByName( rSequenceName );
168         if( pSeqAny == NULL )
169         {
170             ::com::sun::star::uno::Sequence < beans::PropertyValue > aSeq;
171             beans::PropertyValue aValue;
172             aValue.Name = rSequenceName;
173             aValue.Value = ::com::sun::star::uno::makeAny( aSeq );
174 
175             sal_uInt32 nIndex = aPropSeq.getLength();
176             aPropSeq.realloc( nIndex + 1 );
177             aPropSeq[ nIndex ] = aValue;
178             aPropHashMap[ rSequenceName ] = nIndex;
179 
180             pSeqAny = &aPropSeq[ nIndex ].Value;
181         }
182 
183         DBG_ASSERT( pSeqAny, "SdrCustomShapeGeometryItem::SetPropertyValue() - No Value??" );
184 
185         if( pSeqAny )
186         {
187             if ( pSeqAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
188             {
189                 PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rSequenceName, rPropVal.Name ) ) );
190                 if ( aHashIter != aPropPairHashMap.end() )
191                 {
192                     ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
193                         *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pSeqAny->getValue());
194                     rSecSequence[ (*aHashIter).second ].Value = rPropVal.Value;
195                 }
196                 else
197                 {
198                     ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
199                         *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pSeqAny->getValue());
200 
201                     sal_Int32 nCount = rSecSequence.getLength();
202                     rSecSequence.realloc( nCount + 1 );
203                     rSecSequence[ nCount ] = rPropVal;
204 
205                     aPropPairHashMap[ PropertyPair( rSequenceName, rPropVal.Name ) ] = nCount;
206                 }
207             }
208         }
209     }
210 }
211 
212 void SdrCustomShapeGeometryItem::ClearPropertyValue( const rtl::OUString& rPropName )
213 {
214     if ( aPropSeq.getLength() )
215     {
216         PropertyHashMap::iterator aHashIter( aPropHashMap.find( rPropName ) );
217         if ( aHashIter != aPropHashMap.end() )
218         {
219             com::sun::star::uno::Any* pSeqAny = &aPropSeq[ (*aHashIter).second ].Value;
220             if ( pSeqAny )
221             {
222                 if ( pSeqAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
223                 {
224                     ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
225                         *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pSeqAny->getValue());
226 
227                     sal_Int32 i;
228                     for ( i = 0; i < rSecSequence.getLength(); i++ )
229                     {
230                         PropertyPairHashMap::iterator _aHashIter( aPropPairHashMap.find( PropertyPair( rPropName, rSecSequence[ i ].Name ) ) );
231                         if ( _aHashIter != aPropPairHashMap.end() )
232                             aPropPairHashMap.erase( _aHashIter );       // removing property from pair hashmap
233                     }
234                 }
235             }
236             sal_Int32 nLength = aPropSeq.getLength();
237             if ( nLength )
238             {
239                 sal_Int32 nIndex  = (*aHashIter).second;
240                 if ( nIndex != ( nLength - 1 ) )                        // resizing sequence
241                 {
242                     PropertyHashMap::iterator aHashIter2( aPropHashMap.find( aPropSeq[ nLength - 1 ].Name ) );
243                     (*aHashIter2).second = nIndex;
244                     aPropSeq[ (*aHashIter).second ] = aPropSeq[ aPropSeq.getLength() - 1 ];
245                 }
246                 aPropSeq.realloc( aPropSeq.getLength() - 1 );
247             }
248             aPropHashMap.erase( aHashIter );                            // removing property from hashmap
249         }
250     }
251 }
252 
253 void SdrCustomShapeGeometryItem::ClearPropertyValue( const rtl::OUString& rSequenceName, const rtl::OUString& rPropName )
254 {
255     com::sun::star::uno::Any* pSeqAny = GetPropertyValueByName( rSequenceName );
256     if ( pSeqAny )
257     {
258         if ( pSeqAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Sequence < beans::PropertyValue >*)0) )
259         {
260             PropertyPairHashMap::iterator aHashIter( aPropPairHashMap.find( PropertyPair( rSequenceName, rPropName ) ) );
261             if ( aHashIter != aPropPairHashMap.end() )
262             {
263                 ::com::sun::star::uno::Sequence < beans::PropertyValue >& rSecSequence =
264                     *((::com::sun::star::uno::Sequence < beans::PropertyValue >*)pSeqAny->getValue());
265 
266                 sal_Int32 nLength = rSecSequence.getLength();
267                 if ( nLength )
268                 {
269                     sal_Int32 nIndex  = (*aHashIter).second;
270                     if ( nIndex != ( nLength - 1 ) )                            // resizing sequence
271                     {
272                         PropertyPairHashMap::iterator aHashIter2( aPropPairHashMap.find( PropertyPair( rSequenceName, rSecSequence[ nLength - 1 ].Name ) ) );
273                         (*aHashIter2).second = nIndex;
274                         rSecSequence[ nIndex ] = rSecSequence[ nLength - 1 ];
275                     }
276                     rSecSequence.realloc( aPropSeq.getLength() - 1 );
277                 }
278                 aPropPairHashMap.erase( aHashIter );
279             }
280         }
281     }
282 }
283 
284 SdrCustomShapeGeometryItem::~SdrCustomShapeGeometryItem()
285 {
286 }
287 SdrCustomShapeGeometryItem::SdrCustomShapeGeometryItem( SvStream& /*rIn*/, sal_uInt16 nVersion ):
288     SfxPoolItem( SDRATTR_CUSTOMSHAPE_GEOMETRY )
289 {
290     if ( nVersion )
291     {
292 
293     }
294 }
295 int __EXPORT SdrCustomShapeGeometryItem::operator==( const SfxPoolItem& rCmp ) const
296 {
297     int bRet = SfxPoolItem::operator==( rCmp );
298     if ( bRet )
299         bRet = ((SdrCustomShapeGeometryItem&)rCmp).aPropSeq == aPropSeq;
300     return bRet;
301 }
302 
303 SfxItemPresentation __EXPORT SdrCustomShapeGeometryItem::GetPresentation(
304     SfxItemPresentation ePresentation, SfxMapUnit /*eCoreMetric*/,
305     SfxMapUnit /*ePresentationMetric*/, XubString &rText, const IntlWrapper *) const
306 {
307     rText += sal_Unicode( ' ' );
308     if ( ePresentation == SFX_ITEM_PRESENTATION_COMPLETE )
309     {
310         XubString aStr;
311 //      SdrItemPool::TakeItemName( Which(), aStr );
312         aStr += sal_Unicode( ' ' );
313         rText.Insert( aStr, 0 );
314     }
315     return ePresentation;
316 }
317 
318 SfxPoolItem* __EXPORT SdrCustomShapeGeometryItem::Create( SvStream& rIn, sal_uInt16 nItemVersion ) const
319 {
320     return new SdrCustomShapeGeometryItem( rIn, nItemVersion );
321 }
322 
323 SvStream& __EXPORT SdrCustomShapeGeometryItem::Store( SvStream& rOut, sal_uInt16 nItemVersion ) const
324 {
325     if ( nItemVersion )
326     {
327 
328     }
329     return rOut;
330 }
331 
332 SfxPoolItem* __EXPORT SdrCustomShapeGeometryItem::Clone( SfxItemPool * /*pPool*/ ) const
333 {
334     SdrCustomShapeGeometryItem* pItem = new SdrCustomShapeGeometryItem( GetGeometry() );
335 //  SdrCustomShapeGeometryItem* pItem = new SdrCustomShapeGeometryItem( *this );
336 
337 /*
338     for ( i = 0; i < GetCount(); i++ )
339     {
340         const SdrCustomShapeAdjustmentValue& rVal = GetValue( i );
341         pItem->SetValue( i, rVal );
342     }
343 */
344     return pItem;
345 }
346 
347 #ifdef SDR_ISPOOLABLE
348 int __EXPORT SdrCustomShapeGeometryItem::IsPoolable() const
349 {
350     sal_uInt16 nId=Which();
351     return nId < SDRATTR_NOTPERSIST_FIRST || nId > SDRATTR_NOTPERSIST_LAST;
352 }
353 #endif
354 sal_uInt16 SdrCustomShapeGeometryItem::GetVersion( sal_uInt16 /*nFileFormatVersion*/ ) const
355 {
356     return 1;
357 }
358 sal_Bool SdrCustomShapeGeometryItem::QueryValue( uno::Any& rVal, sal_uInt8 /*nMemberId*/ ) const
359 {
360     rVal <<= aPropSeq;
361     return sal_True;
362 }
363 sal_Bool SdrCustomShapeGeometryItem::PutValue( const uno::Any& rVal, sal_uInt8 /*nMemberId*/ )
364 {
365     if ( ! ( rVal >>= aPropSeq ) )
366         return sal_False;
367     else
368         return sal_True;
369 }
370 const uno::Sequence< beans::PropertyValue >& SdrCustomShapeGeometryItem::GetGeometry() const
371 {
372     return aPropSeq;
373 }
374 /*
375 const uno::Any* GetValueByName( const rtl::OUString& rProperty ) const
376 {
377 
378 }
379 */
380 SdrCustomShapeReplacementURLItem::SdrCustomShapeReplacementURLItem()
381 :   SfxStringItem( SDRATTR_CUSTOMSHAPE_REPLACEMENT_URL, String() )
382 {}
383 SdrCustomShapeReplacementURLItem::SdrCustomShapeReplacementURLItem( const String& rVal )
384 :   SfxStringItem( SDRATTR_CUSTOMSHAPE_REPLACEMENT_URL, rVal )
385 {}
386 
387