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

Generated by: LCOV version 1.10