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