bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / PropertyHelper.cxx
blobfb6b0bfadd84f5a03f6ae5b06daa9741e3b9a3b0
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 "PropertyHelper.hxx"
21 #include "ContainerHelper.hxx"
22 #include "macros.hxx"
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <vector>
27 #include <algorithm>
28 #include <iterator>
29 #include <functional>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::beans;
33 using ::com::sun::star::uno::Any;
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::Sequence;
37 namespace
39 struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
41 explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
42 : m_aValue( rValue ), m_xAccess( xAccess )
44 OSL_ASSERT( m_xAccess.is());
47 bool operator() ( const OUString & rName )
49 try
51 return (m_xAccess->getByName( rName ) == m_aValue);
53 catch( const uno::Exception & ex )
55 ASSERT_EXCEPTION( ex );
57 return false;
60 private:
61 Any m_aValue;
62 Reference< container::XNameAccess > m_xAccess;
65 struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
67 lcl_StringMatches( const OUString & rCmpStr ) :
68 m_aCmpStr( rCmpStr )
71 bool operator() ( const OUString & rStr )
73 return rStr.match( m_aCmpStr );
76 private:
77 OUString m_aCmpStr;
80 struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
82 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
83 m_nPrefixLength( nPrefixLength )
85 sal_Int32 operator() ( const OUString & rStr )
87 if( m_nPrefixLength > rStr.getLength() )
88 return 0;
89 return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
91 private:
92 sal_Int32 m_nPrefixLength;
95 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
96 line dash or line marker to the corresponding name container with a unique
97 name.
99 @param rPrefix
100 The prefix used for automated name generation.
102 @param rPreferredName
103 If this string is not empty it is used as name if it is unique in the
104 table. Otherwise a new name is generated using pPrefix.
106 @return the new name under which the property was stored in the table
108 OUString lcl_addNamedPropertyUniqueNameToTable(
109 const Any & rValue,
110 const Reference< container::XNameContainer > & xNameContainer,
111 const OUString & rPrefix,
112 const OUString & rPreferredName )
114 if( ! xNameContainer.is() ||
115 ! rValue.hasValue() ||
116 ( rValue.getValueType() != xNameContainer->getElementType()))
117 return rPreferredName;
121 Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
122 ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
123 ::std::vector< OUString >::const_iterator aIt(
124 ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
126 // element not found in container
127 if( aIt == aNames.end())
129 OUString aUniqueName;
131 // check if preferred name is already used
132 if( !rPreferredName.isEmpty())
134 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
135 if( aIt == aNames.end())
136 aUniqueName = rPreferredName;
139 if( aUniqueName.isEmpty())
141 // create a unique id using the prefix plus a number
142 ::std::vector< sal_Int32 > aNumbers;
143 ::std::vector< OUString >::iterator aNonConstIt(
144 ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
145 ::std::transform( aNames.begin(), aNonConstIt,
146 back_inserter( aNumbers ),
147 lcl_OUStringRestToInt32( rPrefix.getLength() ));
148 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
149 ::std::max_element( aNumbers.begin(), aNumbers.end()));
151 sal_Int32 nIndex = 1;
152 if( aMaxIt != aNumbers.end())
153 nIndex = (*aMaxIt) + 1;
155 aUniqueName = rPrefix + OUString::valueOf( nIndex );
158 OSL_ASSERT( !aUniqueName.isEmpty());
159 xNameContainer->insertByName( aUniqueName, rValue );
160 return aUniqueName;
162 else
163 // element found => return name
164 return *aIt;
166 catch( const uno::Exception & ex )
168 ASSERT_EXCEPTION( ex );
171 return rPreferredName;
174 } // anonymous namespace
176 namespace chart
178 namespace PropertyHelper
181 OUString addLineDashUniqueNameToTable(
182 const Any & rValue,
183 const Reference< lang::XMultiServiceFactory > & xFact,
184 const OUString & rPreferredName )
186 if( xFact.is())
188 Reference< container::XNameContainer > xNameCnt(
189 xFact->createInstance( "com.sun.star.drawing.DashTable"),
190 uno::UNO_QUERY );
191 if( xNameCnt.is())
192 return lcl_addNamedPropertyUniqueNameToTable(
193 rValue, xNameCnt, "ChartDash ", rPreferredName );
195 return OUString();
198 OUString addGradientUniqueNameToTable(
199 const Any & rValue,
200 const Reference< lang::XMultiServiceFactory > & xFact,
201 const OUString & rPreferredName )
203 if( xFact.is())
205 Reference< container::XNameContainer > xNameCnt(
206 xFact->createInstance( "com.sun.star.drawing.GradientTable"),
207 uno::UNO_QUERY );
208 if( xNameCnt.is())
209 return lcl_addNamedPropertyUniqueNameToTable(
210 rValue, xNameCnt, "ChartGradient ", rPreferredName );
212 return OUString();
216 OUString addTransparencyGradientUniqueNameToTable(
217 const Any & rValue,
218 const Reference< lang::XMultiServiceFactory > & xFact,
219 const OUString & rPreferredName )
221 if( xFact.is())
223 Reference< container::XNameContainer > xNameCnt(
224 xFact->createInstance( "com.sun.star.drawing.TransparencyGradientTable"),
225 uno::UNO_QUERY );
226 if( xNameCnt.is())
227 return lcl_addNamedPropertyUniqueNameToTable(
228 rValue, xNameCnt, "ChartTransparencyGradient ", rPreferredName );
230 return OUString();
233 OUString addHatchUniqueNameToTable(
234 const Any & rValue,
235 const Reference< lang::XMultiServiceFactory > & xFact,
236 const OUString & rPreferredName )
238 if( xFact.is())
240 Reference< container::XNameContainer > xNameCnt(
241 xFact->createInstance( "com.sun.star.drawing.HatchTable"),
242 uno::UNO_QUERY );
243 if( xNameCnt.is())
244 return lcl_addNamedPropertyUniqueNameToTable(
245 rValue, xNameCnt, "ChartHatch ", rPreferredName );
247 return OUString();
250 OUString addBitmapUniqueNameToTable(
251 const Any & rValue,
252 const Reference< lang::XMultiServiceFactory > & xFact,
253 const OUString & rPreferredName )
255 if( xFact.is())
257 Reference< container::XNameContainer > xNameCnt(
258 xFact->createInstance( "com.sun.star.drawing.BitmapTable"),
259 uno::UNO_QUERY );
260 if( xNameCnt.is())
261 return lcl_addNamedPropertyUniqueNameToTable(
262 rValue, xNameCnt, "ChartBitmap ", rPreferredName );
264 return OUString();
267 // ----------------------------------------
269 void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
271 tPropertyValueMap::iterator aIt( rOutMap.find( key ));
272 if( aIt == rOutMap.end())
273 rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
274 else
275 (*aIt).second = rAny;
278 template<>
279 void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
281 setPropertyValueAny( rOutMap, key, rAny );
284 void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
286 OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
287 setPropertyValue( rOutMap, key, rAny );
290 template<>
291 void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
293 setPropertyValueDefaultAny( rOutMap, key, rAny );
297 void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
299 setPropertyValueDefault( rOutMap, key, uno::Any());
302 } // namespace PropertyHelper
304 } // namespace chart
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */