Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / sidebar / ChartErrorBarPanel.cxx
blobc9430791b3c9c9f2322c6c5014ea76d14860dd82
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 <com/sun/star/chart/ErrorBarStyle.hpp>
21 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include "ChartErrorBarPanel.hxx"
24 #include <ChartController.hxx>
25 #include <ChartModel.hxx>
26 #include <vcl/svapp.hxx>
27 #include <sal/log.hxx>
30 using namespace css;
31 using namespace css::uno;
33 namespace chart::sidebar {
35 namespace {
37 enum class ErrorBarDirection
39 POSITIVE,
40 NEGATIVE
43 css::uno::Reference<css::beans::XPropertySet> getErrorBarPropSet(
44 const rtl::Reference<::chart::ChartModel>& xModel, std::u16string_view rCID)
46 return ObjectIdentifier::getObjectPropertySet(rCID, xModel);
49 bool showPositiveError(const rtl::Reference<::chart::ChartModel>& xModel,
50 std::u16string_view rCID)
52 css::uno::Reference<css::beans::XPropertySet> xPropSet =
53 getErrorBarPropSet(xModel, rCID);
55 if (!xPropSet.is())
56 return false;
58 css::uno::Any aAny = xPropSet->getPropertyValue("ShowPositiveError");
60 if (!aAny.hasValue())
61 return false;
63 bool bShow = false;
64 aAny >>= bShow;
65 return bShow;
68 bool showNegativeError(const rtl::Reference<::chart::ChartModel>& xModel,
69 std::u16string_view rCID)
71 css::uno::Reference<css::beans::XPropertySet> xPropSet =
72 getErrorBarPropSet(xModel, rCID);
74 if (!xPropSet.is())
75 return false;
77 css::uno::Any aAny = xPropSet->getPropertyValue("ShowNegativeError");
79 if (!aAny.hasValue())
80 return false;
82 bool bShow = false;
83 aAny >>= bShow;
84 return bShow;
87 void setShowPositiveError(const rtl::Reference<::chart::ChartModel>& xModel,
88 std::u16string_view rCID, bool bShow)
90 css::uno::Reference<css::beans::XPropertySet> xPropSet =
91 getErrorBarPropSet(xModel, rCID);
93 if (!xPropSet.is())
94 return;
96 xPropSet->setPropertyValue("ShowPositiveError", css::uno::Any(bShow));
99 void setShowNegativeError(const rtl::Reference<::chart::ChartModel>& xModel,
100 std::u16string_view rCID, bool bShow)
102 css::uno::Reference<css::beans::XPropertySet> xPropSet =
103 getErrorBarPropSet(xModel, rCID);
105 if (!xPropSet.is())
106 return;
108 xPropSet->setPropertyValue("ShowNegativeError", css::uno::Any(bShow));
111 struct ErrorBarTypeMap
113 sal_Int32 nPos;
114 sal_Int32 nApi;
117 ErrorBarTypeMap const aErrorBarType[] = {
118 { 0, css::chart::ErrorBarStyle::ABSOLUTE },
119 { 1, css::chart::ErrorBarStyle::RELATIVE },
120 { 2, css::chart::ErrorBarStyle::FROM_DATA },
121 { 3, css::chart::ErrorBarStyle::STANDARD_DEVIATION },
122 { 4, css::chart::ErrorBarStyle::STANDARD_ERROR },
123 { 5, css::chart::ErrorBarStyle::VARIANCE},
124 { 6, css::chart::ErrorBarStyle::ERROR_MARGIN },
127 sal_Int32 getTypePos(const rtl::Reference<::chart::ChartModel>& xModel,
128 std::u16string_view rCID)
130 css::uno::Reference<css::beans::XPropertySet> xPropSet =
131 getErrorBarPropSet(xModel, rCID);
133 if (!xPropSet.is())
134 return 0;
136 css::uno::Any aAny = xPropSet->getPropertyValue("ErrorBarStyle");
138 if (!aAny.hasValue())
139 return 0;
141 sal_Int32 nApi = 0;
142 aAny >>= nApi;
144 for (ErrorBarTypeMap const & i : aErrorBarType)
146 if (i.nApi == nApi)
147 return i.nPos;
150 return 0;
153 void setTypePos(const rtl::Reference<::chart::ChartModel>& xModel,
154 std::u16string_view rCID, sal_Int32 nPos)
156 css::uno::Reference<css::beans::XPropertySet> xPropSet =
157 getErrorBarPropSet(xModel, rCID);
159 if (!xPropSet.is())
160 return;
162 sal_Int32 nApi = 0;
163 for (ErrorBarTypeMap const & i : aErrorBarType)
165 if (i.nPos == nPos)
166 nApi = i.nApi;
169 xPropSet->setPropertyValue("ErrorBarStyle", css::uno::Any(nApi));
172 double getValue(const rtl::Reference<::chart::ChartModel>& xModel,
173 std::u16string_view rCID, ErrorBarDirection eDir)
175 css::uno::Reference<css::beans::XPropertySet> xPropSet =
176 getErrorBarPropSet(xModel, rCID);
178 if (!xPropSet.is())
179 return 0;
181 OUString aName = "PositiveError";
182 if (eDir == ErrorBarDirection::NEGATIVE)
183 aName = "NegativeError";
185 css::uno::Any aAny = xPropSet->getPropertyValue(aName);
187 if (!aAny.hasValue())
188 return 0;
190 double nVal = 0;
191 aAny >>= nVal;
193 return nVal;
196 void setValue(const rtl::Reference<::chart::ChartModel>& xModel,
197 std::u16string_view rCID, double nVal, ErrorBarDirection eDir)
199 css::uno::Reference<css::beans::XPropertySet> xPropSet =
200 getErrorBarPropSet(xModel, rCID);
202 if (!xPropSet.is())
203 return;
205 OUString aName = "PositiveError";
206 if (eDir == ErrorBarDirection::NEGATIVE)
207 aName = "NegativeError";
209 xPropSet->setPropertyValue(aName, css::uno::Any(nVal));
212 OUString getCID(const rtl::Reference<::chart::ChartModel>& xModel)
214 css::uno::Reference<css::frame::XController> xController(xModel->getCurrentController());
215 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(xController, css::uno::UNO_QUERY);
216 if (!xSelectionSupplier.is())
217 return OUString();
219 uno::Any aAny = xSelectionSupplier->getSelection();
220 assert(aAny.hasValue());
221 OUString aCID;
222 aAny >>= aCID;
223 #if defined DBG_UTIL && !defined NDEBUG
224 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
225 if (eType != OBJECTTYPE_DATA_ERRORS_X &&
226 eType != OBJECTTYPE_DATA_ERRORS_Y &&
227 eType != OBJECTTYPE_DATA_ERRORS_Z)
228 SAL_WARN("chart2","Selected item is not an error bar");
230 #endif
232 return aCID;
237 ChartErrorBarPanel::ChartErrorBarPanel(weld::Widget* pParent, ChartController* pController)
238 : PanelLayout(pParent, "ChartErrorBarPanel", "modules/schart/ui/sidebarerrorbar.ui")
239 , mxRBPosAndNeg(m_xBuilder->weld_radio_button("radiobutton_positive_negative"))
240 , mxRBPos(m_xBuilder->weld_radio_button("radiobutton_positive"))
241 , mxRBNeg(m_xBuilder->weld_radio_button("radiobutton_negative"))
242 , mxLBType(m_xBuilder->weld_combo_box("comboboxtext_type"))
243 , mxMFPos(m_xBuilder->weld_spin_button("spinbutton_pos"))
244 , mxMFNeg(m_xBuilder->weld_spin_button("spinbutton_neg"))
245 , mxModel(pController->getChartModel())
246 , mxListener(new ChartSidebarModifyListener(this))
247 , mbModelValid(true)
249 Initialize();
252 ChartErrorBarPanel::~ChartErrorBarPanel()
254 doUpdateModel(nullptr);
256 mxRBPosAndNeg.reset();
257 mxRBPos.reset();
258 mxRBNeg.reset();
260 mxLBType.reset();
262 mxMFPos.reset();
263 mxMFNeg.reset();
266 void ChartErrorBarPanel::Initialize()
268 mxModel->addModifyListener(mxListener);
269 mxRBNeg->set_active(false);
270 mxRBPos->set_active(false);
271 mxRBPosAndNeg->set_active(false);
273 updateData();
275 Link<weld::Toggleable&,void> aLink = LINK(this, ChartErrorBarPanel, RadioBtnHdl);
276 mxRBPosAndNeg->connect_toggled(aLink);
277 mxRBPos->connect_toggled(aLink);
278 mxRBNeg->connect_toggled(aLink);
280 mxLBType->connect_changed(LINK(this, ChartErrorBarPanel, ListBoxHdl));
282 Link<weld::SpinButton&,void> aLink2 = LINK(this, ChartErrorBarPanel, NumericFieldHdl);
283 mxMFPos->connect_value_changed(aLink2);
284 mxMFNeg->connect_value_changed(aLink2);
287 void ChartErrorBarPanel::updateData()
289 if (!mbModelValid)
290 return;
292 OUString aCID = getCID(mxModel);
293 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
294 if (eType != OBJECTTYPE_DATA_ERRORS_X &&
295 eType != OBJECTTYPE_DATA_ERRORS_Y &&
296 eType != OBJECTTYPE_DATA_ERRORS_Z)
297 return;
299 bool bPos = showPositiveError(mxModel, aCID);
300 bool bNeg = showNegativeError(mxModel, aCID);
302 SolarMutexGuard aGuard;
304 if (bPos && bNeg)
305 mxRBPosAndNeg->set_active(true);
306 else if (bPos)
307 mxRBPos->set_active(true);
308 else if (bNeg)
309 mxRBNeg->set_active(true);
311 sal_Int32 nTypePos = getTypePos(mxModel, aCID);
312 mxLBType->set_active(nTypePos);
314 if (nTypePos <= 1)
316 if (bPos)
317 mxMFPos->set_sensitive(true);
318 else
319 mxMFPos->set_sensitive(false);
321 if (bNeg)
322 mxMFNeg->set_sensitive(true);
323 else
324 mxMFNeg->set_sensitive(false);
326 double nValPos = getValue(mxModel, aCID, ErrorBarDirection::POSITIVE);
327 double nValNeg = getValue(mxModel, aCID, ErrorBarDirection::NEGATIVE);
329 mxMFPos->set_value(nValPos);
330 mxMFNeg->set_value(nValNeg);
332 else
334 mxMFPos->set_sensitive(false);
335 mxMFNeg->set_sensitive(false);
339 std::unique_ptr<PanelLayout> ChartErrorBarPanel::Create (
340 weld::Widget* pParent,
341 ChartController* pController)
343 if (pParent == nullptr)
344 throw lang::IllegalArgumentException("no parent Window given to ChartErrorBarPanel::Create", nullptr, 0);
345 return std::make_unique<ChartErrorBarPanel>(pParent, pController);
348 void ChartErrorBarPanel::DataChanged(const DataChangedEvent& rEvent)
350 PanelLayout::DataChanged(rEvent);
351 updateData();
354 void ChartErrorBarPanel::HandleContextChange(
355 const vcl::EnumContext& )
357 updateData();
360 void ChartErrorBarPanel::NotifyItemUpdate(
361 sal_uInt16 /*nSID*/,
362 SfxItemState /*eState*/,
363 const SfxPoolItem* /*pState*/ )
367 void ChartErrorBarPanel::modelInvalid()
369 mbModelValid = false;
372 void ChartErrorBarPanel::doUpdateModel(const rtl::Reference<::chart::ChartModel>& xModel)
374 if (mbModelValid)
376 mxModel->removeModifyListener(mxListener);
379 mxModel = xModel;
380 mbModelValid = mxModel.is();
382 if (!mbModelValid)
383 return;
385 mxModel->addModifyListener(mxListener);
388 void ChartErrorBarPanel::updateModel(css::uno::Reference<css::frame::XModel> xModel)
390 ::chart::ChartModel* pModel = dynamic_cast<::chart::ChartModel*>(xModel.get());
391 assert(!xModel || pModel);
392 doUpdateModel(pModel);
395 IMPL_LINK_NOARG(ChartErrorBarPanel, RadioBtnHdl, weld::Toggleable&, void)
397 OUString aCID = getCID(mxModel);
398 bool bPos = mxRBPosAndNeg->get_active() || mxRBPos->get_active();
399 bool bNeg = mxRBPosAndNeg->get_active() || mxRBNeg->get_active();
401 setShowPositiveError(mxModel, aCID, bPos);
402 setShowNegativeError(mxModel, aCID, bNeg);
405 IMPL_LINK_NOARG(ChartErrorBarPanel, ListBoxHdl, weld::ComboBox&, void)
407 OUString aCID = getCID(mxModel);
408 sal_Int32 nPos = mxLBType->get_active();
410 setTypePos(mxModel, aCID, nPos);
413 IMPL_LINK(ChartErrorBarPanel, NumericFieldHdl, weld::SpinButton&, rMetricField, void)
415 OUString aCID = getCID(mxModel);
416 double nVal = rMetricField.get_value();
417 if (&rMetricField == mxMFPos.get())
418 setValue(mxModel, aCID, nVal, ErrorBarDirection::POSITIVE);
419 else if (&rMetricField == mxMFNeg.get())
420 setValue(mxModel, aCID, nVal, ErrorBarDirection::NEGATIVE);
423 } // end of namespace ::chart::sidebar
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */