LibreOffice Module chart2 (master) 1
ChartColorWrapper.cxx
Go to the documentation of this file.
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
36namespace chart::sidebar {
37
38namespace {
39
40OUString 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
60css::uno::Reference<css::beans::XPropertySet> getPropSet(
62{
63 OUString aCID = getCID(xModel);
64 css::uno::Reference<css::beans::XPropertySet> xPropSet =
66
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
85 SvxColorToolBoxControl* pControl,
86 OUString aName):
87 mxModel(std::move(xModel)),
88 mpControl(pControl),
89 maPropertyName(std::move(aName))
90{
91}
92
93void 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
107{
108 mxModel = xModel;
109}
110
112{
113 static constexpr OUStringLiteral aLineColor = u"LineColor";
114 static const std::u16string_view aCommands[2] = {u".uno:XLineColor", u".uno:FillColor"};
115
116 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
117 if (!xPropSet.is())
118 return;
119
120 css::util::URL aUrl;
121 aUrl.Complete = (maPropertyName == aLineColor) ? aCommands[0] : aCommands[1];
122
123 css::frame::FeatureStateEvent aEvent;
124 aEvent.FeatureURL = aUrl;
125 aEvent.IsEnabled = true;
126 aEvent.State = xPropSet->getPropertyValue(maPropertyName);
128
129 SfxViewShell* pViewShell = SfxViewShell::Current();
130 if (comphelper::LibreOfficeKit::isActive() && pViewShell && (maPropertyName == aLineColor))
131 {
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));
137 }
138}
139
143 : mxModel(std::move(xModel))
144 , mpControl(pControl)
145{
146}
147
149{
150 mxModel = xModel;
151}
152
153namespace
154{
155 css::uno::Any getLineDash(
156 const css::uno::Reference<css::frame::XModel>& xModel, const OUString& rDashName)
157 {
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())
163 {
164 if (!xNameAccess->hasByName(rDashName))
165 return css::uno::Any();
166
167 return xNameAccess->getByName(rDashName);
168 }
169
170 return css::uno::Any();
171 }
172}
173
175{
176 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
177 if (!xPropSet.is())
178 return;
179
180 css::util::URL aUrl;
181 aUrl.Complete = ".uno:XLineStyle";
182
183 css::frame::FeatureStateEvent aEvent;
184 aEvent.IsEnabled = true;
185
186 aEvent.FeatureURL = aUrl;
187 aEvent.State = xPropSet->getPropertyValue("LineStyle");
189
190 aUrl.Complete = ".uno:LineDash";
191
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);
198
199 aEvent.FeatureURL = aUrl;
200 aDashItem.QueryValue(aEvent.State);
202}
203
204bool ChartLineStyleWrapper::operator()(std::u16string_view rCommand, const css::uno::Any& rValue)
205{
206 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
207
208 if (!xPropSet.is())
209 {
210 SAL_WARN("chart2", "Invalid reference to xPropSet");
211 return false;
212 }
213
214 if (rCommand == u".uno:XLineStyle")
215 {
216 xPropSet->setPropertyValue("LineStyle", rValue);
217 return true;
218 }
219 else if (rCommand == u".uno:LineDash")
220 {
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;
231 }
232 return false;
233}
234
235}
236
237/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XModel2 > mxModel
AnyEventRef aEvent
virtual void libreOfficeKitViewCallback(int nType, const OString &pPayload) const override
static SAL_WARN_UNUSED_RESULT SfxViewShell * Current()
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
static css::uno::Reference< css::beans::XPropertySet > getObjectPropertySet(std::u16string_view rObjectCID, const rtl::Reference< ::chart::ChartModel > &xChartDocument)
ObjectType getObjectType() const
ChartColorWrapper(rtl::Reference<::chart::ChartModel > xModel, SvxColorToolBoxControl *pControl, OUString rPropertyName)
void operator()(const OUString &rCommand, const NamedColor &rColor)
rtl::Reference<::chart::ChartModel > mxModel
SvxColorToolBoxControl * mpControl
void updateModel(const rtl::Reference<::chart::ChartModel > &xModel)
ChartLineStyleWrapper(rtl::Reference<::chart::ChartModel > xModel, SvxLineStyleToolBoxControl *pControl)
void updateModel(const rtl::Reference<::chart::ChartModel > &xModel)
bool operator()(std::u16string_view rCommand, const css::uno::Any &rValue)
rtl::Reference<::chart::ChartModel > mxModel
SvxLineStyleToolBoxControl * mpControl
float u
DocumentType eType
OUString aName
#define SAL_WARN(area, stream)
OOO_DLLPUBLIC_CHARTTOOLS OUString addLineDashUniqueNameToTable(const css::uno::Any &rValue, const css::uno::Reference< css::lang::XMultiServiceFactory > &xFact, const OUString &rPreferredName)
adds a line dash with a unique name to the gradient obtained by the given factory.
@ OBJECTTYPE_DIAGRAM
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
ObjectType
Color m_aColor
Reference< XController > xController
Reference< XModel > xModel
#define MID_LINEDASH