1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
34 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
36 namespace chart::sidebar
{
40 OUString
getCID(const css::uno::Reference
<css::frame::XModel
>& xModel
)
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())
50 css::uno::Any aAny
= xSelectionSupplier
->getSelection();
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
);
75 xPropSet
.set(xDiagram
->getWall());
83 ChartColorWrapper::ChartColorWrapper(
84 rtl::Reference
<::chart::ChartModel
> xModel
,
85 SvxColorToolBoxControl
* pControl
,
87 mxModel(std::move(xModel
)),
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
);
99 SAL_WARN("chart2", "Invalid reference to xPropSet");
103 xPropSet
->setPropertyValue(maPropertyName
, css::uno::Any(rColor
.m_aColor
));
106 void ChartColorWrapper::updateModel(const rtl::Reference
<::chart::ChartModel
>& 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
);
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
)
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(u
"com.sun.star.drawing.DashTable"_ustr
),
161 css::uno::UNO_QUERY
);
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
);
181 aUrl
.Complete
= ".uno:XLineStyle";
183 css::frame::FeatureStateEvent aEvent
;
184 aEvent
.IsEnabled
= true;
186 aEvent
.FeatureURL
= aUrl
;
187 aEvent
.State
= xPropSet
->getPropertyValue(u
"LineStyle"_ustr
);
188 mpControl
->statusChanged(aEvent
);
190 aUrl
.Complete
= ".uno:LineDash";
192 auto aLineDashName
= xPropSet
->getPropertyValue(u
"LineDashName"_ustr
);
194 aLineDashName
>>= aDashName
;
195 css::uno::Any aLineDash
= getLineDash(mxModel
, aDashName
);
196 XLineDashItem aDashItem
;
197 aDashItem
.PutValue(aLineDash
, MID_LINEDASH
);
199 aEvent
.FeatureURL
= std::move(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
);
210 SAL_WARN("chart2", "Invalid reference to xPropSet");
214 if (rCommand
== u
".uno:XLineStyle")
216 xPropSet
->setPropertyValue(u
"LineStyle"_ustr
, rValue
);
219 else if (rCommand
== u
".uno:LineDash")
221 XLineDashItem aDashItem
;
222 aDashItem
.PutValue(rValue
, 0);
224 aDashItem
.QueryValue(aAny
, MID_LINEDASH
);
225 OUString aDashName
= PropertyHelper::addLineDashUniqueNameToTable(aAny
,
228 xPropSet
->setPropertyValue(u
"LineDash"_ustr
, aAny
);
229 xPropSet
->setPropertyValue(u
"LineDashName"_ustr
, css::uno::Any(aDashName
));
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */