LCOV - code coverage report
Current view: top level - chart2/source/controller/chartapiwrapper - LegendWrapper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 159 179 88.8 %
Date: 2014-04-11 Functions: 29 34 85.3 %
Legend: Lines: hit not hit

          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         120 : WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
      73         120 :     : ::chart::WrappedProperty( "Alignment", "AnchorPosition" )
      74             : {
      75         120 : }
      76         234 : WrappedLegendAlignmentProperty::~WrappedLegendAlignmentProperty()
      77             : {
      78         234 : }
      79             : 
      80         184 : Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
      81             :                         throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
      82             : {
      83         184 :     Any aRet;
      84         184 :     if( xInnerPropertySet.is() )
      85             :     {
      86         184 :         sal_Bool bShowLegend = sal_True;
      87         184 :         xInnerPropertySet->getPropertyValue( "Show" ) >>= bShowLegend;
      88         184 :         if(!bShowLegend)
      89             :         {
      90           1 :             aRet = uno::makeAny( ::com::sun::star::chart::ChartLegendPosition_NONE );
      91             :         }
      92             :         else
      93             :         {
      94         183 :             aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
      95         183 :             aRet = this->convertInnerToOuterValue( aRet );
      96             :         }
      97             :     }
      98         184 :     return aRet;
      99             : }
     100             : 
     101          67 : 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          67 :     if(xInnerPropertySet.is())
     105             :     {
     106          67 :         sal_Bool bNewShowLegend = sal_True;
     107          67 :         sal_Bool bOldShowLegend = sal_True;
     108             :         {
     109          67 :             ::com::sun::star::chart::ChartLegendPosition eOuterPos(::com::sun::star::chart::ChartLegendPosition_NONE);
     110          67 :             if( (rOuterValue >>= eOuterPos)  && eOuterPos == ::com::sun::star::chart::ChartLegendPosition_NONE )
     111           1 :                 bNewShowLegend = sal_False;
     112          67 :             xInnerPropertySet->getPropertyValue( "Show" ) >>= bOldShowLegend;
     113             :         }
     114          67 :         if(bNewShowLegend!=bOldShowLegend)
     115             :         {
     116           1 :             xInnerPropertySet->setPropertyValue( "Show", uno::makeAny(bNewShowLegend) );
     117             :         }
     118          67 :         if(!bNewShowLegend)
     119          68 :             return;
     120             : 
     121             :         //set corresponding LegendPosition
     122          66 :         Any aInnerValue = this->convertOuterToInnerValue( rOuterValue );
     123          66 :         xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
     124             : 
     125             :         //correct LegendExpansion
     126          66 :         chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
     127          66 :         if( aInnerValue >>= eNewInnerPos )
     128             :         {
     129             :             ::com::sun::star::chart::ChartLegendExpansion eNewExpansion =
     130          73 :                 ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
     131           7 :                   eNewInnerPos == chart2::LegendPosition_LINE_START )
     132             :                 ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
     133         127 :                 : ::com::sun::star::chart::ChartLegendExpansion_WIDE;
     134             : 
     135          66 :             ::com::sun::star::chart::ChartLegendExpansion eOldExpansion( ::com::sun::star::chart::ChartLegendExpansion_HIGH );
     136             :             bool bExpansionWasSet(
     137          66 :                 xInnerPropertySet->getPropertyValue( "Expansion" ) >>= eOldExpansion );
     138             : 
     139          66 :             if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
     140           5 :                 xInnerPropertySet->setPropertyValue( "Expansion", uno::makeAny( eNewExpansion ));
     141             :         }
     142             : 
     143             :         //correct RelativePosition
     144         132 :         Any aRelativePosition( xInnerPropertySet->getPropertyValue("RelativePosition") );
     145          66 :         if(aRelativePosition.hasValue())
     146             :         {
     147           0 :             xInnerPropertySet->setPropertyValue( "RelativePosition", Any() );
     148          66 :         }
     149             :     }
     150             : }
     151             : 
     152         183 : Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
     153             : {
     154         183 :     ::com::sun::star::chart::ChartLegendPosition ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
     155             : 
     156             :     chart2::LegendPosition eNewPos;
     157         183 :     if( rInnerValue >>= eNewPos )
     158             :     {
     159         183 :         switch( eNewPos )
     160             :         {
     161             :             case chart2::LegendPosition_LINE_START:
     162           7 :                 ePos = ::com::sun::star::chart::ChartLegendPosition_LEFT;
     163           7 :                 break;
     164             :             case chart2::LegendPosition_LINE_END:
     165         160 :                 ePos = ::com::sun::star::chart::ChartLegendPosition_RIGHT;
     166         160 :                 break;
     167             :             case chart2::LegendPosition_PAGE_START:
     168           7 :                 ePos = ::com::sun::star::chart::ChartLegendPosition_TOP;
     169           7 :                 break;
     170             :             case chart2::LegendPosition_PAGE_END:
     171           9 :                 ePos = ::com::sun::star::chart::ChartLegendPosition_BOTTOM;
     172           9 :                 break;
     173             : 
     174             :             default:
     175           0 :                 ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
     176           0 :                 break;
     177             :         }
     178             :     }
     179         183 :     return uno::makeAny( ePos );
     180             : }
     181          66 : Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
     182             : {
     183          66 :     chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
     184             : 
     185             :     ::com::sun::star::chart::ChartLegendPosition ePos;
     186          66 :     if( rOuterValue >>= ePos )
     187             :     {
     188          66 :         switch( ePos )
     189             :         {
     190             :             case ::com::sun::star::chart::ChartLegendPosition_LEFT:
     191           2 :                 eNewPos = chart2::LegendPosition_LINE_START;
     192           2 :                 break;
     193             :             case ::com::sun::star::chart::ChartLegendPosition_RIGHT:
     194          59 :                 eNewPos = chart2::LegendPosition_LINE_END;
     195          59 :                 break;
     196             :             case ::com::sun::star::chart::ChartLegendPosition_TOP:
     197           2 :                 eNewPos = chart2::LegendPosition_PAGE_START;
     198           2 :                 break;
     199             :             case ::com::sun::star::chart::ChartLegendPosition_BOTTOM:
     200           3 :                 eNewPos = chart2::LegendPosition_PAGE_END;
     201           3 :                 break;
     202             :             default: // NONE
     203           0 :                 break;
     204             :         }
     205             :     }
     206             : 
     207          66 :     return uno::makeAny( eNewPos );
     208             : }
     209             : }
     210             : 
     211             : namespace
     212             : {
     213          15 : 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          10 : void lcl_AddPropertiesToVector(
     222             :     ::std::vector< Property > & rOutProperties )
     223             : {
     224             :     rOutProperties.push_back(
     225             :         Property( "Alignment",
     226             :                   PROP_LEGEND_ALIGNMENT,
     227          10 :                   ::getCppuType( reinterpret_cast< const ::com::sun::star::chart::ChartLegendPosition * >(0)),
     228             :                   //#i111967# no PropertyChangeEvent is fired on change so far
     229          10 :                   beans::PropertyAttribute::MAYBEDEFAULT ));
     230             : 
     231             :     rOutProperties.push_back(
     232             :         Property( "Expansion",
     233             :                   PROP_LEGEND_EXPANSION,
     234          10 :                   ::getCppuType( reinterpret_cast< const ::com::sun::star::chart::ChartLegendExpansion * >(0)),
     235             :                   //#i111967# no PropertyChangeEvent is fired on change so far
     236          10 :                   beans::PropertyAttribute::MAYBEDEFAULT ));
     237          10 : }
     238             : 
     239             : struct StaticLegendWrapperPropertyArray_Initializer
     240             : {
     241          10 :     Sequence< Property >* operator()()
     242             :     {
     243          10 :         static Sequence< Property > aPropSeq( lcl_GetPropertySequence() );
     244          10 :         return &aPropSeq;
     245             :     }
     246             : 
     247             : private:
     248          10 :     Sequence< Property > lcl_GetPropertySequence()
     249             :     {
     250          10 :         ::std::vector< ::com::sun::star::beans::Property > aProperties;
     251          10 :         lcl_AddPropertiesToVector( aProperties );
     252          10 :         ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
     253          10 :         ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
     254          10 :         ::chart::FillProperties::AddPropertiesToVector( aProperties );
     255          10 :         ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
     256          10 :         ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
     257          10 :         ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
     258             : 
     259             :         ::std::sort( aProperties.begin(), aProperties.end(),
     260          10 :                      ::chart::PropertyNameLess() );
     261             : 
     262          10 :         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         121 : LegendWrapper::LegendWrapper( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact ) :
     278             :         m_spChart2ModelContact( spChart2ModelContact ),
     279         121 :         m_aEventListenerContainer( m_aMutex )
     280             : {
     281         121 : }
     282             : 
     283         236 : LegendWrapper::~LegendWrapper()
     284             : {
     285         236 : }
     286             : 
     287             : // ____ XShape ____
     288         156 : awt::Point SAL_CALL LegendWrapper::getPosition()
     289             :     throw (uno::RuntimeException, std::exception)
     290             : {
     291         156 :     return m_spChart2ModelContact->GetLegendPosition();
     292             : }
     293             : 
     294          66 : void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
     295             :     throw (uno::RuntimeException, std::exception)
     296             : {
     297          66 :     Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
     298          66 :     if( xProp.is() )
     299             :     {
     300          66 :         awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
     301             : 
     302          66 :         chart2::RelativePosition aRelativePosition;
     303          66 :         aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
     304          66 :         aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
     305          66 :         aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
     306          66 :         xProp->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition) );
     307          66 :     }
     308          66 : }
     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           1 : OUString SAL_CALL LegendWrapper::getShapeType()
     336             :     throw (uno::RuntimeException, std::exception)
     337             : {
     338           1 :     return OUString( "com.sun.star.chart.ChartLegend" );
     339             : }
     340             : 
     341             : // ____ XComponent ____
     342         119 : void SAL_CALL LegendWrapper::dispose()
     343             :     throw (uno::RuntimeException, std::exception)
     344             : {
     345         119 :     Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
     346         119 :     m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
     347             : 
     348         238 :     MutexGuard aGuard( GetMutex());
     349         238 :     clearWrappedPropertySet();
     350         119 : }
     351             : 
     352           2 : void SAL_CALL LegendWrapper::addEventListener(
     353             :     const Reference< lang::XEventListener >& xListener )
     354             :     throw (uno::RuntimeException, std::exception)
     355             : {
     356           2 :     m_aEventListenerContainer.addInterface( xListener );
     357           2 : }
     358             : 
     359           1 : void SAL_CALL LegendWrapper::removeEventListener(
     360             :     const Reference< lang::XEventListener >& aListener )
     361             :     throw (uno::RuntimeException, std::exception)
     362             : {
     363           1 :     m_aEventListenerContainer.removeInterface( aListener );
     364           1 : }
     365             : 
     366             : //ReferenceSizePropertyProvider
     367         199 : void LegendWrapper::updateReferenceSize()
     368             : {
     369         199 :     Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
     370         199 :     if( xProp.is() )
     371             :     {
     372         199 :         if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
     373           0 :             xProp->setPropertyValue( "ReferencePageSize", uno::makeAny(
     374           0 :                 m_spChart2ModelContact->GetPageSize() ));
     375         199 :     }
     376         199 : }
     377         939 : Any LegendWrapper::getReferenceSize()
     378             : {
     379         939 :     Any aRet;
     380        1878 :     Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
     381         939 :     if( xProp.is() )
     382         939 :         aRet = xProp->getPropertyValue( "ReferencePageSize" );
     383             : 
     384        1878 :     return aRet;
     385             : }
     386           0 : awt::Size LegendWrapper::getCurrentSizeForReference()
     387             : {
     388           0 :     return m_spChart2ModelContact->GetPageSize();
     389             : }
     390             : 
     391             : // WrappedPropertySet
     392       29600 : Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
     393             : {
     394       29600 :     Reference< beans::XPropertySet > xRet;
     395       59200 :     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
     396       29600 :     if( xDiagram.is() )
     397       29600 :         xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
     398             :     OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
     399       59200 :     return xRet;
     400             : }
     401             : 
     402         120 : const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
     403             : {
     404         120 :     return *StaticLegendWrapperPropertyArray::get();
     405             : }
     406             : 
     407         120 : const std::vector< WrappedProperty* > LegendWrapper::createWrappedProperties()
     408             : {
     409         120 :     ::std::vector< ::chart::WrappedProperty* > aWrappedProperties;
     410             : 
     411         120 :     aWrappedProperties.push_back( new WrappedLegendAlignmentProperty() );
     412         120 :     aWrappedProperties.push_back( new WrappedProperty( "Expansion", "Expansion"));
     413         120 :     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         120 :     aWrappedProperties.push_back( new WrappedDirectStateProperty("FillStyle", "FillStyle"));
     416         120 :     aWrappedProperties.push_back( new WrappedDirectStateProperty("FillColor", "FillColor"));
     417         120 :     WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
     418         120 :     WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
     419             : 
     420         120 :     return aWrappedProperties;
     421             : }
     422             : 
     423           6 : Sequence< OUString > LegendWrapper::getSupportedServiceNames_Static()
     424             : {
     425           6 :     Sequence< OUString > aServices( 4 );
     426           6 :     aServices[ 0 ] = "com.sun.star.chart.ChartLegend";
     427           6 :     aServices[ 1 ] = "com.sun.star.drawing.Shape";
     428           6 :     aServices[ 2 ] = "com.sun.star.xml.UserDefinedAttributesSupplier";
     429           6 :     aServices[ 3 ] = "com.sun.star.style.CharacterProperties";
     430             : 
     431           6 :     return aServices;
     432             : }
     433             : 
     434             : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
     435          11 : APPHELPER_XSERVICEINFO_IMPL( LegendWrapper, lcl_aServiceName );
     436             : 
     437             : } //  namespace wrapper
     438          45 : } //  namespace chart
     439             : 
     440             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10