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