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 "LineChartTypeTemplate.hxx"
27 #include "macros.hxx"
28 #include "DiagramHelper.hxx"
29 #include "servicenames_charttypes.hxx"
30 #include "ContainerHelper.hxx"
31 #include "DataSeriesHelper.hxx"
32 #include <com/sun/star/chart2/SymbolStyle.hpp>
33 #include <com/sun/star/chart2/Symbol.hpp>
34 #include <com/sun/star/drawing/LineStyle.hpp>
35 #include "PropertyHelper.hxx"
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 
38 #include <algorithm>
39 
40 using namespace ::com::sun::star;
41 
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::rtl::OUString;
45 using ::com::sun::star::beans::Property;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Any;
48 using ::osl::MutexGuard;
49 
50 namespace
51 {
52 
53 static const OUString lcl_aServiceName(
54     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LineChartTypeTemplate" ));
55 
56 enum
57 {
58     PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE,
59     PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION,
60     PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER
61 
62 };
63 
lcl_AddPropertiesToVector(::std::vector<Property> & rOutProperties)64 void lcl_AddPropertiesToVector(
65     ::std::vector< Property > & rOutProperties )
66 {
67     rOutProperties.push_back(
68         Property( C2U( "CurveStyle" ),
69                   PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE,
70                   ::getCppuType( reinterpret_cast< const chart2::CurveStyle * >(0)),
71                   beans::PropertyAttribute::BOUND
72                   | beans::PropertyAttribute::MAYBEDEFAULT ));
73     rOutProperties.push_back(
74         Property( C2U( "CurveResolution" ),
75                   PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION,
76                   ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
77                   beans::PropertyAttribute::BOUND
78                   | beans::PropertyAttribute::MAYBEDEFAULT ));
79     rOutProperties.push_back(
80         Property( C2U( "SplineOrder" ),
81                   PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER,
82                   ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
83                   beans::PropertyAttribute::BOUND
84                   | beans::PropertyAttribute::MAYBEDEFAULT ));
85 }
86 
87 struct StaticLineChartTypeTemplateDefaults_Initializer
88 {
operator ()__anon343dfb560111::StaticLineChartTypeTemplateDefaults_Initializer89     ::chart::tPropertyValueMap* operator()()
90     {
91         static ::chart::tPropertyValueMap aStaticDefaults;
92         lcl_AddDefaultsToMap( aStaticDefaults );
93         return &aStaticDefaults;
94     }
95 private:
lcl_AddDefaultsToMap__anon343dfb560111::StaticLineChartTypeTemplateDefaults_Initializer96     void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
97     {
98         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE, chart2::CurveStyle_LINES );
99         ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION, 20 );
100 
101         // todo: check whether order 3 means polygons of order 3 or 2. (see
102         // http://www.people.nnov.ru/fractal/Splines/Basis.htm )
103         ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER, 3 );
104     }
105 };
106 
107 struct StaticLineChartTypeTemplateDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticLineChartTypeTemplateDefaults_Initializer >
108 {
109 };
110 
111 struct StaticLineChartTypeTemplateInfoHelper_Initializer
112 {
operator ()__anon343dfb560111::StaticLineChartTypeTemplateInfoHelper_Initializer113     ::cppu::OPropertyArrayHelper* operator()()
114     {
115         static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
116         return &aPropHelper;
117     }
118 
119 private:
lcl_GetPropertySequence__anon343dfb560111::StaticLineChartTypeTemplateInfoHelper_Initializer120     Sequence< Property > lcl_GetPropertySequence()
121     {
122         ::std::vector< ::com::sun::star::beans::Property > aProperties;
123         lcl_AddPropertiesToVector( aProperties );
124 
125         ::std::sort( aProperties.begin(), aProperties.end(),
126                      ::chart::PropertyNameLess() );
127 
128         return ::chart::ContainerHelper::ContainerToSequence( aProperties );
129     }
130 
131 };
132 
133 struct StaticLineChartTypeTemplateInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticLineChartTypeTemplateInfoHelper_Initializer >
134 {
135 };
136 
137 struct StaticLineChartTypeTemplateInfo_Initializer
138 {
operator ()__anon343dfb560111::StaticLineChartTypeTemplateInfo_Initializer139     uno::Reference< beans::XPropertySetInfo >* operator()()
140     {
141         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
142             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticLineChartTypeTemplateInfoHelper::get() ) );
143         return &xPropertySetInfo;
144     }
145 };
146 
147 struct StaticLineChartTypeTemplateInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticLineChartTypeTemplateInfo_Initializer >
148 {
149 };
150 
151 } // anonymous namespace
152 
153 namespace chart
154 {
155 
LineChartTypeTemplate(uno::Reference<uno::XComponentContext> const & xContext,const::rtl::OUString & rServiceName,StackMode eStackMode,bool bSymbols,bool bHasLines,sal_Int32 nDim)156 LineChartTypeTemplate::LineChartTypeTemplate(
157     uno::Reference<
158         uno::XComponentContext > const & xContext,
159     const ::rtl::OUString & rServiceName,
160     StackMode eStackMode,
161     bool bSymbols,
162     bool bHasLines /* = true */,
163     sal_Int32 nDim /* = 2 */ ) :
164         ChartTypeTemplate( xContext, rServiceName ),
165         ::property::OPropertySet( m_aMutex ),
166         m_eStackMode( eStackMode ),
167         m_bHasSymbols( bSymbols ),
168         m_bHasLines( bHasLines ),
169         m_nDim( nDim )
170 {
171     if( nDim == 3 )
172         m_bHasSymbols = false;
173 }
174 
~LineChartTypeTemplate()175 LineChartTypeTemplate::~LineChartTypeTemplate()
176 {}
177 // ____ OPropertySet ____
GetDefaultValue(sal_Int32 nHandle) const178 uno::Any LineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
179     throw(beans::UnknownPropertyException)
180 {
181     const tPropertyValueMap& rStaticDefaults = *StaticLineChartTypeTemplateDefaults::get();
182     tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
183     if( aFound == rStaticDefaults.end() )
184         return uno::Any();
185     return (*aFound).second;
186 }
187 
getInfoHelper()188 ::cppu::IPropertyArrayHelper & SAL_CALL LineChartTypeTemplate::getInfoHelper()
189 {
190     return *StaticLineChartTypeTemplateInfoHelper::get();
191 }
192 
193 // ____ XPropertySet ____
getPropertySetInfo()194 uno::Reference< beans::XPropertySetInfo > SAL_CALL LineChartTypeTemplate::getPropertySetInfo()
195     throw (uno::RuntimeException)
196 {
197     return *StaticLineChartTypeTemplateInfo::get();
198 }
199 
getDimension() const200 sal_Int32 LineChartTypeTemplate::getDimension() const
201 {
202     return m_nDim;
203 }
204 
getStackMode(sal_Int32) const205 StackMode LineChartTypeTemplate::getStackMode( sal_Int32 /* nChartTypeIndex */ ) const
206 {
207     return m_eStackMode;
208 }
209 
210 // ____ XChartTypeTemplate ____
matchesTemplate(const uno::Reference<chart2::XDiagram> & xDiagram,sal_Bool bAdaptProperties)211 sal_Bool SAL_CALL LineChartTypeTemplate::matchesTemplate(
212     const uno::Reference< chart2::XDiagram >& xDiagram,
213     sal_Bool bAdaptProperties )
214     throw (uno::RuntimeException)
215 {
216     sal_Bool bResult = ChartTypeTemplate::matchesTemplate( xDiagram, bAdaptProperties );
217 
218     // check symbol-style and line-style
219     // for a template with symbols (or with lines) it is ok, if there is at least one series
220     // with symbols (or with lines)
221     if( bResult )
222     {
223         bool bSymbolFound = false;
224         bool bLineFound = false;
225 
226         ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
227             DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
228 
229         for( ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aIt =
230                  aSeriesVec.begin(); aIt != aSeriesVec.end(); ++aIt )
231         {
232             try
233             {
234                 chart2::Symbol aSymbProp;
235                 drawing::LineStyle eLineStyle;
236                 Reference< beans::XPropertySet > xProp( *aIt, uno::UNO_QUERY_THROW );
237 
238                 bool bCurrentHasSymbol = (xProp->getPropertyValue( C2U( "Symbol" )) >>= aSymbProp) &&
239                     (aSymbProp.Style != chart2::SymbolStyle_NONE);
240 
241                 if( bCurrentHasSymbol )
242                     bSymbolFound = true;
243 
244                 if( bCurrentHasSymbol && (!m_bHasSymbols) )
245                 {
246                     bResult = false;
247                     break;
248                 }
249 
250                 bool bCurrentHasLine = (xProp->getPropertyValue( C2U( "LineStyle" )) >>= eLineStyle) &&
251                     ( eLineStyle != drawing::LineStyle_NONE );
252 
253                 if( bCurrentHasLine )
254                     bLineFound = true;
255 
256                 if( bCurrentHasLine && (!m_bHasLines) )
257                 {
258                     bResult = false;
259                     break;
260                 }
261             }
262             catch( uno::Exception & ex )
263             {
264                 ASSERT_EXCEPTION( ex );
265             }
266         }
267 
268         if(bResult)
269         {
270             if( !bLineFound && m_bHasLines && bSymbolFound )
271                 bResult = false;
272             else if( !bSymbolFound && m_bHasSymbols && bLineFound )
273                 bResult = false;
274             else if( !bLineFound && !bSymbolFound )
275                 return m_bHasLines && m_bHasSymbols;
276         }
277     }
278 
279     // adapt curve style, spline order and resolution
280     if( bResult && bAdaptProperties )
281     {
282         try
283         {
284             uno::Reference< beans::XPropertySet > xChartTypeProp(
285                 DiagramHelper::getChartTypeByIndex( xDiagram, 0 ),
286                 uno::UNO_QUERY_THROW );
287             setFastPropertyValue_NoBroadcast( PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE, xChartTypeProp->getPropertyValue(C2U("CurveStyle" )) );
288             setFastPropertyValue_NoBroadcast( PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION, xChartTypeProp->getPropertyValue(C2U("CurveResolution" )) );
289             setFastPropertyValue_NoBroadcast( PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER, xChartTypeProp->getPropertyValue(C2U("SplineOrder" )) );
290         }
291         catch( uno::Exception & ex )
292         {
293             ASSERT_EXCEPTION( ex );
294         }
295     }
296 
297     return bResult;
298 }
299 
getChartTypeForIndex(sal_Int32)300 Reference< chart2::XChartType > LineChartTypeTemplate::getChartTypeForIndex( sal_Int32 /*nChartTypeIndex*/ )
301 {
302     Reference< chart2::XChartType > xResult;
303 
304     try
305     {
306         Reference< lang::XMultiServiceFactory > xFact(
307             GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
308         xResult.set( xFact->createInstance(
309                          CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
310 
311         Reference< beans::XPropertySet > xCTProp( xResult, uno::UNO_QUERY );
312         if( xCTProp.is())
313         {
314             xCTProp->setPropertyValue(
315                 C2U( "CurveStyle" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE ));
316             xCTProp->setPropertyValue(
317                 C2U( "CurveResolution" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION ));
318             xCTProp->setPropertyValue(
319                 C2U( "SplineOrder" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER ));
320         }
321     }
322     catch( uno::Exception & ex )
323     {
324         ASSERT_EXCEPTION( ex );
325     }
326 
327     return xResult;
328 }
329 
getChartTypeForNewSeries(const uno::Sequence<Reference<chart2::XChartType>> & aFormerlyUsedChartTypes)330 Reference< chart2::XChartType > SAL_CALL LineChartTypeTemplate::getChartTypeForNewSeries(
331         const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
332     throw (uno::RuntimeException)
333 {
334     Reference< chart2::XChartType > xResult;
335 
336     try
337     {
338         Reference< lang::XMultiServiceFactory > xFact(
339             GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
340         xResult.set( xFact->createInstance(
341                          CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
342 
343         ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
344 
345         Reference< beans::XPropertySet > xCTProp( xResult, uno::UNO_QUERY );
346         if( xCTProp.is())
347         {
348             xCTProp->setPropertyValue(
349                 C2U( "CurveStyle" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE ));
350             xCTProp->setPropertyValue(
351                 C2U( "CurveResolution" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION ));
352             xCTProp->setPropertyValue(
353                 C2U( "SplineOrder" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER ));
354         }
355     }
356     catch( uno::Exception & ex )
357     {
358         ASSERT_EXCEPTION( ex );
359     }
360 
361     return xResult;
362 }
363 
applyStyle(const Reference<chart2::XDataSeries> & xSeries,::sal_Int32 nChartTypeIndex,::sal_Int32 nSeriesIndex,::sal_Int32 nSeriesCount)364 void SAL_CALL LineChartTypeTemplate::applyStyle(
365     const Reference< chart2::XDataSeries >& xSeries,
366     ::sal_Int32 nChartTypeIndex,
367     ::sal_Int32 nSeriesIndex,
368     ::sal_Int32 nSeriesCount )
369     throw (uno::RuntimeException)
370 {
371     ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
372 
373     try
374     {
375         Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY_THROW );
376 
377         DataSeriesHelper::switchSymbolsOnOrOff( xProp, m_bHasSymbols, nSeriesIndex );
378         DataSeriesHelper::switchLinesOnOrOff( xProp, m_bHasLines );
379         DataSeriesHelper::makeLinesThickOrThin( xProp, m_nDim==2 );
380     }
381     catch( uno::Exception & ex )
382     {
383         ASSERT_EXCEPTION( ex );
384     }
385 }
386 
387 // ----------------------------------------
388 
getSupportedServiceNames_Static()389 Sequence< OUString > LineChartTypeTemplate::getSupportedServiceNames_Static()
390 {
391     Sequence< OUString > aServices( 2 );
392     aServices[ 0 ] = lcl_aServiceName;
393     aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
394     return aServices;
395 }
396 
397 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
398 APPHELPER_XSERVICEINFO_IMPL( LineChartTypeTemplate, lcl_aServiceName );
399 
400 IMPLEMENT_FORWARD_XINTERFACE2( LineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
401 IMPLEMENT_FORWARD_XTYPEPROVIDER2( LineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
402 
403 } //  namespace chart
404