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