1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "VLegendSymbolFactory.hxx"
27 #include "macros.hxx"
28 #include "PropertyMapper.hxx"
29 #include "ShapeFactory.hxx"
30 #include "ObjectIdentifier.hxx"
31 #include <com/sun/star/drawing/LineStyle.hpp>
32 #include <com/sun/star/chart2/Symbol.hpp>
33 
34 // header for define DBG_ASSERT
35 #include <tools/debug.hxx>
36 
37 using namespace ::com::sun::star;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Sequence;
40 using ::rtl::OUString;
41 
42 namespace
43 {
lcl_setPropetiesToShape(const Reference<beans::XPropertySet> & xProp,const Reference<drawing::XShape> & xShape,::chart::VLegendSymbolFactory::tPropertyType ePropertyType)44 void lcl_setPropetiesToShape(
45     const Reference< beans::XPropertySet > & xProp,
46     const Reference< drawing::XShape > & xShape,
47     ::chart::VLegendSymbolFactory::tPropertyType ePropertyType )
48 {
49     const ::chart::tPropertyNameMap & aFilledSeriesNameMap( ::chart::PropertyMapper::getPropertyNameMapForFilledSeriesProperties());
50     const ::chart::tPropertyNameMap & aLineSeriesNameMap( ::chart::PropertyMapper::getPropertyNameMapForLineSeriesProperties());
51     const ::chart::tPropertyNameMap & aLineNameMap( ::chart::PropertyMapper::getPropertyNameMapForLineProperties());
52     const ::chart::tPropertyNameMap & aFillNameMap( ::chart::PropertyMapper::getPropertyNameMapForFillProperties());
53     const ::chart::tPropertyNameMap & aFillLineNameMap( ::chart::PropertyMapper::getPropertyNameMapForFillAndLineProperties());
54 
55     Reference< beans::XPropertySet > xShapeProp( xShape, uno::UNO_QUERY );
56     if( xProp.is() && xShapeProp.is() )
57     {
58         ::chart::tPropertyNameValueMap aValueMap;
59         switch( ePropertyType )
60         {
61             case ::chart::VLegendSymbolFactory::PROP_TYPE_FILLED_SERIES:
62                 ::chart::PropertyMapper::getValueMap( aValueMap, aFilledSeriesNameMap, xProp );
63                 break;
64             case ::chart::VLegendSymbolFactory::PROP_TYPE_LINE_SERIES:
65                 ::chart::PropertyMapper::getValueMap( aValueMap, aLineSeriesNameMap, xProp );
66                 break;
67             case ::chart::VLegendSymbolFactory::PROP_TYPE_LINE:
68                 ::chart::PropertyMapper::getValueMap( aValueMap, aLineNameMap, xProp );
69                 break;
70             case ::chart::VLegendSymbolFactory::PROP_TYPE_FILL:
71                 ::chart::PropertyMapper::getValueMap( aValueMap, aFillNameMap, xProp );
72                 break;
73             case ::chart::VLegendSymbolFactory::PROP_TYPE_FILL_AND_LINE:
74                 ::chart::PropertyMapper::getValueMap( aValueMap, aFillLineNameMap, xProp );
75                 break;
76         }
77 
78         ::chart::tNameSequence aPropNames;
79         ::chart::tAnySequence aPropValues;
80         ::chart::PropertyMapper::getMultiPropertyListsFromValueMap( aPropNames, aPropValues, aValueMap );
81 
82         uno::Any* pLineWidthAny = ::chart::PropertyMapper::getValuePointer(aPropValues,aPropNames,C2U("LineWidth"));
83         sal_Int32 nLineWidth = 0;
84         if( pLineWidthAny && (*pLineWidthAny>>=nLineWidth) )
85         {
86             const sal_Int32 nMaxLineWidthForLegend = 50;/*1/100 mm*///todo: make this dependent from legend entry height
87             if( nLineWidth>nMaxLineWidthForLegend )
88                 *pLineWidthAny = uno::makeAny( nMaxLineWidthForLegend );
89         }
90 
91         ::chart::PropertyMapper::setMultiProperties( aPropNames, aPropValues, xShapeProp );
92     }
93 }
94 
95 } // anonymous namespace
96 
97 namespace chart
98 {
99 
createSymbol(const awt::Size & rEntryKeyAspectRatio,const Reference<drawing::XShapes> xSymbolContainer,LegendSymbolStyle eStyle,const Reference<lang::XMultiServiceFactory> & xShapeFactory,const Reference<beans::XPropertySet> & xLegendEntryProperties,tPropertyType ePropertyType,const uno::Any & rExplicitSymbol)100 Reference< drawing::XShape > VLegendSymbolFactory::createSymbol(
101     const awt::Size& rEntryKeyAspectRatio,
102     const Reference< drawing::XShapes > xSymbolContainer,
103     LegendSymbolStyle eStyle,
104     const Reference< lang::XMultiServiceFactory > & xShapeFactory,
105     const Reference< beans::XPropertySet > & xLegendEntryProperties,
106     tPropertyType ePropertyType, const uno::Any& rExplicitSymbol )
107 {
108     Reference< drawing::XShape > xResult;
109 
110     if( ! (xSymbolContainer.is() && xShapeFactory.is()))
111         return xResult;
112 
113     xResult.set( xShapeFactory->createInstance(
114                      C2U( "com.sun.star.drawing.GroupShape" )), uno::UNO_QUERY );
115     xSymbolContainer->add( xResult );
116     Reference< drawing::XShapes > xResultGroup( xResult, uno::UNO_QUERY );
117     if( ! xResultGroup.is())
118         return xResult;
119 
120     // add an invisible square box to maintain aspect ratio
121     Reference< drawing::XShape > xBound( ShapeFactory(xShapeFactory).createInvisibleRectangle(
122                 xResultGroup, rEntryKeyAspectRatio  ));
123 
124     // create symbol
125     try
126     {
127         if( eStyle == LegendSymbolStyle_LINE )
128         {
129             Reference< drawing::XShape > xLine( xShapeFactory->createInstance(
130                     C2U( "com.sun.star.drawing.LineShape" )), uno::UNO_QUERY );
131             if( xLine.is())
132             {
133                 xResultGroup->add( xLine );
134                 xLine->setSize(  awt::Size( rEntryKeyAspectRatio.Width, 0 ));
135                 xLine->setPosition( awt::Point( 0, rEntryKeyAspectRatio.Height/2 ));
136 
137                 lcl_setPropetiesToShape( xLegendEntryProperties, xLine, ePropertyType );
138             }
139 
140             Reference< drawing::XShape > xSymbol;
141             const sal_Int32 nSize = std::min(rEntryKeyAspectRatio.Width,rEntryKeyAspectRatio.Height);
142             chart2::Symbol aSymbol;
143             if( rExplicitSymbol >>= aSymbol )
144             {
145                 drawing::Direction3D aSymbolSize( nSize, nSize, 0 );
146                 drawing::Position3D aPos( rEntryKeyAspectRatio.Width/2, rEntryKeyAspectRatio.Height/2, 0 );
147                 ShapeFactory aFactory( xShapeFactory );
148                 if( aSymbol.Style == chart2::SymbolStyle_STANDARD )
149                 {
150                     // take series color as fill color
151                     xLegendEntryProperties->getPropertyValue( C2U("Color")) >>= aSymbol.FillColor;
152                     // border of symbols always same as fill color
153                     aSymbol.BorderColor = aSymbol.FillColor;
154 
155                     xSymbol.set( aFactory.createSymbol2D(
156                                      xResultGroup,
157                                      aPos,
158                                      aSymbolSize,
159                                      aSymbol.StandardSymbol,
160                                      aSymbol.BorderColor,
161                                      aSymbol.FillColor ));
162                 }
163                 else if( aSymbol.Style == chart2::SymbolStyle_GRAPHIC )
164                 {
165                     xSymbol.set( aFactory.createGraphic2D(
166                                      xResultGroup,
167                                      aPos,
168                                      aSymbolSize,
169                                      aSymbol.Graphic ));
170                 }
171                 else if( aSymbol.Style == chart2::SymbolStyle_AUTO )
172                 {
173                     DBG_ERROR("the given parameter is not allowed to contain an automatic symbol style");
174                 }
175             }
176         }
177         else if( eStyle == LegendSymbolStyle_CIRCLE )
178         {
179             Reference< drawing::XShape > xShape( xShapeFactory->createInstance(
180                 C2U( "com.sun.star.drawing.EllipseShape" )), uno::UNO_QUERY );
181             if( xShape.is() )
182             {
183                 xResultGroup->add( xShape );
184                 sal_Int32 nSize = std::min( rEntryKeyAspectRatio.Width, rEntryKeyAspectRatio.Height );
185                 xShape->setSize( awt::Size( nSize, nSize ) );
186                 xShape->setPosition( awt::Point( rEntryKeyAspectRatio.Width/2-nSize/2, rEntryKeyAspectRatio.Height/2-nSize/2 ) );
187                 lcl_setPropetiesToShape( xLegendEntryProperties, xShape, ePropertyType ); // PROP_TYPE_FILLED_SERIES );
188             }
189         }
190         else // eStyle == LegendSymbolStyle_BOX
191         {
192             Reference< drawing::XShape > xShape( xShapeFactory->createInstance(
193                 C2U( "com.sun.star.drawing.RectangleShape" )), uno::UNO_QUERY );
194             if( xShape.is() )
195             {
196                 xResultGroup->add( xShape );
197                 xShape->setSize( rEntryKeyAspectRatio );
198                 xShape->setPosition( awt::Point( 0, 0 ) );
199                 lcl_setPropetiesToShape( xLegendEntryProperties, xShape, ePropertyType ); // PROP_TYPE_FILLED_SERIES );
200             }
201         }
202     }
203     catch( uno::Exception & ex )
204     {
205         ASSERT_EXCEPTION( ex );
206     }
207 
208     return xResult;
209 }
210 
211 } //  namespace chart
212