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 "BarChartTypeTemplate.hxx"
27 #include "macros.hxx"
28 #include "DiagramHelper.hxx"
29 #include "servicenames_charttypes.hxx"
30 #include "ContainerHelper.hxx"
31 #include "DataSeriesHelper.hxx"
32 #include "PropertyHelper.hxx"
33 #include <com/sun/star/beans/PropertyAttribute.hpp>
34 #include <com/sun/star/drawing/LineStyle.hpp>
35 #include <com/sun/star/chart2/DataPointGeometry3D.hpp>
36 
37 #include <algorithm>
38 
39 using namespace ::com::sun::star;
40 
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::beans::Property;
44 using ::osl::MutexGuard;
45 using ::rtl::OUString;
46 
47 namespace
48 {
49 
50 static const OUString lcl_aServiceName(
51     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.BarChartTypeTemplate" ));
52 
53 enum
54 {
55     PROP_BAR_TEMPLATE_DIMENSION,
56     PROP_BAR_TEMPLATE_GEOMETRY3D
57 };
58 
lcl_AddPropertiesToVector(::std::vector<Property> & rOutProperties)59 void lcl_AddPropertiesToVector(
60     ::std::vector< Property > & rOutProperties )
61 {
62     rOutProperties.push_back(
63         Property( C2U( "Dimension" ),
64                   PROP_BAR_TEMPLATE_DIMENSION,
65                   ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
66                   beans::PropertyAttribute::BOUND
67                   | beans::PropertyAttribute::MAYBEDEFAULT ));
68     rOutProperties.push_back(
69         Property( C2U( "Geometry3D" ),
70                   PROP_BAR_TEMPLATE_GEOMETRY3D,
71                   ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
72                   beans::PropertyAttribute::BOUND
73                   | beans::PropertyAttribute::MAYBEDEFAULT ));
74 }
75 
76 struct StaticBarChartTypeTemplateDefaults_Initializer
77 {
operator ()__anonaf1892d20111::StaticBarChartTypeTemplateDefaults_Initializer78     ::chart::tPropertyValueMap* operator()()
79     {
80         static ::chart::tPropertyValueMap aStaticDefaults;
81         lcl_AddDefaultsToMap( aStaticDefaults );
82         return &aStaticDefaults;
83     }
84 private:
lcl_AddDefaultsToMap__anonaf1892d20111::StaticBarChartTypeTemplateDefaults_Initializer85     void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
86     {
87         ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_BAR_TEMPLATE_DIMENSION, 2 );
88         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_BAR_TEMPLATE_GEOMETRY3D, ::chart2::DataPointGeometry3D::CUBOID );
89     }
90 };
91 
92 struct StaticBarChartTypeTemplateDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticBarChartTypeTemplateDefaults_Initializer >
93 {
94 };
95 
96 struct StaticBarChartTypeTemplateInfoHelper_Initializer
97 {
operator ()__anonaf1892d20111::StaticBarChartTypeTemplateInfoHelper_Initializer98     ::cppu::OPropertyArrayHelper* operator()()
99     {
100         static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
101         return &aPropHelper;
102     }
103 
104 private:
lcl_GetPropertySequence__anonaf1892d20111::StaticBarChartTypeTemplateInfoHelper_Initializer105     Sequence< Property > lcl_GetPropertySequence()
106     {
107         ::std::vector< ::com::sun::star::beans::Property > aProperties;
108         lcl_AddPropertiesToVector( aProperties );
109 
110         ::std::sort( aProperties.begin(), aProperties.end(),
111                      ::chart::PropertyNameLess() );
112 
113         return ::chart::ContainerHelper::ContainerToSequence( aProperties );
114     }
115 
116 };
117 
118 struct StaticBarChartTypeTemplateInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticBarChartTypeTemplateInfoHelper_Initializer >
119 {
120 };
121 
122 struct StaticBarChartTypeTemplateInfo_Initializer
123 {
operator ()__anonaf1892d20111::StaticBarChartTypeTemplateInfo_Initializer124     uno::Reference< beans::XPropertySetInfo >* operator()()
125     {
126         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
127             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticBarChartTypeTemplateInfoHelper::get() ) );
128         return &xPropertySetInfo;
129     }
130 };
131 
132 struct StaticBarChartTypeTemplateInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticBarChartTypeTemplateInfo_Initializer >
133 {
134 };
135 
136 } // anonymous namespace
137 
138 namespace chart
139 {
140 
BarChartTypeTemplate(Reference<uno::XComponentContext> const & xContext,const OUString & rServiceName,StackMode eStackMode,BarDirection eDirection,sal_Int32 nDim)141 BarChartTypeTemplate::BarChartTypeTemplate(
142     Reference<
143         uno::XComponentContext > const & xContext,
144     const OUString & rServiceName,
145     StackMode eStackMode,
146     BarDirection eDirection,
147     sal_Int32 nDim         /* = 2 */ ) :
148         ChartTypeTemplate( xContext, rServiceName ),
149         ::property::OPropertySet( m_aMutex ),
150         m_eStackMode( eStackMode ),
151         m_eBarDirection( eDirection ),
152         m_nDim( nDim )
153 {}
154 
~BarChartTypeTemplate()155 BarChartTypeTemplate::~BarChartTypeTemplate()
156 {}
157 
getDimension() const158 sal_Int32 BarChartTypeTemplate::getDimension() const
159 {
160     return m_nDim;
161 }
162 
getStackMode(sal_Int32) const163 StackMode BarChartTypeTemplate::getStackMode( sal_Int32 /* nChartTypeIndex */ ) const
164 {
165     return m_eStackMode;
166 }
167 
isSwapXAndY() const168 bool BarChartTypeTemplate::isSwapXAndY() const
169 {
170     return (m_eBarDirection == HORIZONTAL);
171 }
172 
173 // ____ XChartTypeTemplate ____
matchesTemplate(const Reference<chart2::XDiagram> & xDiagram,sal_Bool bAdaptProperties)174 sal_Bool SAL_CALL BarChartTypeTemplate::matchesTemplate(
175     const Reference< chart2::XDiagram >& xDiagram,
176     sal_Bool bAdaptProperties )
177     throw (uno::RuntimeException)
178 {
179     sal_Bool bResult = ChartTypeTemplate::matchesTemplate( xDiagram, bAdaptProperties );
180 
181     //check BarDirection
182     if( bResult )
183     {
184         bool bFound = false;
185         bool bAmbiguous = false;
186         bool bVertical = DiagramHelper::getVertical( xDiagram, bFound, bAmbiguous );
187         if( m_eBarDirection == HORIZONTAL )
188             bResult = sal_Bool( bVertical );
189         else if( m_eBarDirection == VERTICAL )
190             bResult = sal_Bool( !bVertical );
191     }
192 
193     // adapt solid-type of template according to values in series
194     if( bAdaptProperties &&
195         bResult &&
196         getDimension() == 3 )
197     {
198         ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
199             DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
200 
201         bool bGeomFound = false, bGeomAmbiguous = false;
202         sal_Int32 aCommonGeom = DiagramHelper::getGeometry3D( xDiagram, bGeomFound, bGeomAmbiguous );
203 
204         if( !bGeomAmbiguous )
205         {
206             setFastPropertyValue_NoBroadcast(
207                 PROP_BAR_TEMPLATE_GEOMETRY3D, uno::makeAny( aCommonGeom ));
208         }
209     }
210 
211     return bResult;
212 }
getChartTypeForIndex(sal_Int32)213 Reference< chart2::XChartType > BarChartTypeTemplate::getChartTypeForIndex( sal_Int32 /*nChartTypeIndex*/ )
214 {
215     Reference< chart2::XChartType > xResult;
216 
217     try
218     {
219         Reference< lang::XMultiServiceFactory > xFact(
220             GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
221         xResult.set( xFact->createInstance(
222                          CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
223     }
224     catch( uno::Exception & ex )
225     {
226         ASSERT_EXCEPTION( ex );
227     }
228 
229     return xResult;
230 }
231 
getChartTypeForNewSeries(const uno::Sequence<Reference<chart2::XChartType>> & aFormerlyUsedChartTypes)232 Reference< chart2::XChartType > SAL_CALL BarChartTypeTemplate::getChartTypeForNewSeries(
233         const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
234     throw (uno::RuntimeException)
235 {
236     Reference< chart2::XChartType > xResult( getChartTypeForIndex( 0 ) );
237     ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
238     return xResult;
239 }
240 
241 
242 // ____ OPropertySet ____
GetDefaultValue(sal_Int32 nHandle) const243 uno::Any BarChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
244     throw(beans::UnknownPropertyException)
245 {
246     const tPropertyValueMap& rStaticDefaults = *StaticBarChartTypeTemplateDefaults::get();
247     tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
248     if( aFound == rStaticDefaults.end() )
249         return uno::Any();
250     return (*aFound).second;
251 }
252 
getInfoHelper()253 ::cppu::IPropertyArrayHelper & SAL_CALL BarChartTypeTemplate::getInfoHelper()
254 {
255     return *StaticBarChartTypeTemplateInfoHelper::get();
256 }
257 
258 // ____ XPropertySet ____
getPropertySetInfo()259 Reference< beans::XPropertySetInfo > SAL_CALL BarChartTypeTemplate::getPropertySetInfo()
260     throw (uno::RuntimeException)
261 {
262     return *StaticBarChartTypeTemplateInfo::get();
263 }
264 
applyStyle(const Reference<chart2::XDataSeries> & xSeries,::sal_Int32 nChartTypeIndex,::sal_Int32 nSeriesIndex,::sal_Int32 nSeriesCount)265 void SAL_CALL BarChartTypeTemplate::applyStyle(
266     const Reference< chart2::XDataSeries >& xSeries,
267     ::sal_Int32 nChartTypeIndex,
268     ::sal_Int32 nSeriesIndex,
269     ::sal_Int32 nSeriesCount )
270     throw (uno::RuntimeException)
271 {
272     ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
273     DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, C2U( "BorderStyle" ), uno::makeAny( drawing::LineStyle_NONE ) );
274     if( getDimension() == 3 )
275     {
276         try
277         {
278             //apply Geometry3D
279             uno::Any aAGeometry3D;
280             this->getFastPropertyValue( aAGeometry3D, PROP_BAR_TEMPLATE_GEOMETRY3D );
281             DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, C2U( "Geometry3D" ), aAGeometry3D );
282         }
283         catch( uno::Exception & ex )
284         {
285             ASSERT_EXCEPTION( ex );
286         }
287     }
288 }
289 
resetStyles(const Reference<chart2::XDiagram> & xDiagram)290 void SAL_CALL BarChartTypeTemplate::resetStyles(
291     const Reference< chart2::XDiagram >& xDiagram )
292     throw (uno::RuntimeException)
293 {
294     ChartTypeTemplate::resetStyles( xDiagram );
295     ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
296         DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
297     uno::Any aLineStyleAny( uno::makeAny( drawing::LineStyle_NONE ));
298     for( ::std::vector< Reference< chart2::XDataSeries > >::iterator aIt( aSeriesVec.begin());
299          aIt != aSeriesVec.end(); ++aIt )
300     {
301         Reference< beans::XPropertyState > xState( *aIt, uno::UNO_QUERY );
302         if( xState.is())
303         {
304             if( getDimension() == 3 )
305                 xState->setPropertyToDefault( C2U("Geometry3D"));
306             Reference< beans::XPropertySet > xProp( xState, uno::UNO_QUERY );
307             if( xProp.is() &&
308                 xProp->getPropertyValue( C2U("BorderStyle")) == aLineStyleAny )
309             {
310                 xState->setPropertyToDefault( C2U("BorderStyle"));
311             }
312         }
313     }
314 
315     DiagramHelper::setVertical( xDiagram, false );
316 }
317 
318 
createCoordinateSystems(const Reference<chart2::XCoordinateSystemContainer> & xCooSysCnt)319 void BarChartTypeTemplate::createCoordinateSystems(
320     const Reference< chart2::XCoordinateSystemContainer > & xCooSysCnt )
321 {
322     ChartTypeTemplate::createCoordinateSystems( xCooSysCnt );
323 
324     Reference< chart2::XDiagram > xDiagram( xCooSysCnt, uno::UNO_QUERY );
325     DiagramHelper::setVertical( xDiagram, m_eBarDirection == HORIZONTAL );
326 }
327 
328 // ----------------------------------------
329 
getSupportedServiceNames_Static()330 Sequence< OUString > BarChartTypeTemplate::getSupportedServiceNames_Static()
331 {
332     Sequence< OUString > aServices( 2 );
333     aServices[ 0 ] = lcl_aServiceName;
334     aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
335     return aServices;
336 }
337 
338 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
339 APPHELPER_XSERVICEINFO_IMPL( BarChartTypeTemplate, lcl_aServiceName );
340 
341 IMPLEMENT_FORWARD_XINTERFACE2( BarChartTypeTemplate, ChartTypeTemplate, OPropertySet )
342 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BarChartTypeTemplate, ChartTypeTemplate, OPropertySet )
343 
344 } //  namespace chart
345