xref: /trunk/main/chart2/source/tools/PropertyHelper.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 : public ::std::unary_function< OUString, bool >
50 {
lcl_EqualsElement__anona49d89af0111::lcl_EqualsElement51     explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
52             : m_aValue( rValue ), m_xAccess( xAccess )
53     {
54         OSL_ASSERT( m_xAccess.is());
55     }
56 
operator ()__anona49d89af0111::lcl_EqualsElement57     bool operator() ( const OUString & rName )
58     {
59         try
60         {
61             return (m_xAccess->getByName( rName ) == m_aValue);
62         }
63         catch( const uno::Exception & ex )
64         {
65             ASSERT_EXCEPTION( ex );
66         }
67         return false;
68     }
69 
70 private:
71     Any m_aValue;
72     Reference< container::XNameAccess > m_xAccess;
73 };
74 
75 struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
76 {
lcl_StringMatches__anona49d89af0111::lcl_StringMatches77     lcl_StringMatches( const OUString & rCmpStr ) :
78             m_aCmpStr( rCmpStr )
79     {}
80 
operator ()__anona49d89af0111::lcl_StringMatches81     bool operator() ( const OUString & rStr )
82     {
83         return rStr.match( m_aCmpStr );
84     }
85 
86 private:
87     OUString m_aCmpStr;
88 };
89 
90 struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
91 {
lcl_OUStringRestToInt32__anona49d89af0111::lcl_OUStringRestToInt3292     lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
93             m_nPrefixLength( nPrefixLength )
94     {}
operator ()__anona49d89af0111::lcl_OUStringRestToInt3295     sal_Int32 operator() ( const OUString & rStr )
96     {
97         if( m_nPrefixLength > rStr.getLength() )
98             return 0;
99         return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
100     }
101 private:
102     sal_Int32 m_nPrefixLength;
103 };
104 
105 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
106     line dash or line marker to the corresponding name container with a unique
107     name.
108 
109     @param rPrefix
110         The prefix used for automated name generation.
111 
112     @param rPreferredName
113         If this string is not empty it is used as name if it is unique in the
114         table. Otherwise a new name is generated using pPrefix.
115 
116     @return the new name under which the property was stored in the table
117 */
lcl_addNamedPropertyUniqueNameToTable(const Any & rValue,const Reference<container::XNameContainer> & xNameContainer,const OUString & rPrefix,const OUString & rPreferredName)118 OUString lcl_addNamedPropertyUniqueNameToTable(
119     const Any & rValue,
120     const Reference< container::XNameContainer > & xNameContainer,
121     const OUString & rPrefix,
122     const OUString & rPreferredName )
123 {
124     if( ! xNameContainer.is() ||
125         ! rValue.hasValue() ||
126         ( rValue.getValueType() != xNameContainer->getElementType()))
127         return rPreferredName;
128 
129     try
130     {
131         Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
132         ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
133         ::std::vector< OUString >::const_iterator aIt(
134             ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
135 
136         // element not found in container
137         if( aIt == aNames.end())
138         {
139             OUString aUniqueName;
140 
141             // check if preferred name is already used
142             if( !rPreferredName.isEmpty() )
143             {
144                 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
145                 if( aIt == aNames.end())
146                     aUniqueName = rPreferredName;
147             }
148 
149             if( aUniqueName.isEmpty() )
150             {
151                 // create a unique id using the prefix plus a number
152                 ::std::vector< sal_Int32 > aNumbers;
153                 ::std::vector< OUString >::iterator aNonConstIt(
154                     ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
155                 ::std::transform( aNames.begin(), aNonConstIt,
156                                   back_inserter( aNumbers ),
157                                   lcl_OUStringRestToInt32( rPrefix.getLength() ));
158                 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
159                     ::std::max_element( aNumbers.begin(), aNumbers.end()));
160 
161                 sal_Int32 nIndex = 1;
162                 if( aMaxIt != aNumbers.end())
163                     nIndex = (*aMaxIt) + 1;
164 
165                 aUniqueName = rPrefix + OUString::valueOf( nIndex );
166             }
167 
168             OSL_ASSERT( !aUniqueName.isEmpty() );
169             xNameContainer->insertByName( aUniqueName, rValue );
170             return aUniqueName;
171         }
172         else
173             // element found => return name
174             return *aIt;
175     }
176     catch( const uno::Exception & ex )
177     {
178         ASSERT_EXCEPTION( ex );
179     }
180 
181     return rPreferredName;
182 }
183 
184 } // anonymous namespace
185 
186 namespace chart
187 {
188 namespace PropertyHelper
189 {
190 
addLineDashUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)191 OUString addLineDashUniqueNameToTable(
192     const Any & rValue,
193     const Reference< lang::XMultiServiceFactory > & xFact,
194     const OUString & rPreferredName )
195 {
196     if( xFact.is())
197     {
198         Reference< container::XNameContainer > xNameCnt(
199             xFact->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
200             uno::UNO_QUERY );
201         if( xNameCnt.is())
202             return lcl_addNamedPropertyUniqueNameToTable(
203                 rValue, xNameCnt, C2U( "ChartDash " ), rPreferredName );
204     }
205     return OUString();
206 }
207 
addGradientUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)208 OUString addGradientUniqueNameToTable(
209     const Any & rValue,
210     const Reference< lang::XMultiServiceFactory > & xFact,
211     const OUString & rPreferredName )
212 {
213     if( xFact.is())
214     {
215         Reference< container::XNameContainer > xNameCnt(
216             xFact->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
217             uno::UNO_QUERY );
218         if( xNameCnt.is())
219             return lcl_addNamedPropertyUniqueNameToTable(
220                 rValue, xNameCnt, C2U( "ChartGradient " ), rPreferredName );
221     }
222     return OUString();
223 }
224 
225 
addTransparencyGradientUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)226 OUString addTransparencyGradientUniqueNameToTable(
227     const Any & rValue,
228     const Reference< lang::XMultiServiceFactory > & xFact,
229     const OUString & rPreferredName )
230 {
231     if( xFact.is())
232     {
233         Reference< container::XNameContainer > xNameCnt(
234             xFact->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
235             uno::UNO_QUERY );
236         if( xNameCnt.is())
237             return lcl_addNamedPropertyUniqueNameToTable(
238                 rValue, xNameCnt, C2U( "ChartTransparencyGradient " ), rPreferredName );
239     }
240     return OUString();
241 }
242 
addHatchUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)243 OUString addHatchUniqueNameToTable(
244     const Any & rValue,
245     const Reference< lang::XMultiServiceFactory > & xFact,
246     const OUString & rPreferredName )
247 {
248     if( xFact.is())
249     {
250         Reference< container::XNameContainer > xNameCnt(
251             xFact->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
252             uno::UNO_QUERY );
253         if( xNameCnt.is())
254             return lcl_addNamedPropertyUniqueNameToTable(
255                 rValue, xNameCnt, C2U( "ChartHatch " ), rPreferredName );
256     }
257     return OUString();
258 }
259 
addBitmapUniqueNameToTable(const Any & rValue,const Reference<lang::XMultiServiceFactory> & xFact,const OUString & rPreferredName)260 OUString addBitmapUniqueNameToTable(
261     const Any & rValue,
262     const Reference< lang::XMultiServiceFactory > & xFact,
263     const OUString & rPreferredName )
264 {
265     if( xFact.is())
266     {
267         Reference< container::XNameContainer > xNameCnt(
268             xFact->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
269             uno::UNO_QUERY );
270         if( xNameCnt.is())
271             return lcl_addNamedPropertyUniqueNameToTable(
272                 rValue, xNameCnt, C2U( "ChartBitmap " ), rPreferredName );
273     }
274     return OUString();
275 }
276 
277 // ----------------------------------------
278 
setPropertyValueAny(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const uno::Any & rAny)279 void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
280 {
281     tPropertyValueMap::iterator aIt( rOutMap.find( key ));
282     if( aIt == rOutMap.end())
283         rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
284     else
285         (*aIt).second = rAny;
286 }
287 
288 template<>
setPropertyValue(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const::com::sun::star::uno::Any & rAny)289     void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
290 {
291     setPropertyValueAny( rOutMap, key, rAny );
292 }
293 
setPropertyValueDefaultAny(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const uno::Any & rAny)294 void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
295 {
296     OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
297     setPropertyValue( rOutMap, key, rAny );
298 }
299 
300 template<>
setPropertyValueDefault(tPropertyValueMap & rOutMap,tPropertyValueMapKey key,const::com::sun::star::uno::Any & rAny)301     void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
302 {
303     setPropertyValueDefaultAny( rOutMap, key, rAny );
304 }
305 
306 
setEmptyPropertyValueDefault(tPropertyValueMap & rOutMap,tPropertyValueMapKey key)307 void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
308 {
309     setPropertyValueDefault( rOutMap, key, uno::Any());
310 }
311 
312 } // namespace PropertyHelper
313 
314 } //  namespace chart
315