update dev300-m58
[ooovba.git] / chart2 / source / tools / PropertyHelper.cxx
blobbcadafef36dcd9db2d2ef5226cf3ba9636641687
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PropertyHelper.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "PropertyHelper.hxx"
34 #include "ContainerHelper.hxx"
35 #include "macros.hxx"
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/container/XNameContainer.hpp>
39 #include <vector>
40 #include <algorithm>
41 #include <functional>
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::beans;
45 using ::rtl::OUString;
46 using ::com::sun::star::uno::Any;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
50 namespace
52 struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
54 explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
55 : m_aValue( rValue ), m_xAccess( xAccess )
57 OSL_ASSERT( m_xAccess.is());
60 bool operator() ( const OUString & rName )
62 try
64 return (m_xAccess->getByName( rName ) == m_aValue);
66 catch( const uno::Exception & ex )
68 ASSERT_EXCEPTION( ex );
70 return false;
73 private:
74 Any m_aValue;
75 Reference< container::XNameAccess > m_xAccess;
78 struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
80 lcl_StringMatches( const OUString & rCmpStr ) :
81 m_aCmpStr( rCmpStr )
84 bool operator() ( const OUString & rStr )
86 return rStr.match( m_aCmpStr );
89 private:
90 OUString m_aCmpStr;
93 struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
95 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
96 m_nPrefixLength( nPrefixLength )
98 sal_Int32 operator() ( const OUString & rStr )
100 if( m_nPrefixLength > rStr.getLength() )
101 return 0;
102 return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
104 private:
105 sal_Int32 m_nPrefixLength;
108 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
109 line dash or line marker to the corresponding name container with a unique
110 name.
112 @param rPrefix
113 The prefix used for automated name generation.
115 @param rPreferredName
116 If this string is not empty it is used as name if it is unique in the
117 table. Otherwise a new name is generated using pPrefix.
119 @return the new name under which the property was stored in the table
121 OUString lcl_addNamedPropertyUniqueNameToTable(
122 const Any & rValue,
123 const Reference< container::XNameContainer > & xNameContainer,
124 const OUString & rPrefix,
125 const OUString & rPreferredName )
127 if( ! xNameContainer.is() ||
128 ! rValue.hasValue() ||
129 ( rValue.getValueType() != xNameContainer->getElementType()))
130 return rPreferredName;
134 Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
135 ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
136 ::std::vector< OUString >::const_iterator aIt(
137 ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
139 // element not found in container
140 if( aIt == aNames.end())
142 OUString aUniqueName;
144 // check if preferred name is already used
145 if( rPreferredName.getLength())
147 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
148 if( aIt == aNames.end())
149 aUniqueName = rPreferredName;
152 if( ! aUniqueName.getLength())
154 // create a unique id using the prefix plus a number
155 ::std::vector< sal_Int32 > aNumbers;
156 ::std::vector< OUString >::iterator aNonConstIt(
157 ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
158 ::std::transform( aNames.begin(), aNonConstIt,
159 back_inserter( aNumbers ),
160 lcl_OUStringRestToInt32( rPrefix.getLength() ));
161 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
162 ::std::max_element( aNumbers.begin(), aNumbers.end()));
164 sal_Int32 nIndex = 1;
165 if( aMaxIt != aNumbers.end())
166 nIndex = (*aMaxIt) + 1;
168 aUniqueName = rPrefix + OUString::valueOf( nIndex );
171 OSL_ASSERT( aUniqueName.getLength());
172 xNameContainer->insertByName( aUniqueName, rValue );
173 return aUniqueName;
175 else
176 // element found => return name
177 return *aIt;
179 catch( const uno::Exception & ex )
181 ASSERT_EXCEPTION( ex );
184 return rPreferredName;
187 } // anonymous namespace
189 namespace chart
191 namespace PropertyHelper
194 OUString addLineDashUniqueNameToTable(
195 const Any & rValue,
196 const Reference< lang::XMultiServiceFactory > & xFact,
197 const OUString & rPreferredName )
199 if( xFact.is())
201 Reference< container::XNameContainer > xNameCnt(
202 xFact->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
203 uno::UNO_QUERY );
204 if( xNameCnt.is())
205 return lcl_addNamedPropertyUniqueNameToTable(
206 rValue, xNameCnt, C2U( "ChartDash " ), rPreferredName );
208 return OUString();
211 OUString addGradientUniqueNameToTable(
212 const Any & rValue,
213 const Reference< lang::XMultiServiceFactory > & xFact,
214 const OUString & rPreferredName )
216 if( xFact.is())
218 Reference< container::XNameContainer > xNameCnt(
219 xFact->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
220 uno::UNO_QUERY );
221 if( xNameCnt.is())
222 return lcl_addNamedPropertyUniqueNameToTable(
223 rValue, xNameCnt, C2U( "ChartGradient " ), rPreferredName );
225 return OUString();
229 OUString addTransparencyGradientUniqueNameToTable(
230 const Any & rValue,
231 const Reference< lang::XMultiServiceFactory > & xFact,
232 const OUString & rPreferredName )
234 if( xFact.is())
236 Reference< container::XNameContainer > xNameCnt(
237 xFact->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
238 uno::UNO_QUERY );
239 if( xNameCnt.is())
240 return lcl_addNamedPropertyUniqueNameToTable(
241 rValue, xNameCnt, C2U( "ChartTransparencyGradient " ), rPreferredName );
243 return OUString();
246 OUString addHatchUniqueNameToTable(
247 const Any & rValue,
248 const Reference< lang::XMultiServiceFactory > & xFact,
249 const OUString & rPreferredName )
251 if( xFact.is())
253 Reference< container::XNameContainer > xNameCnt(
254 xFact->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
255 uno::UNO_QUERY );
256 if( xNameCnt.is())
257 return lcl_addNamedPropertyUniqueNameToTable(
258 rValue, xNameCnt, C2U( "ChartHatch " ), rPreferredName );
260 return OUString();
263 OUString addBitmapUniqueNameToTable(
264 const Any & rValue,
265 const Reference< lang::XMultiServiceFactory > & xFact,
266 const OUString & rPreferredName )
268 if( xFact.is())
270 Reference< container::XNameContainer > xNameCnt(
271 xFact->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
272 uno::UNO_QUERY );
273 if( xNameCnt.is())
274 return lcl_addNamedPropertyUniqueNameToTable(
275 rValue, xNameCnt, C2U( "ChartBitmap " ), rPreferredName );
277 return OUString();
280 // ----------------------------------------
282 void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
284 tPropertyValueMap::iterator aIt( rOutMap.find( key ));
285 if( aIt == rOutMap.end())
286 rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
287 else
288 (*aIt).second = rAny;
291 template<>
292 void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
294 setPropertyValueAny( rOutMap, key, rAny );
297 void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
299 OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
300 setPropertyValue( rOutMap, key, rAny );
303 template<>
304 void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
306 setPropertyValueDefaultAny( rOutMap, key, rAny );
310 void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
312 setPropertyValueDefault( rOutMap, key, uno::Any());
315 } // namespace PropertyHelper
317 } // namespace chart