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