Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / dialogs / res_LegendPosition.cxx
blob1562fcc71943a9040e5195817c7710014191fa8d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <res_LegendPosition.hxx>
21 #include <ChartModelHelper.hxx>
22 #include <Legend.hxx>
23 #include <LegendHelper.hxx>
24 #include <ChartModel.hxx>
25 #include <Diagram.hxx>
27 #include <com/sun/star/chart2/LegendPosition.hpp>
28 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
30 //itemset stuff
31 #include <chartview/ChartSfxItemIds.hxx>
32 #include <svl/intitem.hxx>
33 #include <svl/eitem.hxx>
34 #include <comphelper/diagnose_ex.hxx>
35 #include <utility>
36 #include <vcl/weld.hxx>
38 namespace chart
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::chart2;
44 LegendPositionResources::LegendPositionResources(weld::Builder& rBuilder)
45 : m_xRbtLeft(rBuilder.weld_radio_button("left"))
46 , m_xRbtRight(rBuilder.weld_radio_button("right"))
47 , m_xRbtTop(rBuilder.weld_radio_button("top"))
48 , m_xRbtBottom(rBuilder.weld_radio_button("bottom"))
50 impl_setRadioButtonToggleHdl();
53 LegendPositionResources::LegendPositionResources(weld::Builder& rBuilder,
54 uno::Reference< uno::XComponentContext > xCC)
55 : m_xCC(std::move(xCC))
56 , m_xCbxShow(rBuilder.weld_check_button("show"))
57 , m_xRbtLeft(rBuilder.weld_radio_button("left"))
58 , m_xRbtRight(rBuilder.weld_radio_button("right"))
59 , m_xRbtTop(rBuilder.weld_radio_button("top"))
60 , m_xRbtBottom(rBuilder.weld_radio_button("bottom"))
62 m_xCbxShow->connect_toggled( LINK( this, LegendPositionResources, PositionEnableHdl ) );
63 impl_setRadioButtonToggleHdl();
66 void LegendPositionResources::impl_setRadioButtonToggleHdl()
68 m_xRbtLeft->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) );
69 m_xRbtTop->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) );
70 m_xRbtRight->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) );
71 m_xRbtBottom->connect_toggled( LINK( this, LegendPositionResources, PositionChangeHdl ) );
74 LegendPositionResources::~LegendPositionResources()
78 void LegendPositionResources::writeToResources( const rtl::Reference<::chart::ChartModel>& xChartModel )
80 try
82 rtl::Reference< Diagram > xDiagram = xChartModel->getFirstChartDiagram();
83 rtl::Reference< Legend > xLegend = xDiagram->getLegend2();
84 if( xLegend.is() )
86 //show
87 bool bShowLegend = false;
88 xLegend->getPropertyValue( "Show" ) >>= bShowLegend;
89 if (m_xCbxShow)
90 m_xCbxShow->set_active( bShowLegend );
91 PositionEnable();
93 //position
94 chart2::LegendPosition ePos;
95 xLegend->getPropertyValue( "AnchorPosition" ) >>= ePos;
96 switch( ePos )
98 case chart2::LegendPosition_LINE_START:
99 m_xRbtLeft->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_LINE_END:
108 default:
109 m_xRbtRight->set_active(true);
110 break;
114 catch( const uno::Exception & )
116 DBG_UNHANDLED_EXCEPTION("chart2");
120 void LegendPositionResources::writeToModel( const rtl::Reference<::chart::ChartModel>& xChartModel ) const
124 bool bShowLegend = m_xCbxShow && m_xCbxShow->get_active();
125 ChartModel& rModel = *xChartModel;
126 rtl::Reference< Legend > xProp = LegendHelper::getLegend(rModel, m_xCC, bShowLegend);
127 if( xProp.is() )
129 //show
130 xProp->setPropertyValue( "Show" , uno::Any( bShowLegend ));
132 //position
133 chart2::LegendPosition eNewPos;
134 css::chart::ChartLegendExpansion eExp = css::chart::ChartLegendExpansion_HIGH;
136 if( m_xRbtLeft->get_active() )
137 eNewPos = chart2::LegendPosition_LINE_START;
138 else if( m_xRbtRight->get_active() )
140 eNewPos = chart2::LegendPosition_LINE_END;
142 else if( m_xRbtTop->get_active() )
144 eNewPos = chart2::LegendPosition_PAGE_START;
145 eExp = css::chart::ChartLegendExpansion_WIDE;
147 else if( m_xRbtBottom->get_active() )
149 eNewPos = chart2::LegendPosition_PAGE_END;
150 eExp = css::chart::ChartLegendExpansion_WIDE;
153 xProp->setPropertyValue( "AnchorPosition" , uno::Any( eNewPos ));
154 xProp->setPropertyValue( "Expansion" , uno::Any( eExp ));
155 xProp->setPropertyValue( "RelativePosition" , uno::Any());
158 catch( const uno::Exception & )
160 DBG_UNHANDLED_EXCEPTION("chart2" );
164 IMPL_LINK_NOARG(LegendPositionResources, PositionEnableHdl, weld::Toggleable&, void)
166 PositionEnable();
169 void LegendPositionResources::PositionEnable()
171 bool bEnable = !m_xCbxShow || m_xCbxShow->get_active();
173 m_xRbtLeft->set_sensitive( bEnable );
174 m_xRbtTop->set_sensitive( bEnable );
175 m_xRbtRight->set_sensitive( bEnable );
176 m_xRbtBottom->set_sensitive( bEnable );
178 m_aChangeLink.Call(nullptr);
181 void LegendPositionResources::initFromItemSet( const SfxItemSet& rInAttrs )
183 if( const SfxInt32Item* pPosItem = rInAttrs.GetItemIfSet( SCHATTR_LEGEND_POS ) )
185 chart2::LegendPosition nLegendPosition = static_cast<chart2::LegendPosition>(pPosItem->GetValue());
186 switch( nLegendPosition )
188 case chart2::LegendPosition_LINE_START:
189 m_xRbtLeft->set_active(true);
190 break;
191 case chart2::LegendPosition_PAGE_START:
192 m_xRbtTop->set_active(true);
193 break;
194 case chart2::LegendPosition_LINE_END:
195 m_xRbtRight->set_active(true);
196 break;
197 case chart2::LegendPosition_PAGE_END:
198 m_xRbtBottom->set_active(true);
199 break;
200 default:
201 break;
205 const SfxBoolItem* pShowItem;
206 if( m_xCbxShow && (pShowItem = rInAttrs.GetItemIfSet( SCHATTR_LEGEND_SHOW )) )
208 m_xCbxShow->set_active(pShowItem->GetValue());
212 void LegendPositionResources::writeToItemSet( SfxItemSet& rOutAttrs ) const
214 chart2::LegendPosition nLegendPosition = chart2::LegendPosition_LINE_END;
215 if( m_xRbtLeft->get_active() )
216 nLegendPosition = chart2::LegendPosition_LINE_START;
217 else if( m_xRbtTop->get_active() )
218 nLegendPosition = chart2::LegendPosition_PAGE_START;
219 else if( m_xRbtRight->get_active() )
220 nLegendPosition = chart2::LegendPosition_LINE_END;
221 else if( m_xRbtBottom->get_active() )
222 nLegendPosition = chart2::LegendPosition_PAGE_END;
223 rOutAttrs.Put( SfxInt32Item(SCHATTR_LEGEND_POS, static_cast<sal_Int32>(nLegendPosition) ) );
225 rOutAttrs.Put( SfxBoolItem(SCHATTR_LEGEND_SHOW, !m_xCbxShow || m_xCbxShow->get_active()) );
228 IMPL_LINK (LegendPositionResources, PositionChangeHdl, weld::Toggleable&, rRadio, void)
230 //for each radio click there are coming two change events
231 //first uncheck of previous button -> ignore that call
232 //the second call gives the check of the new button
233 if( rRadio.get_active() )
234 m_aChangeLink.Call(nullptr);
237 void LegendPositionResources::SetChangeHdl( const Link<LinkParamNone*,void>& rLink )
239 m_aChangeLink = rLink;
242 } //namespace chart
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */