Debian Codegraph @

chart2/source/controller/sidebar/ChartColorWrapper.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 <sal/config.h>
11
12 #include <string_view>
13
14 #include "ChartColorWrapper.hxx"
15 #include <ChartModel.hxx>
16
17 #include <ObjectIdentifier.hxx>
18 #include <PropertyHelper.hxx>
19 #include <com/sun/star/chart2/XDiagram.hpp>
20 #include <com/sun/star/container/XNameAccess.hpp>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/view/XSelectionSupplier.hpp>
23 #include <com/sun/star/frame/XController.hpp>
24
25 #include <svx/linectrl.hxx>
26 #include <svx/tbcontrl.hxx>
27 #include <svx/xlndsit.hxx>
28 #include <svx/unomid.hxx>
29
30 #include <comphelper/lok.hxx>
31 #include <sal/log.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <utility>
34 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
35
36 namespace chart::sidebar {
37
38 namespace {
39
40 OUString getCID(const css::uno::Reference<css::frame::XModel>& xModel)
41 {
42 if (!xModel.is())
43 return OUString();
44
45 css::uno::Reference<css::frame::XController> xController(xModel->getCurrentController());
46 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(xController, css::uno::UNO_QUERY);
47 if (!xSelectionSupplier.is())
48 return OUString();
49
50 css::uno::Any aAny = xSelectionSupplier->getSelection();
51 if (!aAny.hasValue())
52 return OUString();
53
54 OUString aCID;
55 aAny >>= aCID;
56
57 return aCID;
58 }
59
60 css::uno::Reference<css::beans::XPropertySet> getPropSet(
61 const rtl::Reference<::chart::ChartModel>& xModel)
62 {
63 OUString aCID = getCID(xModel);
64 css::uno::Reference<css::beans::XPropertySet> xPropSet =
65 ObjectIdentifier::getObjectPropertySet(aCID, xModel);
66
67 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
68 if (eType == OBJECTTYPE_DIAGRAM)
69 {
70 css::uno::Reference<css::chart2::XDiagram> xDiagram(
71 xPropSet, css::uno::UNO_QUERY);
72 if (!xDiagram.is())
73 return xPropSet;
74
75 xPropSet.set(xDiagram->getWall());
76 }
77
78 return xPropSet;
79 }
80
81 }
82
83 ChartColorWrapper::ChartColorWrapper(
84 rtl::Reference<::chart::ChartModel> xModel,
85 SvxColorToolBoxControl* pControl,
86 OUString aName):
87 mxModel(std::move(xModel)),
88 mpControl(pControl),
89 maPropertyName(std::move(aName))
90 {
91 }
92
93 void ChartColorWrapper::operator()([[maybe_unused]] const OUString& , const NamedColor& rColor)
94 {
95 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
96
97 if (!xPropSet.is())
98 {
99 SAL_WARN("chart2", "Invalid reference to xPropSet");
100 return;
101 }
102
103 xPropSet->setPropertyValue(maPropertyName, css::uno::Any(rColor.m_aColor));
104
105 OUString aCID = getCID(mxModel);
106 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
107 if (eType == OBJECTTYPE_DATA_SERIES || eType == OBJECTTYPE_DATA_POINT)
108 mxModel->clearColorPalette();
109 }
110
111 void ChartColorWrapper::updateModel(const rtl::Reference<::chart::ChartModel>& xModel)
112 {
113 mxModel = xModel;
114 }
115
116 void ChartColorWrapper::updateData()
117 {
118 static constexpr OUString aLineColor = u"LineColor"_ustr;
119 static const std::u16string_view aCommands[2] = {u".uno:XLineColor", u".uno:FillColor"};
120
121 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
122 if (!xPropSet.is())
123 return;
124
125 css::util::URL aUrl;
126 aUrl.Complete = (maPropertyName == aLineColor) ? aCommands[0] : aCommands[1];
127
128 css::frame::FeatureStateEvent aEvent;
129 aEvent.FeatureURL = aUrl;
130 aEvent.IsEnabled = true;
131 aEvent.State = xPropSet->getPropertyValue(maPropertyName);
132 mpControl->statusChanged(aEvent);
133
134 SfxViewShell* pViewShell = SfxViewShell::Current();
135 if (comphelper::LibreOfficeKit::isActive() && pViewShell && (maPropertyName == aLineColor))
136 {
137 OString sCommand = OUStringToOString(aUrl.Complete, RTL_TEXTENCODING_ASCII_US);
138 sal_Int32 nColor = -1;
139 aEvent.State >>= nColor;
140 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_STATE_CHANGED,
141 sCommand + "=" + OString::number(nColor));
142 }
143 }
144
145 ChartLineStyleWrapper::ChartLineStyleWrapper(
146 rtl::Reference<::chart::ChartModel> xModel,
147 SvxLineStyleToolBoxControl* pControl)
148 : mxModel(std::move(xModel))
149 , mpControl(pControl)
150 {
151 }
152
153 void ChartLineStyleWrapper::updateModel(const rtl::Reference<::chart::ChartModel>& xModel)
154 {
155 mxModel = xModel;
156 }
157
158 namespace
159 {
160 css::uno::Any getLineDash(
161 const css::uno::Reference<css::frame::XModel>& xModel, const OUString& rDashName)
162 {
163 css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
164 css::uno::Reference<css::container::XNameAccess> xNameAccess(
165 xFact->createInstance(u"com.sun.star.drawing.DashTable"_ustr),
166 css::uno::UNO_QUERY );
167 if(xNameAccess.is())
168 {
169 if (!xNameAccess->hasByName(rDashName))
170 return css::uno::Any();
171
172 return xNameAccess->getByName(rDashName);
173 }
174
175 return css::uno::Any();
176 }
177 }
178
179 void ChartLineStyleWrapper::updateData()
180 {
181 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
182 if (!xPropSet.is())
183 return;
184
185 css::util::URL aUrl;
186 aUrl.Complete = ".uno:XLineStyle";
187
188 css::frame::FeatureStateEvent aEvent;
189 aEvent.IsEnabled = true;
190
191 aEvent.FeatureURL = aUrl;
192 aEvent.State = xPropSet->getPropertyValue(u"LineStyle"_ustr);
193 mpControl->statusChanged(aEvent);
194
195 aUrl.Complete = ".uno:LineDash";
196
197 auto aLineDashName = xPropSet->getPropertyValue(u"LineDashName"_ustr);
198 OUString aDashName;
199 aLineDashName >>= aDashName;
200 css::uno::Any aLineDash = getLineDash(mxModel, aDashName);
201 XLineDashItem aDashItem;
202 aDashItem.PutValue(aLineDash, MID_LINEDASH);
203
204 aEvent.FeatureURL = std::move(aUrl);
205 aDashItem.QueryValue(aEvent.State);
206 mpControl->statusChanged(aEvent);
207 }
208
209 bool ChartLineStyleWrapper::operator()(std::u16string_view rCommand, const css::uno::Any& rValue)
210 {
211 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
212
213 if (!xPropSet.is())
214 {
215 SAL_WARN("chart2", "Invalid reference to xPropSet");
216 return false;
217 }
218
219 if (rCommand == u".uno:XLineStyle")
220 {
221 xPropSet->setPropertyValue(u"LineStyle"_ustr, rValue);
222 return true;
223 }
224 else if (rCommand == u".uno:LineDash")
225 {
226 XLineDashItem aDashItem;
227 aDashItem.PutValue(rValue, 0);
228 css::uno::Any aAny;
229 aDashItem.QueryValue(aAny, MID_LINEDASH);
230 OUString aDashName = PropertyHelper::addLineDashUniqueNameToTable(aAny,
231 mxModel,
232 u""_ustr);
233 xPropSet->setPropertyValue(u"LineDash"_ustr, aAny);
234 xPropSet->setPropertyValue(u"LineDashName"_ustr, css::uno::Any(aDashName));
235 return true;
236 }
237 return false;
238 }
239
240 }
241
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */