/src/libreoffice/chart2/source/tools/PropertyHelper.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <PropertyHelper.hxx> |
21 | | #include <com/sun/star/container/XNameContainer.hpp> |
22 | | #include <com/sun/star/lang/XMultiServiceFactory.hpp> |
23 | | #include <docmodel/uno/UnoGradientTools.hxx> |
24 | | #include <comphelper/sequence.hxx> |
25 | | #include <osl/diagnose.h> |
26 | | #include <comphelper/diagnose_ex.hxx> |
27 | | #include <o3tl/string_view.hxx> |
28 | | |
29 | | #include <utility> |
30 | | #include <vector> |
31 | | #include <algorithm> |
32 | | #include <iterator> |
33 | | |
34 | | using namespace ::com::sun::star; |
35 | | using namespace ::com::sun::star::beans; |
36 | | using ::com::sun::star::uno::Any; |
37 | | using ::com::sun::star::uno::Reference; |
38 | | |
39 | | namespace |
40 | | { |
41 | | struct lcl_EqualsElement |
42 | | { |
43 | | explicit lcl_EqualsElement( Any rValue, const Reference< container::XNameAccess > & xAccess ) |
44 | 0 | : m_aValue(std::move( rValue )), m_xAccess( xAccess ) |
45 | 0 | { |
46 | 0 | OSL_ASSERT( m_xAccess.is()); |
47 | 0 | } |
48 | | |
49 | | bool operator() ( const OUString & rName ) |
50 | 0 | { |
51 | 0 | try |
52 | 0 | { |
53 | 0 | return (m_xAccess->getByName( rName ) == m_aValue); |
54 | 0 | } |
55 | 0 | catch( const uno::Exception & ) |
56 | 0 | { |
57 | 0 | DBG_UNHANDLED_EXCEPTION("chart2"); |
58 | 0 | } |
59 | 0 | return false; |
60 | 0 | } |
61 | | |
62 | | private: |
63 | | Any m_aValue; |
64 | | Reference< container::XNameAccess > m_xAccess; |
65 | | }; |
66 | | |
67 | | struct lcl_StringMatches |
68 | | { |
69 | | explicit lcl_StringMatches( OUString aCmpStr ) : |
70 | 0 | m_aCmpStr(std::move( aCmpStr )) |
71 | 0 | {} |
72 | | |
73 | | bool operator() ( std::u16string_view rStr ) |
74 | 0 | { |
75 | 0 | return o3tl::starts_with( rStr, m_aCmpStr ); |
76 | 0 | } |
77 | | |
78 | | private: |
79 | | OUString m_aCmpStr; |
80 | | }; |
81 | | |
82 | | struct lcl_OUStringRestToInt32 |
83 | | { |
84 | | explicit lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) : |
85 | 0 | m_nPrefixLength( nPrefixLength ) |
86 | 0 | {} |
87 | | sal_Int32 operator() ( std::u16string_view rStr ) |
88 | 0 | { |
89 | 0 | if( m_nPrefixLength > static_cast<sal_Int32>(rStr.size()) ) |
90 | 0 | return 0; |
91 | 0 | return o3tl::toInt32(rStr.substr( m_nPrefixLength )); |
92 | 0 | } |
93 | | private: |
94 | | sal_Int32 m_nPrefixLength; |
95 | | }; |
96 | | |
97 | | /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient, |
98 | | line dash or line marker to the corresponding name container with a unique |
99 | | name. |
100 | | |
101 | | @param rPrefix |
102 | | The prefix used for automated name generation. |
103 | | |
104 | | @param rPreferredName |
105 | | If this string is not empty it is used as name if it is unique in the |
106 | | table. Otherwise a new name is generated using pPrefix. |
107 | | |
108 | | @return the new name under which the property was stored in the table |
109 | | */ |
110 | | OUString lcl_addNamedPropertyUniqueNameToTable( |
111 | | const Any & rValue, |
112 | | const Reference< container::XNameContainer > & xNameContainer, |
113 | | const OUString & rPrefix, |
114 | | const OUString & rPreferredName ) |
115 | 0 | { |
116 | 0 | if( ! xNameContainer.is() || |
117 | 0 | ! rValue.hasValue() ) |
118 | 0 | return rPreferredName; |
119 | | |
120 | 0 | Any aValue(rValue); |
121 | |
|
122 | 0 | if ( rValue.has<css::awt::Gradient>()) |
123 | 0 | { |
124 | | // tdf#158421 the lists for Gradients needs awt::Gradient2 |
125 | | // as type, convert input data if needed (and warn about it, |
126 | | // the caller should be changed to offer the needed type) |
127 | 0 | SAL_WARN("chart2","input value needs to be awt::Gradient2"); |
128 | 0 | const basegfx::BGradient aTemp(model::gradient::getFromAny(rValue)); |
129 | 0 | aValue <<= model::gradient::createUnoGradient2(aTemp); |
130 | 0 | } |
131 | | |
132 | 0 | if ( aValue.getValueType() != xNameContainer->getElementType()) |
133 | 0 | return rPreferredName; |
134 | | |
135 | 0 | try |
136 | 0 | { |
137 | 0 | Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW ); |
138 | 0 | const uno::Sequence<OUString> aElementNames = xNameAccess->getElementNames(); |
139 | 0 | auto it = std::find_if( aElementNames.begin(), aElementNames.end(), lcl_EqualsElement( aValue, xNameAccess )); |
140 | | |
141 | | // element found => return name |
142 | 0 | if( it != aElementNames.end()) |
143 | 0 | return *it; |
144 | | |
145 | | // element not found in container |
146 | 0 | OUString aUniqueName; |
147 | | |
148 | | // check if preferred name is already used |
149 | 0 | if( !rPreferredName.isEmpty()) |
150 | 0 | { |
151 | 0 | auto aIt = std::find( aElementNames.begin(), aElementNames.end(), rPreferredName ); |
152 | 0 | if( aIt == aElementNames.end()) |
153 | 0 | aUniqueName = rPreferredName; |
154 | 0 | } |
155 | |
|
156 | 0 | if( aUniqueName.isEmpty()) |
157 | 0 | { |
158 | 0 | auto aNames( comphelper::sequenceToContainer<std::vector< OUString >>( aElementNames )); |
159 | | // create a unique id using the prefix plus a number |
160 | 0 | std::vector< sal_Int32 > aNumbers; |
161 | 0 | std::vector< OUString >::iterator aNonConstIt( |
162 | 0 | std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix ))); |
163 | 0 | std::transform( aNames.begin(), aNonConstIt, |
164 | 0 | back_inserter( aNumbers ), |
165 | 0 | lcl_OUStringRestToInt32( rPrefix.getLength() )); |
166 | 0 | std::vector< sal_Int32 >::const_iterator aMaxIt( |
167 | 0 | std::max_element( aNumbers.begin(), aNumbers.end())); |
168 | |
|
169 | 0 | sal_Int32 nIndex = 1; |
170 | 0 | if( aMaxIt != aNumbers.end()) |
171 | 0 | nIndex = (*aMaxIt) + 1; |
172 | |
|
173 | 0 | aUniqueName = rPrefix + OUString::number( nIndex ); |
174 | 0 | } |
175 | |
|
176 | 0 | OSL_ASSERT( !aUniqueName.isEmpty()); |
177 | 0 | xNameContainer->insertByName( aUniqueName, aValue ); |
178 | 0 | return aUniqueName; |
179 | 0 | } |
180 | 0 | catch( const uno::Exception & ) |
181 | 0 | { |
182 | 0 | DBG_UNHANDLED_EXCEPTION("chart2"); |
183 | 0 | } |
184 | | |
185 | 0 | return rPreferredName; |
186 | 0 | } |
187 | | |
188 | | } // anonymous namespace |
189 | | |
190 | | namespace chart::PropertyHelper |
191 | | { |
192 | | |
193 | | OUString addLineDashUniqueNameToTable( |
194 | | const Any & rValue, |
195 | | const Reference< lang::XMultiServiceFactory > & xFact, |
196 | | const OUString & rPreferredName ) |
197 | 0 | { |
198 | 0 | if( xFact.is()) |
199 | 0 | { |
200 | 0 | Reference< container::XNameContainer > xNameCnt( |
201 | 0 | xFact->createInstance( u"com.sun.star.drawing.DashTable"_ustr), |
202 | 0 | uno::UNO_QUERY ); |
203 | 0 | if( xNameCnt.is()) |
204 | 0 | return lcl_addNamedPropertyUniqueNameToTable( |
205 | 0 | rValue, xNameCnt, u"ChartDash "_ustr, rPreferredName ); |
206 | 0 | } |
207 | 0 | return OUString(); |
208 | 0 | } |
209 | | |
210 | | OUString addGradientUniqueNameToTable( |
211 | | const Any & rValue, |
212 | | const Reference< lang::XMultiServiceFactory > & xFact, |
213 | | const OUString & rPreferredName ) |
214 | 0 | { |
215 | 0 | if( xFact.is()) |
216 | 0 | { |
217 | 0 | Reference< container::XNameContainer > xNameCnt( |
218 | 0 | xFact->createInstance( u"com.sun.star.drawing.GradientTable"_ustr), |
219 | 0 | uno::UNO_QUERY ); |
220 | 0 | if( xNameCnt.is()) |
221 | 0 | return lcl_addNamedPropertyUniqueNameToTable( |
222 | 0 | rValue, xNameCnt, u"ChartGradient "_ustr, rPreferredName ); |
223 | 0 | } |
224 | 0 | return OUString(); |
225 | 0 | } |
226 | | |
227 | | OUString addTransparencyGradientUniqueNameToTable( |
228 | | const Any & rValue, |
229 | | const Reference< lang::XMultiServiceFactory > & xFact, |
230 | | const OUString & rPreferredName ) |
231 | 0 | { |
232 | 0 | if( xFact.is()) |
233 | 0 | { |
234 | 0 | Reference< container::XNameContainer > xNameCnt( |
235 | 0 | xFact->createInstance( u"com.sun.star.drawing.TransparencyGradientTable"_ustr), |
236 | 0 | uno::UNO_QUERY ); |
237 | 0 | if( xNameCnt.is()) |
238 | 0 | return lcl_addNamedPropertyUniqueNameToTable( |
239 | 0 | rValue, xNameCnt, u"ChartTransparencyGradient "_ustr, rPreferredName ); |
240 | 0 | } |
241 | 0 | return OUString(); |
242 | 0 | } |
243 | | |
244 | | OUString addHatchUniqueNameToTable( |
245 | | const Any & rValue, |
246 | | const Reference< lang::XMultiServiceFactory > & xFact, |
247 | | const OUString & rPreferredName ) |
248 | 0 | { |
249 | 0 | if( xFact.is()) |
250 | 0 | { |
251 | 0 | Reference< container::XNameContainer > xNameCnt( |
252 | 0 | xFact->createInstance( u"com.sun.star.drawing.HatchTable"_ustr), |
253 | 0 | uno::UNO_QUERY ); |
254 | 0 | if( xNameCnt.is()) |
255 | 0 | return lcl_addNamedPropertyUniqueNameToTable( |
256 | 0 | rValue, xNameCnt, u"ChartHatch "_ustr, rPreferredName ); |
257 | 0 | } |
258 | 0 | return OUString(); |
259 | 0 | } |
260 | | |
261 | | OUString addBitmapUniqueNameToTable( |
262 | | const Any & rValue, |
263 | | const Reference< lang::XMultiServiceFactory > & xFact, |
264 | | const OUString & rPreferredName ) |
265 | 0 | { |
266 | 0 | if( xFact.is()) |
267 | 0 | { |
268 | 0 | Reference< container::XNameContainer > xNameCnt( |
269 | 0 | xFact->createInstance( u"com.sun.star.drawing.BitmapTable"_ustr), |
270 | 0 | uno::UNO_QUERY ); |
271 | 0 | if( xNameCnt.is()) |
272 | 0 | return lcl_addNamedPropertyUniqueNameToTable( |
273 | 0 | rValue, xNameCnt, u"ChartBitmap "_ustr, rPreferredName ); |
274 | 0 | } |
275 | 0 | return OUString(); |
276 | 0 | } |
277 | | |
278 | | void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny ) |
279 | 0 | { |
280 | 0 | tPropertyValueMap::iterator aIt( rOutMap.find( key )); |
281 | 0 | if( aIt == rOutMap.end()) |
282 | 0 | rOutMap.emplace( key, rAny ); |
283 | 0 | else |
284 | 0 | (*aIt).second = rAny; |
285 | 0 | } |
286 | | |
287 | | template<> |
288 | | void setPropertyValue< css::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const css::uno::Any & rAny ) |
289 | 0 | { |
290 | 0 | setPropertyValueAny( rOutMap, key, rAny ); |
291 | 0 | } |
292 | | |
293 | | void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny ) |
294 | 0 | { |
295 | 0 | OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" ); |
296 | 0 | setPropertyValue( rOutMap, key, rAny ); |
297 | 0 | } |
298 | | |
299 | | template<> |
300 | | void setPropertyValueDefault< css::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const css::uno::Any & rAny ) |
301 | 0 | { |
302 | 0 | setPropertyValueDefaultAny( rOutMap, key, rAny ); |
303 | 0 | } |
304 | | |
305 | | void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key ) |
306 | 0 | { |
307 | 0 | setPropertyValueDefault( rOutMap, key, uno::Any()); |
308 | 0 | } |
309 | | |
310 | | } // namespace chart::PropertyHelper |
311 | | |
312 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |