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