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 "ChartAreaPanel.hxx"
12 #include <ChartController.hxx>
13 #include <ViewElementListProvider.hxx>
14 #include <PropertyHelper.hxx>
16 #include <chartview/DrawModelWrapper.hxx>
17 #include <com/sun/star/util/XModifyBroadcaster.hpp>
18 #include <com/sun/star/chart2/XDiagram.hpp>
20 #include <svx/xfltrit.hxx>
21 #include <svx/xflftrit.hxx>
22 #include <svx/xbtmpit.hxx>
23 #include <svx/unomid.hxx>
24 #include <vcl/svapp.hxx>
26 #include <svx/tbcontrl.hxx>
28 namespace chart
{ namespace sidebar
{
32 SvxColorToolBoxControl
* getColorToolBoxControl(sfx2::sidebar::SidebarToolBox
* pToolBoxColor
)
34 css::uno::Reference
<css::frame::XToolbarController
> xController
= pToolBoxColor
->GetFirstController();
35 SvxColorToolBoxControl
* pToolBoxColorControl
= dynamic_cast<SvxColorToolBoxControl
*>(xController
.get());
36 return pToolBoxColorControl
;
39 OUString
getCID(const css::uno::Reference
<css::frame::XModel
>& xModel
)
41 css::uno::Reference
<css::frame::XController
> xController(xModel
->getCurrentController());
42 css::uno::Reference
<css::view::XSelectionSupplier
> xSelectionSupplier(xController
, css::uno::UNO_QUERY
);
43 if (!xSelectionSupplier
.is())
46 css::uno::Any aAny
= xSelectionSupplier
->getSelection();
49 // if no selection, default to diagram wall so sidebar can show some editable properties
50 ChartController
* pController
= dynamic_cast<ChartController
*>(xController
.get());
53 pController
->select( css::uno::Any( ObjectIdentifier::createClassifiedIdentifier( OBJECTTYPE_PAGE
, OUString() ) ) );
54 xSelectionSupplier
= css::uno::Reference
<css::view::XSelectionSupplier
>(xController
, css::uno::UNO_QUERY
);
55 if (xSelectionSupplier
.is())
56 aAny
= xSelectionSupplier
->getSelection();
69 css::uno::Reference
<css::beans::XPropertySet
> getPropSet(
70 const css::uno::Reference
<css::frame::XModel
>& xModel
)
72 OUString aCID
= getCID(xModel
);
73 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
=
74 ObjectIdentifier::getObjectPropertySet(aCID
, xModel
);
76 ObjectType eType
= ObjectIdentifier::getObjectType(aCID
);
77 if (eType
== OBJECTTYPE_DIAGRAM
)
79 css::uno::Reference
<css::chart2::XDiagram
> xDiagram(
80 xPropSet
, css::uno::UNO_QUERY
);
84 xPropSet
.set(xDiagram
->getWall());
90 ChartController
* getController(const css::uno::Reference
<css::frame::XModel
>& xModel
)
92 css::uno::Reference
<css::frame::XController
>xController
= xModel
->getCurrentController();
93 if (!xController
.is())
94 throw std::exception();
96 ChartController
* pController
= dynamic_cast<ChartController
*>(xController
.get());
98 throw std::exception();
103 ViewElementListProvider
getViewElementListProvider( const css::uno::Reference
<css::frame::XModel
>& xModel
)
105 ChartController
* pController
= getController(xModel
);
106 ViewElementListProvider aProvider
= pController
->getViewElementListProvider();
110 DrawModelWrapper
* getDrawModelWrapper(const css::uno::Reference
<css::frame::XModel
>& xModel
)
112 ChartController
* pController
= getController(xModel
);
113 return pController
->GetDrawModelWrapper();
116 XFillGradientItem
getXGradientForName(const css::uno::Reference
<css::frame::XModel
>& xModel
,
117 const OUString
& rName
)
119 css::uno::Reference
<css::lang::XMultiServiceFactory
> xFact(xModel
, css::uno::UNO_QUERY
);
120 css::uno::Reference
<css::container::XNameAccess
> xNameAccess(
121 xFact
->createInstance("com.sun.star.drawing.GradientTable"), css::uno::UNO_QUERY
);
122 if (!xNameAccess
.is())
123 return XFillGradientItem();
125 if (!xNameAccess
->hasByName(rName
))
126 return XFillGradientItem();
128 css::uno::Any aAny
= xNameAccess
->getByName(rName
);
130 XFillGradientItem aItem
;
131 aItem
.SetName(rName
);
132 aItem
.PutValue(aAny
, MID_FILLGRADIENT
);
138 XFillFloatTransparenceItem
getXTransparencyGradientForName(const css::uno::Reference
<css::frame::XModel
>& xModel
,
139 const OUString
& rName
)
141 css::uno::Reference
<css::lang::XMultiServiceFactory
> xFact(xModel
, css::uno::UNO_QUERY
);
142 css::uno::Reference
<css::container::XNameAccess
> xNameAccess(
143 xFact
->createInstance("com.sun.star.drawing.TransparencyGradientTable"), css::uno::UNO_QUERY
);
144 if (!xNameAccess
.is())
145 return XFillFloatTransparenceItem();
147 if (!xNameAccess
->hasByName(rName
))
148 return XFillFloatTransparenceItem();
150 css::uno::Any aAny
= xNameAccess
->getByName(rName
);
152 XFillFloatTransparenceItem aItem
;
153 aItem
.SetName(rName
);
154 aItem
.PutValue(aAny
, MID_FILLGRADIENT
);
155 aItem
.SetEnabled(true);
160 XHatch
getXHatchFromName(const css::uno::Reference
<css::frame::XModel
>& xModel
,
165 ViewElementListProvider aProvider
= getViewElementListProvider(xModel
);
166 XHatchListRef aRef
= aProvider
.GetHatchList();
167 size_t n
= aRef
->Count();
168 for (size_t i
= 0; i
< n
; ++i
)
170 const XHatchEntry
* pHatch
= aRef
->GetHatch(i
);
174 if (pHatch
->GetName().equalsIgnoreAsciiCase(rName
))
176 // we need to update the hatch name
177 rName
= pHatch
->GetName();
178 return pHatch
->GetHatch();
190 GraphicObject
getXBitmapFromName(const css::uno::Reference
<css::frame::XModel
>& xModel
,
191 const OUString
& rName
)
195 ViewElementListProvider aProvider
= getViewElementListProvider(xModel
);
196 XBitmapListRef aBmpRef
= aProvider
.GetBitmapList();
197 XPatternListRef aPatRef
= aProvider
.GetPatternList();
199 size_t n
= aBmpRef
->Count();
200 for (size_t i
= 0; i
< n
; ++i
)
202 const XBitmapEntry
* pBitmap
= aBmpRef
->GetBitmap(i
);
206 if (pBitmap
->GetName().equalsIgnoreAsciiCase(rName
))
208 return pBitmap
->GetGraphicObject();
212 // perhaps it's a pattern
213 size_t m
= aPatRef
->Count();
214 for (size_t i
= 0; i
< m
; ++i
)
216 const XBitmapEntry
* pBitmap
= aPatRef
->GetBitmap(i
);
220 if (pBitmap
->GetName().equalsIgnoreAsciiCase(rName
))
222 return pBitmap
->GetGraphicObject();
231 return GraphicObject();
237 explicit PreventUpdate(bool& bUpdate
):
254 VclPtr
<vcl::Window
> ChartAreaPanel::Create(
255 vcl::Window
* pParent
,
256 const css::uno::Reference
<css::frame::XFrame
>& rxFrame
,
257 ChartController
* pController
)
259 if (pParent
== nullptr)
260 throw css::lang::IllegalArgumentException("no parent Window given to ChartAxisPanel::Create", nullptr, 0);
262 throw css::lang::IllegalArgumentException("no XFrame given to ChartAxisPanel::Create", nullptr, 1);
264 return VclPtr
<ChartAreaPanel
>::Create(
265 pParent
, rxFrame
, pController
);
268 ChartAreaPanel::ChartAreaPanel(vcl::Window
* pParent
,
269 const css::uno::Reference
<css::frame::XFrame
>& rxFrame
,
270 ChartController
* pController
):
271 svx::sidebar::AreaPropertyPanelBase(pParent
, rxFrame
),
272 mxModel(pController
->getModel()),
273 mxListener(new ChartSidebarModifyListener(this)),
274 mxSelectionListener(new ChartSidebarSelectionListener(this)),
277 maFillColorWrapper(mxModel
, getColorToolBoxControl(mpToolBoxColor
.get()), "FillColor")
279 std::vector
<ObjectType
> aAcceptedTypes
{ OBJECTTYPE_PAGE
, OBJECTTYPE_DIAGRAM
,
280 OBJECTTYPE_DATA_SERIES
, OBJECTTYPE_DATA_POINT
,
281 OBJECTTYPE_TITLE
, OBJECTTYPE_LEGEND
};
282 mxSelectionListener
->setAcceptedTypes(aAcceptedTypes
);
286 ChartAreaPanel::~ChartAreaPanel()
291 void ChartAreaPanel::dispose()
293 css::uno::Reference
<css::util::XModifyBroadcaster
> xBroadcaster(mxModel
, css::uno::UNO_QUERY_THROW
);
294 xBroadcaster
->removeModifyListener(mxListener
);
296 css::uno::Reference
<css::view::XSelectionSupplier
> xSelectionSupplier(mxModel
->getCurrentController(), css::uno::UNO_QUERY
);
297 if (xSelectionSupplier
.is())
298 xSelectionSupplier
->removeSelectionChangeListener(mxSelectionListener
.get());
300 AreaPropertyPanelBase::dispose();
303 void ChartAreaPanel::Initialize()
305 css::uno::Reference
<css::util::XModifyBroadcaster
> xBroadcaster(mxModel
, css::uno::UNO_QUERY_THROW
);
306 xBroadcaster
->addModifyListener(mxListener
);
308 css::uno::Reference
<css::view::XSelectionSupplier
> xSelectionSupplier(mxModel
->getCurrentController(), css::uno::UNO_QUERY
);
309 if (xSelectionSupplier
.is())
310 xSelectionSupplier
->addSelectionChangeListener(mxSelectionListener
.get());
312 SvxColorToolBoxControl
* pToolBoxColor
= getColorToolBoxControl(mpToolBoxColor
.get());
313 pToolBoxColor
->setColorSelectFunction(maFillColorWrapper
);
318 void ChartAreaPanel::setFillTransparence(const XFillTransparenceItem
& rItem
)
320 PreventUpdate
aProtector(mbUpdate
);
321 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
325 xPropSet
->setPropertyValue("FillTransparence", css::uno::Any(rItem
.GetValue()));
328 void ChartAreaPanel::setFillFloatTransparence(
329 const XFillFloatTransparenceItem
& rItem
)
331 PreventUpdate
aProtector(mbUpdate
);
332 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
336 if (!rItem
.IsEnabled())
338 xPropSet
->setPropertyValue("FillTransparenceGradientName", css::uno::Any(OUString()));
342 const OUString
& aName
= rItem
.GetName();
343 css::uno::Any aGradientVal
;
344 rItem
.QueryValue(aGradientVal
, MID_FILLGRADIENT
);
345 OUString aNewName
= PropertyHelper::addTransparencyGradientUniqueNameToTable(aGradientVal
, css::uno::Reference
<css::lang::XMultiServiceFactory
>(mxModel
, css::uno::UNO_QUERY_THROW
), aName
);
346 xPropSet
->setPropertyValue("FillTransparenceGradientName", css::uno::Any(aNewName
));
349 void ChartAreaPanel::setFillStyle(const XFillStyleItem
& rItem
)
351 PreventUpdate
aProtector(mbUpdate
);
352 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
356 xPropSet
->setPropertyValue("FillStyle", css::uno::Any(rItem
.GetValue()));
359 void ChartAreaPanel::setFillStyleAndColor(const XFillStyleItem
* pStyleItem
,
360 const XFillColorItem
& rColorItem
)
362 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
367 xPropSet
->setPropertyValue("FillStyle", css::uno::Any(pStyleItem
->GetValue()));
368 xPropSet
->setPropertyValue("FillColor", css::uno::Any(rColorItem
.GetValue()));
371 void ChartAreaPanel::setFillStyleAndGradient(const XFillStyleItem
* pStyleItem
,
372 const XFillGradientItem
& rGradientItem
)
374 PreventUpdate
aProtector(mbUpdate
);
375 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
380 xPropSet
->setPropertyValue("FillStyle", css::uno::Any(pStyleItem
->GetValue()));
382 const OUString
& aName
= rGradientItem
.GetName();
383 css::uno::Any aGradientVal
;
384 rGradientItem
.QueryValue(aGradientVal
, MID_FILLGRADIENT
);
385 OUString aNewName
= PropertyHelper::addGradientUniqueNameToTable(aGradientVal
, css::uno::Reference
<css::lang::XMultiServiceFactory
>(mxModel
, css::uno::UNO_QUERY_THROW
), aName
);
386 xPropSet
->setPropertyValue("FillGradientName", css::uno::Any(aNewName
));
389 void ChartAreaPanel::setFillStyleAndHatch(const XFillStyleItem
* pStyleItem
,
390 const XFillHatchItem
& rHatchItem
)
392 PreventUpdate
aProtector(mbUpdate
);
393 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
398 xPropSet
->setPropertyValue("FillStyle", css::uno::Any(pStyleItem
->GetValue()));
399 xPropSet
->setPropertyValue("FillHatchName", css::uno::Any(rHatchItem
.GetValue()));
402 void ChartAreaPanel::setFillStyleAndBitmap(const XFillStyleItem
* pStyleItem
,
403 const XFillBitmapItem
& rBitmapItem
)
405 PreventUpdate
aProtector(mbUpdate
);
406 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
411 xPropSet
->setPropertyValue("FillStyle", css::uno::Any(pStyleItem
->GetValue()));
413 css::uno::Any aBitmap
;
414 rBitmapItem
.QueryValue(aBitmap
, MID_BITMAP
);
415 const OUString
& aPreferredName
= rBitmapItem
.GetName();
416 aBitmap
<<= PropertyHelper::addBitmapUniqueNameToTable(aBitmap
, css::uno::Reference
<css::lang::XMultiServiceFactory
>(mxModel
, css::uno::UNO_QUERY_THROW
), aPreferredName
);
417 xPropSet
->setPropertyValue("FillBitmapName", aBitmap
);
420 void ChartAreaPanel::updateData()
422 if (!mbUpdate
|| !mbModelValid
)
425 css::uno::Reference
<css::beans::XPropertySet
> xPropSet
= getPropSet(mxModel
);
429 css::uno::Reference
<css::beans::XPropertySetInfo
> xInfo(xPropSet
->getPropertySetInfo());
433 SolarMutexGuard aGuard
;
434 if (xInfo
->hasPropertyByName("FillStyle"))
436 css::drawing::FillStyle eFillStyle
= css::drawing::FillStyle_SOLID
;
437 xPropSet
->getPropertyValue("FillStyle") >>= eFillStyle
;
438 XFillStyleItem
aFillStyleItem(eFillStyle
);
439 updateFillStyle(false, true, &aFillStyleItem
);
442 if (xInfo
->hasPropertyByName("FillTransparence"))
444 sal_uInt16 nFillTransparence
= 0;
445 xPropSet
->getPropertyValue("FillTransparence") >>= nFillTransparence
;
446 SfxUInt16Item
aTransparenceItem(0, nFillTransparence
);
447 updateFillTransparence(false, true, &aTransparenceItem
);
450 if (xInfo
->hasPropertyByName("FillGradientName"))
452 OUString aGradientName
;
453 xPropSet
->getPropertyValue("FillGradientName") >>= aGradientName
;
454 XFillGradientItem aGradientItem
= getXGradientForName(mxModel
, aGradientName
);
455 updateFillGradient(false, true, &aGradientItem
);
458 if (xInfo
->hasPropertyByName("FillHatchName"))
461 xPropSet
->getPropertyValue("FillHatchName") >>= aHatchName
;
462 XHatch aHatch
= getXHatchFromName(mxModel
, aHatchName
);
463 XFillHatchItem
aHatchItem(aHatchName
, aHatch
);
464 updateFillHatch(false, true, &aHatchItem
);
467 if (xInfo
->hasPropertyByName("FillBitmapName"))
469 OUString aBitmapName
;
470 xPropSet
->getPropertyValue("FillBitmapName") >>= aBitmapName
;
471 GraphicObject aBitmap
= getXBitmapFromName(mxModel
, aBitmapName
);
472 XFillBitmapItem
aBitmapItem(aBitmapName
, aBitmap
);
473 std::unique_ptr
<XFillBitmapItem
> pBitmapItem
;
474 DrawModelWrapper
* pModelWrapper
= nullptr;
477 pModelWrapper
= getDrawModelWrapper(mxModel
);
480 pBitmapItem
= aBitmapItem
.checkForUniqueItem(&pModelWrapper
->getSdrModel());
486 updateFillBitmap(false, true, pBitmapItem
? pBitmapItem
.get() : &aBitmapItem
);
489 if (xInfo
->hasPropertyByName("FillTransparenceGradientName"))
491 OUString aFillFloatTransparenceName
;
492 xPropSet
->getPropertyValue("FillTransparenceGradientName") >>= aFillFloatTransparenceName
;
493 XFillFloatTransparenceItem aFillFloatTransparenceItem
= getXTransparencyGradientForName(mxModel
, aFillFloatTransparenceName
);
494 updateFillFloatTransparence(false, true, &aFillFloatTransparenceItem
);
496 maFillColorWrapper
.updateData();
499 if (xInfo
->hasPropertyByName("FillColor"))
501 sal_uInt32 nFillColor
= 0;
502 xPropSet
->getPropertyValue("FillColor") >>= nFillColor
;
503 XFillColorItem
aFillColorItem("", Color(nFillColor
));
504 updateFillColor(true, &aFillColorItem
);
508 void ChartAreaPanel::modelInvalid()
510 mbModelValid
= false;
513 void ChartAreaPanel::selectionChanged(bool bCorrectType
)
519 void ChartAreaPanel::updateModel(
520 css::uno::Reference
<css::frame::XModel
> xModel
)
524 css::uno::Reference
<css::util::XModifyBroadcaster
> xBroadcaster(mxModel
, css::uno::UNO_QUERY_THROW
);
525 xBroadcaster
->removeModifyListener(mxListener
);
531 css::uno::Reference
<css::util::XModifyBroadcaster
> xBroadcasterNew(mxModel
, css::uno::UNO_QUERY_THROW
);
532 xBroadcasterNew
->addModifyListener(mxListener
);
534 css::uno::Reference
<css::view::XSelectionSupplier
> xSelectionSupplier(mxModel
->getCurrentController(), css::uno::UNO_QUERY
);
535 if (xSelectionSupplier
.is())
536 xSelectionSupplier
->addSelectionChangeListener(mxSelectionListener
.get());
542 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */