xref: /trunk/main/chart2/source/model/template/ChartType.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_chartmodel.hxx"
26 #include "ChartType.hxx"
27 #include "PropertyHelper.hxx"
28 #include "CommonFunctors.hxx"
29 #include "macros.hxx"
30 #include "CartesianCoordinateSystem.hxx"
31 #include "AxisHelper.hxx"
32 #include "CloneHelper.hxx"
33 #include "AxisIndexDefines.hxx"
34 #include "ContainerHelper.hxx"
35 #include <com/sun/star/chart2/AxisType.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 
38 using namespace ::com::sun::star;
39 
40 using ::rtl::OUString;
41 using ::com::sun::star::beans::Property;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::uno::Any;
45 using ::osl::MutexGuard;
46 
47 namespace chart
48 {
49 
ChartType(const Reference<uno::XComponentContext> & xContext)50 ChartType::ChartType(
51     const Reference< uno::XComponentContext > & xContext ) :
52         ::property::OPropertySet( m_aMutex ),
53         m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()),
54         m_xContext( xContext ),
55         m_bNotifyChanges( true )
56 {}
57 
ChartType(const ChartType & rOther)58 ChartType::ChartType( const ChartType & rOther ) :
59         MutexContainer(),
60         impl::ChartType_Base(),
61         ::property::OPropertySet( rOther, m_aMutex ),
62     m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()),
63     m_xContext( rOther.m_xContext ),
64     m_bNotifyChanges( true )
65 {
66     CloneHelper::CloneRefVector< Reference< chart2::XDataSeries > >( rOther.m_aDataSeries, m_aDataSeries );
67     ModifyListenerHelper::addListenerToAllElements( m_aDataSeries, m_xModifyEventForwarder );
68 }
69 
~ChartType()70 ChartType::~ChartType()
71 {
72     ModifyListenerHelper::removeListenerFromAllElements( m_aDataSeries, m_xModifyEventForwarder );
73     m_aDataSeries.clear();
74 }
75 
GetComponentContext() const76 Reference< uno::XComponentContext > ChartType::GetComponentContext() const
77 {
78     return m_xContext;
79 }
80 
81 // ____ XChartType ____
82 Reference< chart2::XCoordinateSystem > SAL_CALL
createCoordinateSystem(::sal_Int32 DimensionCount)83     ChartType::createCoordinateSystem( ::sal_Int32 DimensionCount )
84 {
85     Reference< chart2::XCoordinateSystem > xResult(
86         new CartesianCoordinateSystem(
87             GetComponentContext(), DimensionCount, /* bSwapXAndYAxis */ sal_False ));
88 
89     for( sal_Int32 i=0; i<DimensionCount; ++i )
90     {
91         Reference< chart2::XAxis > xAxis( xResult->getAxisByDimension( i, MAIN_AXIS_INDEX ) );
92         if( !xAxis.is() )
93         {
94             OSL_ENSURE(false,"a created coordinate system should have an axis for each dimension");
95             continue;
96         }
97 
98         chart2::ScaleData aScaleData = xAxis->getScaleData();
99         aScaleData.Orientation = chart2::AxisOrientation_MATHEMATICAL;
100         aScaleData.Scaling = AxisHelper::createLinearScaling();
101 
102         switch( i )
103         {
104             case 0: aScaleData.AxisType = chart2::AxisType::CATEGORY; break;
105             case 2: aScaleData.AxisType = chart2::AxisType::SERIES; break;
106             default: aScaleData.AxisType = chart2::AxisType::REALNUMBER; break;
107         }
108 
109         xAxis->setScaleData( aScaleData );
110     }
111 
112     return xResult;
113 }
114 
getSupportedMandatoryRoles()115 Sequence< OUString > SAL_CALL ChartType::getSupportedMandatoryRoles()
116 {
117     static Sequence< OUString > aDefaultSeq;
118 
119     if( aDefaultSeq.getLength() == 0 )
120     {
121         aDefaultSeq.realloc( 2 );
122         aDefaultSeq[0] = C2U( "label" );
123         aDefaultSeq[1] = C2U( "values-y" );
124     }
125 
126     return aDefaultSeq;
127 }
128 
getSupportedOptionalRoles()129 Sequence< OUString > SAL_CALL ChartType::getSupportedOptionalRoles()
130 {
131     static Sequence< OUString > aDefaultOptRolesSeq;
132 
133 //     if( aDefaultOptRolesSeq.getLength() == 0 )
134 //     {
135 //         aDefaultOptRolesSeq.realloc( 1 );
136 //         aDefaultOptRolesSeq[0] = C2U( "error-bars-y" );
137 //     }
138 
139     return aDefaultOptRolesSeq;
140 }
141 
getRoleOfSequenceForSeriesLabel()142 OUString SAL_CALL ChartType::getRoleOfSequenceForSeriesLabel()
143 {
144     return C2U( "values-y" );
145 }
146 
impl_addDataSeriesWithoutNotification(const Reference<chart2::XDataSeries> & xDataSeries)147 void ChartType::impl_addDataSeriesWithoutNotification(
148         const Reference< chart2::XDataSeries >& xDataSeries )
149 {
150     if( ::std::find( m_aDataSeries.begin(), m_aDataSeries.end(), xDataSeries )
151         != m_aDataSeries.end())
152         throw lang::IllegalArgumentException();
153 
154     m_aDataSeries.push_back( xDataSeries );
155     ModifyListenerHelper::addListener( xDataSeries, m_xModifyEventForwarder );
156 }
157 
158 // ____ XDataSeriesContainer ____
addDataSeries(const Reference<chart2::XDataSeries> & xDataSeries)159 void SAL_CALL ChartType::addDataSeries( const Reference< chart2::XDataSeries >& xDataSeries )
160 {
161     impl_addDataSeriesWithoutNotification( xDataSeries );
162     fireModifyEvent();
163 }
164 
removeDataSeries(const Reference<chart2::XDataSeries> & xDataSeries)165 void SAL_CALL ChartType::removeDataSeries( const Reference< chart2::XDataSeries >& xDataSeries )
166 {
167     if( !xDataSeries.is())
168         throw container::NoSuchElementException();
169 
170     tDataSeriesContainerType::iterator aIt(
171             ::std::find( m_aDataSeries.begin(), m_aDataSeries.end(), xDataSeries ) );
172 
173     if( aIt == m_aDataSeries.end())
174         throw container::NoSuchElementException(
175             C2U( "The given series is no element of this charttype" ),
176             static_cast< uno::XWeak * >( this ));
177 
178     ModifyListenerHelper::removeListener( xDataSeries, m_xModifyEventForwarder );
179     m_aDataSeries.erase( aIt );
180     fireModifyEvent();
181 }
182 
getDataSeries()183 Sequence< Reference< chart2::XDataSeries > > SAL_CALL ChartType::getDataSeries()
184 {
185     return ContainerHelper::ContainerToSequence( m_aDataSeries );
186 }
187 
setDataSeries(const Sequence<Reference<chart2::XDataSeries>> & aDataSeries)188 void SAL_CALL ChartType::setDataSeries( const Sequence< Reference< chart2::XDataSeries > >& aDataSeries )
189 {
190     m_bNotifyChanges = false;
191     try
192     {
193         Sequence< Reference< chart2::XDataSeries > > aOldSeries( this->getDataSeries() );
194         for( sal_Int32 nN=0; nN<aOldSeries.getLength(); ++nN )
195             ModifyListenerHelper::removeListener( aOldSeries[nN], m_xModifyEventForwarder );
196         m_aDataSeries.clear();
197 
198         for( sal_Int32 i=0; i<aDataSeries.getLength(); ++i )
199             impl_addDataSeriesWithoutNotification( aDataSeries[i] );
200     }
201     catch( ... )
202     {
203         m_bNotifyChanges = true;
204         throw;
205     }
206     m_bNotifyChanges = true;
207     fireModifyEvent();
208 }
209 
210 // ____ OPropertySet ____
GetDefaultValue(sal_Int32) const211 uno::Any ChartType::GetDefaultValue( sal_Int32 /* nHandle */ ) const
212 {
213     return uno::Any();
214 }
215 
216 namespace
217 {
218 
219 struct StaticChartTypeInfoHelper_Initializer
220 {
operator ()chart::__anonac5e4b690111::StaticChartTypeInfoHelper_Initializer221     ::cppu::OPropertyArrayHelper* operator()()
222     {
223         // using assignment for broken gcc 3.3
224         static ::cppu::OPropertyArrayHelper aPropHelper = ::cppu::OPropertyArrayHelper(
225             Sequence< beans::Property >() );
226         return &aPropHelper;
227     }
228 };
229 
230 struct StaticChartTypeInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticChartTypeInfoHelper_Initializer >
231 {
232 };
233 
234 struct StaticChartTypeInfo_Initializer
235 {
operator ()chart::__anonac5e4b690111::StaticChartTypeInfo_Initializer236     uno::Reference< beans::XPropertySetInfo >* operator()()
237     {
238         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
239             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticChartTypeInfoHelper::get() ) );
240         return &xPropertySetInfo;
241     }
242 };
243 
244 struct StaticChartTypeInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticChartTypeInfo_Initializer >
245 {
246 };
247 
248 }
249 
250 // ____ OPropertySet ____
getInfoHelper()251 ::cppu::IPropertyArrayHelper & SAL_CALL ChartType::getInfoHelper()
252 {
253     return *StaticChartTypeInfoHelper::get();
254 }
255 
256 // ____ XPropertySet ____
getPropertySetInfo()257 uno::Reference< beans::XPropertySetInfo > SAL_CALL ChartType::getPropertySetInfo()
258 {
259     return *StaticChartTypeInfo::get();
260 }
261 
262 // ____ XModifyBroadcaster ____
addModifyListener(const uno::Reference<util::XModifyListener> & aListener)263 void SAL_CALL ChartType::addModifyListener( const uno::Reference< util::XModifyListener >& aListener )
264 {
265     try
266     {
267         uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
268         xBroadcaster->addModifyListener( aListener );
269     }
270     catch( const uno::Exception & ex )
271     {
272         ASSERT_EXCEPTION( ex );
273     }
274 }
275 
removeModifyListener(const uno::Reference<util::XModifyListener> & aListener)276 void SAL_CALL ChartType::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener )
277 {
278     try
279     {
280         uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
281         xBroadcaster->removeModifyListener( aListener );
282     }
283     catch( const uno::Exception & ex )
284     {
285         ASSERT_EXCEPTION( ex );
286     }
287 }
288 
289 // ____ XModifyListener ____
modified(const lang::EventObject & aEvent)290 void SAL_CALL ChartType::modified( const lang::EventObject& aEvent )
291 {
292     m_xModifyEventForwarder->modified( aEvent );
293 }
294 
295 // ____ XEventListener (base of XModifyListener) ____
disposing(const lang::EventObject &)296 void SAL_CALL ChartType::disposing( const lang::EventObject& /* Source */ )
297 {
298     // nothing
299 }
300 
301 // ____ OPropertySet ____
firePropertyChangeEvent()302 void ChartType::firePropertyChangeEvent()
303 {
304     fireModifyEvent();
305 }
306 
fireModifyEvent()307 void ChartType::fireModifyEvent()
308 {
309     if( m_bNotifyChanges )
310         m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
311 }
312 
313 // ================================================================================
314 
315 using impl::ChartType_Base;
316 
317 IMPLEMENT_FORWARD_XINTERFACE2( ChartType, ChartType_Base, ::property::OPropertySet )
318 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ChartType, ChartType_Base, ::property::OPropertySet )
319 
320 } //  namespace chart
321