1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "LegendItemConverter.hxx"
31 #include "SchWhichPairs.hxx"
32 #include "macros.hxx"
33 #include "ItemPropertyMap.hxx"
34 #include "GraphicPropertyItemConverter.hxx"
35 #include "CharacterPropertyItemConverter.hxx"
36 #include <com/sun/star/chart2/XLegend.hpp>
37 #include <com/sun/star/chart2/LegendPosition.hpp>
38 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
39 
40 #include <svl/intitem.hxx>
41 #include <svl/eitem.hxx>
42 
43 #include <functional>
44 #include <algorithm>
45 
46 using namespace ::com::sun::star;
47 
48 namespace chart
49 {
50 namespace wrapper
51 {
52 
53 LegendItemConverter::LegendItemConverter(
54     const ::com::sun::star::uno::Reference<
55     ::com::sun::star::beans::XPropertySet > & rPropertySet,
56     SfxItemPool& rItemPool,
57     SdrModel& rDrawModel,
58     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory,
59     ::std::auto_ptr< ::com::sun::star::awt::Size > pRefSize ) :
60         ItemConverter( rPropertySet, rItemPool )
61 {
62     m_aConverters.push_back( new GraphicPropertyItemConverter(
63                                  rPropertySet, rItemPool, rDrawModel, xNamedPropertyContainerFactory,
64                                  GraphicPropertyItemConverter::LINE_AND_FILL_PROPERTIES ));
65     m_aConverters.push_back( new CharacterPropertyItemConverter(
66                                  rPropertySet, rItemPool, pRefSize,
67                                  C2U( "ReferencePageSize" ) ));
68 }
69 
70 LegendItemConverter::~LegendItemConverter()
71 {
72     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
73                      ::comphelper::DeleteItemConverterPtr() );
74 }
75 
76 void LegendItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
77 {
78     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
79                      ::comphelper::FillItemSetFunc( rOutItemSet ));
80 
81     // own items
82     ItemConverter::FillItemSet( rOutItemSet );
83 }
84 
85 bool LegendItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
86 {
87     bool bResult = false;
88 
89     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
90                      ::comphelper::ApplyItemSetFunc( rItemSet, bResult ));
91 
92     // own items
93     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
94 }
95 
96 const sal_uInt16 * LegendItemConverter::GetWhichPairs() const
97 {
98     // must span all used items!
99     return nLegendWhichPairs;
100 }
101 
102 bool LegendItemConverter::GetItemProperty( tWhichIdType /*nWhichId*/, tPropertyNameWithMemberId & /*rOutProperty*/ ) const
103 {
104     // No own (non-special) properties
105     return false;
106 }
107 
108 
109 bool LegendItemConverter::ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet& rInItemSet )
110     throw( uno::Exception )
111 {
112     bool bChanged = false;
113 
114     switch( nWhichId )
115     {
116         case SCHATTR_LEGEND_SHOW:
117         {
118             const SfxPoolItem* pPoolItem = NULL;
119             if( rInItemSet.GetItemState( SCHATTR_LEGEND_SHOW, sal_True, &pPoolItem ) == SFX_ITEM_SET )
120             {
121                 sal_Bool bShow = static_cast< const SfxBoolItem * >( pPoolItem )->GetValue();
122                 sal_Bool bWasShown = sal_True;
123                 if( ! (GetPropertySet()->getPropertyValue( C2U("Show")) >>= bWasShown) ||
124                     ( bWasShown != bShow ))
125                 {
126                     GetPropertySet()->setPropertyValue( C2U("Show"), uno::makeAny( bShow ));
127                     bChanged = true;
128                 }
129             }
130 
131         }
132         break;
133         case SCHATTR_LEGEND_POS:
134         {
135             const SfxPoolItem* pPoolItem = NULL;
136             if( rInItemSet.GetItemState( SCHATTR_LEGEND_POS, sal_True, &pPoolItem ) == SFX_ITEM_SET )
137             {
138                 chart2::LegendPosition eNewPos = static_cast<chart2::LegendPosition>(((const SfxInt32Item*)pPoolItem)->GetValue());
139 
140                 ::com::sun::star::chart::ChartLegendExpansion eExpansion = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
141                 switch( eNewPos )
142                 {
143                     case chart2::LegendPosition_LINE_START:
144                     case chart2::LegendPosition_LINE_END:
145                         eExpansion = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
146                         break;
147                     case chart2::LegendPosition_PAGE_START:
148                     case chart2::LegendPosition_PAGE_END:
149                         eExpansion = ::com::sun::star::chart::ChartLegendExpansion_WIDE;
150                         break;
151                     default:
152                         break;
153                 }
154 
155                 try
156                 {
157                     chart2::LegendPosition eOldPos;
158                     if( ! ( GetPropertySet()->getPropertyValue( C2U( "AnchorPosition" )) >>= eOldPos ) ||
159                         ( eOldPos != eNewPos ))
160                     {
161                         GetPropertySet()->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( eNewPos ));
162                         GetPropertySet()->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eExpansion ));
163                         GetPropertySet()->setPropertyValue( C2U( "RelativePosition" ), uno::Any());
164                         bChanged = true;
165                     }
166                 }
167                 catch( uno::Exception & ex )
168                 {
169                     ASSERT_EXCEPTION( ex );
170                 }
171             }
172         }
173         break;
174     }
175 
176     return bChanged;
177 }
178 
179 void LegendItemConverter::FillSpecialItem(
180     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
181     throw( uno::Exception )
182 {
183     switch( nWhichId )
184     {
185         case SCHATTR_LEGEND_SHOW:
186         {
187             sal_Bool bShow = sal_True;
188             GetPropertySet()->getPropertyValue( C2U( "Show" )) >>= bShow;
189             rOutItemSet.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, bShow) );
190         }
191         break;
192         case SCHATTR_LEGEND_POS:
193         {
194             chart2::LegendPosition eLegendPos( chart2::LegendPosition_LINE_END );
195             GetPropertySet()->getPropertyValue( C2U( "AnchorPosition" )) >>= eLegendPos;
196             rOutItemSet.Put( SfxInt32Item(SCHATTR_LEGEND_POS, eLegendPos ) );
197         }
198         break;
199    }
200 }
201 
202 } //  namespace wrapper
203 } //  namespace chart
204