Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / sidebar / ChartAxisPanel.cxx
blob419c287d3eeb4aa9ea71bb38daecd25736452544
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/beans/XPropertySet.hpp>
21 #include <com/sun/star/chart/ChartAxisLabelPosition.hpp>
22 #include <com/sun/star/chart2/AxisOrientation.hpp>
23 #include <com/sun/star/chart2/XAxis.hpp>
25 #include <com/sun/star/util/XModifyBroadcaster.hpp>
27 #include "ChartAxisPanel.hxx"
28 #include <ChartController.hxx>
29 #include <vcl/lstbox.hxx>
30 #include <vcl/field.hxx>
32 using namespace css;
33 using namespace css::uno;
35 namespace chart { namespace sidebar {
37 namespace {
39 bool isLabelShown(const css::uno::Reference<css::frame::XModel>& xModel,
40 const OUString& rCID)
42 css::uno::Reference< css::beans::XPropertySet > xAxis(
43 ObjectIdentifier::getAxisForCID(rCID, xModel), uno::UNO_QUERY );
45 if (!xAxis.is())
46 return false;
48 uno::Any aAny = xAxis->getPropertyValue("DisplayLabels");
49 if (!aAny.hasValue())
50 return false;
52 bool bVisible = false;
53 aAny >>= bVisible;
54 return bVisible;
57 void setLabelShown(const css::uno::Reference<css::frame::XModel>& xModel,
58 const OUString& rCID, bool bVisible)
60 css::uno::Reference< css::beans::XPropertySet > xAxis(
61 ObjectIdentifier::getAxisForCID(rCID, xModel), uno::UNO_QUERY );
63 if (!xAxis.is())
64 return;
66 xAxis->setPropertyValue("DisplayLabels", css::uno::Any(bVisible));
69 struct AxisLabelPosMap
71 sal_Int32 nPos;
72 css::chart::ChartAxisLabelPosition ePos;
75 static AxisLabelPosMap const aLabelPosMap[] = {
76 { 0, css::chart::ChartAxisLabelPosition_NEAR_AXIS },
77 { 1, css::chart::ChartAxisLabelPosition_NEAR_AXIS_OTHER_SIDE },
78 { 2, css::chart::ChartAxisLabelPosition_OUTSIDE_START },
79 { 3, css::chart::ChartAxisLabelPosition_OUTSIDE_END }
82 sal_Int32 getLabelPosition(const css::uno::Reference<css::frame::XModel>& xModel,
83 const OUString& rCID)
85 css::uno::Reference< css::beans::XPropertySet > xAxis(
86 ObjectIdentifier::getAxisForCID(rCID, xModel), uno::UNO_QUERY );
88 if (!xAxis.is())
89 return 0;
91 uno::Any aAny = xAxis->getPropertyValue("LabelPosition");
92 if (!aAny.hasValue())
93 return 0;
95 css::chart::ChartAxisLabelPosition ePos;
96 aAny >>= ePos;
97 for (AxisLabelPosMap const & i : aLabelPosMap)
99 if (i.ePos == ePos)
100 return i.nPos;
103 return 0;
106 void setLabelPosition(const css::uno::Reference<css::frame::XModel>& xModel,
107 const OUString& rCID, sal_Int32 nPos)
109 css::uno::Reference< css::beans::XPropertySet > xAxis(
110 ObjectIdentifier::getAxisForCID(rCID, xModel), uno::UNO_QUERY );
112 if (!xAxis.is())
113 return;
115 css::chart::ChartAxisLabelPosition ePos;
116 for (AxisLabelPosMap const & i : aLabelPosMap)
118 if (i.nPos == nPos)
119 ePos = i.ePos;
122 xAxis->setPropertyValue("LabelPosition", css::uno::Any(ePos));
125 bool isReverse(const css::uno::Reference<css::frame::XModel>& xModel,
126 const OUString& rCID)
128 css::uno::Reference< css::chart2::XAxis > xAxis =
129 ObjectIdentifier::getAxisForCID(rCID, xModel);
131 if (!xAxis.is())
132 return false;
134 css::chart2::ScaleData aData = xAxis->getScaleData();
136 return aData.Orientation == css::chart2::AxisOrientation_REVERSE;
139 void setReverse(const css::uno::Reference<css::frame::XModel>& xModel,
140 const OUString& rCID, bool bReverse)
142 css::uno::Reference< css::chart2::XAxis > xAxis =
143 ObjectIdentifier::getAxisForCID(rCID, xModel);
145 if (!xAxis.is())
146 return;
148 css::chart2::ScaleData aData = xAxis->getScaleData();
149 if (bReverse)
150 aData.Orientation = css::chart2::AxisOrientation_REVERSE;
151 else
152 aData.Orientation = css::chart2::AxisOrientation_MATHEMATICAL;
154 xAxis->setScaleData(aData);
157 OUString getCID(const css::uno::Reference<css::frame::XModel>& xModel)
159 css::uno::Reference<css::frame::XController> xController(xModel->getCurrentController());
160 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(xController, css::uno::UNO_QUERY);
161 if (!xSelectionSupplier.is())
162 return OUString();
164 uno::Any aAny = xSelectionSupplier->getSelection();
165 assert(aAny.hasValue());
166 OUString aCID;
167 aAny >>= aCID;
168 #if defined DBG_UTIL && !defined NDEBUG
169 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
170 assert(eType == OBJECTTYPE_AXIS);
171 #endif
173 return aCID;
176 void setAxisRotation(const css::uno::Reference<css::frame::XModel>& xModel,
177 const OUString& rCID, double nVal)
179 css::uno::Reference< css::beans::XPropertySet > xAxis(
180 ObjectIdentifier::getAxisForCID(rCID, xModel), uno::UNO_QUERY );
182 if (!xAxis.is())
183 return;
185 xAxis->setPropertyValue("TextRotation", css::uno::Any(nVal));
188 double getAxisRotation(const css::uno::Reference<css::frame::XModel>& xModel,
189 const OUString& rCID)
191 css::uno::Reference< css::beans::XPropertySet > xAxis(
192 ObjectIdentifier::getAxisForCID(rCID, xModel), uno::UNO_QUERY );
194 if (!xAxis.is())
195 return 0;
197 css::uno::Any aAny = xAxis->getPropertyValue("TextRotation");
198 double nVal = 0;
199 aAny >>= nVal;
200 return nVal;
205 ChartAxisPanel::ChartAxisPanel(
206 vcl::Window* pParent,
207 const css::uno::Reference<css::frame::XFrame>& rxFrame,
208 ChartController* pController)
209 : PanelLayout(pParent, "ChartAxisPanel", "modules/schart/ui/sidebaraxis.ui", rxFrame),
210 mxModel(pController->getModel()),
211 mxModifyListener(new ChartSidebarModifyListener(this)),
212 mxSelectionListener(new ChartSidebarSelectionListener(this, OBJECTTYPE_AXIS)),
213 mbModelValid(true)
215 get(mpCBShowLabel, "checkbutton_show_label");
216 get(mpCBReverse, "checkbutton_reverse");
218 get(mpLBLabelPos, "comboboxtext_label_position");
219 get(mpNFRotation, "spinbutton1");
220 get(mpGridLabel, "label_props");
222 Initialize();
225 ChartAxisPanel::~ChartAxisPanel()
227 disposeOnce();
230 void ChartAxisPanel::dispose()
232 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
233 xBroadcaster->removeModifyListener(mxModifyListener);
235 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
236 if (xSelectionSupplier.is())
237 xSelectionSupplier->removeSelectionChangeListener(mxSelectionListener);
239 mpCBShowLabel.clear();
240 mpCBReverse.clear();
242 mpLBLabelPos.clear();
243 mpGridLabel.clear();
245 mpNFRotation.clear();
247 PanelLayout::dispose();
250 void ChartAxisPanel::Initialize()
252 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
253 xBroadcaster->addModifyListener(mxModifyListener);
255 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
256 if (xSelectionSupplier.is())
257 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
259 updateData();
261 Link<Button*,void> aLink = LINK(this, ChartAxisPanel, CheckBoxHdl);
262 mpCBShowLabel->SetClickHdl(aLink);
263 mpCBReverse->SetClickHdl(aLink);
265 Link<Edit&, void> aSpinButtonLink = LINK(this, ChartAxisPanel, TextRotationHdl);
266 mpNFRotation->SetModifyHdl(aSpinButtonLink);
268 mpLBLabelPos->SetSelectHdl(LINK(this, ChartAxisPanel, ListBoxHdl));
271 void ChartAxisPanel::updateData()
273 if (!mbModelValid)
274 return;
276 OUString aCID = getCID(mxModel);
277 SolarMutexGuard aGuard;
279 mpCBShowLabel->Check(isLabelShown(mxModel, aCID));
280 mpCBReverse->Check(isReverse(mxModel, aCID));
282 mpLBLabelPos->SelectEntryPos(getLabelPosition(mxModel, aCID));
283 mpNFRotation->SetValue(getAxisRotation(mxModel, aCID));
286 VclPtr<vcl::Window> ChartAxisPanel::Create (
287 vcl::Window* pParent,
288 const css::uno::Reference<css::frame::XFrame>& rxFrame,
289 ChartController* pController)
291 if (pParent == nullptr)
292 throw lang::IllegalArgumentException("no parent Window given to ChartAxisPanel::Create", nullptr, 0);
293 if ( ! rxFrame.is())
294 throw lang::IllegalArgumentException("no XFrame given to ChartAxisPanel::Create", nullptr, 1);
296 return VclPtr<ChartAxisPanel>::Create(
297 pParent, rxFrame, pController);
300 void ChartAxisPanel::DataChanged(
301 const DataChangedEvent& )
303 updateData();
306 void ChartAxisPanel::HandleContextChange(
307 const vcl::EnumContext& )
309 updateData();
312 void ChartAxisPanel::NotifyItemUpdate(
313 sal_uInt16 /*nSID*/,
314 SfxItemState /*eState*/,
315 const SfxPoolItem* /*pState*/ )
319 void ChartAxisPanel::modelInvalid()
321 mbModelValid = false;
324 void ChartAxisPanel::updateModel(
325 css::uno::Reference<css::frame::XModel> xModel)
327 if (mbModelValid)
329 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
330 xBroadcaster->removeModifyListener(mxModifyListener);
333 mxModel = xModel;
334 mbModelValid = true;
336 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcasterNew(mxModel, css::uno::UNO_QUERY_THROW);
337 xBroadcasterNew->addModifyListener(mxModifyListener);
339 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
340 if (xSelectionSupplier.is())
341 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
344 void ChartAxisPanel::selectionChanged(bool bCorrectType)
346 if (bCorrectType)
347 updateData();
350 IMPL_LINK(ChartAxisPanel, CheckBoxHdl, Button*, pButton, void)
352 CheckBox* pCheckbox = static_cast<CheckBox*>(pButton);
353 OUString aCID = getCID(mxModel);
354 bool bChecked = pCheckbox->IsChecked();
356 if (pCheckbox == mpCBShowLabel.get())
358 mpGridLabel->Enable(bChecked);
359 setLabelShown(mxModel, aCID, bChecked);
361 else if (pCheckbox == mpCBReverse.get())
362 setReverse(mxModel, aCID, bChecked);
365 IMPL_LINK_NOARG(ChartAxisPanel, ListBoxHdl, ListBox&, void)
367 OUString aCID = getCID(mxModel);
368 sal_Int32 nPos = mpLBLabelPos->GetSelectedEntryPos();
370 setLabelPosition(mxModel, aCID, nPos);
373 IMPL_LINK(ChartAxisPanel, TextRotationHdl, Edit&, rMetricField, void)
375 OUString aCID = getCID(mxModel);
376 double nVal = static_cast<NumericField&>(rMetricField).GetValue();
377 setAxisRotation(mxModel, aCID, nVal);
380 }} // end of namespace ::chart::sidebar
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */