Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / sidebar / ChartAreaPanel.cxx
blob61e72318043ca49dbfd360fbc9f141b71b2cd3e0
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 "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 {
30 namespace {
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())
44 return OUString();
46 css::uno::Any aAny = xSelectionSupplier->getSelection();
47 if (!aAny.hasValue())
48 return OUString();
50 OUString aCID;
51 aAny >>= aCID;
53 return aCID;
56 css::uno::Reference<css::beans::XPropertySet> getPropSet(
57 const css::uno::Reference<css::frame::XModel>& xModel)
59 OUString aCID = getCID(xModel);
60 css::uno::Reference<css::beans::XPropertySet> xPropSet =
61 ObjectIdentifier::getObjectPropertySet(aCID, xModel);
63 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
64 if (eType == OBJECTTYPE_DIAGRAM)
66 css::uno::Reference<css::chart2::XDiagram> xDiagram(
67 xPropSet, css::uno::UNO_QUERY);
68 if (!xDiagram.is())
69 return xPropSet;
71 xPropSet.set(xDiagram->getWall());
74 return xPropSet;
77 ChartController* getController(const css::uno::Reference<css::frame::XModel>& xModel)
79 css::uno::Reference<css::frame::XController>xController = xModel->getCurrentController();
80 if (!xController.is())
81 throw std::exception();
83 ChartController* pController = dynamic_cast<ChartController*>(xController.get());
84 if (!pController)
85 throw std::exception();
87 return pController;
90 ViewElementListProvider getViewElementListProvider( const css::uno::Reference<css::frame::XModel>& xModel)
92 ChartController* pController = getController(xModel);
93 ViewElementListProvider aProvider = pController->getViewElementListProvider();
94 return aProvider;
97 DrawModelWrapper* getDrawModelWrapper(const css::uno::Reference<css::frame::XModel>& xModel)
99 ChartController* pController = getController(xModel);
100 return pController->GetDrawModelWrapper();
103 XFillGradientItem getXGradientForName(const css::uno::Reference<css::frame::XModel>& xModel,
104 const OUString& rName)
106 css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
107 css::uno::Reference<css::container::XNameAccess> xNameAccess(
108 xFact->createInstance("com.sun.star.drawing.GradientTable"), css::uno::UNO_QUERY);
109 if (!xNameAccess.is())
110 return XFillGradientItem();
112 if (!xNameAccess->hasByName(rName))
113 return XFillGradientItem();
115 css::uno::Any aAny = xNameAccess->getByName(rName);
117 XFillGradientItem aItem;
118 aItem.SetName(rName);
119 aItem.PutValue(aAny, MID_FILLGRADIENT);
121 return aItem;
125 XFillFloatTransparenceItem getXTransparencyGradientForName(const css::uno::Reference<css::frame::XModel>& xModel,
126 const OUString& rName)
128 css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
129 css::uno::Reference<css::container::XNameAccess> xNameAccess(
130 xFact->createInstance("com.sun.star.drawing.TransparencyGradientTable"), css::uno::UNO_QUERY);
131 if (!xNameAccess.is())
132 return XFillFloatTransparenceItem();
134 if (!xNameAccess->hasByName(rName))
135 return XFillFloatTransparenceItem();
137 css::uno::Any aAny = xNameAccess->getByName(rName);
139 XFillFloatTransparenceItem aItem;
140 aItem.SetName(rName);
141 aItem.PutValue(aAny, MID_FILLGRADIENT);
142 aItem.SetEnabled(true);
144 return aItem;
147 XHatch getXHatchFromName(const css::uno::Reference<css::frame::XModel>& xModel,
148 OUString& rName)
152 ViewElementListProvider aProvider = getViewElementListProvider(xModel);
153 XHatchListRef aRef = aProvider.GetHatchList();
154 size_t n = aRef->Count();
155 for (size_t i = 0; i < n; ++i)
157 const XHatchEntry* pHatch = aRef->GetHatch(i);
158 if (!pHatch)
159 continue;
161 if (pHatch->GetName().equalsIgnoreAsciiCase(rName))
163 // we need to update the hatch name
164 rName = pHatch->GetName();
165 return pHatch->GetHatch();
169 catch (...)
171 // ignore exception
174 return XHatch();
177 GraphicObject getXBitmapFromName(const css::uno::Reference<css::frame::XModel>& xModel,
178 const OUString& rName)
182 ViewElementListProvider aProvider = getViewElementListProvider(xModel);
183 XBitmapListRef aBmpRef = aProvider.GetBitmapList();
184 XPatternListRef aPatRef = aProvider.GetPatternList();
186 size_t n = aBmpRef->Count();
187 for (size_t i = 0; i < n; ++i)
189 const XBitmapEntry* pBitmap = aBmpRef->GetBitmap(i);
190 if (!pBitmap)
191 continue;
193 if (pBitmap->GetName().equalsIgnoreAsciiCase(rName))
195 return pBitmap->GetGraphicObject();
199 // perhaps it's a pattern
200 size_t m = aPatRef->Count();
201 for (size_t i = 0; i < m; ++i)
203 const XBitmapEntry* pBitmap = aPatRef->GetBitmap(i);
204 if (!pBitmap)
205 continue;
207 if (pBitmap->GetName().equalsIgnoreAsciiCase(rName))
209 return pBitmap->GetGraphicObject();
213 catch (...)
215 // ignore exception
218 return GraphicObject();
221 class PreventUpdate
223 public:
224 explicit PreventUpdate(bool& bUpdate):
225 mbUpdate(bUpdate)
227 mbUpdate = false;
230 ~PreventUpdate()
232 mbUpdate = true;
235 private:
236 bool& mbUpdate;
241 VclPtr<vcl::Window> ChartAreaPanel::Create(
242 vcl::Window* pParent,
243 const css::uno::Reference<css::frame::XFrame>& rxFrame,
244 ChartController* pController)
246 if (pParent == nullptr)
247 throw css::lang::IllegalArgumentException("no parent Window given to ChartAxisPanel::Create", nullptr, 0);
248 if (!rxFrame.is())
249 throw css::lang::IllegalArgumentException("no XFrame given to ChartAxisPanel::Create", nullptr, 1);
251 return VclPtr<ChartAreaPanel>::Create(
252 pParent, rxFrame, pController);
255 ChartAreaPanel::ChartAreaPanel(vcl::Window* pParent,
256 const css::uno::Reference<css::frame::XFrame>& rxFrame,
257 ChartController* pController):
258 svx::sidebar::AreaPropertyPanelBase(pParent, rxFrame),
259 mxModel(pController->getModel()),
260 mxListener(new ChartSidebarModifyListener(this)),
261 mxSelectionListener(new ChartSidebarSelectionListener(this)),
262 mbUpdate(true),
263 mbModelValid(true),
264 maFillColorWrapper(mxModel, getColorToolBoxControl(mpToolBoxColor.get()), "FillColor")
266 std::vector<ObjectType> aAcceptedTypes { OBJECTTYPE_PAGE, OBJECTTYPE_DIAGRAM,
267 OBJECTTYPE_DATA_SERIES, OBJECTTYPE_DATA_POINT,
268 OBJECTTYPE_TITLE, OBJECTTYPE_LEGEND};
269 mxSelectionListener->setAcceptedTypes(aAcceptedTypes);
270 Initialize();
273 ChartAreaPanel::~ChartAreaPanel()
275 disposeOnce();
278 void ChartAreaPanel::dispose()
280 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
281 xBroadcaster->removeModifyListener(mxListener);
283 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
284 if (xSelectionSupplier.is())
285 xSelectionSupplier->removeSelectionChangeListener(mxSelectionListener.get());
287 AreaPropertyPanelBase::dispose();
290 void ChartAreaPanel::Initialize()
292 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
293 xBroadcaster->addModifyListener(mxListener);
295 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
296 if (xSelectionSupplier.is())
297 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener.get());
299 SvxColorToolBoxControl* pToolBoxColor = getColorToolBoxControl(mpToolBoxColor.get());
300 pToolBoxColor->setColorSelectFunction(maFillColorWrapper);
302 updateData();
305 void ChartAreaPanel::setFillTransparence(const XFillTransparenceItem& rItem)
307 PreventUpdate aProtector(mbUpdate);
308 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
309 if (!xPropSet.is())
310 return;
312 xPropSet->setPropertyValue("FillTransparence", css::uno::Any(rItem.GetValue()));
315 void ChartAreaPanel::setFillFloatTransparence(
316 const XFillFloatTransparenceItem& rItem)
318 PreventUpdate aProtector(mbUpdate);
319 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
320 if (!xPropSet.is())
321 return;
323 if (!rItem.IsEnabled())
325 xPropSet->setPropertyValue("FillTransparenceGradientName", css::uno::Any(OUString()));
326 return;
329 const OUString& aName = rItem.GetName();
330 css::uno::Any aGradientVal;
331 rItem.QueryValue(aGradientVal, MID_FILLGRADIENT);
332 OUString aNewName = PropertyHelper::addTransparencyGradientUniqueNameToTable(aGradientVal, css::uno::Reference<css::lang::XMultiServiceFactory>(mxModel, css::uno::UNO_QUERY_THROW), aName);
333 xPropSet->setPropertyValue("FillTransparenceGradientName", css::uno::Any(aNewName));
336 void ChartAreaPanel::setFillStyle(const XFillStyleItem& rItem)
338 PreventUpdate aProtector(mbUpdate);
339 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
340 if (!xPropSet.is())
341 return;
343 xPropSet->setPropertyValue("FillStyle", css::uno::Any(rItem.GetValue()));
346 void ChartAreaPanel::setFillStyleAndColor(const XFillStyleItem* pStyleItem,
347 const XFillColorItem& rColorItem)
349 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
350 if (!xPropSet.is())
351 return;
353 if (pStyleItem)
354 xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
355 xPropSet->setPropertyValue("FillColor", css::uno::Any(rColorItem.GetValue()));
358 void ChartAreaPanel::setFillStyleAndGradient(const XFillStyleItem* pStyleItem,
359 const XFillGradientItem& rGradientItem)
361 PreventUpdate aProtector(mbUpdate);
362 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
363 if (!xPropSet.is())
364 return;
366 if (pStyleItem)
367 xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
369 const OUString& aName = rGradientItem.GetName();
370 css::uno::Any aGradientVal;
371 rGradientItem.QueryValue(aGradientVal, MID_FILLGRADIENT);
372 OUString aNewName = PropertyHelper::addGradientUniqueNameToTable(aGradientVal, css::uno::Reference<css::lang::XMultiServiceFactory>(mxModel, css::uno::UNO_QUERY_THROW), aName);
373 xPropSet->setPropertyValue("FillGradientName", css::uno::Any(aNewName));
376 void ChartAreaPanel::setFillStyleAndHatch(const XFillStyleItem* pStyleItem,
377 const XFillHatchItem& rHatchItem)
379 PreventUpdate aProtector(mbUpdate);
380 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
381 if (!xPropSet.is())
382 return;
384 if (pStyleItem)
385 xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
386 xPropSet->setPropertyValue("FillHatchName", css::uno::Any(rHatchItem.GetValue()));
389 void ChartAreaPanel::setFillStyleAndBitmap(const XFillStyleItem* pStyleItem,
390 const XFillBitmapItem& rBitmapItem)
392 PreventUpdate aProtector(mbUpdate);
393 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
394 if (!xPropSet.is())
395 return;
397 if (pStyleItem)
398 xPropSet->setPropertyValue("FillStyle", css::uno::Any(pStyleItem->GetValue()));
400 css::uno::Any aBitmap;
401 rBitmapItem.QueryValue(aBitmap, MID_BITMAP);
402 const OUString& aPreferredName = rBitmapItem.GetName();
403 aBitmap <<= PropertyHelper::addBitmapUniqueNameToTable(aBitmap, css::uno::Reference<css::lang::XMultiServiceFactory>(mxModel, css::uno::UNO_QUERY_THROW), aPreferredName);
404 xPropSet->setPropertyValue("FillBitmapName", aBitmap);
407 void ChartAreaPanel::updateData()
409 if (!mbUpdate || !mbModelValid)
410 return;
412 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
413 if (!xPropSet.is())
414 return;
416 css::uno::Reference<css::beans::XPropertySetInfo> xInfo(xPropSet->getPropertySetInfo());
417 if (!xInfo.is())
418 return;
420 SolarMutexGuard aGuard;
421 if (xInfo->hasPropertyByName("FillStyle"))
423 css::drawing::FillStyle eFillStyle = css::drawing::FillStyle_SOLID;
424 xPropSet->getPropertyValue("FillStyle") >>= eFillStyle;
425 XFillStyleItem aFillStyleItem(eFillStyle);
426 updateFillStyle(false, true, &aFillStyleItem);
429 if (xInfo->hasPropertyByName("FillTransparence"))
431 sal_uInt16 nFillTransparence = 0;
432 xPropSet->getPropertyValue("FillTransparence") >>= nFillTransparence;
433 SfxUInt16Item aTransparenceItem(0, nFillTransparence);
434 updateFillTransparence(false, true, &aTransparenceItem);
437 if (xInfo->hasPropertyByName("FillGradientName"))
439 OUString aGradientName;
440 xPropSet->getPropertyValue("FillGradientName") >>= aGradientName;
441 XFillGradientItem aGradientItem = getXGradientForName(mxModel, aGradientName);
442 updateFillGradient(false, true, &aGradientItem);
445 if (xInfo->hasPropertyByName("FillHatchName"))
447 OUString aHatchName;
448 xPropSet->getPropertyValue("FillHatchName") >>= aHatchName;
449 XHatch aHatch = getXHatchFromName(mxModel, aHatchName);
450 XFillHatchItem aHatchItem(aHatchName, aHatch);
451 updateFillHatch(false, true, &aHatchItem);
454 if (xInfo->hasPropertyByName("FillBitmapName"))
456 OUString aBitmapName;
457 xPropSet->getPropertyValue("FillBitmapName") >>= aBitmapName;
458 GraphicObject aBitmap = getXBitmapFromName(mxModel, aBitmapName);
459 XFillBitmapItem aBitmapItem(aBitmapName, aBitmap);
460 std::unique_ptr<XFillBitmapItem> pBitmapItem;
461 DrawModelWrapper* pModelWrapper = nullptr;
464 pModelWrapper = getDrawModelWrapper(mxModel);
465 if (pModelWrapper)
467 pBitmapItem = aBitmapItem.checkForUniqueItem(&pModelWrapper->getSdrModel());
470 catch (...)
473 updateFillBitmap(false, true, pBitmapItem ? pBitmapItem.get() : &aBitmapItem);
476 if (xInfo->hasPropertyByName("FillTransparenceGradientName"))
478 OUString aFillFloatTransparenceName;
479 xPropSet->getPropertyValue("FillTransparenceGradientName") >>= aFillFloatTransparenceName;
480 XFillFloatTransparenceItem aFillFloatTransparenceItem = getXTransparencyGradientForName(mxModel, aFillFloatTransparenceName);
481 updateFillFloatTransparence(false, true, &aFillFloatTransparenceItem);
483 maFillColorWrapper.updateData();
486 if (xInfo->hasPropertyByName("FillColor"))
488 sal_uInt32 nFillColor = 0;
489 xPropSet->getPropertyValue("FillColor") >>= nFillColor;
490 XFillColorItem aFillColorItem("", Color(nFillColor));
491 updateFillColor(true, &aFillColorItem);
495 void ChartAreaPanel::modelInvalid()
497 mbModelValid = false;
500 void ChartAreaPanel::selectionChanged(bool bCorrectType)
502 if (bCorrectType)
503 updateData();
506 void ChartAreaPanel::updateModel(
507 css::uno::Reference<css::frame::XModel> xModel)
509 if (mbModelValid)
511 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcaster(mxModel, css::uno::UNO_QUERY_THROW);
512 xBroadcaster->removeModifyListener(mxListener);
515 mxModel = xModel;
516 mbModelValid = true;
518 css::uno::Reference<css::util::XModifyBroadcaster> xBroadcasterNew(mxModel, css::uno::UNO_QUERY_THROW);
519 xBroadcasterNew->addModifyListener(mxListener);
521 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
522 if (xSelectionSupplier.is())
523 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener.get());
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */