1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::beans
;
33 using ::com::sun::star::uno::Any
;
34 using ::com::sun::star::uno::Reference
;
35 using ::com::sun::star::uno::Sequence
;
39 struct lcl_EqualsElement
: public ::std::unary_function
< OUString
, bool >
41 explicit lcl_EqualsElement( const Any
& rValue
, const Reference
< container::XNameAccess
> & xAccess
)
42 : m_aValue( rValue
), m_xAccess( xAccess
)
44 OSL_ASSERT( m_xAccess
.is());
47 bool operator() ( const OUString
& rName
)
51 return (m_xAccess
->getByName( rName
) == m_aValue
);
53 catch( const uno::Exception
& ex
)
55 ASSERT_EXCEPTION( ex
);
62 Reference
< container::XNameAccess
> m_xAccess
;
65 struct lcl_StringMatches
: public ::std::unary_function
< OUString
,bool >
67 lcl_StringMatches( const OUString
& rCmpStr
) :
71 bool operator() ( const OUString
& rStr
)
73 return rStr
.match( m_aCmpStr
);
80 struct lcl_OUStringRestToInt32
: public ::std::unary_function
< OUString
, sal_Int32
>
82 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength
) :
83 m_nPrefixLength( nPrefixLength
)
85 sal_Int32
operator() ( const OUString
& rStr
)
87 if( m_nPrefixLength
> rStr
.getLength() )
89 return rStr
.copy( m_nPrefixLength
).toInt32( 10 /* radix */ );
92 sal_Int32 m_nPrefixLength
;
95 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
96 line dash or line marker to the corresponding name container with a unique
100 The prefix used for automated name generation.
102 @param rPreferredName
103 If this string is not empty it is used as name if it is unique in the
104 table. Otherwise a new name is generated using pPrefix.
106 @return the new name under which the property was stored in the table
108 OUString
lcl_addNamedPropertyUniqueNameToTable(
110 const Reference
< container::XNameContainer
> & xNameContainer
,
111 const OUString
& rPrefix
,
112 const OUString
& rPreferredName
)
114 if( ! xNameContainer
.is() ||
115 ! rValue
.hasValue() ||
116 ( rValue
.getValueType() != xNameContainer
->getElementType()))
117 return rPreferredName
;
121 Reference
< container::XNameAccess
> xNameAccess( xNameContainer
, uno::UNO_QUERY_THROW
);
122 ::std::vector
< OUString
> aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess
->getElementNames()));
123 ::std::vector
< OUString
>::const_iterator
aIt(
124 ::std::find_if( aNames
.begin(), aNames
.end(), lcl_EqualsElement( rValue
, xNameAccess
)));
126 // element not found in container
127 if( aIt
== aNames
.end())
129 OUString aUniqueName
;
131 // check if preferred name is already used
132 if( !rPreferredName
.isEmpty())
134 aIt
= ::std::find( aNames
.begin(), aNames
.end(), rPreferredName
);
135 if( aIt
== aNames
.end())
136 aUniqueName
= rPreferredName
;
139 if( aUniqueName
.isEmpty())
141 // create a unique id using the prefix plus a number
142 ::std::vector
< sal_Int32
> aNumbers
;
143 ::std::vector
< OUString
>::iterator
aNonConstIt(
144 ::std::partition( aNames
.begin(), aNames
.end(), lcl_StringMatches( rPrefix
)));
145 ::std::transform( aNames
.begin(), aNonConstIt
,
146 back_inserter( aNumbers
),
147 lcl_OUStringRestToInt32( rPrefix
.getLength() ));
148 ::std::vector
< sal_Int32
>::const_iterator
aMaxIt(
149 ::std::max_element( aNumbers
.begin(), aNumbers
.end()));
151 sal_Int32 nIndex
= 1;
152 if( aMaxIt
!= aNumbers
.end())
153 nIndex
= (*aMaxIt
) + 1;
155 aUniqueName
= rPrefix
+ OUString::valueOf( nIndex
);
158 OSL_ASSERT( !aUniqueName
.isEmpty());
159 xNameContainer
->insertByName( aUniqueName
, rValue
);
163 // element found => return name
166 catch( const uno::Exception
& ex
)
168 ASSERT_EXCEPTION( ex
);
171 return rPreferredName
;
174 } // anonymous namespace
178 namespace PropertyHelper
181 OUString
addLineDashUniqueNameToTable(
183 const Reference
< lang::XMultiServiceFactory
> & xFact
,
184 const OUString
& rPreferredName
)
188 Reference
< container::XNameContainer
> xNameCnt(
189 xFact
->createInstance( "com.sun.star.drawing.DashTable"),
192 return lcl_addNamedPropertyUniqueNameToTable(
193 rValue
, xNameCnt
, "ChartDash ", rPreferredName
);
198 OUString
addGradientUniqueNameToTable(
200 const Reference
< lang::XMultiServiceFactory
> & xFact
,
201 const OUString
& rPreferredName
)
205 Reference
< container::XNameContainer
> xNameCnt(
206 xFact
->createInstance( "com.sun.star.drawing.GradientTable"),
209 return lcl_addNamedPropertyUniqueNameToTable(
210 rValue
, xNameCnt
, "ChartGradient ", rPreferredName
);
216 OUString
addTransparencyGradientUniqueNameToTable(
218 const Reference
< lang::XMultiServiceFactory
> & xFact
,
219 const OUString
& rPreferredName
)
223 Reference
< container::XNameContainer
> xNameCnt(
224 xFact
->createInstance( "com.sun.star.drawing.TransparencyGradientTable"),
227 return lcl_addNamedPropertyUniqueNameToTable(
228 rValue
, xNameCnt
, "ChartTransparencyGradient ", rPreferredName
);
233 OUString
addHatchUniqueNameToTable(
235 const Reference
< lang::XMultiServiceFactory
> & xFact
,
236 const OUString
& rPreferredName
)
240 Reference
< container::XNameContainer
> xNameCnt(
241 xFact
->createInstance( "com.sun.star.drawing.HatchTable"),
244 return lcl_addNamedPropertyUniqueNameToTable(
245 rValue
, xNameCnt
, "ChartHatch ", rPreferredName
);
250 OUString
addBitmapUniqueNameToTable(
252 const Reference
< lang::XMultiServiceFactory
> & xFact
,
253 const OUString
& rPreferredName
)
257 Reference
< container::XNameContainer
> xNameCnt(
258 xFact
->createInstance( "com.sun.star.drawing.BitmapTable"),
261 return lcl_addNamedPropertyUniqueNameToTable(
262 rValue
, xNameCnt
, "ChartBitmap ", rPreferredName
);
267 // ----------------------------------------
269 void setPropertyValueAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
271 tPropertyValueMap::iterator
aIt( rOutMap
.find( key
));
272 if( aIt
== rOutMap
.end())
273 rOutMap
.insert( tPropertyValueMap::value_type( key
, rAny
));
275 (*aIt
).second
= rAny
;
279 void setPropertyValue
< ::com::sun::star::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const ::com::sun::star::uno::Any
& rAny
)
281 setPropertyValueAny( rOutMap
, key
, rAny
);
284 void setPropertyValueDefaultAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
286 OSL_ENSURE( rOutMap
.end() == rOutMap
.find( key
), "Default already exists for property" );
287 setPropertyValue( rOutMap
, key
, rAny
);
291 void setPropertyValueDefault
< ::com::sun::star::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const ::com::sun::star::uno::Any
& rAny
)
293 setPropertyValueDefaultAny( rOutMap
, key
, rAny
);
297 void setEmptyPropertyValueDefault( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
)
299 setPropertyValueDefault( rOutMap
, key
, uno::Any());
302 } // namespace PropertyHelper
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */