Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / view / main / VLegendSymbolFactory.cxx
blobf2f49f22e6d6ea7ea4767ded9072f45b6c7cb21b
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 <VLegendSymbolFactory.hxx>
21 #include <PropertyMapper.hxx>
22 #include <AbstractShapeFactory.hxx>
23 #include <ObjectIdentifier.hxx>
24 #include <com/sun/star/drawing/LineStyle.hpp>
25 #include <com/sun/star/chart2/Symbol.hpp>
26 #include <tools/diagnose_ex.h>
28 using namespace ::com::sun::star;
29 using ::com::sun::star::uno::Reference;
31 namespace
34 void getPropNamesAndValues( const Reference< beans::XPropertySet >& xProp,
35 ::chart::tNameSequence& rNames,
36 ::chart::tAnySequence& rValues,
37 ::chart::VLegendSymbolFactory::PropertyType ePropertyType,
38 const awt::Size& aMaxSymbolExtent)
40 const ::chart::tPropertyNameMap & aFilledSeriesNameMap( ::chart::PropertyMapper::getPropertyNameMapForFilledSeriesProperties());
41 const ::chart::tPropertyNameMap & aLineSeriesNameMap( ::chart::PropertyMapper::getPropertyNameMapForLineSeriesProperties());
42 const ::chart::tPropertyNameMap & aLineNameMap( ::chart::PropertyMapper::getPropertyNameMapForLineProperties());
44 ::chart::tPropertyNameValueMap aValueMap;
45 switch( ePropertyType )
47 case ::chart::VLegendSymbolFactory::PropertyType::FilledSeries:
48 ::chart::PropertyMapper::getValueMap( aValueMap, aFilledSeriesNameMap, xProp );
49 break;
50 case ::chart::VLegendSymbolFactory::PropertyType::LineSeries:
51 ::chart::PropertyMapper::getValueMap( aValueMap, aLineSeriesNameMap, xProp );
52 break;
53 case ::chart::VLegendSymbolFactory::PropertyType::Line:
54 ::chart::PropertyMapper::getValueMap( aValueMap, aLineNameMap, xProp );
55 break;
58 ::chart::PropertyMapper::getMultiPropertyListsFromValueMap( rNames, rValues, aValueMap );
60 uno::Any* pLineWidthAny = ::chart::PropertyMapper::getValuePointer(rValues,rNames,"LineWidth");
61 sal_Int32 nLineWidth = 0;
62 if( pLineWidthAny && (*pLineWidthAny>>=nLineWidth) )
64 // use legend entry height as upper limit for line width
65 sal_Int32 nMaxLineWidthForLegend = aMaxSymbolExtent.Height;
66 if( nLineWidth>nMaxLineWidthForLegend )
67 *pLineWidthAny <<= nMaxLineWidthForLegend;
71 void lcl_setPropertiesToShape(
72 const Reference< beans::XPropertySet > & xProp,
73 const Reference< drawing::XShape > & xShape,
74 ::chart::VLegendSymbolFactory::PropertyType ePropertyType,
75 const awt::Size& aMaxSymbolExtent)
77 ::chart::tNameSequence aPropNames;
78 ::chart::tAnySequence aPropValues;
79 getPropNamesAndValues( xProp, aPropNames, aPropValues,
80 ePropertyType, aMaxSymbolExtent );
82 Reference< beans::XPropertySet > xShapeProp( xShape, uno::UNO_QUERY );
83 ::chart::PropertyMapper::setMultiProperties( aPropNames, aPropValues, xShapeProp );
86 } // anonymous namespace
88 namespace chart
91 Reference< drawing::XShape > VLegendSymbolFactory::createSymbol(
92 const awt::Size& rEntryKeyAspectRatio,
93 const Reference< drawing::XShapes >& rSymbolContainer,
94 LegendSymbolStyle eStyle,
95 const Reference< lang::XMultiServiceFactory > & xShapeFactory,
96 const Reference< beans::XPropertySet > & xLegendEntryProperties,
97 PropertyType ePropertyType, const uno::Any& rExplicitSymbol )
99 Reference< drawing::XShape > xResult;
101 if( ! (rSymbolContainer.is() && xShapeFactory.is()))
102 return xResult;
104 AbstractShapeFactory* pShapeFactory = AbstractShapeFactory::getOrCreateShapeFactory(xShapeFactory);
105 xResult.set( pShapeFactory->createGroup2D( rSymbolContainer ), uno::UNO_QUERY );
107 Reference< drawing::XShapes > xResultGroup( xResult, uno::UNO_QUERY );
108 if( ! xResultGroup.is())
109 return xResult;
111 // add an invisible square box to maintain aspect ratio
112 Reference< drawing::XShape > xBound( pShapeFactory->createInvisibleRectangle(
113 xResultGroup, rEntryKeyAspectRatio ));
115 // create symbol
118 if( eStyle == LegendSymbolStyle_LINE )
120 Reference< drawing::XShape > xLine =
121 pShapeFactory->createLine( xResultGroup, awt::Size( rEntryKeyAspectRatio.Width, 0 ),
122 awt::Point( 0, rEntryKeyAspectRatio.Height/2 ));
123 if( xLine.is())
125 lcl_setPropertiesToShape( xLegendEntryProperties, xLine, ePropertyType, rEntryKeyAspectRatio );
128 Reference< drawing::XShape > xSymbol;
129 const sal_Int32 nSize = std::min(rEntryKeyAspectRatio.Width,rEntryKeyAspectRatio.Height);
130 chart2::Symbol aSymbol;
131 if( rExplicitSymbol >>= aSymbol )
133 drawing::Direction3D aSymbolSize( nSize, nSize, 0 );
134 drawing::Position3D aPos( rEntryKeyAspectRatio.Width/2.0, rEntryKeyAspectRatio.Height/2.0, 0 );
135 AbstractShapeFactory* pFactory = AbstractShapeFactory::getOrCreateShapeFactory( xShapeFactory );
136 if( aSymbol.Style == chart2::SymbolStyle_STANDARD )
138 // take series color as fill color
139 xLegendEntryProperties->getPropertyValue( "Color") >>= aSymbol.FillColor;
140 // border of symbols always same as fill color
141 aSymbol.BorderColor = aSymbol.FillColor;
143 xSymbol.set( pFactory->createSymbol2D(
144 xResultGroup,
145 aPos,
146 aSymbolSize,
147 aSymbol.StandardSymbol,
148 aSymbol.BorderColor,
149 aSymbol.FillColor ));
151 else if( aSymbol.Style == chart2::SymbolStyle_GRAPHIC )
153 xSymbol.set( pFactory->createGraphic2D(
154 xResultGroup,
155 aPos,
156 aSymbolSize,
157 aSymbol.Graphic ));
159 else if( aSymbol.Style == chart2::SymbolStyle_AUTO )
161 SAL_WARN("chart2", "the given parameter is not allowed to contain an automatic symbol style");
165 else if( eStyle == LegendSymbolStyle_CIRCLE )
167 sal_Int32 nSize = std::min( rEntryKeyAspectRatio.Width, rEntryKeyAspectRatio.Height );
168 Reference< drawing::XShape > xShape =
169 pShapeFactory->createCircle( xResultGroup, awt::Size( nSize, nSize ),
170 awt::Point( rEntryKeyAspectRatio.Width/2-nSize/2, rEntryKeyAspectRatio.Height/2-nSize/2 ));
171 if( xShape.is() )
173 lcl_setPropertiesToShape( xLegendEntryProperties, xShape, ePropertyType, awt::Size(0,0) ); // PropertyType::FilledSeries );
176 else // eStyle == LegendSymbolStyle_BOX
178 tNameSequence aPropNames;
179 tAnySequence aPropValues;
181 getPropNamesAndValues( xLegendEntryProperties, aPropNames, aPropValues,
182 ePropertyType, awt::Size(0,0) );// PropertyType::FilledSeries
184 pShapeFactory->createRectangle( xResultGroup,
185 rEntryKeyAspectRatio, awt::Point( 0, 0 ),
186 aPropNames, aPropValues );
189 catch( const uno::Exception & )
191 DBG_UNHANDLED_EXCEPTION("chart2");
194 return xResult;
197 } // namespace chart
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */