xref: /aoo42x/main/chart2/source/model/main/Legend.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "Legend.hxx"
31 #include "macros.hxx"
32 #include "LineProperties.hxx"
33 #include "FillProperties.hxx"
34 #include "CharacterProperties.hxx"
35 #include "UserDefinedProperties.hxx"
36 #include "LegendHelper.hxx"
37 #include "ContainerHelper.hxx"
38 #include "CloneHelper.hxx"
39 #include "PropertyHelper.hxx"
40 #include <com/sun/star/beans/PropertyAttribute.hpp>
41 #include <com/sun/star/awt/Size.hpp>
42 #include <com/sun/star/chart2/LegendPosition.hpp>
43 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
44 #include <com/sun/star/chart2/RelativePosition.hpp>
45 #include <com/sun/star/chart2/RelativeSize.hpp>
46 
47 #include <algorithm>
48 
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::beans::PropertyAttribute;
51 
52 using ::rtl::OUString;
53 using ::com::sun::star::uno::Sequence;
54 using ::com::sun::star::uno::Reference;
55 using ::com::sun::star::uno::Any;
56 using ::com::sun::star::beans::Property;
57 
58 namespace
59 {
60 
61 static const OUString lcl_aServiceName(
62     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.Legend" ));
63 
64 enum
65 {
66     PROP_LEGEND_ANCHOR_POSITION,
67     PROP_LEGEND_EXPANSION,
68     PROP_LEGEND_SHOW,
69     PROP_LEGEND_REF_PAGE_SIZE,
70     PROP_LEGEND_REL_POS,
71     PROP_LEGEND_REL_SIZE
72 };
73 
74 void lcl_AddPropertiesToVector(
75     ::std::vector< Property > & rOutProperties )
76 {
77     rOutProperties.push_back(
78         Property( C2U( "AnchorPosition" ),
79                   PROP_LEGEND_ANCHOR_POSITION,
80                   ::getCppuType( reinterpret_cast< const chart2::LegendPosition * >(0)),
81                   beans::PropertyAttribute::BOUND
82                   | beans::PropertyAttribute::MAYBEDEFAULT ));
83 
84     rOutProperties.push_back(
85         Property( C2U( "Expansion" ),
86                   PROP_LEGEND_EXPANSION,
87                   ::getCppuType( reinterpret_cast< const ::com::sun::star::chart::ChartLegendExpansion * >(0)),
88                   beans::PropertyAttribute::BOUND
89                   | beans::PropertyAttribute::MAYBEDEFAULT ));
90 
91     rOutProperties.push_back(
92         Property( C2U( "Show" ),
93                   PROP_LEGEND_SHOW,
94                   ::getBooleanCppuType(),
95                   beans::PropertyAttribute::BOUND
96                   | beans::PropertyAttribute::MAYBEDEFAULT ));
97     rOutProperties.push_back(
98         Property( C2U( "ReferencePageSize" ),
99                   PROP_LEGEND_REF_PAGE_SIZE,
100                   ::getCppuType( reinterpret_cast< const awt::Size * >(0)),
101                   beans::PropertyAttribute::BOUND
102                   | beans::PropertyAttribute::MAYBEVOID ));
103 
104     rOutProperties.push_back(
105         Property( C2U( "RelativePosition" ),
106                   PROP_LEGEND_REL_POS,
107                   ::getCppuType( reinterpret_cast< const chart2::RelativePosition * >(0)),
108                   beans::PropertyAttribute::BOUND
109                   | beans::PropertyAttribute::MAYBEVOID ));
110 
111     rOutProperties.push_back(
112         Property( C2U( "RelativeSize" ),
113                   PROP_LEGEND_REL_SIZE,
114                   ::getCppuType( reinterpret_cast< const chart2::RelativeSize * >(0)),
115                   beans::PropertyAttribute::BOUND
116                   | beans::PropertyAttribute::MAYBEVOID ));
117 
118 }
119 
120 struct StaticLegendDefaults_Initializer
121 {
122     ::chart::tPropertyValueMap* operator()()
123     {
124         static ::chart::tPropertyValueMap aStaticDefaults;
125         lcl_AddDefaultsToMap( aStaticDefaults );
126         return &aStaticDefaults;
127     }
128 private:
129     void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
130     {
131         ::chart::LineProperties::AddDefaultsToMap( rOutMap );
132         ::chart::FillProperties::AddDefaultsToMap( rOutMap );
133         ::chart::CharacterProperties::AddDefaultsToMap( rOutMap );
134 
135         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_ANCHOR_POSITION, chart2::LegendPosition_LINE_END );
136         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_EXPANSION, ::com::sun::star::chart::ChartLegendExpansion_HIGH );
137         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_SHOW, true );
138 
139         float fDefaultCharHeight = 10.0;
140         ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_CHAR_HEIGHT, fDefaultCharHeight );
141         ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_ASIAN_CHAR_HEIGHT, fDefaultCharHeight );
142         ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_COMPLEX_CHAR_HEIGHT, fDefaultCharHeight );
143     }
144 };
145 
146 struct StaticLegendDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticLegendDefaults_Initializer >
147 {
148 };
149 
150 struct StaticLegendInfoHelper_Initializer
151 {
152     ::cppu::OPropertyArrayHelper* operator()()
153     {
154         static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
155         return &aPropHelper;
156     }
157 
158 private:
159     Sequence< Property > lcl_GetPropertySequence()
160     {
161         ::std::vector< ::com::sun::star::beans::Property > aProperties;
162         lcl_AddPropertiesToVector( aProperties );
163         ::chart::LineProperties::AddPropertiesToVector( aProperties );
164         ::chart::FillProperties::AddPropertiesToVector( aProperties );
165         ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
166         ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
167 
168         ::std::sort( aProperties.begin(), aProperties.end(),
169                      ::chart::PropertyNameLess() );
170 
171         return ::chart::ContainerHelper::ContainerToSequence( aProperties );
172     }
173 };
174 
175 struct StaticLegendInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticLegendInfoHelper_Initializer >
176 {
177 };
178 
179 struct StaticLegendInfo_Initializer
180 {
181     uno::Reference< beans::XPropertySetInfo >* operator()()
182     {
183         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
184             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticLegendInfoHelper::get() ) );
185         return &xPropertySetInfo;
186     }
187 };
188 
189 struct StaticLegendInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticLegendInfo_Initializer >
190 {
191 };
192 
193 } // anonymous namespace
194 
195 namespace chart
196 {
197 
198 Legend::Legend( Reference< uno::XComponentContext > const & /* xContext */ ) :
199         ::property::OPropertySet( m_aMutex ),
200         m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
201 {
202 }
203 
204 Legend::Legend( const Legend & rOther ) :
205         MutexContainer(),
206         impl::Legend_Base(),
207         ::property::OPropertySet( rOther, m_aMutex ),
208     m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
209 {
210 }
211 
212 Legend::~Legend()
213 {
214 }
215 
216 // ____ XCloneable ____
217 Reference< util::XCloneable > SAL_CALL Legend::createClone()
218     throw (uno::RuntimeException)
219 {
220     return Reference< util::XCloneable >( new Legend( *this ));
221 }
222 
223 // ____ XModifyBroadcaster ____
224 void SAL_CALL Legend::addModifyListener( const Reference< util::XModifyListener >& aListener )
225     throw (uno::RuntimeException)
226 {
227     try
228     {
229         Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
230         xBroadcaster->addModifyListener( aListener );
231     }
232     catch( const uno::Exception & ex )
233     {
234         ASSERT_EXCEPTION( ex );
235     }
236 }
237 
238 void SAL_CALL Legend::removeModifyListener( const Reference< util::XModifyListener >& aListener )
239     throw (uno::RuntimeException)
240 {
241     try
242     {
243         Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
244         xBroadcaster->removeModifyListener( aListener );
245     }
246     catch( const uno::Exception & ex )
247     {
248         ASSERT_EXCEPTION( ex );
249     }
250 }
251 
252 // ____ XModifyListener ____
253 void SAL_CALL Legend::modified( const lang::EventObject& aEvent )
254     throw (uno::RuntimeException)
255 {
256     m_xModifyEventForwarder->modified( aEvent );
257 }
258 
259 // ____ XEventListener (base of XModifyListener) ____
260 void SAL_CALL Legend::disposing( const lang::EventObject& /* Source */ )
261     throw (uno::RuntimeException)
262 {
263     // nothing
264 }
265 
266 // ____ OPropertySet ____
267 void Legend::firePropertyChangeEvent()
268 {
269     fireModifyEvent();
270 }
271 
272 void Legend::fireModifyEvent()
273 {
274     m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
275 }
276 
277 // ================================================================================
278 
279 Sequence< OUString > Legend::getSupportedServiceNames_Static()
280 {
281     const sal_Int32 nNumServices( 6 );
282     sal_Int32 nI = 0;
283     Sequence< OUString > aServices( nNumServices );
284     aServices[ nI++ ] = C2U( "com.sun.star.chart2.Legend" );
285     aServices[ nI++ ] = C2U( "com.sun.star.beans.PropertySet" );
286     aServices[ nI++ ] = C2U( "com.sun.star.drawing.FillProperties" );
287     aServices[ nI++ ] = C2U( "com.sun.star.drawing.LineProperties" );
288     aServices[ nI++ ] = C2U( "com.sun.star.style.CharacterProperties" );
289     aServices[ nI++ ] = C2U( "com.sun.star.layout.LayoutElement" );
290     OSL_ASSERT( nNumServices == nI );
291     return aServices;
292 }
293 
294 // ____ OPropertySet ____
295 Any Legend::GetDefaultValue( sal_Int32 nHandle ) const
296     throw(beans::UnknownPropertyException)
297 {
298     const tPropertyValueMap& rStaticDefaults = *StaticLegendDefaults::get();
299     tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
300     if( aFound == rStaticDefaults.end() )
301         return uno::Any();
302     return (*aFound).second;
303 }
304 
305 ::cppu::IPropertyArrayHelper & SAL_CALL Legend::getInfoHelper()
306 {
307     return *StaticLegendInfoHelper::get();
308 }
309 
310 // ____ XPropertySet ____
311 Reference< beans::XPropertySetInfo > SAL_CALL Legend::getPropertySetInfo()
312     throw (uno::RuntimeException)
313 {
314     return *StaticLegendInfo::get();
315 }
316 
317 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
318 APPHELPER_XSERVICEINFO_IMPL( Legend, lcl_aServiceName );
319 
320 // needed by MSC compiler
321 using impl::Legend_Base;
322 
323 IMPLEMENT_FORWARD_XINTERFACE2( Legend, Legend_Base, ::property::OPropertySet )
324 IMPLEMENT_FORWARD_XTYPEPROVIDER2( Legend, Legend_Base, ::property::OPropertySet )
325 
326 } //  namespace chart
327