Bump version to 6.4-15
[LibreOffice.git] / chart2 / source / view / main / PropertyMapper.cxx
blob45275ed13b2ed13deb491ffe02d800c5abfa665f
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 <PropertyMapper.hxx>
21 #include <unonames.hxx>
22 #include <sal/log.hxx>
24 #include <com/sun/star/beans/XMultiPropertySet.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/drawing/TextVerticalAdjust.hpp>
27 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
28 #include <com/sun/star/drawing/LineJoint.hpp>
29 #include <comphelper/sequence.hxx>
30 #include <tools/diagnose_ex.h>
32 namespace chart
34 using namespace ::com::sun::star;
36 namespace
39 void lcl_overwriteOrAppendValues(
40 tPropertyNameValueMap &rMap, const tPropertyNameValueMap& rOverwriteMap )
42 for (auto const& elem : rOverwriteMap)
43 rMap[ elem.first ] = elem.second;
46 } // anonymous namespace
48 void PropertyMapper::setMappedProperties(
49 const uno::Reference< beans::XPropertySet >& xTarget
50 , const uno::Reference< beans::XPropertySet >& xSource
51 , const tPropertyNameMap& rMap
52 , tPropertyNameValueMap const * pOverwriteMap )
54 if( !xTarget.is() || !xSource.is() )
55 return;
57 tNameSequence aNames;
58 tAnySequence aValues;
59 getMultiPropertyLists(aNames, aValues, xSource, rMap );
60 if(pOverwriteMap && (aNames.getLength() == aValues.getLength()))
62 tPropertyNameValueMap aNewMap;
63 for( sal_Int32 nI=0; nI<aNames.getLength(); ++nI )
64 aNewMap[ aNames[nI] ] = aValues[nI];
65 lcl_overwriteOrAppendValues( aNewMap, *pOverwriteMap );
66 aNames = comphelper::mapKeysToSequence( aNewMap );
67 aValues = comphelper::mapValuesToSequence( aNewMap );
70 PropertyMapper::setMultiProperties( aNames, aValues, xTarget );
73 void PropertyMapper::getValueMap(
74 tPropertyNameValueMap& rValueMap
75 , const tPropertyNameMap& rNameMap
76 , const uno::Reference< beans::XPropertySet >& xSourceProp
79 uno::Reference< beans::XMultiPropertySet > xMultiPropSet(xSourceProp, uno::UNO_QUERY);
80 if((false) && xMultiPropSet.is())
82 uno::Sequence< OUString > aPropSourceNames(rNameMap.size());
83 uno::Sequence< OUString > aPropTargetNames(rNameMap.size());
84 sal_Int32 i = 0;
85 for (auto const& elem : rNameMap)
87 aPropTargetNames[i] = elem.first;
88 aPropSourceNames[i] = elem.second;
89 ++i;
92 uno::Sequence< uno::Any > xValues = xMultiPropSet->getPropertyValues(aPropSourceNames);
93 sal_Int32 n = rNameMap.size();
94 for(i = 0;i < n; ++i)
96 if( xValues[i].hasValue() )
97 rValueMap.emplace( aPropTargetNames[i], xValues[i] );
100 else
102 for (auto const& elem : rNameMap)
104 OUString aTarget = elem.first;
105 OUString aSource = elem.second;
108 uno::Any aAny( xSourceProp->getPropertyValue(aSource) );
109 if( aAny.hasValue() )
110 rValueMap.emplace( aTarget, aAny );
112 catch( const uno::Exception& )
114 TOOLS_WARN_EXCEPTION("chart2", "" );
120 void PropertyMapper::getMultiPropertyLists(
121 tNameSequence& rNames
122 , tAnySequence& rValues
123 , const uno::Reference< beans::XPropertySet >& xSourceProp
124 , const tPropertyNameMap& rNameMap
127 tPropertyNameValueMap aValueMap;
128 getValueMap( aValueMap, rNameMap, xSourceProp );
129 getMultiPropertyListsFromValueMap( rNames, rValues, aValueMap );
132 void PropertyMapper::getMultiPropertyListsFromValueMap(
133 tNameSequence& rNames
134 , tAnySequence& rValues
135 , const tPropertyNameValueMap& rValueMap
138 sal_Int32 nPropertyCount = rValueMap.size();
139 rNames.realloc(nPropertyCount);
140 rValues.realloc(nPropertyCount);
142 //fill sequences
143 sal_Int32 nN=0;
144 for (auto const& elem : rValueMap)
146 const uno::Any& rAny = elem.second;
147 if( rAny.hasValue() )
149 //do not set empty anys because of performance (otherwise SdrAttrObj::ItemChange will take much longer)
150 rNames[nN] = elem.first;
151 rValues[nN] = rAny;
152 ++nN;
155 //reduce to real property count
156 rNames.realloc(nN);
157 rValues.realloc(nN);
160 uno::Any* PropertyMapper::getValuePointer( tAnySequence& rPropValues
161 , const tNameSequence& rPropNames
162 , const OUString& rPropName )
164 sal_Int32 nCount = rPropNames.getLength();
165 for( sal_Int32 nN = 0; nN < nCount; nN++ )
167 if(rPropNames[nN] == rPropName)
168 return &rPropValues[nN];
170 return nullptr;
173 uno::Any* PropertyMapper::getValuePointerForLimitedSpace( tAnySequence& rPropValues
174 , const tNameSequence& rPropNames
175 , bool bLimitedHeight)
177 return PropertyMapper::getValuePointer( rPropValues, rPropNames
178 , bLimitedHeight ? OUString("TextMaximumFrameHeight") : OUString("TextMaximumFrameWidth") );
181 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForCharacterProperties()
183 //shape property -- chart model object property
184 static tPropertyNameMap s_aShapePropertyMapForCharacterProperties{
185 {"CharColor", "CharColor"},
186 {"CharContoured", "CharContoured"},
187 {"CharEmphasis", "CharEmphasis"},//the service style::CharacterProperties describes a property called 'CharEmphasize' which is nowhere implemented
189 {"CharFontFamily", "CharFontFamily"},
190 {"CharFontFamilyAsian", "CharFontFamilyAsian"},
191 {"CharFontFamilyComplex", "CharFontFamilyComplex"},
192 {"CharFontCharSet", "CharFontCharSet"},
193 {"CharFontCharSetAsian", "CharFontCharSetAsian"},
194 {"CharFontCharSetComplex", "CharFontCharSetComplex"},
195 {"CharFontName", "CharFontName"},
196 {"CharFontNameAsian", "CharFontNameAsian"},
197 {"CharFontNameComplex", "CharFontNameComplex"},
198 {"CharFontPitch", "CharFontPitch"},
199 {"CharFontPitchAsian", "CharFontPitchAsian"},
200 {"CharFontPitchComplex", "CharFontPitchComplex"},
201 {"CharFontStyleName", "CharFontStyleName"},
202 {"CharFontStyleNameAsian", "CharFontStyleNameAsian"},
203 {"CharFontStyleNameComplex", "CharFontStyleNameComplex"},
205 {"CharHeight", "CharHeight"},
206 {"CharHeightAsian", "CharHeightAsian"},
207 {"CharHeightComplex", "CharHeightComplex"},
208 {"CharKerning", "CharKerning"},
209 {"CharLocale", "CharLocale"},
210 {"CharLocaleAsian", "CharLocaleAsian"},
211 {"CharLocaleComplex", "CharLocaleComplex"},
212 {"CharPosture", "CharPosture"},
213 {"CharPostureAsian", "CharPostureAsian"},
214 {"CharPostureComplex", "CharPostureComplex"},
215 {"CharRelief", "CharRelief"},
216 {"CharShadowed", "CharShadowed"},
217 {"CharStrikeout", "CharStrikeout"},
218 {"CharUnderline", "CharUnderline"},
219 {"CharUnderlineColor", "CharUnderlineColor"},
220 {"CharUnderlineHasColor", "CharUnderlineHasColor"},
221 {"CharOverline", "CharOverline"},
222 {"CharOverlineColor", "CharOverlineColor"},
223 {"CharOverlineHasColor", "CharOverlineHasColor"},
224 {"CharWeight", "CharWeight"},
225 {"CharWeightAsian", "CharWeightAsian"},
226 {"CharWeightComplex", "CharWeightComplex"},
227 {"CharWordMode", "CharWordMode"},
229 {"WritingMode", "WritingMode"},
231 {"ParaIsCharacterDistance", "ParaIsCharacterDistance"}};
233 return s_aShapePropertyMapForCharacterProperties;
236 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForParagraphProperties()
238 //shape property -- chart model object property
239 static tPropertyNameMap s_aShapePropertyMapForParagraphProperties{
240 {"ParaAdjust", "ParaAdjust"},
241 {"ParaBottomMargin", "ParaBottomMargin"},
242 {"ParaIsHyphenation", "ParaIsHyphenation"},
243 {"ParaLastLineAdjust", "ParaLastLineAdjust"},
244 {"ParaLeftMargin", "ParaLeftMargin"},
245 {"ParaRightMargin", "ParaRightMargin"},
246 {"ParaTopMargin", "ParaTopMargin"}};
247 return s_aShapePropertyMapForParagraphProperties;
250 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForFillProperties()
252 //shape property -- chart model object property
253 static tPropertyNameMap s_aShapePropertyMapForFillProperties{
254 {"FillBackground", "FillBackground"},
255 {"FillBitmapName", "FillBitmapName"},
256 {"FillColor", "FillColor"},
257 {"FillGradientName", "FillGradientName"},
258 {"FillGradientStepCount", "FillGradientStepCount"},
259 {"FillHatchName", "FillHatchName"},
260 {"FillStyle", "FillStyle"},
261 {"FillTransparence", "FillTransparence"},
262 {"FillTransparenceGradientName", "FillTransparenceGradientName"},
263 //bitmap properties
264 {"FillBitmapMode", "FillBitmapMode"},
265 {"FillBitmapSizeX", "FillBitmapSizeX"},
266 {"FillBitmapSizeY", "FillBitmapSizeY"},
267 {"FillBitmapLogicalSize", "FillBitmapLogicalSize"},
268 {"FillBitmapOffsetX", "FillBitmapOffsetX"},
269 {"FillBitmapOffsetY", "FillBitmapOffsetY"},
270 {"FillBitmapRectanglePoint", "FillBitmapRectanglePoint"},
271 {"FillBitmapPositionOffsetX", "FillBitmapPositionOffsetX"},
272 {"FillBitmapPositionOffsetY", "FillBitmapPositionOffsetY"}};
273 return s_aShapePropertyMapForFillProperties;
276 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForLineProperties()
278 //shape property -- chart model object property
279 static tPropertyNameMap s_aShapePropertyMapForLineProperties{
280 {"LineColor", "LineColor"},
281 {"LineDashName", "LineDashName"},
282 {"LineJoint", "LineJoint"},
283 {"LineStyle", "LineStyle"},
284 {"LineTransparence", "LineTransparence"},
285 {"LineWidth", "LineWidth"}};
286 return s_aShapePropertyMapForLineProperties;
289 namespace {
290 tPropertyNameMap getPropertyNameMapForFillAndLineProperties_() {
291 auto map = PropertyMapper::getPropertyNameMapForFillProperties();
292 auto const & add
293 = PropertyMapper::getPropertyNameMapForLineProperties();
294 map.insert(add.begin(), add.end());
295 return map;
298 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForFillAndLineProperties()
300 static tPropertyNameMap s_aShapePropertyMapForFillAndLineProperties
301 = getPropertyNameMapForFillAndLineProperties_();
302 return s_aShapePropertyMapForFillAndLineProperties;
305 namespace {
306 tPropertyNameMap getPropertyNameMapForTextShapeProperties_() {
307 auto map = PropertyMapper::getPropertyNameMapForCharacterProperties();
308 auto const & add1
309 = PropertyMapper::getPropertyNameMapForFillProperties();
310 map.insert(add1.begin(), add1.end());
311 auto const & add2
312 = PropertyMapper::getPropertyNameMapForLineProperties();
313 map.insert(add2.begin(), add2.end());
314 return map;
317 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForTextShapeProperties()
319 static tPropertyNameMap s_aShapePropertyMapForTextShapeProperties
320 = getPropertyNameMapForTextShapeProperties_();
321 return s_aShapePropertyMapForTextShapeProperties;
324 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForLineSeriesProperties()
326 //shape property -- chart model object property
327 static tPropertyNameMap s_aShapePropertyMapForLineSeriesProperties{
328 {"LineColor", "Color"},
329 {"LineDashName", "LineDashName"},
330 {"LineStyle", "LineStyle"},
331 {"LineTransparence", "Transparency"},
332 {"LineWidth", "LineWidth"}};
333 return s_aShapePropertyMapForLineSeriesProperties;
336 namespace {
337 tPropertyNameMap getPropertyNameMapForTextLabelProperties_() {
338 auto map = PropertyMapper::getPropertyNameMapForCharacterProperties();
339 map.insert({
340 {"LineStyle", CHART_UNONAME_LABEL_BORDER_STYLE},
341 {"LineWidth", CHART_UNONAME_LABEL_BORDER_WIDTH},
342 {"LineColor", CHART_UNONAME_LABEL_BORDER_COLOR},
343 {"LineTransparence", CHART_UNONAME_LABEL_BORDER_TRANS},
344 {"FillStyle", CHART_UNONAME_LABEL_FILL_STYLE},
345 {"FillColor", CHART_UNONAME_LABEL_FILL_COLOR},
346 {"FillBackground", CHART_UNONAME_LABEL_FILL_BACKGROUND},
347 {"FillHatchName", CHART_UNONAME_LABEL_FILL_HATCH_NAME}
349 // fix the spelling!
350 return map;
353 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForTextLabelProperties()
355 // target name (drawing layer) : source name (chart model)
356 static tPropertyNameMap aMap = getPropertyNameMapForTextLabelProperties_();
357 return aMap;
360 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForFilledSeriesProperties()
362 //shape property -- chart model object property
363 static tPropertyNameMap s_aShapePropertyMapForFilledSeriesProperties{
364 {"FillBackground", "FillBackground"},
365 {"FillBitmapName", "FillBitmapName"},
366 {"FillColor", "Color"},
367 {"FillGradientName", "GradientName"},
368 {"FillGradientStepCount", "GradientStepCount"},
369 {"FillHatchName", "HatchName"},
370 {"FillStyle", "FillStyle"},
371 {"FillTransparence", "Transparency"},
372 {"FillTransparenceGradientName", "TransparencyGradientName"},
373 //bitmap properties
374 {"FillBitmapMode", "FillBitmapMode"},
375 {"FillBitmapSizeX", "FillBitmapSizeX"},
376 {"FillBitmapSizeY", "FillBitmapSizeY"},
377 {"FillBitmapLogicalSize", "FillBitmapLogicalSize"},
378 {"FillBitmapOffsetX", "FillBitmapOffsetX"},
379 {"FillBitmapOffsetY", "FillBitmapOffsetY"},
380 {"FillBitmapRectanglePoint", "FillBitmapRectanglePoint"},
381 {"FillBitmapPositionOffsetX", "FillBitmapPositionOffsetX"},
382 {"FillBitmapPositionOffsetY", "FillBitmapPositionOffsetY"},
383 //line properties
384 {"LineColor", "BorderColor"},
385 {"LineDashName", "BorderDashName"},
386 {"LineStyle", "BorderStyle"},
387 {"LineTransparence", "BorderTransparency"},
388 {"LineWidth", "BorderWidth"}};
389 return s_aShapePropertyMapForFilledSeriesProperties;
392 void PropertyMapper::setMultiProperties(
393 const tNameSequence& rNames
394 , const tAnySequence& rValues
395 , const css::uno::Reference<
396 css::beans::XPropertySet >& xTarget )
398 bool bSuccess = false;
401 uno::Reference< beans::XMultiPropertySet > xShapeMultiProp( xTarget, uno::UNO_QUERY );
402 if( xShapeMultiProp.is() )
404 xShapeMultiProp->setPropertyValues( rNames, rValues );
405 bSuccess = true;
408 catch( const uno::Exception& )
410 TOOLS_WARN_EXCEPTION("chart2", "" ); //if this occurs more often think of removing the XMultiPropertySet completely for better performance
413 if(bSuccess)
414 return;
418 sal_Int32 nCount = std::max( rNames.getLength(), rValues.getLength() );
419 OUString aPropName;
420 uno::Any aValue;
421 for( sal_Int32 nN = 0; nN < nCount; nN++ )
423 aPropName = rNames[nN];
424 aValue = rValues[nN];
428 xTarget->setPropertyValue( aPropName, aValue );
430 catch( const uno::Exception& )
432 TOOLS_WARN_EXCEPTION("chart2", "" );
436 catch( const uno::Exception& )
438 TOOLS_WARN_EXCEPTION("chart2", "" );
442 void PropertyMapper::getTextLabelMultiPropertyLists(
443 const uno::Reference< beans::XPropertySet >& xSourceProp
444 , tNameSequence& rPropNames, tAnySequence& rPropValues
445 , bool bName
446 , sal_Int32 nLimitedSpace
447 , bool bLimitedHeight
448 , bool bSupportsLabelBorder)
450 //fill character properties into the ValueMap
451 tPropertyNameValueMap aValueMap;
452 tPropertyNameMap const & aNameMap = bSupportsLabelBorder ? PropertyMapper::getPropertyNameMapForTextLabelProperties() : getPropertyNameMapForCharacterProperties();
454 PropertyMapper::getValueMap(aValueMap, aNameMap, xSourceProp);
456 //some more shape properties apart from character properties, position-matrix and label string
457 aValueMap.emplace( "TextHorizontalAdjust", uno::Any(drawing::TextHorizontalAdjust_CENTER) ); // drawing::TextHorizontalAdjust - needs to be overwritten
458 aValueMap.emplace( "TextVerticalAdjust", uno::Any(drawing::TextVerticalAdjust_CENTER) ); //drawing::TextVerticalAdjust - needs to be overwritten
459 aValueMap.emplace( "TextAutoGrowHeight", uno::Any(true) ); // sal_Bool
460 aValueMap.emplace( "TextAutoGrowWidth", uno::Any(true) ); // sal_Bool
461 if( bName )
462 aValueMap.emplace( "Name", uno::Any( OUString() ) ); //CID OUString - needs to be overwritten for each point
464 if( nLimitedSpace > 0 )
466 if(bLimitedHeight)
467 aValueMap.emplace( "TextMaximumFrameHeight", uno::Any(nLimitedSpace) ); //sal_Int32
468 else
469 aValueMap.emplace( "TextMaximumFrameWidth", uno::Any(nLimitedSpace) ); //sal_Int32
470 aValueMap.emplace( "ParaIsHyphenation", uno::Any(true) );
473 PropertyMapper::getMultiPropertyListsFromValueMap( rPropNames, rPropValues, aValueMap );
476 void PropertyMapper::getPreparedTextShapePropertyLists(
477 const uno::Reference< beans::XPropertySet >& xSourceProp
478 , tNameSequence& rPropNames, tAnySequence& rPropValues )
480 //fill character, line and fill properties into the ValueMap
481 tPropertyNameValueMap aValueMap;
482 PropertyMapper::getValueMap( aValueMap
483 , PropertyMapper::getPropertyNameMapForTextShapeProperties()
484 , xSourceProp );
486 // auto-grow makes sure the shape has the correct size after setting text
487 aValueMap.emplace( "TextHorizontalAdjust", uno::Any( drawing::TextHorizontalAdjust_CENTER ));
488 aValueMap.emplace( "TextVerticalAdjust", uno::Any( drawing::TextVerticalAdjust_CENTER ));
489 aValueMap.emplace( "TextAutoGrowHeight", uno::Any( true ));
490 aValueMap.emplace( "TextAutoGrowWidth", uno::Any( true ));
492 // set some distance to the border, in case it is shown
493 const sal_Int32 nWidthDist = 250;
494 const sal_Int32 nHeightDist = 125;
495 aValueMap.emplace( "TextLeftDistance", uno::Any( nWidthDist ));
496 aValueMap.emplace( "TextRightDistance", uno::Any( nWidthDist ));
497 aValueMap.emplace( "TextUpperDistance", uno::Any( nHeightDist ));
498 aValueMap.emplace( "TextLowerDistance", uno::Any( nHeightDist ));
500 // use a line-joint showing the border of thick lines like two rectangles
501 // filled in between.
502 aValueMap["LineJoint"] <<= drawing::LineJoint_ROUND;
504 PropertyMapper::getMultiPropertyListsFromValueMap( rPropNames, rPropValues, aValueMap );
507 } //namespace chart
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */