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 <com/sun/star/container/XNameContainer.hpp>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <comphelper/sequence.hxx>
24 #include <osl/diagnose.h>
25 #include <tools/diagnose_ex.h>
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
;
38 struct lcl_EqualsElement
40 explicit lcl_EqualsElement( const Any
& rValue
, const Reference
< container::XNameAccess
> & xAccess
)
41 : m_aValue( rValue
), m_xAccess( xAccess
)
43 OSL_ASSERT( m_xAccess
.is());
46 bool operator() ( const OUString
& rName
)
50 return (m_xAccess
->getByName( rName
) == m_aValue
);
52 catch( const uno::Exception
& )
54 DBG_UNHANDLED_EXCEPTION("chart2");
61 Reference
< container::XNameAccess
> m_xAccess
;
64 struct lcl_StringMatches
66 explicit lcl_StringMatches( const OUString
& rCmpStr
) :
70 bool operator() ( const OUString
& rStr
)
72 return rStr
.match( m_aCmpStr
);
79 struct lcl_OUStringRestToInt32
81 explicit lcl_OUStringRestToInt32( sal_Int32 nPrefixLength
) :
82 m_nPrefixLength( nPrefixLength
)
84 sal_Int32
operator() ( const OUString
& rStr
)
86 if( m_nPrefixLength
> rStr
.getLength() )
88 return rStr
.copy( m_nPrefixLength
).toInt32();
91 sal_Int32 m_nPrefixLength
;
94 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
95 line dash or line marker to the corresponding name container with a unique
99 The prefix used for automated name generation.
101 @param rPreferredName
102 If this string is not empty it is used as name if it is unique in the
103 table. Otherwise a new name is generated using pPrefix.
105 @return the new name under which the property was stored in the table
107 OUString
lcl_addNamedPropertyUniqueNameToTable(
109 const Reference
< container::XNameContainer
> & xNameContainer
,
110 const OUString
& rPrefix
,
111 const OUString
& rPreferredName
)
113 if( ! xNameContainer
.is() ||
114 ! rValue
.hasValue() ||
115 ( rValue
.getValueType() != xNameContainer
->getElementType()))
116 return rPreferredName
;
120 Reference
< container::XNameAccess
> xNameAccess( xNameContainer
, uno::UNO_QUERY_THROW
);
121 auto aNames( comphelper::sequenceToContainer
<std::vector
< OUString
>>( xNameAccess
->getElementNames()));
122 std::vector
< OUString
>::const_iterator
aIt(
123 std::find_if( aNames
.begin(), aNames
.end(), lcl_EqualsElement( rValue
, xNameAccess
)));
125 // element not found in container
126 if( aIt
== aNames
.end())
128 OUString aUniqueName
;
130 // check if preferred name is already used
131 if( !rPreferredName
.isEmpty())
133 aIt
= std::find( aNames
.begin(), aNames
.end(), rPreferredName
);
134 if( aIt
== aNames
.end())
135 aUniqueName
= rPreferredName
;
138 if( aUniqueName
.isEmpty())
140 // create a unique id using the prefix plus a number
141 std::vector
< sal_Int32
> aNumbers
;
142 std::vector
< OUString
>::iterator
aNonConstIt(
143 std::partition( aNames
.begin(), aNames
.end(), lcl_StringMatches( rPrefix
)));
144 std::transform( aNames
.begin(), aNonConstIt
,
145 back_inserter( aNumbers
),
146 lcl_OUStringRestToInt32( rPrefix
.getLength() ));
147 std::vector
< sal_Int32
>::const_iterator
aMaxIt(
148 std::max_element( aNumbers
.begin(), aNumbers
.end()));
150 sal_Int32 nIndex
= 1;
151 if( aMaxIt
!= aNumbers
.end())
152 nIndex
= (*aMaxIt
) + 1;
154 aUniqueName
= rPrefix
+ OUString::number( nIndex
);
157 OSL_ASSERT( !aUniqueName
.isEmpty());
158 xNameContainer
->insertByName( aUniqueName
, rValue
);
162 // element found => return name
165 catch( const uno::Exception
& )
167 DBG_UNHANDLED_EXCEPTION("chart2");
170 return rPreferredName
;
173 } // anonymous namespace
175 namespace chart::PropertyHelper
178 OUString
addLineDashUniqueNameToTable(
180 const Reference
< lang::XMultiServiceFactory
> & xFact
,
181 const OUString
& rPreferredName
)
185 Reference
< container::XNameContainer
> xNameCnt(
186 xFact
->createInstance( "com.sun.star.drawing.DashTable"),
189 return lcl_addNamedPropertyUniqueNameToTable(
190 rValue
, xNameCnt
, "ChartDash ", rPreferredName
);
195 OUString
addGradientUniqueNameToTable(
197 const Reference
< lang::XMultiServiceFactory
> & xFact
,
198 const OUString
& rPreferredName
)
202 Reference
< container::XNameContainer
> xNameCnt(
203 xFact
->createInstance( "com.sun.star.drawing.GradientTable"),
206 return lcl_addNamedPropertyUniqueNameToTable(
207 rValue
, xNameCnt
, "ChartGradient ", rPreferredName
);
212 OUString
addTransparencyGradientUniqueNameToTable(
214 const Reference
< lang::XMultiServiceFactory
> & xFact
,
215 const OUString
& rPreferredName
)
219 Reference
< container::XNameContainer
> xNameCnt(
220 xFact
->createInstance( "com.sun.star.drawing.TransparencyGradientTable"),
223 return lcl_addNamedPropertyUniqueNameToTable(
224 rValue
, xNameCnt
, "ChartTransparencyGradient ", rPreferredName
);
229 OUString
addHatchUniqueNameToTable(
231 const Reference
< lang::XMultiServiceFactory
> & xFact
,
232 const OUString
& rPreferredName
)
236 Reference
< container::XNameContainer
> xNameCnt(
237 xFact
->createInstance( "com.sun.star.drawing.HatchTable"),
240 return lcl_addNamedPropertyUniqueNameToTable(
241 rValue
, xNameCnt
, "ChartHatch ", rPreferredName
);
246 OUString
addBitmapUniqueNameToTable(
248 const Reference
< lang::XMultiServiceFactory
> & xFact
,
249 const OUString
& rPreferredName
)
253 Reference
< container::XNameContainer
> xNameCnt(
254 xFact
->createInstance( "com.sun.star.drawing.BitmapTable"),
257 return lcl_addNamedPropertyUniqueNameToTable(
258 rValue
, xNameCnt
, "ChartBitmap ", rPreferredName
);
263 void setPropertyValueAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
265 tPropertyValueMap::iterator
aIt( rOutMap
.find( key
));
266 if( aIt
== rOutMap
.end())
267 rOutMap
.emplace( key
, rAny
);
269 (*aIt
).second
= rAny
;
273 void setPropertyValue
< css::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const css::uno::Any
& rAny
)
275 setPropertyValueAny( rOutMap
, key
, rAny
);
278 void setPropertyValueDefaultAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
280 OSL_ENSURE( rOutMap
.end() == rOutMap
.find( key
), "Default already exists for property" );
281 setPropertyValue( rOutMap
, key
, rAny
);
285 void setPropertyValueDefault
< css::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const css::uno::Any
& rAny
)
287 setPropertyValueDefaultAny( rOutMap
, key
, rAny
);
290 void setEmptyPropertyValueDefault( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
)
292 setPropertyValueDefault( rOutMap
, key
, uno::Any());
295 } // namespace chart::PropertyHelper
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */