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

Generated by: LCOV version 1.10