1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_chart2.hxx"
30*cdf0e10cSrcweir #include "PropertyHelper.hxx"
31*cdf0e10cSrcweir #include "ContainerHelper.hxx"
32*cdf0e10cSrcweir #include "macros.hxx"
33*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <vector>
37*cdf0e10cSrcweir #include <algorithm>
38*cdf0e10cSrcweir #include <functional>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::com::sun::star;
41*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
42*cdf0e10cSrcweir using ::rtl::OUString;
43*cdf0e10cSrcweir using ::com::sun::star::uno::Any;
44*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
45*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
52*cdf0e10cSrcweir             : m_aValue( rValue ), m_xAccess( xAccess )
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         OSL_ASSERT( m_xAccess.is());
55*cdf0e10cSrcweir     }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     bool operator() ( const OUString & rName )
58*cdf0e10cSrcweir     {
59*cdf0e10cSrcweir         try
60*cdf0e10cSrcweir         {
61*cdf0e10cSrcweir             return (m_xAccess->getByName( rName ) == m_aValue);
62*cdf0e10cSrcweir         }
63*cdf0e10cSrcweir         catch( const uno::Exception & ex )
64*cdf0e10cSrcweir         {
65*cdf0e10cSrcweir             ASSERT_EXCEPTION( ex );
66*cdf0e10cSrcweir         }
67*cdf0e10cSrcweir         return false;
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir private:
71*cdf0e10cSrcweir     Any m_aValue;
72*cdf0e10cSrcweir     Reference< container::XNameAccess > m_xAccess;
73*cdf0e10cSrcweir };
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir     lcl_StringMatches( const OUString & rCmpStr ) :
78*cdf0e10cSrcweir             m_aCmpStr( rCmpStr )
79*cdf0e10cSrcweir     {}
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     bool operator() ( const OUString & rStr )
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir         return rStr.match( m_aCmpStr );
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir private:
87*cdf0e10cSrcweir     OUString m_aCmpStr;
88*cdf0e10cSrcweir };
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir     lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
93*cdf0e10cSrcweir             m_nPrefixLength( nPrefixLength )
94*cdf0e10cSrcweir     {}
95*cdf0e10cSrcweir     sal_Int32 operator() ( const OUString & rStr )
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         if( m_nPrefixLength > rStr.getLength() )
98*cdf0e10cSrcweir             return 0;
99*cdf0e10cSrcweir         return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir private:
102*cdf0e10cSrcweir     sal_Int32 m_nPrefixLength;
103*cdf0e10cSrcweir };
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
106*cdf0e10cSrcweir     line dash or line marker to the corresponding name container with a unique
107*cdf0e10cSrcweir     name.
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     @param rPrefix
110*cdf0e10cSrcweir         The prefix used for automated name generation.
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     @param rPreferredName
113*cdf0e10cSrcweir         If this string is not empty it is used as name if it is unique in the
114*cdf0e10cSrcweir         table. Otherwise a new name is generated using pPrefix.
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     @return the new name under which the property was stored in the table
117*cdf0e10cSrcweir */
118*cdf0e10cSrcweir OUString lcl_addNamedPropertyUniqueNameToTable(
119*cdf0e10cSrcweir     const Any & rValue,
120*cdf0e10cSrcweir     const Reference< container::XNameContainer > & xNameContainer,
121*cdf0e10cSrcweir     const OUString & rPrefix,
122*cdf0e10cSrcweir     const OUString & rPreferredName )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     if( ! xNameContainer.is() ||
125*cdf0e10cSrcweir         ! rValue.hasValue() ||
126*cdf0e10cSrcweir         ( rValue.getValueType() != xNameContainer->getElementType()))
127*cdf0e10cSrcweir         return rPreferredName;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     try
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
132*cdf0e10cSrcweir         ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
133*cdf0e10cSrcweir         ::std::vector< OUString >::const_iterator aIt(
134*cdf0e10cSrcweir             ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         // element not found in container
137*cdf0e10cSrcweir         if( aIt == aNames.end())
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             OUString aUniqueName;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir             // check if preferred name is already used
142*cdf0e10cSrcweir             if( rPreferredName.getLength())
143*cdf0e10cSrcweir             {
144*cdf0e10cSrcweir                 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
145*cdf0e10cSrcweir                 if( aIt == aNames.end())
146*cdf0e10cSrcweir                     aUniqueName = rPreferredName;
147*cdf0e10cSrcweir             }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             if( ! aUniqueName.getLength())
150*cdf0e10cSrcweir             {
151*cdf0e10cSrcweir                 // create a unique id using the prefix plus a number
152*cdf0e10cSrcweir                 ::std::vector< sal_Int32 > aNumbers;
153*cdf0e10cSrcweir                 ::std::vector< OUString >::iterator aNonConstIt(
154*cdf0e10cSrcweir                     ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
155*cdf0e10cSrcweir                 ::std::transform( aNames.begin(), aNonConstIt,
156*cdf0e10cSrcweir                                   back_inserter( aNumbers ),
157*cdf0e10cSrcweir                                   lcl_OUStringRestToInt32( rPrefix.getLength() ));
158*cdf0e10cSrcweir                 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
159*cdf0e10cSrcweir                     ::std::max_element( aNumbers.begin(), aNumbers.end()));
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir                 sal_Int32 nIndex = 1;
162*cdf0e10cSrcweir                 if( aMaxIt != aNumbers.end())
163*cdf0e10cSrcweir                     nIndex = (*aMaxIt) + 1;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir                 aUniqueName = rPrefix + OUString::valueOf( nIndex );
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             OSL_ASSERT( aUniqueName.getLength());
169*cdf0e10cSrcweir             xNameContainer->insertByName( aUniqueName, rValue );
170*cdf0e10cSrcweir             return aUniqueName;
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir         else
173*cdf0e10cSrcweir             // element found => return name
174*cdf0e10cSrcweir             return *aIt;
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir     catch( const uno::Exception & ex )
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir         ASSERT_EXCEPTION( ex );
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     return rPreferredName;
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir } // anonymous namespace
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir namespace chart
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir namespace PropertyHelper
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir OUString addLineDashUniqueNameToTable(
192*cdf0e10cSrcweir     const Any & rValue,
193*cdf0e10cSrcweir     const Reference< lang::XMultiServiceFactory > & xFact,
194*cdf0e10cSrcweir     const OUString & rPreferredName )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     if( xFact.is())
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         Reference< container::XNameContainer > xNameCnt(
199*cdf0e10cSrcweir             xFact->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
200*cdf0e10cSrcweir             uno::UNO_QUERY );
201*cdf0e10cSrcweir         if( xNameCnt.is())
202*cdf0e10cSrcweir             return lcl_addNamedPropertyUniqueNameToTable(
203*cdf0e10cSrcweir                 rValue, xNameCnt, C2U( "ChartDash " ), rPreferredName );
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir     return OUString();
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir OUString addGradientUniqueNameToTable(
209*cdf0e10cSrcweir     const Any & rValue,
210*cdf0e10cSrcweir     const Reference< lang::XMultiServiceFactory > & xFact,
211*cdf0e10cSrcweir     const OUString & rPreferredName )
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     if( xFact.is())
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         Reference< container::XNameContainer > xNameCnt(
216*cdf0e10cSrcweir             xFact->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
217*cdf0e10cSrcweir             uno::UNO_QUERY );
218*cdf0e10cSrcweir         if( xNameCnt.is())
219*cdf0e10cSrcweir             return lcl_addNamedPropertyUniqueNameToTable(
220*cdf0e10cSrcweir                 rValue, xNameCnt, C2U( "ChartGradient " ), rPreferredName );
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir     return OUString();
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir OUString addTransparencyGradientUniqueNameToTable(
227*cdf0e10cSrcweir     const Any & rValue,
228*cdf0e10cSrcweir     const Reference< lang::XMultiServiceFactory > & xFact,
229*cdf0e10cSrcweir     const OUString & rPreferredName )
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     if( xFact.is())
232*cdf0e10cSrcweir     {
233*cdf0e10cSrcweir         Reference< container::XNameContainer > xNameCnt(
234*cdf0e10cSrcweir             xFact->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
235*cdf0e10cSrcweir             uno::UNO_QUERY );
236*cdf0e10cSrcweir         if( xNameCnt.is())
237*cdf0e10cSrcweir             return lcl_addNamedPropertyUniqueNameToTable(
238*cdf0e10cSrcweir                 rValue, xNameCnt, C2U( "ChartTransparencyGradient " ), rPreferredName );
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir     return OUString();
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir OUString addHatchUniqueNameToTable(
244*cdf0e10cSrcweir     const Any & rValue,
245*cdf0e10cSrcweir     const Reference< lang::XMultiServiceFactory > & xFact,
246*cdf0e10cSrcweir     const OUString & rPreferredName )
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir     if( xFact.is())
249*cdf0e10cSrcweir     {
250*cdf0e10cSrcweir         Reference< container::XNameContainer > xNameCnt(
251*cdf0e10cSrcweir             xFact->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
252*cdf0e10cSrcweir             uno::UNO_QUERY );
253*cdf0e10cSrcweir         if( xNameCnt.is())
254*cdf0e10cSrcweir             return lcl_addNamedPropertyUniqueNameToTable(
255*cdf0e10cSrcweir                 rValue, xNameCnt, C2U( "ChartHatch " ), rPreferredName );
256*cdf0e10cSrcweir     }
257*cdf0e10cSrcweir     return OUString();
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir OUString addBitmapUniqueNameToTable(
261*cdf0e10cSrcweir     const Any & rValue,
262*cdf0e10cSrcweir     const Reference< lang::XMultiServiceFactory > & xFact,
263*cdf0e10cSrcweir     const OUString & rPreferredName )
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir     if( xFact.is())
266*cdf0e10cSrcweir     {
267*cdf0e10cSrcweir         Reference< container::XNameContainer > xNameCnt(
268*cdf0e10cSrcweir             xFact->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
269*cdf0e10cSrcweir             uno::UNO_QUERY );
270*cdf0e10cSrcweir         if( xNameCnt.is())
271*cdf0e10cSrcweir             return lcl_addNamedPropertyUniqueNameToTable(
272*cdf0e10cSrcweir                 rValue, xNameCnt, C2U( "ChartBitmap " ), rPreferredName );
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir     return OUString();
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir // ----------------------------------------
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
280*cdf0e10cSrcweir {
281*cdf0e10cSrcweir     tPropertyValueMap::iterator aIt( rOutMap.find( key ));
282*cdf0e10cSrcweir     if( aIt == rOutMap.end())
283*cdf0e10cSrcweir         rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
284*cdf0e10cSrcweir     else
285*cdf0e10cSrcweir         (*aIt).second = rAny;
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir template<>
289*cdf0e10cSrcweir     void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
290*cdf0e10cSrcweir {
291*cdf0e10cSrcweir     setPropertyValueAny( rOutMap, key, rAny );
292*cdf0e10cSrcweir }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir     OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
297*cdf0e10cSrcweir     setPropertyValue( rOutMap, key, rAny );
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir template<>
301*cdf0e10cSrcweir     void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir     setPropertyValueDefaultAny( rOutMap, key, rAny );
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
308*cdf0e10cSrcweir {
309*cdf0e10cSrcweir     setPropertyValueDefault( rOutMap, key, uno::Any());
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir } // namespace PropertyHelper
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir } //  namespace chart
315