Debian Codegraph @

chart2/source/controller/sidebar/ChartLinePanel.cxx

raw
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
10 #include "ChartLinePanel.hxx"
11
12 #include <ChartController.hxx>
13 #include <ChartModel.hxx>
14
15 #include <svx/xlnwtit.hxx>
16 #include <svx/xlntrit.hxx>
17
18 #include <svx/linectrl.hxx>
19 #include <svx/tbcontrl.hxx>
20 #include <sfx2/weldutils.hxx>
21 #include <vcl/svapp.hxx>
22
23 #include <com/sun/star/view/XSelectionSupplier.hpp>
24 #include <com/sun/star/chart2/XDiagram.hpp>
25
26 #include <comphelper/lok.hxx>
27 #include <sfx2/viewsh.hxx>
28 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
29
30 namespace chart::sidebar {
31
32 namespace {
33
34 SvxLineStyleToolBoxControl* getLineStyleToolBoxControl(const ToolbarUnoDispatcher& rToolBoxColor)
35 {
36 css::uno::Reference<css::frame::XToolbarController> xController = rToolBoxColor.GetControllerForCommand(u".uno:XLineStyle"_ustr);
37 SvxLineStyleToolBoxControl* pToolBoxLineStyleControl = dynamic_cast<SvxLineStyleToolBoxControl*>(xController.get());
38 return pToolBoxLineStyleControl;
39 }
40
41 SvxColorToolBoxControl* getColorToolBoxControl(const ToolbarUnoDispatcher& rToolBoxLineStyle)
42 {
43 css::uno::Reference<css::frame::XToolbarController> xController = rToolBoxLineStyle.GetControllerForCommand(u".uno:XLineColor"_ustr);
44 SvxColorToolBoxControl* pToolBoxColorControl = dynamic_cast<SvxColorToolBoxControl*>(xController.get());
45 return pToolBoxColorControl;
46 }
47
48 OUString getCID(const rtl::Reference<::chart::ChartModel>& xModel)
49 {
50 css::uno::Reference<css::frame::XController> xController(xModel->getCurrentController());
51 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(xController, css::uno::UNO_QUERY);
52 if (!xSelectionSupplier.is())
53 return OUString();
54
55 css::uno::Any aAny = xSelectionSupplier->getSelection();
56 if (!aAny.hasValue())
57 return OUString();
58
59 OUString aCID;
60 aAny >>= aCID;
61
62 return aCID;
63 }
64
65 css::uno::Reference<css::beans::XPropertySet> getPropSet(
66 const rtl::Reference<::chart::ChartModel>& xModel)
67 {
68 OUString aCID = getCID(xModel);
69 css::uno::Reference<css::beans::XPropertySet> xPropSet =
70 ObjectIdentifier::getObjectPropertySet(aCID, xModel);
71
72 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
73 if (eType == OBJECTTYPE_DIAGRAM)
74 {
75 css::uno::Reference<css::chart2::XDiagram> xDiagram(
76 xPropSet, css::uno::UNO_QUERY);
77 if (!xDiagram.is())
78 return xPropSet;
79
80 xPropSet.set(xDiagram->getWall());
81 }
82
83 return xPropSet;
84 }
85
86 class PreventUpdate
87 {
88 public:
89 explicit PreventUpdate(bool& bUpdate):
90 mbUpdate(bUpdate)
91 {
92 mbUpdate = false;
93 }
94
95 ~PreventUpdate()
96 {
97 mbUpdate = true;
98 }
99
100 private:
101 bool& mbUpdate;
102 };
103
104 }
105
106 std::unique_ptr<PanelLayout> ChartLinePanel::Create(
107 weld::Widget* pParent,
108 const css::uno::Reference<css::frame::XFrame>& rxFrame,
109 ChartController* pController)
110 {
111 if (pParent == nullptr)
112 throw css::lang::IllegalArgumentException(u"no parent Window given to ChartAxisPanel::Create"_ustr, nullptr, 0);
113 if (!rxFrame.is())
114 throw css::lang::IllegalArgumentException(u"no XFrame given to ChartAxisPanel::Create"_ustr, nullptr, 1);
115
116 return std::make_unique<ChartLinePanel>(pParent, rxFrame, pController);
117 }
118
119 ChartLinePanel::ChartLinePanel(weld::Widget* pParent,
120 const css::uno::Reference<css::frame::XFrame>& rxFrame,
121 ChartController* pController):
122 svx::sidebar::LinePropertyPanelBase(pParent, rxFrame),
123 mxModel(pController->getChartModel()),
124 mxListener(new ChartSidebarModifyListener(this)),
125 mxSelectionListener(new ChartSidebarSelectionListener(this)),
126 mbUpdate(true),
127 mbModelValid(true),
128 maLineColorWrapper(mxModel, getColorToolBoxControl(*mxColorDispatch), u"LineColor"_ustr),
129 maLineStyleWrapper(mxModel, getLineStyleToolBoxControl(*mxLineStyleDispatch))
130 {
131 disableArrowHead();
132 std::vector<ObjectType> aAcceptedTypes { OBJECTTYPE_PAGE, OBJECTTYPE_DIAGRAM,
133 OBJECTTYPE_DATA_SERIES, OBJECTTYPE_DATA_POINT,
134 OBJECTTYPE_TITLE, OBJECTTYPE_LEGEND, OBJECTTYPE_DATA_CURVE,
135 OBJECTTYPE_DATA_AVERAGE_LINE, OBJECTTYPE_AXIS};
136 mxSelectionListener->setAcceptedTypes(std::move(aAcceptedTypes));
137 Initialize();
138 }
139
140 ChartLinePanel::~ChartLinePanel()
141 {
142 doUpdateModel(nullptr);
143 }
144
145 void ChartLinePanel::Initialize()
146 {
147 mxModel->addModifyListener(mxListener);
148
149 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
150 if (xSelectionSupplier.is())
151 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
152
153 SvxColorToolBoxControl* pToolBoxColor = getColorToolBoxControl(*mxColorDispatch);
154 pToolBoxColor->setColorSelectFunction(maLineColorWrapper);
155
156 SvxLineStyleToolBoxControl* pToolBoxLineStyle = getLineStyleToolBoxControl(*mxLineStyleDispatch);
157 pToolBoxLineStyle->setLineStyleSelectFunction(maLineStyleWrapper);
158
159 setMapUnit(MapUnit::Map100thMM);
160 updateData();
161 }
162
163 void ChartLinePanel::updateData()
164 {
165 if (!mbUpdate || !mbModelValid)
166 return;
167
168 SolarMutexGuard aGuard;
169 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
170 if (!xPropSet.is())
171 return;
172
173 sal_uInt16 nLineTransparence = 0;
174 xPropSet->getPropertyValue(u"LineTransparence"_ustr) >>= nLineTransparence;
175 XLineTransparenceItem aLineTransparenceItem(nLineTransparence);
176 updateLineTransparence(false, true, &aLineTransparenceItem);
177
178 sal_uInt32 nWidth = 0;
179 xPropSet->getPropertyValue(u"LineWidth"_ustr) >>= nWidth;
180 XLineWidthItem aWidthItem(nWidth);
181 updateLineWidth(false, true, &aWidthItem);
182
183 maLineStyleWrapper.updateData();
184 maLineColorWrapper.updateData();
185 }
186
187 void ChartLinePanel::modelInvalid()
188 {
189 mbModelValid = false;
190 }
191
192 void ChartLinePanel::selectionChanged(bool bCorrectType)
193 {
194 if (bCorrectType)
195 updateData();
196 }
197
198 void ChartLinePanel::doUpdateModel(const rtl::Reference<::chart::ChartModel>& xModel)
199 {
200 if (mbModelValid)
201 {
202 mxModel->removeModifyListener(mxListener);
203
204 css::uno::Reference<css::view::XSelectionSupplier> oldSelectionSupplier(
205 mxModel->getCurrentController(), css::uno::UNO_QUERY);
206 if (oldSelectionSupplier.is()) {
207 oldSelectionSupplier->removeSelectionChangeListener(mxSelectionListener);
208 }
209 }
210
211 mxModel = xModel;
212 mbModelValid = mxModel.is();
213
214 if (!mbModelValid)
215 return;
216
217 maLineStyleWrapper.updateModel(mxModel);
218 maLineColorWrapper.updateModel(mxModel);
219
220 mxModel->addModifyListener(mxListener);
221
222 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
223 if (xSelectionSupplier.is())
224 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
225 }
226
227 void ChartLinePanel::updateModel(css::uno::Reference<css::frame::XModel> xModel)
228 {
229 ::chart::ChartModel* pModel = dynamic_cast<::chart::ChartModel*>(xModel.get());
230 assert(!xModel || pModel);
231 doUpdateModel(pModel);
232 }
233
234 void ChartLinePanel::setLineTransparency(const XLineTransparenceItem& rItem)
235 {
236 css::uno::Reference<css::beans::XPropertySet> xPropSet =
237 getPropSet(mxModel);
238
239 if (!xPropSet.is())
240 return;
241
242 PreventUpdate aPreventUpdate(mbUpdate);
243 xPropSet->setPropertyValue(u"LineTransparence"_ustr, css::uno::Any(rItem.GetValue()));
244 }
245
246 void ChartLinePanel::setLineWidth(const XLineWidthItem& rItem)
247 {
248 css::uno::Reference<css::beans::XPropertySet> xPropSet =
249 getPropSet(mxModel);
250
251 if (!xPropSet.is())
252 return;
253
254 PreventUpdate aPreventUpdate(mbUpdate);
255 xPropSet->setPropertyValue(u"LineWidth"_ustr, css::uno::Any(rItem.GetValue()));
256 }
257
258 void ChartLinePanel::updateLineWidth(bool bDisabled, bool bSetOrDefault, const SfxPoolItem* pItem)
259 {
260 LinePropertyPanelBase::updateLineWidth(bDisabled, bSetOrDefault, pItem);
261
262 SfxViewShell* pViewShell = SfxViewShell::Current();
263 if (comphelper::LibreOfficeKit::isActive() && pViewShell)
264 {
265 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_STATE_CHANGED,
266 ".uno:LineWidth=" + OString::number(mnWidthCoreValue));
267 }
268 }
269
270 }
271
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */