xref: /AOO41X/main/chart2/source/view/main/VLegendSymbolFactory.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_chart2.hxx"
30*cdf0e10cSrcweir #include "VLegendSymbolFactory.hxx"
31*cdf0e10cSrcweir #include "macros.hxx"
32*cdf0e10cSrcweir #include "PropertyMapper.hxx"
33*cdf0e10cSrcweir #include "ShapeFactory.hxx"
34*cdf0e10cSrcweir #include "ObjectIdentifier.hxx"
35*cdf0e10cSrcweir #include <com/sun/star/drawing/LineStyle.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/chart2/Symbol.hpp>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir // header for define DBG_ASSERT
39*cdf0e10cSrcweir #include <tools/debug.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::com::sun::star;
42*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
43*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
44*cdf0e10cSrcweir using ::rtl::OUString;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir namespace
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir void lcl_setPropetiesToShape(
49*cdf0e10cSrcweir     const Reference< beans::XPropertySet > & xProp,
50*cdf0e10cSrcweir     const Reference< drawing::XShape > & xShape,
51*cdf0e10cSrcweir     ::chart::VLegendSymbolFactory::tPropertyType ePropertyType )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     const ::chart::tPropertyNameMap & aFilledSeriesNameMap( ::chart::PropertyMapper::getPropertyNameMapForFilledSeriesProperties());
54*cdf0e10cSrcweir     const ::chart::tPropertyNameMap & aLineSeriesNameMap( ::chart::PropertyMapper::getPropertyNameMapForLineSeriesProperties());
55*cdf0e10cSrcweir     const ::chart::tPropertyNameMap & aLineNameMap( ::chart::PropertyMapper::getPropertyNameMapForLineProperties());
56*cdf0e10cSrcweir     const ::chart::tPropertyNameMap & aFillNameMap( ::chart::PropertyMapper::getPropertyNameMapForFillProperties());
57*cdf0e10cSrcweir     const ::chart::tPropertyNameMap & aFillLineNameMap( ::chart::PropertyMapper::getPropertyNameMapForFillAndLineProperties());
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     Reference< beans::XPropertySet > xShapeProp( xShape, uno::UNO_QUERY );
60*cdf0e10cSrcweir     if( xProp.is() && xShapeProp.is() )
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         ::chart::tPropertyNameValueMap aValueMap;
63*cdf0e10cSrcweir         switch( ePropertyType )
64*cdf0e10cSrcweir         {
65*cdf0e10cSrcweir             case ::chart::VLegendSymbolFactory::PROP_TYPE_FILLED_SERIES:
66*cdf0e10cSrcweir                 ::chart::PropertyMapper::getValueMap( aValueMap, aFilledSeriesNameMap, xProp );
67*cdf0e10cSrcweir                 break;
68*cdf0e10cSrcweir             case ::chart::VLegendSymbolFactory::PROP_TYPE_LINE_SERIES:
69*cdf0e10cSrcweir                 ::chart::PropertyMapper::getValueMap( aValueMap, aLineSeriesNameMap, xProp );
70*cdf0e10cSrcweir                 break;
71*cdf0e10cSrcweir             case ::chart::VLegendSymbolFactory::PROP_TYPE_LINE:
72*cdf0e10cSrcweir                 ::chart::PropertyMapper::getValueMap( aValueMap, aLineNameMap, xProp );
73*cdf0e10cSrcweir                 break;
74*cdf0e10cSrcweir             case ::chart::VLegendSymbolFactory::PROP_TYPE_FILL:
75*cdf0e10cSrcweir                 ::chart::PropertyMapper::getValueMap( aValueMap, aFillNameMap, xProp );
76*cdf0e10cSrcweir                 break;
77*cdf0e10cSrcweir             case ::chart::VLegendSymbolFactory::PROP_TYPE_FILL_AND_LINE:
78*cdf0e10cSrcweir                 ::chart::PropertyMapper::getValueMap( aValueMap, aFillLineNameMap, xProp );
79*cdf0e10cSrcweir                 break;
80*cdf0e10cSrcweir         }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         ::chart::tNameSequence aPropNames;
83*cdf0e10cSrcweir         ::chart::tAnySequence aPropValues;
84*cdf0e10cSrcweir         ::chart::PropertyMapper::getMultiPropertyListsFromValueMap( aPropNames, aPropValues, aValueMap );
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         uno::Any* pLineWidthAny = ::chart::PropertyMapper::getValuePointer(aPropValues,aPropNames,C2U("LineWidth"));
87*cdf0e10cSrcweir         sal_Int32 nLineWidth = 0;
88*cdf0e10cSrcweir         if( pLineWidthAny && (*pLineWidthAny>>=nLineWidth) )
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             const sal_Int32 nMaxLineWidthForLegend = 50;/*1/100 mm*///todo: make this dependent from legend entry height
91*cdf0e10cSrcweir             if( nLineWidth>nMaxLineWidthForLegend )
92*cdf0e10cSrcweir                 *pLineWidthAny = uno::makeAny( nMaxLineWidthForLegend );
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         ::chart::PropertyMapper::setMultiProperties( aPropNames, aPropValues, xShapeProp );
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir } // anonymous namespace
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir namespace chart
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir Reference< drawing::XShape > VLegendSymbolFactory::createSymbol(
105*cdf0e10cSrcweir     const awt::Size& rEntryKeyAspectRatio,
106*cdf0e10cSrcweir     const Reference< drawing::XShapes > xSymbolContainer,
107*cdf0e10cSrcweir     LegendSymbolStyle eStyle,
108*cdf0e10cSrcweir     const Reference< lang::XMultiServiceFactory > & xShapeFactory,
109*cdf0e10cSrcweir     const Reference< beans::XPropertySet > & xLegendEntryProperties,
110*cdf0e10cSrcweir     tPropertyType ePropertyType, const uno::Any& rExplicitSymbol )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     Reference< drawing::XShape > xResult;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     if( ! (xSymbolContainer.is() && xShapeFactory.is()))
115*cdf0e10cSrcweir         return xResult;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     xResult.set( xShapeFactory->createInstance(
118*cdf0e10cSrcweir                      C2U( "com.sun.star.drawing.GroupShape" )), uno::UNO_QUERY );
119*cdf0e10cSrcweir     xSymbolContainer->add( xResult );
120*cdf0e10cSrcweir     Reference< drawing::XShapes > xResultGroup( xResult, uno::UNO_QUERY );
121*cdf0e10cSrcweir     if( ! xResultGroup.is())
122*cdf0e10cSrcweir         return xResult;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     // add an invisible square box to maintain aspect ratio
125*cdf0e10cSrcweir     Reference< drawing::XShape > xBound( ShapeFactory(xShapeFactory).createInvisibleRectangle(
126*cdf0e10cSrcweir                 xResultGroup, rEntryKeyAspectRatio  ));
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     // create symbol
129*cdf0e10cSrcweir     try
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         if( eStyle == LegendSymbolStyle_LINE )
132*cdf0e10cSrcweir         {
133*cdf0e10cSrcweir             Reference< drawing::XShape > xLine( xShapeFactory->createInstance(
134*cdf0e10cSrcweir                     C2U( "com.sun.star.drawing.LineShape" )), uno::UNO_QUERY );
135*cdf0e10cSrcweir             if( xLine.is())
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 xResultGroup->add( xLine );
138*cdf0e10cSrcweir                 xLine->setSize(  awt::Size( rEntryKeyAspectRatio.Width, 0 ));
139*cdf0e10cSrcweir                 xLine->setPosition( awt::Point( 0, rEntryKeyAspectRatio.Height/2 ));
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir                 lcl_setPropetiesToShape( xLegendEntryProperties, xLine, ePropertyType );
142*cdf0e10cSrcweir             }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir             Reference< drawing::XShape > xSymbol;
145*cdf0e10cSrcweir             const sal_Int32 nSize = std::min(rEntryKeyAspectRatio.Width,rEntryKeyAspectRatio.Height);
146*cdf0e10cSrcweir             chart2::Symbol aSymbol;
147*cdf0e10cSrcweir             if( rExplicitSymbol >>= aSymbol )
148*cdf0e10cSrcweir             {
149*cdf0e10cSrcweir                 drawing::Direction3D aSymbolSize( nSize, nSize, 0 );
150*cdf0e10cSrcweir                 drawing::Position3D aPos( rEntryKeyAspectRatio.Width/2, rEntryKeyAspectRatio.Height/2, 0 );
151*cdf0e10cSrcweir                 ShapeFactory aFactory( xShapeFactory );
152*cdf0e10cSrcweir                 if( aSymbol.Style == chart2::SymbolStyle_STANDARD )
153*cdf0e10cSrcweir                 {
154*cdf0e10cSrcweir                     // take series color as fill color
155*cdf0e10cSrcweir                     xLegendEntryProperties->getPropertyValue( C2U("Color")) >>= aSymbol.FillColor;
156*cdf0e10cSrcweir                     // border of symbols always same as fill color
157*cdf0e10cSrcweir                     aSymbol.BorderColor = aSymbol.FillColor;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir                     xSymbol.set( aFactory.createSymbol2D(
160*cdf0e10cSrcweir                                      xResultGroup,
161*cdf0e10cSrcweir                                      aPos,
162*cdf0e10cSrcweir                                      aSymbolSize,
163*cdf0e10cSrcweir                                      aSymbol.StandardSymbol,
164*cdf0e10cSrcweir                                      aSymbol.BorderColor,
165*cdf0e10cSrcweir                                      aSymbol.FillColor ));
166*cdf0e10cSrcweir                 }
167*cdf0e10cSrcweir                 else if( aSymbol.Style == chart2::SymbolStyle_GRAPHIC )
168*cdf0e10cSrcweir                 {
169*cdf0e10cSrcweir                     xSymbol.set( aFactory.createGraphic2D(
170*cdf0e10cSrcweir                                      xResultGroup,
171*cdf0e10cSrcweir                                      aPos,
172*cdf0e10cSrcweir                                      aSymbolSize,
173*cdf0e10cSrcweir                                      aSymbol.Graphic ));
174*cdf0e10cSrcweir                 }
175*cdf0e10cSrcweir                 else if( aSymbol.Style == chart2::SymbolStyle_AUTO )
176*cdf0e10cSrcweir                 {
177*cdf0e10cSrcweir                     DBG_ERROR("the given parameter is not allowed to contain an automatic symbol style");
178*cdf0e10cSrcweir                 }
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir         else if( eStyle == LegendSymbolStyle_CIRCLE )
182*cdf0e10cSrcweir         {
183*cdf0e10cSrcweir             Reference< drawing::XShape > xShape( xShapeFactory->createInstance(
184*cdf0e10cSrcweir                 C2U( "com.sun.star.drawing.EllipseShape" )), uno::UNO_QUERY );
185*cdf0e10cSrcweir             if( xShape.is() )
186*cdf0e10cSrcweir             {
187*cdf0e10cSrcweir                 xResultGroup->add( xShape );
188*cdf0e10cSrcweir                 sal_Int32 nSize = std::min( rEntryKeyAspectRatio.Width, rEntryKeyAspectRatio.Height );
189*cdf0e10cSrcweir                 xShape->setSize( awt::Size( nSize, nSize ) );
190*cdf0e10cSrcweir                 xShape->setPosition( awt::Point( rEntryKeyAspectRatio.Width/2-nSize/2, rEntryKeyAspectRatio.Height/2-nSize/2 ) );
191*cdf0e10cSrcweir                 lcl_setPropetiesToShape( xLegendEntryProperties, xShape, ePropertyType ); // PROP_TYPE_FILLED_SERIES );
192*cdf0e10cSrcweir             }
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir         else // eStyle == LegendSymbolStyle_BOX
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             Reference< drawing::XShape > xShape( xShapeFactory->createInstance(
197*cdf0e10cSrcweir                 C2U( "com.sun.star.drawing.RectangleShape" )), uno::UNO_QUERY );
198*cdf0e10cSrcweir             if( xShape.is() )
199*cdf0e10cSrcweir             {
200*cdf0e10cSrcweir                 xResultGroup->add( xShape );
201*cdf0e10cSrcweir                 xShape->setSize( rEntryKeyAspectRatio );
202*cdf0e10cSrcweir                 xShape->setPosition( awt::Point( 0, 0 ) );
203*cdf0e10cSrcweir                 lcl_setPropetiesToShape( xLegendEntryProperties, xShape, ePropertyType ); // PROP_TYPE_FILLED_SERIES );
204*cdf0e10cSrcweir             }
205*cdf0e10cSrcweir         }
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir     catch( uno::Exception & ex )
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         ASSERT_EXCEPTION( ex );
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     return xResult;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir } //  namespace chart
216