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>
25 #include <osl/diagnose.h>
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::beans
;
34 using ::com::sun::star::uno::Any
;
35 using ::com::sun::star::uno::Reference
;
36 using ::com::sun::star::uno::Sequence
;
40 struct lcl_EqualsElement
: public ::std::unary_function
< OUString
, bool >
42 explicit lcl_EqualsElement( const Any
& rValue
, const Reference
< container::XNameAccess
> & xAccess
)
43 : m_aValue( rValue
), m_xAccess( xAccess
)
45 OSL_ASSERT( m_xAccess
.is());
48 bool operator() ( const OUString
& rName
)
52 return (m_xAccess
->getByName( rName
) == m_aValue
);
54 catch( const uno::Exception
& ex
)
56 ASSERT_EXCEPTION( ex
);
63 Reference
< container::XNameAccess
> m_xAccess
;
66 struct lcl_StringMatches
: public ::std::unary_function
< OUString
,bool >
68 lcl_StringMatches( const OUString
& rCmpStr
) :
72 bool operator() ( const OUString
& rStr
)
74 return rStr
.match( m_aCmpStr
);
81 struct lcl_OUStringRestToInt32
: public ::std::unary_function
< OUString
, sal_Int32
>
83 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength
) :
84 m_nPrefixLength( nPrefixLength
)
86 sal_Int32
operator() ( const OUString
& rStr
)
88 if( m_nPrefixLength
> rStr
.getLength() )
90 return rStr
.copy( m_nPrefixLength
).toInt32();
93 sal_Int32 m_nPrefixLength
;
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
101 The prefix used for automated name generation.
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.
107 @return the new name under which the property was stored in the table
109 OUString
lcl_addNamedPropertyUniqueNameToTable(
111 const Reference
< container::XNameContainer
> & xNameContainer
,
112 const OUString
& rPrefix
,
113 const OUString
& rPreferredName
)
115 if( ! xNameContainer
.is() ||
116 ! rValue
.hasValue() ||
117 ( rValue
.getValueType() != xNameContainer
->getElementType()))
118 return rPreferredName
;
122 Reference
< container::XNameAccess
> xNameAccess( xNameContainer
, uno::UNO_QUERY_THROW
);
123 ::std::vector
< OUString
> aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess
->getElementNames()));
124 ::std::vector
< OUString
>::const_iterator
aIt(
125 ::std::find_if( aNames
.begin(), aNames
.end(), lcl_EqualsElement( rValue
, xNameAccess
)));
127 // element not found in container
128 if( aIt
== aNames
.end())
130 OUString aUniqueName
;
132 // check if preferred name is already used
133 if( !rPreferredName
.isEmpty())
135 aIt
= ::std::find( aNames
.begin(), aNames
.end(), rPreferredName
);
136 if( aIt
== aNames
.end())
137 aUniqueName
= rPreferredName
;
140 if( aUniqueName
.isEmpty())
142 // create a unique id using the prefix plus a number
143 ::std::vector
< sal_Int32
> aNumbers
;
144 ::std::vector
< OUString
>::iterator
aNonConstIt(
145 ::std::partition( aNames
.begin(), aNames
.end(), lcl_StringMatches( rPrefix
)));
146 ::std::transform( aNames
.begin(), aNonConstIt
,
147 back_inserter( aNumbers
),
148 lcl_OUStringRestToInt32( rPrefix
.getLength() ));
149 ::std::vector
< sal_Int32
>::const_iterator
aMaxIt(
150 ::std::max_element( aNumbers
.begin(), aNumbers
.end()));
152 sal_Int32 nIndex
= 1;
153 if( aMaxIt
!= aNumbers
.end())
154 nIndex
= (*aMaxIt
) + 1;
156 aUniqueName
= rPrefix
+ OUString::number( nIndex
);
159 OSL_ASSERT( !aUniqueName
.isEmpty());
160 xNameContainer
->insertByName( aUniqueName
, rValue
);
164 // element found => return name
167 catch( const uno::Exception
& ex
)
169 ASSERT_EXCEPTION( ex
);
172 return rPreferredName
;
175 } // anonymous namespace
179 namespace PropertyHelper
182 OUString
addLineDashUniqueNameToTable(
184 const Reference
< lang::XMultiServiceFactory
> & xFact
,
185 const OUString
& rPreferredName
)
189 Reference
< container::XNameContainer
> xNameCnt(
190 xFact
->createInstance( "com.sun.star.drawing.DashTable"),
193 return lcl_addNamedPropertyUniqueNameToTable(
194 rValue
, xNameCnt
, "ChartDash ", rPreferredName
);
199 OUString
addGradientUniqueNameToTable(
201 const Reference
< lang::XMultiServiceFactory
> & xFact
,
202 const OUString
& rPreferredName
)
206 Reference
< container::XNameContainer
> xNameCnt(
207 xFact
->createInstance( "com.sun.star.drawing.GradientTable"),
210 return lcl_addNamedPropertyUniqueNameToTable(
211 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 void setPropertyValueAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
269 tPropertyValueMap::iterator
aIt( rOutMap
.find( key
));
270 if( aIt
== rOutMap
.end())
271 rOutMap
.insert( tPropertyValueMap::value_type( key
, rAny
));
273 (*aIt
).second
= rAny
;
277 void setPropertyValue
< ::com::sun::star::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const ::com::sun::star::uno::Any
& rAny
)
279 setPropertyValueAny( rOutMap
, key
, rAny
);
282 void setPropertyValueDefaultAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
284 OSL_ENSURE( rOutMap
.end() == rOutMap
.find( key
), "Default already exists for property" );
285 setPropertyValue( rOutMap
, key
, rAny
);
289 void setPropertyValueDefault
< ::com::sun::star::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const ::com::sun::star::uno::Any
& rAny
)
291 setPropertyValueDefaultAny( rOutMap
, key
, rAny
);
294 void setEmptyPropertyValueDefault( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
)
296 setPropertyValueDefault( rOutMap
, key
, uno::Any());
299 } // namespace PropertyHelper
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */