Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 :
26 : #include <vector>
27 : #include <algorithm>
28 : #include <iterator>
29 : #include <functional>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::beans;
33 : using ::rtl::OUString;
34 : using ::com::sun::star::uno::Any;
35 : using ::com::sun::star::uno::Reference;
36 : using ::com::sun::star::uno::Sequence;
37 :
38 : namespace
39 : {
40 0 : struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
41 : {
42 0 : explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
43 0 : : m_aValue( rValue ), m_xAccess( xAccess )
44 : {
45 : OSL_ASSERT( m_xAccess.is());
46 0 : }
47 :
48 0 : bool operator() ( const OUString & rName )
49 : {
50 : try
51 : {
52 0 : return (m_xAccess->getByName( rName ) == m_aValue);
53 : }
54 0 : catch( const uno::Exception & ex )
55 : {
56 : ASSERT_EXCEPTION( ex );
57 : }
58 0 : return false;
59 : }
60 :
61 : private:
62 : Any m_aValue;
63 : Reference< container::XNameAccess > m_xAccess;
64 : };
65 :
66 0 : struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
67 : {
68 0 : lcl_StringMatches( const OUString & rCmpStr ) :
69 0 : m_aCmpStr( rCmpStr )
70 0 : {}
71 :
72 0 : bool operator() ( const OUString & rStr )
73 : {
74 0 : return rStr.match( m_aCmpStr );
75 : }
76 :
77 : private:
78 : OUString m_aCmpStr;
79 : };
80 :
81 : struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
82 : {
83 0 : lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
84 0 : m_nPrefixLength( nPrefixLength )
85 0 : {}
86 0 : sal_Int32 operator() ( const OUString & rStr )
87 : {
88 0 : if( m_nPrefixLength > rStr.getLength() )
89 0 : return 0;
90 0 : return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
91 : }
92 : private:
93 : sal_Int32 m_nPrefixLength;
94 : };
95 :
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.
99 :
100 : @param rPrefix
101 : The prefix used for automated name generation.
102 :
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.
106 :
107 : @return the new name under which the property was stored in the table
108 : */
109 0 : OUString lcl_addNamedPropertyUniqueNameToTable(
110 : const Any & rValue,
111 : const Reference< container::XNameContainer > & xNameContainer,
112 : const OUString & rPrefix,
113 : const OUString & rPreferredName )
114 : {
115 0 : if( ! xNameContainer.is() ||
116 0 : ! rValue.hasValue() ||
117 0 : ( rValue.getValueType() != xNameContainer->getElementType()))
118 0 : return rPreferredName;
119 :
120 : try
121 : {
122 0 : Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
123 0 : ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
124 : ::std::vector< OUString >::const_iterator aIt(
125 0 : ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
126 :
127 : // element not found in container
128 0 : if( aIt == aNames.end())
129 : {
130 0 : OUString aUniqueName;
131 :
132 : // check if preferred name is already used
133 0 : if( !rPreferredName.isEmpty())
134 : {
135 0 : aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
136 0 : if( aIt == aNames.end())
137 0 : aUniqueName = rPreferredName;
138 : }
139 :
140 0 : if( aUniqueName.isEmpty())
141 : {
142 : // create a unique id using the prefix plus a number
143 0 : ::std::vector< sal_Int32 > aNumbers;
144 : ::std::vector< OUString >::iterator aNonConstIt(
145 0 : ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
146 : ::std::transform( aNames.begin(), aNonConstIt,
147 : back_inserter( aNumbers ),
148 0 : lcl_OUStringRestToInt32( rPrefix.getLength() ));
149 : ::std::vector< sal_Int32 >::const_iterator aMaxIt(
150 0 : ::std::max_element( aNumbers.begin(), aNumbers.end()));
151 :
152 0 : sal_Int32 nIndex = 1;
153 0 : if( aMaxIt != aNumbers.end())
154 0 : nIndex = (*aMaxIt) + 1;
155 :
156 0 : aUniqueName = rPrefix + OUString::valueOf( nIndex );
157 : }
158 :
159 : OSL_ASSERT( !aUniqueName.isEmpty());
160 0 : xNameContainer->insertByName( aUniqueName, rValue );
161 0 : return aUniqueName;
162 : }
163 : else
164 : // element found => return name
165 0 : return *aIt;
166 : }
167 0 : catch( const uno::Exception & ex )
168 : {
169 : ASSERT_EXCEPTION( ex );
170 : }
171 :
172 0 : return rPreferredName;
173 : }
174 :
175 : } // anonymous namespace
176 :
177 : namespace chart
178 : {
179 : namespace PropertyHelper
180 : {
181 :
182 0 : OUString addLineDashUniqueNameToTable(
183 : const Any & rValue,
184 : const Reference< lang::XMultiServiceFactory > & xFact,
185 : const OUString & rPreferredName )
186 : {
187 0 : if( xFact.is())
188 : {
189 : Reference< container::XNameContainer > xNameCnt(
190 0 : xFact->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
191 0 : uno::UNO_QUERY );
192 0 : if( xNameCnt.is())
193 : return lcl_addNamedPropertyUniqueNameToTable(
194 0 : rValue, xNameCnt, C2U( "ChartDash " ), rPreferredName );
195 : }
196 0 : return OUString();
197 : }
198 :
199 0 : OUString addGradientUniqueNameToTable(
200 : const Any & rValue,
201 : const Reference< lang::XMultiServiceFactory > & xFact,
202 : const OUString & rPreferredName )
203 : {
204 0 : if( xFact.is())
205 : {
206 : Reference< container::XNameContainer > xNameCnt(
207 0 : xFact->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
208 0 : uno::UNO_QUERY );
209 0 : if( xNameCnt.is())
210 : return lcl_addNamedPropertyUniqueNameToTable(
211 0 : rValue, xNameCnt, C2U( "ChartGradient " ), rPreferredName );
212 : }
213 0 : return OUString();
214 : }
215 :
216 :
217 0 : OUString addTransparencyGradientUniqueNameToTable(
218 : const Any & rValue,
219 : const Reference< lang::XMultiServiceFactory > & xFact,
220 : const OUString & rPreferredName )
221 : {
222 0 : if( xFact.is())
223 : {
224 : Reference< container::XNameContainer > xNameCnt(
225 0 : xFact->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
226 0 : uno::UNO_QUERY );
227 0 : if( xNameCnt.is())
228 : return lcl_addNamedPropertyUniqueNameToTable(
229 0 : rValue, xNameCnt, C2U( "ChartTransparencyGradient " ), rPreferredName );
230 : }
231 0 : return OUString();
232 : }
233 :
234 0 : OUString addHatchUniqueNameToTable(
235 : const Any & rValue,
236 : const Reference< lang::XMultiServiceFactory > & xFact,
237 : const OUString & rPreferredName )
238 : {
239 0 : if( xFact.is())
240 : {
241 : Reference< container::XNameContainer > xNameCnt(
242 0 : xFact->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
243 0 : uno::UNO_QUERY );
244 0 : if( xNameCnt.is())
245 : return lcl_addNamedPropertyUniqueNameToTable(
246 0 : rValue, xNameCnt, C2U( "ChartHatch " ), rPreferredName );
247 : }
248 0 : return OUString();
249 : }
250 :
251 0 : OUString addBitmapUniqueNameToTable(
252 : const Any & rValue,
253 : const Reference< lang::XMultiServiceFactory > & xFact,
254 : const OUString & rPreferredName )
255 : {
256 0 : if( xFact.is())
257 : {
258 : Reference< container::XNameContainer > xNameCnt(
259 0 : xFact->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
260 0 : uno::UNO_QUERY );
261 0 : if( xNameCnt.is())
262 : return lcl_addNamedPropertyUniqueNameToTable(
263 0 : rValue, xNameCnt, C2U( "ChartBitmap " ), rPreferredName );
264 : }
265 0 : return OUString();
266 : }
267 :
268 : // ----------------------------------------
269 :
270 378 : void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
271 : {
272 378 : tPropertyValueMap::iterator aIt( rOutMap.find( key ));
273 378 : if( aIt == rOutMap.end())
274 363 : rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
275 : else
276 15 : (*aIt).second = rAny;
277 378 : }
278 :
279 : template<>
280 363 : void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
281 : {
282 363 : setPropertyValueAny( rOutMap, key, rAny );
283 363 : }
284 :
285 363 : void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
286 : {
287 : OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
288 363 : setPropertyValue( rOutMap, key, rAny );
289 363 : }
290 :
291 : template<>
292 21 : void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
293 : {
294 21 : setPropertyValueDefaultAny( rOutMap, key, rAny );
295 21 : }
296 :
297 :
298 6 : void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
299 : {
300 6 : setPropertyValueDefault( rOutMap, key, uno::Any());
301 6 : }
302 :
303 : } // namespace PropertyHelper
304 :
305 : } // namespace chart
306 :
307 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|