Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / view / main / PropertyMapper.cxx
blobf96c7b101509f9850bf39b5b967275b06830933e
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 // fix the spelling!
345 return map;
348 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForTextLabelProperties()
350 // target name (drawing layer) : source name (chart model)
351 static tPropertyNameMap aMap = getPropertyNameMapForTextLabelProperties_();
352 return aMap;
355 const tPropertyNameMap& PropertyMapper::getPropertyNameMapForFilledSeriesProperties()
357 //shape property -- chart model object property
358 static tPropertyNameMap s_aShapePropertyMapForFilledSeriesProperties{
359 {"FillBackground", "FillBackground"},
360 {"FillBitmapName", "FillBitmapName"},
361 {"FillColor", "Color"},
362 {"FillGradientName", "GradientName"},
363 {"FillGradientStepCount", "GradientStepCount"},
364 {"FillHatchName", "HatchName"},
365 {"FillStyle", "FillStyle"},
366 {"FillTransparence", "Transparency"},
367 {"FillTransparenceGradientName", "TransparencyGradientName"},
368 //bitmap properties
369 {"FillBitmapMode", "FillBitmapMode"},
370 {"FillBitmapSizeX", "FillBitmapSizeX"},
371 {"FillBitmapSizeY", "FillBitmapSizeY"},
372 {"FillBitmapLogicalSize", "FillBitmapLogicalSize"},
373 {"FillBitmapOffsetX", "FillBitmapOffsetX"},
374 {"FillBitmapOffsetY", "FillBitmapOffsetY"},
375 {"FillBitmapRectanglePoint", "FillBitmapRectanglePoint"},
376 {"FillBitmapPositionOffsetX", "FillBitmapPositionOffsetX"},
377 {"FillBitmapPositionOffsetY", "FillBitmapPositionOffsetY"},
378 //line properties
379 {"LineColor", "BorderColor"},
380 {"LineDashName", "BorderDashName"},
381 {"LineStyle", "BorderStyle"},
382 {"LineTransparence", "BorderTransparency"},
383 {"LineWidth", "BorderWidth"}};
384 return s_aShapePropertyMapForFilledSeriesProperties;
387 void PropertyMapper::setMultiProperties(
388 const tNameSequence& rNames
389 , const tAnySequence& rValues
390 , const css::uno::Reference<
391 css::beans::XPropertySet >& xTarget )
393 bool bSuccess = false;
396 uno::Reference< beans::XMultiPropertySet > xShapeMultiProp( xTarget, uno::UNO_QUERY );
397 if( xShapeMultiProp.is() )
399 xShapeMultiProp->setPropertyValues( rNames, rValues );
400 bSuccess = true;
403 catch( const uno::Exception& )
405 TOOLS_WARN_EXCEPTION("chart2", "" ); //if this occurs more often think of removing the XMultiPropertySet completely for better performance
408 if(bSuccess)
409 return;
413 sal_Int32 nCount = std::max( rNames.getLength(), rValues.getLength() );
414 OUString aPropName;
415 uno::Any aValue;
416 for( sal_Int32 nN = 0; nN < nCount; nN++ )
418 aPropName = rNames[nN];
419 aValue = rValues[nN];
423 xTarget->setPropertyValue( aPropName, aValue );
425 catch( const uno::Exception& )
427 TOOLS_WARN_EXCEPTION("chart2", "" );
431 catch( const uno::Exception& )
433 TOOLS_WARN_EXCEPTION("chart2", "" );
437 void PropertyMapper::getTextLabelMultiPropertyLists(
438 const uno::Reference< beans::XPropertySet >& xSourceProp
439 , tNameSequence& rPropNames, tAnySequence& rPropValues
440 , bool bName
441 , sal_Int32 nLimitedSpace
442 , bool bLimitedHeight
443 , bool bSupportsLabelBorder)
445 //fill character properties into the ValueMap
446 tPropertyNameValueMap aValueMap;
447 tPropertyNameMap const & aNameMap = bSupportsLabelBorder ? PropertyMapper::getPropertyNameMapForTextLabelProperties() : getPropertyNameMapForCharacterProperties();
449 PropertyMapper::getValueMap(aValueMap, aNameMap, xSourceProp);
451 //some more shape properties apart from character properties, position-matrix and label string
452 aValueMap.emplace( "TextHorizontalAdjust", uno::Any(drawing::TextHorizontalAdjust_CENTER) ); // drawing::TextHorizontalAdjust - needs to be overwritten
453 aValueMap.emplace( "TextVerticalAdjust", uno::Any(drawing::TextVerticalAdjust_CENTER) ); //drawing::TextVerticalAdjust - needs to be overwritten
454 aValueMap.emplace( "TextAutoGrowHeight", uno::Any(true) ); // sal_Bool
455 aValueMap.emplace( "TextAutoGrowWidth", uno::Any(true) ); // sal_Bool
456 if( bName )
457 aValueMap.emplace( "Name", uno::Any( OUString() ) ); //CID OUString - needs to be overwritten for each point
459 if( nLimitedSpace > 0 )
461 if(bLimitedHeight)
462 aValueMap.emplace( "TextMaximumFrameHeight", uno::Any(nLimitedSpace) ); //sal_Int32
463 else
464 aValueMap.emplace( "TextMaximumFrameWidth", uno::Any(nLimitedSpace) ); //sal_Int32
465 aValueMap.emplace( "ParaIsHyphenation", uno::Any(true) );
468 PropertyMapper::getMultiPropertyListsFromValueMap( rPropNames, rPropValues, aValueMap );
471 void PropertyMapper::getPreparedTextShapePropertyLists(
472 const uno::Reference< beans::XPropertySet >& xSourceProp
473 , tNameSequence& rPropNames, tAnySequence& rPropValues )
475 //fill character, line and fill properties into the ValueMap
476 tPropertyNameValueMap aValueMap;
477 PropertyMapper::getValueMap( aValueMap
478 , PropertyMapper::getPropertyNameMapForTextShapeProperties()
479 , xSourceProp );
481 // auto-grow makes sure the shape has the correct size after setting text
482 aValueMap.emplace( "TextHorizontalAdjust", uno::Any( drawing::TextHorizontalAdjust_CENTER ));
483 aValueMap.emplace( "TextVerticalAdjust", uno::Any( drawing::TextVerticalAdjust_CENTER ));
484 aValueMap.emplace( "TextAutoGrowHeight", uno::Any( true ));
485 aValueMap.emplace( "TextAutoGrowWidth", uno::Any( true ));
487 // set some distance to the border, in case it is shown
488 const sal_Int32 nWidthDist = 250;
489 const sal_Int32 nHeightDist = 125;
490 aValueMap.emplace( "TextLeftDistance", uno::Any( nWidthDist ));
491 aValueMap.emplace( "TextRightDistance", uno::Any( nWidthDist ));
492 aValueMap.emplace( "TextUpperDistance", uno::Any( nHeightDist ));
493 aValueMap.emplace( "TextLowerDistance", uno::Any( nHeightDist ));
495 // use a line-joint showing the border of thick lines like two rectangles
496 // filled in between.
497 aValueMap["LineJoint"] <<= drawing::LineJoint_ROUND;
499 PropertyMapper::getMultiPropertyListsFromValueMap( rPropNames, rPropValues, aValueMap );
502 } //namespace chart
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */