nss: upgrade to release 3.73
[LibreOffice.git] / chart2 / source / tools / PropertyHelper.cxx
blobcb3e860c0350d09aa3aac9a3a61b022ab4a12d08
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 #include <vector>
28 #include <algorithm>
29 #include <iterator>
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;
36 namespace
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 )
48 try
50 return (m_xAccess->getByName( rName ) == m_aValue);
52 catch( const uno::Exception & )
54 DBG_UNHANDLED_EXCEPTION("chart2");
56 return false;
59 private:
60 Any m_aValue;
61 Reference< container::XNameAccess > m_xAccess;
64 struct lcl_StringMatches
66 explicit lcl_StringMatches( const OUString & rCmpStr ) :
67 m_aCmpStr( rCmpStr )
70 bool operator() ( const OUString & rStr )
72 return rStr.match( m_aCmpStr );
75 private:
76 OUString 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() )
87 return 0;
88 return rStr.copy( m_nPrefixLength ).toInt32();
90 private:
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
96 name.
98 @param rPrefix
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(
108 const Any & rValue,
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 );
159 return aUniqueName;
161 else
162 // element found => return name
163 return *aIt;
165 catch( const uno::Exception & )
167 DBG_UNHANDLED_EXCEPTION("chart2");
170 return rPreferredName;
173 } // anonymous namespace
175 namespace chart::PropertyHelper
178 OUString addLineDashUniqueNameToTable(
179 const Any & rValue,
180 const Reference< lang::XMultiServiceFactory > & xFact,
181 const OUString & rPreferredName )
183 if( xFact.is())
185 Reference< container::XNameContainer > xNameCnt(
186 xFact->createInstance( "com.sun.star.drawing.DashTable"),
187 uno::UNO_QUERY );
188 if( xNameCnt.is())
189 return lcl_addNamedPropertyUniqueNameToTable(
190 rValue, xNameCnt, "ChartDash ", rPreferredName );
192 return OUString();
195 OUString addGradientUniqueNameToTable(
196 const Any & rValue,
197 const Reference< lang::XMultiServiceFactory > & xFact,
198 const OUString & rPreferredName )
200 if( xFact.is())
202 Reference< container::XNameContainer > xNameCnt(
203 xFact->createInstance( "com.sun.star.drawing.GradientTable"),
204 uno::UNO_QUERY );
205 if( xNameCnt.is())
206 return lcl_addNamedPropertyUniqueNameToTable(
207 rValue, xNameCnt, "ChartGradient ", rPreferredName );
209 return OUString();
212 OUString addTransparencyGradientUniqueNameToTable(
213 const Any & rValue,
214 const Reference< lang::XMultiServiceFactory > & xFact,
215 const OUString & rPreferredName )
217 if( xFact.is())
219 Reference< container::XNameContainer > xNameCnt(
220 xFact->createInstance( "com.sun.star.drawing.TransparencyGradientTable"),
221 uno::UNO_QUERY );
222 if( xNameCnt.is())
223 return lcl_addNamedPropertyUniqueNameToTable(
224 rValue, xNameCnt, "ChartTransparencyGradient ", rPreferredName );
226 return OUString();
229 OUString addHatchUniqueNameToTable(
230 const Any & rValue,
231 const Reference< lang::XMultiServiceFactory > & xFact,
232 const OUString & rPreferredName )
234 if( xFact.is())
236 Reference< container::XNameContainer > xNameCnt(
237 xFact->createInstance( "com.sun.star.drawing.HatchTable"),
238 uno::UNO_QUERY );
239 if( xNameCnt.is())
240 return lcl_addNamedPropertyUniqueNameToTable(
241 rValue, xNameCnt, "ChartHatch ", rPreferredName );
243 return OUString();
246 OUString addBitmapUniqueNameToTable(
247 const Any & rValue,
248 const Reference< lang::XMultiServiceFactory > & xFact,
249 const OUString & rPreferredName )
251 if( xFact.is())
253 Reference< container::XNameContainer > xNameCnt(
254 xFact->createInstance( "com.sun.star.drawing.BitmapTable"),
255 uno::UNO_QUERY );
256 if( xNameCnt.is())
257 return lcl_addNamedPropertyUniqueNameToTable(
258 rValue, xNameCnt, "ChartBitmap ", rPreferredName );
260 return OUString();
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 );
268 else
269 (*aIt).second = rAny;
272 template<>
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 );
284 template<>
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: */