1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_charttools.hxx"
26 #include "PropertyHelper.hxx"
27 #include "ContainerHelper.hxx"
28 #include "macros.hxx"
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/container/XNameContainer.hpp>
31
32 #include <vector>
33 #include <algorithm>
34 #include <functional>
35
36
37 // std::back_inserter lives in <iterator>. VC9's headers pulled it in
38 // transitively, a modern one does not.
39 #include <iterator>
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::beans;
42 using ::rtl::OUString;
43 using ::com::sun::star::uno::Any;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Sequence;
46
47 namespace
48 {
49 struct lcl_EqualsElement
50 {
51 typedef OUString argument_type;
52 typedef bool result_type;
lcl_EqualsElement__anona49d89af0111::lcl_EqualsElement53 explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
54 : m_aValue( rValue ), m_xAccess( xAccess )
55 {
56 OSL_ASSERT( m_xAccess.is());
57 }
58
operator ()__anona49d89af0111::lcl_EqualsElement59 bool operator() ( const OUString & rName )
60 {
61 try
62 {
63 return (m_xAccess->getByName( rName ) == m_aValue);
64 }
65 catch( const uno::Exception & ex )
66 {
67 ASSERT_EXCEPTION( ex );
68 }
69 return false;
70 }
71
72 private:
73 Any m_aValue;
74 Reference< container::XNameAccess > m_xAccess;
75 };
76
77 struct lcl_StringMatches
78 {
79 typedef OUString argument_type;
80 typedef bool result_type;
lcl_StringMatches__anona49d89af0111::lcl_StringMatches81 lcl_StringMatches( const OUString & rCmpStr ) :
82 m_aCmpStr( rCmpStr )
83 {}
84
operator ()__anona49d89af0111::lcl_StringMatches85 bool operator() ( const OUString & rStr )
86 {
87 return rStr.match( m_aCmpStr );
88 }
89
90 private:
91 OUString m_aCmpStr;
92 };
93
94 struct lcl_OUStringRestToInt32
95 {
96 typedef OUString argument_type;
97 typedef sal_Int32 result_type;
lcl_OUStringRestToInt32__anona49d89af0111::lcl_OUStringRestToInt3298 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
99 m_nPrefixLength( nPrefixLength )
100 {}
operator ()__anona49d89af0111::lcl_OUStringRestToInt32101 sal_Int32 operator() ( const OUString & rStr )
102 {
103 if( m_nPrefixLength > rStr.getLength() )
104 return 0;
105 return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
106 }
107 private:
108 sal_Int32 m_nPrefixLength;
109 };
110
111 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
112 line dash or line marker to the corresponding name container with a unique
113 name.
114
115 @param rPrefix
116 The prefix used for automated name generation.
117
118 @param rPreferredName
119 If this string is not empty it is used as name if it is unique in the
120 table. Otherwise a new name is generated using pPrefix.
121
122 @return the new name under which the property was stored in the table
123 */
lcl_addNamedPropertyUniqueNameToTable(const Any & rValue,const Reference<container::XNameContainer> & xNameContainer,const OUString & rPrefix,const OUString & rPreferredName)124 OUString lcl_addNamedPropertyUniqueNameToTable(
125 const Any & rValue,
126 const Reference< container::XNameContainer > & xNameContainer,
127 const OUString & rPrefix,
128 const OUString & rPreferredName )
129 {
130 if( ! xNameContainer.is() ||
131 ! rValue.hasValue() ||
132 ( rValue.getValueType() != xNameContainer->getElementType()))
133 return rPreferredName;
134
135 try
136 {
137 Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
138 ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
139 ::std::vector< OUString >::const_iterator aIt(
140 ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
141
142 // element not found in container
143 if( aIt == aNames.end())
144 {
145 OUString aUniqueName;
146
147 // check if preferred name is already used
148 if( !rPreferredName.isEmpty() )
149 {
150 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
151 if( aIt == aNames.end())
152 aUniqueName = rPreferredName;
153 }
154
155 if( aUniqueName.isEmpty() )
156 {
157 // create a unique id using the prefix plus a number
158 ::std::vector< sal_Int32 > aNumbers;
159 ::std::vector< OUString >::iterator aNonConstIt(
160 ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
161 ::std::transform( aNames.begin(), aNonConstIt,
162 back_inserter( aNumbers ),
163 lcl_OUStringRestToInt32( rPrefix.getLength() ));
164 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
165 ::std::max_element( aNumbers.begin(), aNumbers.end()));
166
167 sal_Int32 nIndex = 1;
168 if( aMaxIt != aNumbers.end())
169 nIndex = (*aMaxIt) + 1;
170
171 aUniqueName = rPrefix + OUString::valueOf( nIndex );
172 }
173
174 OSL_ASSERT( !aUniqueName.isEmpty() );
175 xNameContainer->insertByName( aUniqueName, rValue );
176 return aUniqueName;
177 }
178 else
179 // element found => return name
180 return *aIt;
181 }
182 catch( const uno::Exception & ex )
183 {
184 ASSERT_EXCEPTION( ex );
185 }
186
187 return rPreferredName;
188 }
189
190 } // anonymous namespace
191
192 namespace chart
193 {
194 namespace PropertyHelper
195 {
196
addLineDashUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)197 OUString addLineDashUniqueNameToTable(
198 const Any & rValue,
199 const Reference< lang::XMultiServiceFactory > & xFact,
200 const OUString & rPreferredName )
201 {
202 if( xFact.is())
203 {
204 Reference< container::XNameContainer > xNameCnt(
205 xFact->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
206 uno::UNO_QUERY );
207 if( xNameCnt.is())
208 return lcl_addNamedPropertyUniqueNameToTable(
209 rValue, xNameCnt, C2U( "ChartDash " ), rPreferredName );
210 }
211 return OUString();
212 }
213
addGradientUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)214 OUString addGradientUniqueNameToTable(
215 const Any & rValue,
216 const Reference< lang::XMultiServiceFactory > & xFact,
217 const OUString & rPreferredName )
218 {
219 if( xFact.is())
220 {
221 Reference< container::XNameContainer > xNameCnt(
222 xFact->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
223 uno::UNO_QUERY );
224 if( xNameCnt.is())
225 return lcl_addNamedPropertyUniqueNameToTable(
226 rValue, xNameCnt, C2U( "ChartGradient " ), rPreferredName );
227 }
228 return OUString();
229 }
230
231
addTransparencyGradientUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)232 OUString addTransparencyGradientUniqueNameToTable(
233 const Any & rValue,
234 const Reference< lang::XMultiServiceFactory > & xFact,
235 const OUString & rPreferredName )
236 {
237 if( xFact.is())
238 {
239 Reference< container::XNameContainer > xNameCnt(
240 xFact->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
241 uno::UNO_QUERY );
242 if( xNameCnt.is())
243 return lcl_addNamedPropertyUniqueNameToTable(
244 rValue, xNameCnt, C2U( "ChartTransparencyGradient " ), rPreferredName );
245 }
246 return OUString();
247 }
248
addHatchUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)249 OUString addHatchUniqueNameToTable(
250 const Any & rValue,
251 const Reference< lang::XMultiServiceFactory > & xFact,
252 const OUString & rPreferredName )
253 {
254 if( xFact.is())
255 {
256 Reference< container::XNameContainer > xNameCnt(
257 xFact->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
258 uno::UNO_QUERY );
259 if( xNameCnt.is())
260 return lcl_addNamedPropertyUniqueNameToTable(
261 rValue, xNameCnt, C2U( "ChartHatch " ), rPreferredName );
262 }
263 return OUString();
264 }
265
addBitmapUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)266 OUString addBitmapUniqueNameToTable(
267 const Any & rValue,
268 const Reference< lang::XMultiServiceFactory > & xFact,
269 const OUString & rPreferredName )
270 {
271 if( xFact.is())
272 {
273 Reference< container::XNameContainer > xNameCnt(
274 xFact->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
275 uno::UNO_QUERY );
276 if( xNameCnt.is())
277 return lcl_addNamedPropertyUniqueNameToTable(
278 rValue, xNameCnt, C2U( "ChartBitmap " ), rPreferredName );
279 }
280 return OUString();
281 }
282
283 // ----------------------------------------
284
setPropertyValueAny(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const uno::Any & rAny)285 void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
286 {
287 tPropertyValueMap::iterator aIt( rOutMap.find( key ));
288 if( aIt == rOutMap.end())
289 rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
290 else
291 (*aIt).second = rAny;
292 }
293
294 template<>
setPropertyValue(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const::com::sun::star::uno::Any & rAny)295 void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
296 {
297 setPropertyValueAny( rOutMap, key, rAny );
298 }
299
setPropertyValueDefaultAny(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const uno::Any & rAny)300 void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
301 {
302 OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
303 setPropertyValue( rOutMap, key, rAny );
304 }
305
306 template<>
setPropertyValueDefault(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const::com::sun::star::uno::Any & rAny)307 void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
308 {
309 setPropertyValueDefaultAny( rOutMap, key, rAny );
310 }
311
312
setEmptyPropertyValueDefault(tPropertyValueMap & rOutMap,tPropertyValueMapKey key)313 void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
314 {
315 setPropertyValueDefault( rOutMap, key, uno::Any());
316 }
317
318 } // namespace PropertyHelper
319
320 } // namespace chart
321