merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / model / template / LineChartTypeTemplate.cxx
blob8d582178a12835b4680dd5e1515e9fa2d0ecbc6b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "LineChartTypeTemplate.hxx"
31 #include "macros.hxx"
32 #include "DiagramHelper.hxx"
33 #include "servicenames_charttypes.hxx"
34 #include "ContainerHelper.hxx"
35 #include "DataSeriesHelper.hxx"
36 #include <com/sun/star/chart2/SymbolStyle.hpp>
37 #include <com/sun/star/chart2/Symbol.hpp>
38 #include <com/sun/star/drawing/LineStyle.hpp>
39 #include "PropertyHelper.hxx"
40 #include <com/sun/star/beans/PropertyAttribute.hpp>
42 #include <algorithm>
44 using namespace ::com::sun::star;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::rtl::OUString;
49 using ::com::sun::star::beans::Property;
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::uno::Any;
52 using ::osl::MutexGuard;
54 namespace
57 static const OUString lcl_aServiceName(
58 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LineChartTypeTemplate" ));
60 enum
62 PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE,
63 PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION,
64 PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER
68 void lcl_AddPropertiesToVector(
69 ::std::vector< Property > & rOutProperties )
71 rOutProperties.push_back(
72 Property( C2U( "CurveStyle" ),
73 PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE,
74 ::getCppuType( reinterpret_cast< const chart2::CurveStyle * >(0)),
75 beans::PropertyAttribute::BOUND
76 | beans::PropertyAttribute::MAYBEDEFAULT ));
77 rOutProperties.push_back(
78 Property( C2U( "CurveResolution" ),
79 PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION,
80 ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
81 beans::PropertyAttribute::BOUND
82 | beans::PropertyAttribute::MAYBEDEFAULT ));
83 rOutProperties.push_back(
84 Property( C2U( "SplineOrder" ),
85 PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER,
86 ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
87 beans::PropertyAttribute::BOUND
88 | beans::PropertyAttribute::MAYBEDEFAULT ));
91 void lcl_AddDefaultsToMap(
92 ::chart::tPropertyValueMap & rOutMap )
94 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE, chart2::CurveStyle_LINES );
95 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION, 20 );
97 // todo: check whether order 3 means polygons of order 3 or 2. (see
98 // http://www.people.nnov.ru/fractal/Splines/Basis.htm )
99 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER, 3 );
102 const Sequence< Property > & lcl_GetPropertySequence()
104 static Sequence< Property > aPropSeq;
106 // /--
107 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
108 if( 0 == aPropSeq.getLength() )
110 // get properties
111 ::std::vector< ::com::sun::star::beans::Property > aProperties;
112 lcl_AddPropertiesToVector( aProperties );
114 // and sort them for access via bsearch
115 ::std::sort( aProperties.begin(), aProperties.end(),
116 ::chart::PropertyNameLess() );
118 // transfer result to static Sequence
119 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
122 return aPropSeq;
125 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
127 static ::cppu::OPropertyArrayHelper aArrayHelper(
128 lcl_GetPropertySequence(),
129 /* bSorted = */ sal_True );
131 return aArrayHelper;
134 } // anonymous namespace
136 namespace chart
139 LineChartTypeTemplate::LineChartTypeTemplate(
140 uno::Reference<
141 uno::XComponentContext > const & xContext,
142 const ::rtl::OUString & rServiceName,
143 StackMode eStackMode,
144 bool bSymbols,
145 bool bHasLines /* = true */,
146 sal_Int32 nDim /* = 2 */ ) :
147 ChartTypeTemplate( xContext, rServiceName ),
148 ::property::OPropertySet( m_aMutex ),
149 m_eStackMode( eStackMode ),
150 m_bHasSymbols( bSymbols ),
151 m_bHasLines( bHasLines ),
152 m_nDim( nDim )
154 if( nDim == 3 )
155 m_bHasSymbols = false;
158 LineChartTypeTemplate::~LineChartTypeTemplate()
160 // ____ OPropertySet ____
161 uno::Any LineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
162 throw(beans::UnknownPropertyException)
164 static tPropertyValueMap aStaticDefaults;
166 // /--
167 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
168 if( 0 == aStaticDefaults.size() )
170 // initialize defaults
171 lcl_AddDefaultsToMap( aStaticDefaults );
174 tPropertyValueMap::const_iterator aFound(
175 aStaticDefaults.find( nHandle ));
177 if( aFound == aStaticDefaults.end())
178 return uno::Any();
180 return (*aFound).second;
181 // \--
184 ::cppu::IPropertyArrayHelper & SAL_CALL LineChartTypeTemplate::getInfoHelper()
186 return lcl_getInfoHelper();
190 // ____ XPropertySet ____
191 uno::Reference< beans::XPropertySetInfo > SAL_CALL
192 LineChartTypeTemplate::getPropertySetInfo()
193 throw (uno::RuntimeException)
195 static uno::Reference< beans::XPropertySetInfo > xInfo;
197 // /--
198 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
199 if( !xInfo.is())
201 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
202 getInfoHelper());
205 return xInfo;
206 // \--
209 sal_Int32 LineChartTypeTemplate::getDimension() const
211 return m_nDim;
214 StackMode LineChartTypeTemplate::getStackMode( sal_Int32 /* nChartTypeIndex */ ) const
216 return m_eStackMode;
219 // ____ XChartTypeTemplate ____
220 sal_Bool SAL_CALL LineChartTypeTemplate::matchesTemplate(
221 const uno::Reference< chart2::XDiagram >& xDiagram,
222 sal_Bool bAdaptProperties )
223 throw (uno::RuntimeException)
225 sal_Bool bResult = ChartTypeTemplate::matchesTemplate( xDiagram, bAdaptProperties );
227 // check symbol-style and line-style
228 // for a template with symbols (or with lines) it is ok, if there is at least one series
229 // with symbols (or with lines)
230 if( bResult )
232 bool bSymbolFound = false;
233 bool bLineFound = false;
235 ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
236 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
238 for( ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aIt =
239 aSeriesVec.begin(); aIt != aSeriesVec.end(); ++aIt )
243 chart2::Symbol aSymbProp;
244 drawing::LineStyle eLineStyle;
245 Reference< beans::XPropertySet > xProp( *aIt, uno::UNO_QUERY_THROW );
247 bool bCurrentHasSymbol = (xProp->getPropertyValue( C2U( "Symbol" )) >>= aSymbProp) &&
248 (aSymbProp.Style != chart2::SymbolStyle_NONE);
250 if( bCurrentHasSymbol )
251 bSymbolFound = true;
253 if( bCurrentHasSymbol && (!m_bHasSymbols) )
255 bResult = false;
256 break;
259 bool bCurrentHasLine = (xProp->getPropertyValue( C2U( "LineStyle" )) >>= eLineStyle) &&
260 ( eLineStyle != drawing::LineStyle_NONE );
262 if( bCurrentHasLine )
263 bLineFound = true;
265 if( bCurrentHasLine && (!m_bHasLines) )
267 bResult = false;
268 break;
271 catch( uno::Exception & ex )
273 ASSERT_EXCEPTION( ex );
277 if(bResult)
279 if( !bLineFound && m_bHasLines && bSymbolFound )
280 bResult = false;
281 else if( !bSymbolFound && m_bHasSymbols && bLineFound )
282 bResult = false;
283 else if( !bLineFound && !bSymbolFound )
284 return m_bHasLines && m_bHasSymbols;
288 // adapt curve style, spline order and resolution
289 if( bResult && bAdaptProperties )
293 uno::Reference< beans::XPropertySet > xChartTypeProp(
294 DiagramHelper::getChartTypeByIndex( xDiagram, 0 ),
295 uno::UNO_QUERY_THROW );
296 setFastPropertyValue_NoBroadcast( PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE, xChartTypeProp->getPropertyValue(C2U("CurveStyle" )) );
297 setFastPropertyValue_NoBroadcast( PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION, xChartTypeProp->getPropertyValue(C2U("CurveResolution" )) );
298 setFastPropertyValue_NoBroadcast( PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER, xChartTypeProp->getPropertyValue(C2U("SplineOrder" )) );
300 catch( uno::Exception & ex )
302 ASSERT_EXCEPTION( ex );
306 return bResult;
309 Reference< chart2::XChartType > LineChartTypeTemplate::getChartTypeForIndex( sal_Int32 /*nChartTypeIndex*/ )
311 Reference< chart2::XChartType > xResult;
315 Reference< lang::XMultiServiceFactory > xFact(
316 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
317 xResult.set( xFact->createInstance(
318 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
320 Reference< beans::XPropertySet > xCTProp( xResult, uno::UNO_QUERY );
321 if( xCTProp.is())
323 xCTProp->setPropertyValue(
324 C2U( "CurveStyle" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE ));
325 xCTProp->setPropertyValue(
326 C2U( "CurveResolution" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION ));
327 xCTProp->setPropertyValue(
328 C2U( "SplineOrder" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER ));
331 catch( uno::Exception & ex )
333 ASSERT_EXCEPTION( ex );
336 return xResult;
339 Reference< chart2::XChartType > SAL_CALL LineChartTypeTemplate::getChartTypeForNewSeries(
340 const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
341 throw (uno::RuntimeException)
343 Reference< chart2::XChartType > xResult;
347 Reference< lang::XMultiServiceFactory > xFact(
348 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
349 xResult.set( xFact->createInstance(
350 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
352 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
354 Reference< beans::XPropertySet > xCTProp( xResult, uno::UNO_QUERY );
355 if( xCTProp.is())
357 xCTProp->setPropertyValue(
358 C2U( "CurveStyle" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_STYLE ));
359 xCTProp->setPropertyValue(
360 C2U( "CurveResolution" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_CURVE_RESOLUTION ));
361 xCTProp->setPropertyValue(
362 C2U( "SplineOrder" ), getFastPropertyValue( PROP_LINECHARTTYPE_TEMPLATE_SPLINE_ORDER ));
365 catch( uno::Exception & ex )
367 ASSERT_EXCEPTION( ex );
370 return xResult;
373 void SAL_CALL LineChartTypeTemplate::applyStyle(
374 const Reference< chart2::XDataSeries >& xSeries,
375 ::sal_Int32 nChartTypeIndex,
376 ::sal_Int32 nSeriesIndex,
377 ::sal_Int32 nSeriesCount )
378 throw (uno::RuntimeException)
380 ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
384 Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY_THROW );
386 DataSeriesHelper::switchSymbolsOnOrOff( xProp, m_bHasSymbols, nSeriesIndex );
387 DataSeriesHelper::switchLinesOnOrOff( xProp, m_bHasLines );
388 DataSeriesHelper::makeLinesThickOrThin( xProp, m_nDim==2 );
390 catch( uno::Exception & ex )
392 ASSERT_EXCEPTION( ex );
396 // ----------------------------------------
398 Sequence< OUString > LineChartTypeTemplate::getSupportedServiceNames_Static()
400 Sequence< OUString > aServices( 2 );
401 aServices[ 0 ] = lcl_aServiceName;
402 aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
403 return aServices;
406 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
407 APPHELPER_XSERVICEINFO_IMPL( LineChartTypeTemplate, lcl_aServiceName );
409 IMPLEMENT_FORWARD_XINTERFACE2( LineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
410 IMPLEMENT_FORWARD_XTYPEPROVIDER2( LineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
412 } // namespace chart