1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "LegendItemConverter.hxx"
27 #include "SchWhichPairs.hxx"
28 #include "macros.hxx"
29 #include "ItemPropertyMap.hxx"
30 #include "GraphicPropertyItemConverter.hxx"
31 #include "CharacterPropertyItemConverter.hxx"
32 #include <com/sun/star/chart2/XLegend.hpp>
33 #include <com/sun/star/chart2/LegendPosition.hpp>
34 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
35 
36 #include <svl/intitem.hxx>
37 #include <svl/eitem.hxx>
38 
39 #include <functional>
40 #include <algorithm>
41 
42 using namespace ::com::sun::star;
43 
44 namespace chart
45 {
46 namespace wrapper
47 {
48 
LegendItemConverter(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & rPropertySet,SfxItemPool & rItemPool,SdrModel & rDrawModel,const uno::Reference<lang::XMultiServiceFactory> & xNamedPropertyContainerFactory,::std::auto_ptr<::com::sun::star::awt::Size> pRefSize)49 LegendItemConverter::LegendItemConverter(
50     const ::com::sun::star::uno::Reference<
51     ::com::sun::star::beans::XPropertySet > & rPropertySet,
52     SfxItemPool& rItemPool,
53     SdrModel& rDrawModel,
54     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory,
55     ::std::auto_ptr< ::com::sun::star::awt::Size > pRefSize ) :
56         ItemConverter( rPropertySet, rItemPool )
57 {
58     m_aConverters.push_back( new GraphicPropertyItemConverter(
59                                  rPropertySet, rItemPool, rDrawModel, xNamedPropertyContainerFactory,
60                                  GraphicPropertyItemConverter::LINE_AND_FILL_PROPERTIES ));
61     m_aConverters.push_back( new CharacterPropertyItemConverter(
62                                  rPropertySet, rItemPool, pRefSize,
63                                  C2U( "ReferencePageSize" ) ));
64 }
65 
~LegendItemConverter()66 LegendItemConverter::~LegendItemConverter()
67 {
68     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
69                      ::comphelper::DeleteItemConverterPtr() );
70 }
71 
FillItemSet(SfxItemSet & rOutItemSet) const72 void LegendItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
73 {
74     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
75                      ::comphelper::FillItemSetFunc( rOutItemSet ));
76 
77     // own items
78     ItemConverter::FillItemSet( rOutItemSet );
79 }
80 
ApplyItemSet(const SfxItemSet & rItemSet)81 bool LegendItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
82 {
83     bool bResult = false;
84 
85     ::std::for_each( m_aConverters.begin(), m_aConverters.end(),
86                      ::comphelper::ApplyItemSetFunc( rItemSet, bResult ));
87 
88     // own items
89     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
90 }
91 
GetWhichPairs() const92 const sal_uInt16 * LegendItemConverter::GetWhichPairs() const
93 {
94     // must span all used items!
95     return nLegendWhichPairs;
96 }
97 
GetItemProperty(tWhichIdType,tPropertyNameWithMemberId &) const98 bool LegendItemConverter::GetItemProperty( tWhichIdType /*nWhichId*/, tPropertyNameWithMemberId & /*rOutProperty*/ ) const
99 {
100     // No own (non-special) properties
101     return false;
102 }
103 
104 
ApplySpecialItem(sal_uInt16 nWhichId,const SfxItemSet & rInItemSet)105 bool LegendItemConverter::ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet& rInItemSet )
106     throw( uno::Exception )
107 {
108     bool bChanged = false;
109 
110     switch( nWhichId )
111     {
112         case SCHATTR_LEGEND_SHOW:
113         {
114             const SfxPoolItem* pPoolItem = NULL;
115             if( rInItemSet.GetItemState( SCHATTR_LEGEND_SHOW, sal_True, &pPoolItem ) == SFX_ITEM_SET )
116             {
117                 sal_Bool bShow = static_cast< const SfxBoolItem * >( pPoolItem )->GetValue();
118                 sal_Bool bWasShown = sal_True;
119                 if( ! (GetPropertySet()->getPropertyValue( C2U("Show")) >>= bWasShown) ||
120                     ( bWasShown != bShow ))
121                 {
122                     GetPropertySet()->setPropertyValue( C2U("Show"), uno::makeAny( bShow ));
123                     bChanged = true;
124                 }
125             }
126 
127         }
128         break;
129         case SCHATTR_LEGEND_POS:
130         {
131             const SfxPoolItem* pPoolItem = NULL;
132             if( rInItemSet.GetItemState( SCHATTR_LEGEND_POS, sal_True, &pPoolItem ) == SFX_ITEM_SET )
133             {
134                 chart2::LegendPosition eNewPos = static_cast<chart2::LegendPosition>(((const SfxInt32Item*)pPoolItem)->GetValue());
135 
136                 ::com::sun::star::chart::ChartLegendExpansion eExpansion = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
137                 switch( eNewPos )
138                 {
139                     case chart2::LegendPosition_LINE_START:
140                     case chart2::LegendPosition_LINE_END:
141                         eExpansion = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
142                         break;
143                     case chart2::LegendPosition_PAGE_START:
144                     case chart2::LegendPosition_PAGE_END:
145                         eExpansion = ::com::sun::star::chart::ChartLegendExpansion_WIDE;
146                         break;
147                     default:
148                         break;
149                 }
150 
151                 try
152                 {
153                     chart2::LegendPosition eOldPos;
154                     if( ! ( GetPropertySet()->getPropertyValue( C2U( "AnchorPosition" )) >>= eOldPos ) ||
155                         ( eOldPos != eNewPos ))
156                     {
157                         GetPropertySet()->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( eNewPos ));
158                         GetPropertySet()->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eExpansion ));
159                         GetPropertySet()->setPropertyValue( C2U( "RelativePosition" ), uno::Any());
160                         bChanged = true;
161                     }
162                 }
163                 catch( uno::Exception & ex )
164                 {
165                     ASSERT_EXCEPTION( ex );
166                 }
167             }
168         }
169         break;
170     }
171 
172     return bChanged;
173 }
174 
FillSpecialItem(sal_uInt16 nWhichId,SfxItemSet & rOutItemSet) const175 void LegendItemConverter::FillSpecialItem(
176     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
177     throw( uno::Exception )
178 {
179     switch( nWhichId )
180     {
181         case SCHATTR_LEGEND_SHOW:
182         {
183             sal_Bool bShow = sal_True;
184             GetPropertySet()->getPropertyValue( C2U( "Show" )) >>= bShow;
185             rOutItemSet.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, bShow) );
186         }
187         break;
188         case SCHATTR_LEGEND_POS:
189         {
190             chart2::LegendPosition eLegendPos( chart2::LegendPosition_LINE_END );
191             GetPropertySet()->getPropertyValue( C2U( "AnchorPosition" )) >>= eLegendPos;
192             rOutItemSet.Put( SfxInt32Item(SCHATTR_LEGEND_POS, eLegendPos ) );
193         }
194         break;
195    }
196 }
197 
198 } //  namespace wrapper
199 } //  namespace chart
200