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 
27 #include "res_LegendPosition.hxx"
28 #include "ResourceIds.hrc"
29 #include "Strings.hrc"
30 #include "res_LegendPosition_IDs.hrc"
31 #include "ResId.hxx"
32 #include "ChartModelHelper.hxx"
33 #include "macros.hxx"
34 #include "LegendHelper.hxx"
35 
36 #ifndef _SVT_CONTROLDIMS_HRC_
37 #include <svtools/controldims.hrc>
38 #endif
39 #include <com/sun/star/chart2/LegendPosition.hpp>
40 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
41 
42 //itemset stuff
43 #include "chartview/ChartSfxItemIds.hxx"
44 #include <svl/intitem.hxx>
45 #include <svl/eitem.hxx>
46 
47 //.............................................................................
48 namespace chart
49 {
50 //.............................................................................
51 
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::chart2;
54 
55 LegendPositionResources::LegendPositionResources( Window* pWindow )
56     : m_xCC() //unused in this scenario
57     , m_aCbxShow( pWindow ) //unused in this scenario
58 	, m_aRbtLeft( pWindow, SchResId(RBT_LEFT) )
59 	, m_aRbtRight( pWindow, SchResId(RBT_RIGHT) )
60 	, m_aRbtTop( pWindow, SchResId(RBT_TOP) )
61 	, m_aRbtBottom( pWindow, SchResId(RBT_BOTTOM) )
62 {
63     m_aCbxShow.Check();//legend is assumed to be visible in this scenario
64     impl_setRadioButtonToggleHdl();
65 }
66 
67 LegendPositionResources::LegendPositionResources( Window* pWindow, const uno::Reference< uno::XComponentContext >& xCC )
68     : m_xCC( xCC )
69     , m_aCbxShow( pWindow, SchResId(CBX_SHOWLEGEND) )
70 	, m_aRbtLeft( pWindow, SchResId(RBT_LEFT) )
71 	, m_aRbtRight( pWindow, SchResId(RBT_RIGHT) )
72 	, m_aRbtTop( pWindow, SchResId(RBT_TOP) )
73 	, m_aRbtBottom( pWindow, SchResId(RBT_BOTTOM) )
74 {
75     m_aCbxShow.SetToggleHdl( LINK( this, LegendPositionResources, PositionEnableHdl ) );
76     impl_setRadioButtonToggleHdl();
77 	m_aCbxShow.SetAccessibleRelationMemberOf(&m_aCbxShow);
78 	m_aRbtLeft.SetAccessibleRelationMemberOf(&m_aCbxShow);
79 	m_aRbtRight.SetAccessibleRelationMemberOf(&m_aCbxShow);
80 	m_aRbtTop.SetAccessibleRelationMemberOf(&m_aCbxShow);
81 	m_aRbtBottom.SetAccessibleRelationMemberOf(&m_aCbxShow);
82 }
83 
84 void LegendPositionResources::impl_setRadioButtonToggleHdl()
85 {
86     m_aRbtLeft.SetToggleHdl( LINK( this, LegendPositionResources, PositionChangeHdl ) );
87 	m_aRbtTop.SetToggleHdl( LINK( this, LegendPositionResources, PositionChangeHdl ) );
88 	m_aRbtRight.SetToggleHdl( LINK( this, LegendPositionResources, PositionChangeHdl ) );
89 	m_aRbtBottom.SetToggleHdl( LINK( this, LegendPositionResources, PositionChangeHdl ) );
90 }
91 
92 LegendPositionResources::~LegendPositionResources()
93 {
94 }
95 
96 void LegendPositionResources::writeToResources( const uno::Reference< frame::XModel >& xChartModel )
97 {
98     try
99     {
100         uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartModel );
101         uno::Reference< beans::XPropertySet > xProp( xDiagram->getLegend(), uno::UNO_QUERY );
102         if( xProp.is() )
103         {
104             //show
105             sal_Bool bShowLegend = sal_False;
106             xProp->getPropertyValue( C2U("Show") ) >>= bShowLegend;
107             m_aCbxShow.Check( bShowLegend );
108             PositionEnableHdl(0);
109 
110             //position
111             chart2::LegendPosition ePos;
112             xProp->getPropertyValue( C2U( "AnchorPosition" )) >>= ePos;
113             switch( ePos )
114             {
115                 case chart2::LegendPosition_LINE_START:
116                     m_aRbtLeft.Check();
117                     break;
118                 case chart2::LegendPosition_LINE_END:
119                     m_aRbtRight.Check();
120                     break;
121                 case chart2::LegendPosition_PAGE_START:
122                     m_aRbtTop.Check();
123                     break;
124                 case chart2::LegendPosition_PAGE_END:
125                     m_aRbtBottom.Check();
126                     break;
127 
128                 case chart2::LegendPosition_CUSTOM:
129                 default:
130                     m_aRbtRight.Check();
131                     break;
132             }
133         }
134     }
135     catch( uno::Exception & ex )
136     {
137         ASSERT_EXCEPTION( ex );
138     }
139 }
140 
141 void LegendPositionResources::writeToModel( const ::com::sun::star::uno::Reference< frame::XModel >& xChartModel ) const
142 {
143     try
144     {
145         sal_Bool bShowLegend = static_cast<sal_Bool>(m_aCbxShow.IsChecked());
146         uno::Reference< beans::XPropertySet > xProp( LegendHelper::getLegend( xChartModel,m_xCC,bShowLegend ), uno::UNO_QUERY );
147         if( xProp.is() )
148         {
149             //show
150             xProp->setPropertyValue( C2U("Show"), uno::makeAny( bShowLegend ));
151 
152             //position
153             chart2::LegendPosition eNewPos;
154             ::com::sun::star::chart::ChartLegendExpansion eExp = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
155 
156             if( m_aRbtLeft.IsChecked() )
157                 eNewPos = chart2::LegendPosition_LINE_START;
158             else if( m_aRbtRight.IsChecked() )
159             {
160                 eNewPos = chart2::LegendPosition_LINE_END;
161             }
162             else if( m_aRbtTop.IsChecked() )
163             {
164                 eNewPos = chart2::LegendPosition_PAGE_START;
165                 eExp = ::com::sun::star::chart::ChartLegendExpansion_WIDE;
166             }
167             else if( m_aRbtBottom.IsChecked() )
168             {
169                 eNewPos = chart2::LegendPosition_PAGE_END;
170                 eExp = ::com::sun::star::chart::ChartLegendExpansion_WIDE;
171             }
172 
173             xProp->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( eNewPos ));
174             xProp->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eExp ));
175             xProp->setPropertyValue( C2U( "RelativePosition" ), uno::Any());
176         }
177     }
178     catch( uno::Exception & ex )
179     {
180         ASSERT_EXCEPTION( ex );
181     }
182 }
183 
184 IMPL_LINK( LegendPositionResources, PositionEnableHdl, void*, EMPTYARG )
185 {
186     sal_Bool bEnable = m_aCbxShow.IsChecked();
187 
188     m_aRbtLeft.Enable( bEnable );
189 	m_aRbtTop.Enable( bEnable );
190 	m_aRbtRight.Enable( bEnable );
191 	m_aRbtBottom.Enable( bEnable );
192 
193     m_aChangeLink.Call(NULL);
194 
195     return 0;
196 }
197 
198 void LegendPositionResources::initFromItemSet( const SfxItemSet& rInAttrs )
199 {
200     const SfxPoolItem* pPoolItem = NULL;
201 	if( rInAttrs.GetItemState( SCHATTR_LEGEND_POS, sal_True, &pPoolItem ) == SFX_ITEM_SET )
202     {
203         sal_Int32 nLegendPosition = ((const SfxInt32Item*)pPoolItem)->GetValue();
204         switch( nLegendPosition )
205 	    {
206 		    case chart2::LegendPosition_LINE_START:
207 			    m_aRbtLeft.Check(sal_True);
208 			    break;
209 		    case chart2::LegendPosition_PAGE_START:
210 			    m_aRbtTop.Check(sal_True);
211 			    break;
212 		    case chart2::LegendPosition_LINE_END:
213 			    m_aRbtRight.Check(sal_True);
214 			    break;
215 		    case chart2::LegendPosition_PAGE_END:
216 			    m_aRbtBottom.Check(sal_True);
217 			    break;
218             default:
219                 break;
220 	    }
221     }
222 
223     if( rInAttrs.GetItemState( SCHATTR_LEGEND_SHOW, sal_True, &pPoolItem ) == SFX_ITEM_SET )
224     {
225         bool bShow = static_cast< const SfxBoolItem * >( pPoolItem )->GetValue();
226         m_aCbxShow.Check(bShow);
227     }
228 }
229 
230 void LegendPositionResources::writeToItemSet( SfxItemSet& rOutAttrs ) const
231 {
232     sal_Int32 nLegendPosition = chart2::LegendPosition_CUSTOM;
233 	if( m_aRbtLeft.IsChecked() )
234 		nLegendPosition = chart2::LegendPosition_LINE_START;
235 	else if( m_aRbtTop.IsChecked() )
236 		nLegendPosition = chart2::LegendPosition_PAGE_START;
237 	else if( m_aRbtRight.IsChecked() )
238 		nLegendPosition = chart2::LegendPosition_LINE_END;
239 	else if( m_aRbtBottom.IsChecked() )
240 		nLegendPosition = chart2::LegendPosition_PAGE_END;
241 	rOutAttrs.Put(SfxInt32Item(SCHATTR_LEGEND_POS, nLegendPosition ));
242 
243     rOutAttrs.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, m_aCbxShow.IsChecked()) );
244 }
245 
246 IMPL_LINK( LegendPositionResources, PositionChangeHdl, RadioButton*, pRadio )
247 {
248     //for each radio click ther are coming two change events
249     //first uncheck of previous button -> ignore that call
250     //the second call gives the check of the new button
251     if( pRadio && pRadio->IsChecked() )
252         m_aChangeLink.Call(NULL);
253     return 0;
254 }
255 
256 void LegendPositionResources::SetChangeHdl( const Link& rLink )
257 {
258     m_aChangeLink = rLink;
259 }
260 
261 void LegendPositionResources::SetAccessibleRelationMemberOf(Window* pMemberOf)
262 {
263 	m_aRbtLeft.SetAccessibleRelationMemberOf(pMemberOf);
264 	m_aRbtRight.SetAccessibleRelationMemberOf(pMemberOf);
265 	m_aRbtTop.SetAccessibleRelationMemberOf(pMemberOf);
266 	m_aRbtBottom.SetAccessibleRelationMemberOf(pMemberOf);
267 }
268 
269 //.............................................................................
270 } //namespace chart
271 //.............................................................................
272 
273