bump product version to 5.0.4.1
[LibreOffice.git] / chart2 / source / tools / PropertyHelper.cxx
blob365e3a374c677b7994a5715bea36a5e9d0fea1e3
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>
25 #include <osl/diagnose.h>
27 #include <vector>
28 #include <algorithm>
29 #include <iterator>
30 #include <functional>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::beans;
34 using ::com::sun::star::uno::Any;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
38 namespace
40 struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
42 explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
43 : m_aValue( rValue ), m_xAccess( xAccess )
45 OSL_ASSERT( m_xAccess.is());
48 bool operator() ( const OUString & rName )
50 try
52 return (m_xAccess->getByName( rName ) == m_aValue);
54 catch( const uno::Exception & ex )
56 ASSERT_EXCEPTION( ex );
58 return false;
61 private:
62 Any m_aValue;
63 Reference< container::XNameAccess > m_xAccess;
66 struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
68 lcl_StringMatches( const OUString & rCmpStr ) :
69 m_aCmpStr( rCmpStr )
72 bool operator() ( const OUString & rStr )
74 return rStr.match( m_aCmpStr );
77 private:
78 OUString m_aCmpStr;
81 struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
83 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
84 m_nPrefixLength( nPrefixLength )
86 sal_Int32 operator() ( const OUString & rStr )
88 if( m_nPrefixLength > rStr.getLength() )
89 return 0;
90 return rStr.copy( m_nPrefixLength ).toInt32();
92 private:
93 sal_Int32 m_nPrefixLength;
96 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
97 line dash or line marker to the corresponding name container with a unique
98 name.
100 @param rPrefix
101 The prefix used for automated name generation.
103 @param rPreferredName
104 If this string is not empty it is used as name if it is unique in the
105 table. Otherwise a new name is generated using pPrefix.
107 @return the new name under which the property was stored in the table
109 OUString lcl_addNamedPropertyUniqueNameToTable(
110 const Any & rValue,
111 const Reference< container::XNameContainer > & xNameContainer,
112 const OUString & rPrefix,
113 const OUString & rPreferredName )
115 if( ! xNameContainer.is() ||
116 ! rValue.hasValue() ||
117 ( rValue.getValueType() != xNameContainer->getElementType()))
118 return rPreferredName;
122 Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
123 ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
124 ::std::vector< OUString >::const_iterator aIt(
125 ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
127 // element not found in container
128 if( aIt == aNames.end())
130 OUString aUniqueName;
132 // check if preferred name is already used
133 if( !rPreferredName.isEmpty())
135 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
136 if( aIt == aNames.end())
137 aUniqueName = rPreferredName;
140 if( aUniqueName.isEmpty())
142 // create a unique id using the prefix plus a number
143 ::std::vector< sal_Int32 > aNumbers;
144 ::std::vector< OUString >::iterator aNonConstIt(
145 ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
146 ::std::transform( aNames.begin(), aNonConstIt,
147 back_inserter( aNumbers ),
148 lcl_OUStringRestToInt32( rPrefix.getLength() ));
149 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
150 ::std::max_element( aNumbers.begin(), aNumbers.end()));
152 sal_Int32 nIndex = 1;
153 if( aMaxIt != aNumbers.end())
154 nIndex = (*aMaxIt) + 1;
156 aUniqueName = rPrefix + OUString::number( nIndex );
159 OSL_ASSERT( !aUniqueName.isEmpty());
160 xNameContainer->insertByName( aUniqueName, rValue );
161 return aUniqueName;
163 else
164 // element found => return name
165 return *aIt;
167 catch( const uno::Exception & ex )
169 ASSERT_EXCEPTION( ex );
172 return rPreferredName;
175 } // anonymous namespace
177 namespace chart
179 namespace PropertyHelper
182 OUString addLineDashUniqueNameToTable(
183 const Any & rValue,
184 const Reference< lang::XMultiServiceFactory > & xFact,
185 const OUString & rPreferredName )
187 if( xFact.is())
189 Reference< container::XNameContainer > xNameCnt(
190 xFact->createInstance( "com.sun.star.drawing.DashTable"),
191 uno::UNO_QUERY );
192 if( xNameCnt.is())
193 return lcl_addNamedPropertyUniqueNameToTable(
194 rValue, xNameCnt, "ChartDash ", rPreferredName );
196 return OUString();
199 OUString addGradientUniqueNameToTable(
200 const Any & rValue,
201 const Reference< lang::XMultiServiceFactory > & xFact,
202 const OUString & rPreferredName )
204 if( xFact.is())
206 Reference< container::XNameContainer > xNameCnt(
207 xFact->createInstance( "com.sun.star.drawing.GradientTable"),
208 uno::UNO_QUERY );
209 if( xNameCnt.is())
210 return lcl_addNamedPropertyUniqueNameToTable(
211 rValue, xNameCnt, "ChartGradient ", rPreferredName );
213 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 void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
269 tPropertyValueMap::iterator aIt( rOutMap.find( key ));
270 if( aIt == rOutMap.end())
271 rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
272 else
273 (*aIt).second = rAny;
276 template<>
277 void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
279 setPropertyValueAny( rOutMap, key, rAny );
282 void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
284 OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
285 setPropertyValue( rOutMap, key, rAny );
288 template<>
289 void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
291 setPropertyValueDefaultAny( rOutMap, key, rAny );
294 void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
296 setPropertyValueDefault( rOutMap, key, uno::Any());
299 } // namespace PropertyHelper
301 } // namespace chart
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */