Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / sidebar / ChartColorWrapper.cxx
blobf5c79133434cf359704efa5f1281a478fcfcf4b4
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/.
8 */
10 #include <sal/config.h>
12 #include <string_view>
14 #include "ChartColorWrapper.hxx"
15 #include <ChartModel.hxx>
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>
25 #include <svx/linectrl.hxx>
26 #include <svx/tbcontrl.hxx>
27 #include <svx/xlndsit.hxx>
28 #include <svx/unomid.hxx>
30 #include <comphelper/lok.hxx>
31 #include <sal/log.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <utility>
34 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
36 namespace chart::sidebar {
38 namespace {
40 OUString getCID(const css::uno::Reference<css::frame::XModel>& xModel)
42 if (!xModel.is())
43 return OUString();
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();
50 css::uno::Any aAny = xSelectionSupplier->getSelection();
51 if (!aAny.hasValue())
52 return OUString();
54 OUString aCID;
55 aAny >>= aCID;
57 return aCID;
60 css::uno::Reference<css::beans::XPropertySet> getPropSet(
61 const rtl::Reference<::chart::ChartModel>& xModel)
63 OUString aCID = getCID(xModel);
64 css::uno::Reference<css::beans::XPropertySet> xPropSet =
65 ObjectIdentifier::getObjectPropertySet(aCID, xModel);
67 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
68 if (eType == OBJECTTYPE_DIAGRAM)
70 css::uno::Reference<css::chart2::XDiagram> xDiagram(
71 xPropSet, css::uno::UNO_QUERY);
72 if (!xDiagram.is())
73 return xPropSet;
75 xPropSet.set(xDiagram->getWall());
78 return xPropSet;
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))
93 void ChartColorWrapper::operator()([[maybe_unused]] const OUString& , const NamedColor& rColor)
95 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
97 if (!xPropSet.is())
99 SAL_WARN("chart2", "Invalid reference to xPropSet");
100 return;
103 xPropSet->setPropertyValue(maPropertyName, css::uno::Any(rColor.m_aColor));
106 void ChartColorWrapper::updateModel(const rtl::Reference<::chart::ChartModel>& xModel)
108 mxModel = xModel;
111 void ChartColorWrapper::updateData()
113 static constexpr OUString aLineColor = u"LineColor"_ustr;
114 static const std::u16string_view aCommands[2] = {u".uno:XLineColor", u".uno:FillColor"};
116 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
117 if (!xPropSet.is())
118 return;
120 css::util::URL aUrl;
121 aUrl.Complete = (maPropertyName == aLineColor) ? aCommands[0] : aCommands[1];
123 css::frame::FeatureStateEvent aEvent;
124 aEvent.FeatureURL = aUrl;
125 aEvent.IsEnabled = true;
126 aEvent.State = xPropSet->getPropertyValue(maPropertyName);
127 mpControl->statusChanged(aEvent);
129 SfxViewShell* pViewShell = SfxViewShell::Current();
130 if (comphelper::LibreOfficeKit::isActive() && pViewShell && (maPropertyName == aLineColor))
132 OString sCommand = OUStringToOString(aUrl.Complete, RTL_TEXTENCODING_ASCII_US);
133 sal_Int32 nColor = -1;
134 aEvent.State >>= nColor;
135 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_STATE_CHANGED,
136 sCommand + "=" + OString::number(nColor));
140 ChartLineStyleWrapper::ChartLineStyleWrapper(
141 rtl::Reference<::chart::ChartModel> xModel,
142 SvxLineStyleToolBoxControl* pControl)
143 : mxModel(std::move(xModel))
144 , mpControl(pControl)
148 void ChartLineStyleWrapper::updateModel(const rtl::Reference<::chart::ChartModel>& xModel)
150 mxModel = xModel;
153 namespace
155 css::uno::Any getLineDash(
156 const css::uno::Reference<css::frame::XModel>& xModel, const OUString& rDashName)
158 css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
159 css::uno::Reference<css::container::XNameAccess> xNameAccess(
160 xFact->createInstance("com.sun.star.drawing.DashTable"),
161 css::uno::UNO_QUERY );
162 if(xNameAccess.is())
164 if (!xNameAccess->hasByName(rDashName))
165 return css::uno::Any();
167 return xNameAccess->getByName(rDashName);
170 return css::uno::Any();
174 void ChartLineStyleWrapper::updateData()
176 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
177 if (!xPropSet.is())
178 return;
180 css::util::URL aUrl;
181 aUrl.Complete = ".uno:XLineStyle";
183 css::frame::FeatureStateEvent aEvent;
184 aEvent.IsEnabled = true;
186 aEvent.FeatureURL = aUrl;
187 aEvent.State = xPropSet->getPropertyValue("LineStyle");
188 mpControl->statusChanged(aEvent);
190 aUrl.Complete = ".uno:LineDash";
192 auto aLineDashName = xPropSet->getPropertyValue("LineDashName");
193 OUString aDashName;
194 aLineDashName >>= aDashName;
195 css::uno::Any aLineDash = getLineDash(mxModel, aDashName);
196 XLineDashItem aDashItem;
197 aDashItem.PutValue(aLineDash, MID_LINEDASH);
199 aEvent.FeatureURL = aUrl;
200 aDashItem.QueryValue(aEvent.State);
201 mpControl->statusChanged(aEvent);
204 bool ChartLineStyleWrapper::operator()(std::u16string_view rCommand, const css::uno::Any& rValue)
206 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
208 if (!xPropSet.is())
210 SAL_WARN("chart2", "Invalid reference to xPropSet");
211 return false;
214 if (rCommand == u".uno:XLineStyle")
216 xPropSet->setPropertyValue("LineStyle", rValue);
217 return true;
219 else if (rCommand == u".uno:LineDash")
221 XLineDashItem aDashItem;
222 aDashItem.PutValue(rValue, 0);
223 css::uno::Any aAny;
224 aDashItem.QueryValue(aAny, MID_LINEDASH);
225 OUString aDashName = PropertyHelper::addLineDashUniqueNameToTable(aAny,
226 mxModel,
227 "");
228 xPropSet->setPropertyValue("LineDash", aAny);
229 xPropSet->setPropertyValue("LineDashName", css::uno::Any(aDashName));
230 return true;
232 return false;
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */