Debian Codegraph @

chart2/source/controller/sidebar/ChartColorsPanel.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 * 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 <vcl/svapp.hxx>
21
22 #include "ChartColorsPanel.hxx"
23 #include "ChartColorPaletteControl.hxx"
24
25 #include <ChartColorPaletteHelper.hxx>
26 #include <ChartController.hxx>
27 #include <ChartModel.hxx>
28 #include <DataSeries.hxx>
29 #include <Diagram.hxx>
30
31 #include <com/sun/star/drawing/FillStyle.hpp>
32
33 #include <sfx2/weldutils.hxx>
34
35 #include <algorithm>
36
37 using namespace css;
38
39 namespace chart::sidebar
40 {
41 namespace
42 {
43 OUString getCID(const uno::Reference<frame::XModel>& xModel)
44 {
45 const uno::Reference<frame::XController> xController(xModel->getCurrentController());
46 uno::Reference<view::XSelectionSupplier> xSelectionSupplier(xController, uno::UNO_QUERY);
47 if (!xSelectionSupplier.is())
48 return {};
49
50 uno::Any aAny = xSelectionSupplier->getSelection();
51 if (!aAny.hasValue())
52 {
53 // if no selection, default to diagram wall so sidebar can show some editable properties
54 if (auto* pController = dynamic_cast<ChartController*>(xController.get()))
55 {
56 pController->select(
57 uno::Any(ObjectIdentifier::createClassifiedIdentifier(OBJECTTYPE_PAGE, u"")));
58 xSelectionSupplier
59 = uno::Reference<css::view::XSelectionSupplier>(xController, uno::UNO_QUERY);
60 if (xSelectionSupplier.is())
61 aAny = xSelectionSupplier->getSelection();
62 }
63
64 if (!aAny.hasValue())
65 return {};
66 }
67
68 OUString aCID;
69 aAny >>= aCID;
70 return aCID;
71 }
72
73 ChartColorPaletteControl* getChartColorPaletteControl(const ToolbarUnoDispatcher& rToolBoxColor)
74 {
75 const uno::Reference<frame::XToolbarController> xController
76 = rToolBoxColor.GetControllerForCommand(sUnoChartColorPalette);
77 const auto pToolBoxLineStyleControl
78 = dynamic_cast<ChartColorPaletteControl*>(xController.get());
79 return pToolBoxLineStyleControl;
80 }
81 } // end unnamed namespace
82
83 class ColorPaletteWrapper final : public IColorPaletteHandler
84 {
85 public:
86 ColorPaletteWrapper(rtl::Reference<ChartModel> mxModel, ChartColorPaletteControl* pControl);
87
88 void updateModel(const rtl::Reference<ChartModel>& xModel);
89 void updateData() const;
90
91 void createDiagramSnapshot() override;
92 void restoreOriginalDiagram() override;
93
94 void select(ChartColorPaletteType eType, sal_uInt32 nIndex) override;
95 void apply(const ChartColorPalette* pColorPalette) override;
96 [[nodiscard]] std::shared_ptr<ChartColorPaletteHelper> getHelper() const override;
97 [[nodiscard]] ChartColorPaletteType getType() const override;
98 [[nodiscard]] sal_uInt32 getIndex() const override;
99
100 private:
101 rtl::Reference<ChartModel> mxModel;
102 ChartColorPaletteControl* mpControl;
103 rtl::Reference<Diagram> mxDiagramSnapshot;
104 };
105
106 ColorPaletteWrapper::ColorPaletteWrapper(rtl::Reference<ChartModel> xModel,
107 ChartColorPaletteControl* pControl)
108 : mxModel(std::move(xModel))
109 , mpControl(pControl)
110 {
111 }
112
113 void ColorPaletteWrapper::updateModel(const rtl::Reference<ChartModel>& xModel)
114 {
115 mxModel = xModel;
116 }
117
118 void ColorPaletteWrapper::updateData() const
119 {
120 frame::FeatureStateEvent aEvent;
121 aEvent.FeatureURL.Complete = sUnoChartColorPalette;
122 aEvent.IsEnabled = true;
123
124 if (mpControl)
125 mpControl->statusChanged(aEvent);
126 }
127
128 void ColorPaletteWrapper::createDiagramSnapshot()
129 {
130 const rtl::Reference<Diagram> xDiagram = mxModel->getFirstChartDiagram();
131 mxDiagramSnapshot = new ::chart::Diagram(*xDiagram);
132 }
133
134 void ColorPaletteWrapper::restoreOriginalDiagram()
135 {
136 if (mxDiagramSnapshot)
137 {
138 const rtl::Reference<Diagram> xDiagram = new ::chart::Diagram(*mxDiagramSnapshot);
139 // setDiagram didn't make a copy internally, so we need to pass a copy or
140 // the diagram snapshot would be modified on preview
141 mxModel->setFirstDiagram(xDiagram);
142 }
143 }
144
145 void ColorPaletteWrapper::select(ChartColorPaletteType eType, const sal_uInt32 nIndex)
146 {
147 mxModel->setColorPalette(eType, nIndex);
148 }
149
150 void ColorPaletteWrapper::apply(const ChartColorPalette* pColorPalette)
151 {
152 if (pColorPalette)
153 mxModel->applyColorPaletteToDataSeries(*pColorPalette);
154 }
155
156 std::shared_ptr<ChartColorPaletteHelper> ColorPaletteWrapper::getHelper() const
157 {
158 const std::shared_ptr<model::Theme> pTheme = mxModel->getDocumentTheme();
159 return std::make_shared<ChartColorPaletteHelper>(pTheme);
160 }
161
162 ChartColorPaletteType ColorPaletteWrapper::getType() const
163 {
164 return mxModel->getColorPaletteType();
165 }
166
167 sal_uInt32 ColorPaletteWrapper::getIndex() const { return mxModel->getColorPaletteIndex(); }
168
169 const std::vector<ObjectType> ChartColorsPanel::maAcceptedTypes{
170 OBJECTTYPE_PAGE, OBJECTTYPE_LEGEND, OBJECTTYPE_DIAGRAM,
171 OBJECTTYPE_DIAGRAM_WALL, OBJECTTYPE_DIAGRAM_FLOOR, OBJECTTYPE_DATA_SERIES,
172 OBJECTTYPE_DATA_POINT,
173 };
174
175 ChartColorsPanel::ChartColorsPanel(weld::Widget* pParent,
176 const uno::Reference<frame::XFrame>& rxFrame,
177 ChartController* pController)
178 : PanelLayout(pParent, "ChartColorsPanel", "modules/schart/ui/sidebarcolors.ui")
179 , mxModel(pController->getChartModel())
180 , mxModifyListener(new ChartSidebarModifyListener(this))
181 , mxSelectionListener(new ChartSidebarSelectionListener(this))
182 , mbModelValid(true)
183 , mxColorPaletteTB(m_xBuilder->weld_toolbar("colorpalettetype"))
184 , mxColorPaletteDispatch(new ToolbarUnoDispatcher(*mxColorPaletteTB, *m_xBuilder, rxFrame))
185 {
186 auto aAcceptedTypes(maAcceptedTypes);
187 mxSelectionListener->setAcceptedTypes(std::move(aAcceptedTypes));
188 Initialize();
189 }
190
191 ChartColorsPanel::~ChartColorsPanel()
192 {
193 doUpdateModel(nullptr);
194 mxColorPaletteDispatch.reset();
195 mxColorPaletteTB.reset();
196 }
197
198 void ChartColorsPanel::Initialize()
199 {
200 mxModel->addModifyListener(mxModifyListener);
201
202 uno::Reference<view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(),
203 uno::UNO_QUERY);
204 if (xSelectionSupplier.is())
205 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
206
207 ChartColorPaletteControl* pColorPaletteControl
208 = getChartColorPaletteControl(*mxColorPaletteDispatch);
209 assert(pColorPaletteControl);
210 mxColorPaletteWrapper = std::make_shared<ColorPaletteWrapper>(mxModel, pColorPaletteControl);
211 pColorPaletteControl->setColorPaletteHandler(mxColorPaletteWrapper);
212
213 updateData();
214 }
215
216 void ChartColorsPanel::updateData()
217 {
218 if (!mbModelValid)
219 return;
220
221 const OUString aCID = getCID(mxModel);
222 if (aCID.isEmpty())
223 return;
224 const ObjectType eType = ObjectIdentifier::getObjectType(aCID);
225
226 if (std::find(maAcceptedTypes.begin(), maAcceptedTypes.end(), eType) == maAcceptedTypes.end())
227 return;
228
229 // if fill style is not solid clear palette selection
230 if (eType == OBJECTTYPE_DATA_SERIES || eType == OBJECTTYPE_DATA_POINT)
231 {
232 const uno::Reference<beans::XPropertySet> xPropSet
233 = ObjectIdentifier::getObjectPropertySet(aCID, mxModel);
234 if (!xPropSet.is())
235 return;
236
237 uno::Reference<beans::XPropertySetInfo> xInfo(xPropSet->getPropertySetInfo());
238 if (!xInfo.is())
239 return;
240
241 SolarMutexGuard aGuard;
242 if (xInfo->hasPropertyByName("FillStyle"))
243 {
244 drawing::FillStyle eFillStyle = drawing::FillStyle_SOLID;
245 xPropSet->getPropertyValue("FillStyle") >>= eFillStyle;
246 if (eFillStyle != drawing::FillStyle_SOLID)
247 {
248 mxModel->clearColorPalette();
249 }
250 }
251 }
252
253 mxColorPaletteWrapper->updateData();
254 }
255
256 std::unique_ptr<PanelLayout> ChartColorsPanel::Create(weld::Widget* pParent,
257 const uno::Reference<frame::XFrame>& rxFrame,
258 ChartController* pController)
259 {
260 if (pParent == nullptr)
261 throw lang::IllegalArgumentException("no parent Window given to ChartColorsPanel::Create",
262 nullptr, 0);
263 if (!rxFrame.is())
264 throw lang::IllegalArgumentException("no XFrame given to ChartColorsPanel::Create", nullptr,
265 1);
266
267 return std::make_unique<ChartColorsPanel>(pParent, rxFrame, pController);
268 }
269
270 void ChartColorsPanel::DataChanged(const DataChangedEvent& rEvent)
271 {
272 PanelLayout::DataChanged(rEvent);
273 updateData();
274 }
275
276 void ChartColorsPanel::HandleContextChange(const vcl::EnumContext&) { updateData(); }
277
278 void ChartColorsPanel::NotifyItemUpdate(sal_uInt16 /*nSID*/, SfxItemState /*eState*/,
279 const SfxPoolItem* /*pState*/)
280 {
281 }
282
283 void ChartColorsPanel::modelInvalid() { mbModelValid = false; }
284
285 void ChartColorsPanel::doUpdateModel(const rtl::Reference<ChartModel>& xModel)
286 {
287 if (mbModelValid)
288 {
289 mxModel->removeModifyListener(mxModifyListener);
290
291 const uno::Reference<view::XSelectionSupplier> oldSelectionSupplier(
292 mxModel->getCurrentController(), uno::UNO_QUERY);
293 if (oldSelectionSupplier.is())
294 {
295 oldSelectionSupplier->removeSelectionChangeListener(mxSelectionListener);
296 }
297 }
298
299 mxModel = xModel;
300 mbModelValid = mxModel.is();
301
302 if (!mbModelValid)
303 return;
304
305 mxColorPaletteWrapper->updateModel(mxModel);
306
307 mxModel->addModifyListener(mxModifyListener);
308
309 uno::Reference<view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(),
310 uno::UNO_QUERY);
311 if (xSelectionSupplier.is())
312 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
313 }
314
315 void ChartColorsPanel::updateModel(const uno::Reference<frame::XModel> xModel)
316 {
317 const auto pModel = dynamic_cast<ChartModel*>(xModel.get());
318 assert(!xModel || pModel);
319 doUpdateModel(pModel);
320 }
321
322 void ChartColorsPanel::selectionChanged(const bool bCorrectType)
323 {
324 if (bCorrectType)
325 updateData();
326 }
327
328 } // end of namespace ::chart::sidebar
329
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */