Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "LegendWrapper.hxx"
21 : #include "macros.hxx"
22 : #include "Chart2ModelContact.hxx"
23 : #include "LegendHelper.hxx"
24 : #include "ContainerHelper.hxx"
25 : #include <comphelper/InlineContainer.hxx>
26 : #include <com/sun/star/beans/PropertyAttribute.hpp>
27 : #include <com/sun/star/chart2/XTitled.hpp>
28 : #include <com/sun/star/chart/ChartLegendPosition.hpp>
29 : #include <com/sun/star/chart2/LegendPosition.hpp>
30 : #include <com/sun/star/chart/ChartLegendExpansion.hpp>
31 : #include <com/sun/star/chart2/RelativePosition.hpp>
32 : #include <com/sun/star/drawing/FillStyle.hpp>
33 :
34 : #include "CharacterProperties.hxx"
35 : #include "LinePropertiesHelper.hxx"
36 : #include "FillProperties.hxx"
37 : #include "UserDefinedProperties.hxx"
38 : #include "WrappedCharacterHeightProperty.hxx"
39 : #include "PositionAndSizeHelper.hxx"
40 : #include "WrappedDirectStateProperty.hxx"
41 : #include "WrappedAutomaticPositionProperties.hxx"
42 : #include "WrappedScaleTextProperties.hxx"
43 :
44 : #include <algorithm>
45 : #include <rtl/ustrbuf.hxx>
46 :
47 : using namespace ::com::sun::star;
48 : using ::com::sun::star::beans::Property;
49 : using ::osl::MutexGuard;
50 : using ::com::sun::star::uno::Any;
51 : using ::com::sun::star::uno::Reference;
52 : using ::com::sun::star::uno::Sequence;
53 :
54 : namespace chart
55 : {
56 : class WrappedLegendAlignmentProperty : public WrappedProperty
57 : {
58 : public:
59 : WrappedLegendAlignmentProperty();
60 : virtual ~WrappedLegendAlignmentProperty();
61 :
62 : virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
63 : throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException) SAL_OVERRIDE;
64 : virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
65 : throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException) SAL_OVERRIDE;
66 :
67 : protected:
68 : virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const SAL_OVERRIDE;
69 : virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const SAL_OVERRIDE;
70 : };
71 :
72 0 : WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
73 0 : : ::chart::WrappedProperty( "Alignment", "AnchorPosition" )
74 : {
75 0 : }
76 0 : WrappedLegendAlignmentProperty::~WrappedLegendAlignmentProperty()
77 : {
78 0 : }
79 :
80 0 : Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
81 : throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
82 : {
83 0 : Any aRet;
84 0 : if( xInnerPropertySet.is() )
85 : {
86 0 : sal_Bool bShowLegend = sal_True;
87 0 : xInnerPropertySet->getPropertyValue( "Show" ) >>= bShowLegend;
88 0 : if(!bShowLegend)
89 : {
90 0 : aRet = uno::makeAny( ::com::sun::star::chart::ChartLegendPosition_NONE );
91 : }
92 : else
93 : {
94 0 : aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
95 0 : aRet = this->convertInnerToOuterValue( aRet );
96 : }
97 : }
98 0 : return aRet;
99 : }
100 :
101 0 : void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
102 : throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
103 : {
104 0 : if(xInnerPropertySet.is())
105 : {
106 0 : sal_Bool bNewShowLegend = sal_True;
107 0 : sal_Bool bOldShowLegend = sal_True;
108 : {
109 0 : ::com::sun::star::chart::ChartLegendPosition eOuterPos(::com::sun::star::chart::ChartLegendPosition_NONE);
110 0 : if( (rOuterValue >>= eOuterPos) && eOuterPos == ::com::sun::star::chart::ChartLegendPosition_NONE )
111 0 : bNewShowLegend = sal_False;
112 0 : xInnerPropertySet->getPropertyValue( "Show" ) >>= bOldShowLegend;
113 : }
114 0 : if(bNewShowLegend!=bOldShowLegend)
115 : {
116 0 : xInnerPropertySet->setPropertyValue( "Show", uno::makeAny(bNewShowLegend) );
117 : }
118 0 : if(!bNewShowLegend)
119 0 : return;
120 :
121 : //set corresponding LegendPosition
122 0 : Any aInnerValue = this->convertOuterToInnerValue( rOuterValue );
123 0 : xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
124 :
125 : //correct LegendExpansion
126 0 : chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
127 0 : if( aInnerValue >>= eNewInnerPos )
128 : {
129 : ::com::sun::star::chart::ChartLegendExpansion eNewExpansion =
130 0 : ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
131 0 : eNewInnerPos == chart2::LegendPosition_LINE_START )
132 : ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
133 0 : : ::com::sun::star::chart::ChartLegendExpansion_WIDE;
134 :
135 0 : ::com::sun::star::chart::ChartLegendExpansion eOldExpansion( ::com::sun::star::chart::ChartLegendExpansion_HIGH );
136 : bool bExpansionWasSet(
137 0 : xInnerPropertySet->getPropertyValue( "Expansion" ) >>= eOldExpansion );
138 :
139 0 : if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
140 0 : xInnerPropertySet->setPropertyValue( "Expansion", uno::makeAny( eNewExpansion ));
141 : }
142 :
143 : //correct RelativePosition
144 0 : Any aRelativePosition( xInnerPropertySet->getPropertyValue("RelativePosition") );
145 0 : if(aRelativePosition.hasValue())
146 : {
147 0 : xInnerPropertySet->setPropertyValue( "RelativePosition", Any() );
148 0 : }
149 : }
150 : }
151 :
152 0 : Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
153 : {
154 0 : ::com::sun::star::chart::ChartLegendPosition ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
155 :
156 : chart2::LegendPosition eNewPos;
157 0 : if( rInnerValue >>= eNewPos )
158 : {
159 0 : switch( eNewPos )
160 : {
161 : case chart2::LegendPosition_LINE_START:
162 0 : ePos = ::com::sun::star::chart::ChartLegendPosition_LEFT;
163 0 : break;
164 : case chart2::LegendPosition_LINE_END:
165 0 : ePos = ::com::sun::star::chart::ChartLegendPosition_RIGHT;
166 0 : break;
167 : case chart2::LegendPosition_PAGE_START:
168 0 : ePos = ::com::sun::star::chart::ChartLegendPosition_TOP;
169 0 : break;
170 : case chart2::LegendPosition_PAGE_END:
171 0 : ePos = ::com::sun::star::chart::ChartLegendPosition_BOTTOM;
172 0 : break;
173 :
174 : default:
175 0 : ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
176 0 : break;
177 : }
178 : }
179 0 : return uno::makeAny( ePos );
180 : }
181 0 : Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
182 : {
183 0 : chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
184 :
185 : ::com::sun::star::chart::ChartLegendPosition ePos;
186 0 : if( rOuterValue >>= ePos )
187 : {
188 0 : switch( ePos )
189 : {
190 : case ::com::sun::star::chart::ChartLegendPosition_LEFT:
191 0 : eNewPos = chart2::LegendPosition_LINE_START;
192 0 : break;
193 : case ::com::sun::star::chart::ChartLegendPosition_RIGHT:
194 0 : eNewPos = chart2::LegendPosition_LINE_END;
195 0 : break;
196 : case ::com::sun::star::chart::ChartLegendPosition_TOP:
197 0 : eNewPos = chart2::LegendPosition_PAGE_START;
198 0 : break;
199 : case ::com::sun::star::chart::ChartLegendPosition_BOTTOM:
200 0 : eNewPos = chart2::LegendPosition_PAGE_END;
201 0 : break;
202 : default: // NONE
203 0 : break;
204 : }
205 : }
206 :
207 0 : return uno::makeAny( eNewPos );
208 : }
209 : }
210 :
211 : namespace
212 : {
213 0 : static const OUString lcl_aServiceName("com.sun.star.comp.chart.Legend");
214 :
215 : enum
216 : {
217 : PROP_LEGEND_ALIGNMENT,
218 : PROP_LEGEND_EXPANSION
219 : };
220 :
221 0 : void lcl_AddPropertiesToVector(
222 : ::std::vector< Property > & rOutProperties )
223 : {
224 : rOutProperties.push_back(
225 : Property( "Alignment",
226 : PROP_LEGEND_ALIGNMENT,
227 0 : ::getCppuType( reinterpret_cast< const ::com::sun::star::chart::ChartLegendPosition * >(0)),
228 : //#i111967# no PropertyChangeEvent is fired on change so far
229 0 : beans::PropertyAttribute::MAYBEDEFAULT ));
230 :
231 : rOutProperties.push_back(
232 : Property( "Expansion",
233 : PROP_LEGEND_EXPANSION,
234 0 : ::getCppuType( reinterpret_cast< const ::com::sun::star::chart::ChartLegendExpansion * >(0)),
235 : //#i111967# no PropertyChangeEvent is fired on change so far
236 0 : beans::PropertyAttribute::MAYBEDEFAULT ));
237 0 : }
238 :
239 : struct StaticLegendWrapperPropertyArray_Initializer
240 : {
241 0 : Sequence< Property >* operator()()
242 : {
243 0 : static Sequence< Property > aPropSeq( lcl_GetPropertySequence() );
244 0 : return &aPropSeq;
245 : }
246 :
247 : private:
248 0 : Sequence< Property > lcl_GetPropertySequence()
249 : {
250 0 : ::std::vector< ::com::sun::star::beans::Property > aProperties;
251 0 : lcl_AddPropertiesToVector( aProperties );
252 0 : ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
253 0 : ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
254 0 : ::chart::FillProperties::AddPropertiesToVector( aProperties );
255 0 : ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
256 0 : ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
257 0 : ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
258 :
259 : ::std::sort( aProperties.begin(), aProperties.end(),
260 0 : ::chart::PropertyNameLess() );
261 :
262 0 : return ::chart::ContainerHelper::ContainerToSequence( aProperties );
263 : }
264 : };
265 :
266 : struct StaticLegendWrapperPropertyArray : public rtl::StaticAggregate< Sequence< Property >, StaticLegendWrapperPropertyArray_Initializer >
267 : {
268 : };
269 :
270 : } // anonymous namespace
271 :
272 : namespace chart
273 : {
274 : namespace wrapper
275 : {
276 :
277 0 : LegendWrapper::LegendWrapper( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact ) :
278 : m_spChart2ModelContact( spChart2ModelContact ),
279 0 : m_aEventListenerContainer( m_aMutex )
280 : {
281 0 : }
282 :
283 0 : LegendWrapper::~LegendWrapper()
284 : {
285 0 : }
286 :
287 : // ____ XShape ____
288 0 : awt::Point SAL_CALL LegendWrapper::getPosition()
289 : throw (uno::RuntimeException, std::exception)
290 : {
291 0 : return m_spChart2ModelContact->GetLegendPosition();
292 : }
293 :
294 0 : void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
295 : throw (uno::RuntimeException, std::exception)
296 : {
297 0 : Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
298 0 : if( xProp.is() )
299 : {
300 0 : awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
301 :
302 0 : chart2::RelativePosition aRelativePosition;
303 0 : aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
304 0 : aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
305 0 : aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
306 0 : xProp->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition) );
307 0 : }
308 0 : }
309 :
310 0 : awt::Size SAL_CALL LegendWrapper::getSize()
311 : throw (uno::RuntimeException, std::exception)
312 : {
313 0 : return m_spChart2ModelContact->GetLegendSize();
314 : }
315 :
316 0 : void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
317 : throw (beans::PropertyVetoException,
318 : uno::RuntimeException, std::exception)
319 : {
320 0 : Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
321 0 : if( xProp.is() )
322 : {
323 0 : awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
324 0 : awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
325 :
326 0 : awt::Point aPos( this->getPosition() );
327 0 : awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
328 :
329 : PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
330 0 : , xProp, aNewPositionAndSize, aPageRectangle );
331 0 : }
332 0 : }
333 :
334 : // ____ XShapeDescriptor (base of XShape) ____
335 0 : OUString SAL_CALL LegendWrapper::getShapeType()
336 : throw (uno::RuntimeException, std::exception)
337 : {
338 0 : return OUString( "com.sun.star.chart.ChartLegend" );
339 : }
340 :
341 : // ____ XComponent ____
342 0 : void SAL_CALL LegendWrapper::dispose()
343 : throw (uno::RuntimeException, std::exception)
344 : {
345 0 : Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
346 0 : m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
347 :
348 0 : MutexGuard aGuard( GetMutex());
349 0 : clearWrappedPropertySet();
350 0 : }
351 :
352 0 : void SAL_CALL LegendWrapper::addEventListener(
353 : const Reference< lang::XEventListener >& xListener )
354 : throw (uno::RuntimeException, std::exception)
355 : {
356 0 : m_aEventListenerContainer.addInterface( xListener );
357 0 : }
358 :
359 0 : void SAL_CALL LegendWrapper::removeEventListener(
360 : const Reference< lang::XEventListener >& aListener )
361 : throw (uno::RuntimeException, std::exception)
362 : {
363 0 : m_aEventListenerContainer.removeInterface( aListener );
364 0 : }
365 :
366 : //ReferenceSizePropertyProvider
367 0 : void LegendWrapper::updateReferenceSize()
368 : {
369 0 : Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
370 0 : if( xProp.is() )
371 : {
372 0 : if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
373 0 : xProp->setPropertyValue( "ReferencePageSize", uno::makeAny(
374 0 : m_spChart2ModelContact->GetPageSize() ));
375 0 : }
376 0 : }
377 0 : Any LegendWrapper::getReferenceSize()
378 : {
379 0 : Any aRet;
380 0 : Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
381 0 : if( xProp.is() )
382 0 : aRet = xProp->getPropertyValue( "ReferencePageSize" );
383 :
384 0 : return aRet;
385 : }
386 0 : awt::Size LegendWrapper::getCurrentSizeForReference()
387 : {
388 0 : return m_spChart2ModelContact->GetPageSize();
389 : }
390 :
391 : // WrappedPropertySet
392 0 : Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
393 : {
394 0 : Reference< beans::XPropertySet > xRet;
395 0 : Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
396 0 : if( xDiagram.is() )
397 0 : xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
398 : OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
399 0 : return xRet;
400 : }
401 :
402 0 : const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
403 : {
404 0 : return *StaticLegendWrapperPropertyArray::get();
405 : }
406 :
407 0 : const std::vector< WrappedProperty* > LegendWrapper::createWrappedProperties()
408 : {
409 0 : ::std::vector< ::chart::WrappedProperty* > aWrappedProperties;
410 :
411 0 : aWrappedProperties.push_back( new WrappedLegendAlignmentProperty() );
412 0 : aWrappedProperties.push_back( new WrappedProperty( "Expansion", "Expansion"));
413 0 : WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
414 : //same problem as for wall: thje defaults ion the old chart are different for different charttypes, so we need to export explicitly
415 0 : aWrappedProperties.push_back( new WrappedDirectStateProperty("FillStyle", "FillStyle"));
416 0 : aWrappedProperties.push_back( new WrappedDirectStateProperty("FillColor", "FillColor"));
417 0 : WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
418 0 : WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
419 :
420 0 : return aWrappedProperties;
421 : }
422 :
423 0 : Sequence< OUString > LegendWrapper::getSupportedServiceNames_Static()
424 : {
425 0 : Sequence< OUString > aServices( 4 );
426 0 : aServices[ 0 ] = "com.sun.star.chart.ChartLegend";
427 0 : aServices[ 1 ] = "com.sun.star.drawing.Shape";
428 0 : aServices[ 2 ] = "com.sun.star.xml.UserDefinedAttributesSupplier";
429 0 : aServices[ 3 ] = "com.sun.star.style.CharacterProperties";
430 :
431 0 : return aServices;
432 : }
433 :
434 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
435 0 : APPHELPER_XSERVICEINFO_IMPL( LegendWrapper, lcl_aServiceName );
436 :
437 : } // namespace wrapper
438 0 : } // namespace chart
439 :
440 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|