Debian Codegraph @

chart2/source/controller/sidebar/ChartAreaPanel.cxx

raw
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 "ChartAreaPanel.hxx"
15
16 #include <ChartController.hxx>
17 #include <ChartModel.hxx>
18 #include <ViewElementListProvider.hxx>
19 #include <PropertyHelper.hxx>
20 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
21
22 #include <chartview/DrawModelWrapper.hxx>
23 #include <com/sun/star/chart2/XDiagram.hpp>
24 #include <comphelper/lok.hxx>
25 #include <sfx2/viewsh.hxx>
26
27 #include <sfx2/weldutils.hxx>
28 #include <svx/xfltrit.hxx>
29 #include <svx/xflftrit.hxx>
30 #include <svx/xbtmpit.hxx>
31 #include <svx/unomid.hxx>
32 #include <vcl/svapp.hxx>
33
34 #include <svx/tbcontrl.hxx>
35
36 namespace chart::sidebar {
37
38 namespace {
39
40 SvxColorToolBoxControl* getColorToolBoxControl(const ToolbarUnoDispatcher& rColorDispatch)
41 {
42 css::uno::Reference<css::frame::XToolbarController> xController = rColorDispatch.GetControllerForCommand(u".uno:FillColor"_ustr);
43 SvxColorToolBoxControl* pToolBoxColorControl = dynamic_cast<SvxColorToolBoxControl*>(xController.get());
44 return pToolBoxColorControl;
45 }
46
47 OUString getCID(const rtl::Reference<::chart::ChartModel>& xModel)
48 {
49 css::uno::Reference<css::frame::XController> xController(xModel->getCurrentController());
50 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(xController, css::uno::UNO_QUERY);
51 if (!xSelectionSupplier.is())
52 return OUString();
53
54 css::uno::Any aAny = xSelectionSupplier->getSelection();
55 if (!aAny.hasValue())
56 {
57 // if no selection, default to diagram wall so sidebar can show some editable properties
58 ChartController* pController = dynamic_cast<ChartController*>(xController.get());
59 if (pController)
60 {
61 pController->select( css::uno::Any( ObjectIdentifier::createClassifiedIdentifier( OBJECTTYPE_PAGE, u"" ) ) );
62 xSelectionSupplier = css::uno::Reference<css::view::XSelectionSupplier>(xController, css::uno::UNO_QUERY);
63 if (xSelectionSupplier.is())
64 aAny = xSelectionSupplier->getSelection();
65 }
66
67 if (!aAny.hasValue())
68 return OUString();
69 }
70
71 OUString aCID;
72 aAny >>= aCID;
73
74 return aCID;
75 }
76
77 css::uno::Reference<css::beans::XPropertySet> getPropSet(
78 const rtl::Reference<::chart::ChartModel>& xModel)
79 {
80 OUString aCID = getCID(xModel);
81 css::uno::Reference<css::beans::XPropertySet> xPropSet =
82 ObjectIdentifier::getObjectPropertySet(aCID, xModel);
83
84 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
85 if (eType == OBJECTTYPE_DIAGRAM)
86 {
87 css::uno::Reference<css::chart2::XDiagram> xDiagram(
88 xPropSet, css::uno::UNO_QUERY);
89 if (!xDiagram.is())
90 return xPropSet;
91
92 xPropSet.set(xDiagram->getWall());
93 }
94
95 return xPropSet;
96 }
97
98 ChartController* getController(const css::uno::Reference<css::frame::XModel>& xModel)
99 {
100 css::uno::Reference<css::frame::XController>xController = xModel->getCurrentController();
101 if (!xController.is())
102 throw std::exception();
103
104 ChartController* pController = dynamic_cast<ChartController*>(xController.get());
105 if (!pController)
106 throw std::exception();
107
108 return pController;
109 }
110
111 ViewElementListProvider getViewElementListProvider( const css::uno::Reference<css::frame::XModel>& xModel)
112 {
113 ChartController* pController = getController(xModel);
114 ViewElementListProvider aProvider = pController->getViewElementListProvider();
115 return aProvider;
116 }
117
118 DrawModelWrapper* getDrawModelWrapper(const css::uno::Reference<css::frame::XModel>& xModel)
119 {
120 ChartController* pController = getController(xModel);
121 return pController->GetDrawModelWrapper();
122 }
123
124 XFillGradientItem getXGradientForName(const css::uno::Reference<css::frame::XModel>& xModel,
125 const OUString& rName)
126 {
127 css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
128 css::uno::Reference<css::container::XNameAccess> xNameAccess(
129 xFact->createInstance(u"com.sun.star.drawing.GradientTable"_ustr), css::uno::UNO_QUERY);
130 if (!xNameAccess.is())
131 return XFillGradientItem();
132
133 if (!xNameAccess->hasByName(rName))
134 return XFillGradientItem();
135
136 css::uno::Any aAny = xNameAccess->getByName(rName);
137
138 XFillGradientItem aItem;
139 aItem.SetName(rName);
140 aItem.PutValue(aAny, MID_FILLGRADIENT);
141
142 return aItem;
143
144 }
145
146 XFillFloatTransparenceItem getXTransparencyGradientForName(const css::uno::Reference<css::frame::XModel>& xModel,
147 const OUString& rName)
148 {
149 css::uno::Reference<css::lang::XMultiServiceFactory> xFact(xModel, css::uno::UNO_QUERY);
150 css::uno::Reference<css::container::XNameAccess> xNameAccess(
151 xFact->createInstance(u"com.sun.star.drawing.TransparencyGradientTable"_ustr), css::uno::UNO_QUERY);
152 if (!xNameAccess.is())
153 return XFillFloatTransparenceItem();
154
155 if (!xNameAccess->hasByName(rName))
156 return XFillFloatTransparenceItem();
157
158 css::uno::Any aAny = xNameAccess->getByName(rName);
159
160 XFillFloatTransparenceItem aItem;
161 aItem.SetName(rName);
162 aItem.PutValue(aAny, MID_FILLGRADIENT);
163 aItem.SetEnabled(true);
164
165 return aItem;
166 }
167
168 XHatch getXHatchFromName(const css::uno::Reference<css::frame::XModel>& xModel,
169 OUString& rName)
170 {
171 try
172 {
173 ViewElementListProvider aProvider = getViewElementListProvider(xModel);
174 XHatchListRef aRef = aProvider.GetHatchList();
175 size_t n = aRef->Count();
176 for (size_t i = 0; i < n; ++i)
177 {
178 const XHatchEntry* pHatch = aRef->GetHatch(i);
179 if (!pHatch)
180 continue;
181
182 if (pHatch->GetName().equalsIgnoreAsciiCase(rName))
183 {
184 // we need to update the hatch name
185 rName = pHatch->GetName();
186 return pHatch->GetHatch();
187 }
188 }
189 }
190 catch (...)
191 {
192 // ignore exception
193 }
194
195 return XHatch();
196 }
197
198 GraphicObject getXBitmapFromName(const css::uno::Reference<css::frame::XModel>& xModel,
199 std::u16string_view rName)
200 {
201 try
202 {
203 ViewElementListProvider aProvider = getViewElementListProvider(xModel);
204 XBitmapListRef aBmpRef = aProvider.GetBitmapList();
205 XPatternListRef aPatRef = aProvider.GetPatternList();
206
207 size_t n = aBmpRef->Count();
208 for (size_t i = 0; i < n; ++i)
209 {
210 const XBitmapEntry* pBitmap = aBmpRef->GetBitmap(i);
211 if (!pBitmap)
212 continue;
213
214 if (pBitmap->GetName().equalsIgnoreAsciiCase(rName))
215 {
216 return pBitmap->GetGraphicObject();
217 }
218 }
219
220 // perhaps it's a pattern
221 size_t m = aPatRef->Count();
222 for (size_t i = 0; i < m; ++i)
223 {
224 const XBitmapEntry* pBitmap = aPatRef->GetBitmap(i);
225 if (!pBitmap)
226 continue;
227
228 if (pBitmap->GetName().equalsIgnoreAsciiCase(rName))
229 {
230 return pBitmap->GetGraphicObject();
231 }
232 }
233 }
234 catch (...)
235 {
236 // ignore exception
237 }
238
239 return GraphicObject();
240 }
241
242 class PreventUpdate
243 {
244 public:
245 explicit PreventUpdate(bool& bUpdate):
246 mbUpdate(bUpdate)
247 {
248 mbUpdate = false;
249 }
250
251 ~PreventUpdate()
252 {
253 mbUpdate = true;
254 }
255
256 private:
257 bool& mbUpdate;
258 };
259
260 }
261
262 std::unique_ptr<PanelLayout> ChartAreaPanel::Create(
263 weld::Widget* pParent,
264 const css::uno::Reference<css::frame::XFrame>& rxFrame,
265 ChartController* pController)
266 {
267 if (pParent == nullptr)
268 throw css::lang::IllegalArgumentException(u"no parent Window given to ChartAxisPanel::Create"_ustr, nullptr, 0);
269 if (!rxFrame.is())
270 throw css::lang::IllegalArgumentException(u"no XFrame given to ChartAxisPanel::Create"_ustr, nullptr, 1);
271
272 return std::make_unique<ChartAreaPanel>(pParent, rxFrame, pController);
273 }
274
275 ChartAreaPanel::ChartAreaPanel(weld::Widget* pParent,
276 const css::uno::Reference<css::frame::XFrame>& rxFrame,
277 ChartController* pController):
278 svx::sidebar::AreaPropertyPanelBase(pParent, rxFrame),
279 mxModel(pController->getChartModel()),
280 mxListener(new ChartSidebarModifyListener(this)),
281 mxSelectionListener(new ChartSidebarSelectionListener(this)),
282 mbUpdate(true),
283 mbModelValid(true),
284 maFillColorWrapper(mxModel, getColorToolBoxControl(*mxColorDispatch), u"FillColor"_ustr)
285 {
286 std::vector<ObjectType> aAcceptedTypes { OBJECTTYPE_PAGE, OBJECTTYPE_DIAGRAM,
287 OBJECTTYPE_DATA_SERIES, OBJECTTYPE_DATA_POINT,
288 OBJECTTYPE_TITLE, OBJECTTYPE_LEGEND};
289 mxSelectionListener->setAcceptedTypes(std::move(aAcceptedTypes));
290 Initialize();
291 }
292
293 ChartAreaPanel::~ChartAreaPanel()
294 {
295 doUpdateModel(nullptr);
296 }
297
298 void ChartAreaPanel::Initialize()
299 {
300 mxModel->addModifyListener(mxListener);
301
302 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
303 if (xSelectionSupplier.is())
304 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
305
306 SvxColorToolBoxControl* pToolBoxColor = getColorToolBoxControl(*mxColorDispatch);
307 pToolBoxColor->setColorSelectFunction(maFillColorWrapper);
308
309 updateData();
310 }
311
312 bool ChartAreaPanel::selectionIsDataSeries() const
313 {
314 OUString aCID = getCID(mxModel);
315 ObjectType eType = ObjectIdentifier::getObjectType(aCID);
316 return eType == ObjectType::OBJECTTYPE_DATA_SERIES || eType == ObjectType::OBJECTTYPE_DATA_POINT;
317 }
318
319 void ChartAreaPanel::setFillTransparence(const XFillTransparenceItem& rItem)
320 {
321 PreventUpdate aProtector(mbUpdate);
322 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
323 if (!xPropSet.is())
324 return;
325
326 xPropSet->setPropertyValue(u"FillTransparence"_ustr, css::uno::Any(rItem.GetValue()));
327 }
328
329 void ChartAreaPanel::setFillFloatTransparence(
330 const XFillFloatTransparenceItem& rItem)
331 {
332 PreventUpdate aProtector(mbUpdate);
333 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
334 if (!xPropSet.is())
335 return;
336
337 if (!rItem.IsEnabled())
338 {
339 xPropSet->setPropertyValue(u"FillTransparenceGradientName"_ustr, css::uno::Any(OUString()));
340 return;
341 }
342
343 const OUString& aName = rItem.GetName();
344 css::uno::Any aGradientVal;
345 rItem.QueryValue(aGradientVal, MID_FILLGRADIENT);
346 OUString aNewName = PropertyHelper::addTransparencyGradientUniqueNameToTable(aGradientVal, mxModel, aName);
347 xPropSet->setPropertyValue(u"FillTransparenceGradientName"_ustr, css::uno::Any(aNewName));
348 }
349
350 void ChartAreaPanel::setFillStyle(const XFillStyleItem& rItem)
351 {
352 PreventUpdate aProtector(mbUpdate);
353 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
354 if (!xPropSet.is())
355 return;
356
357 xPropSet->setPropertyValue(u"FillStyle"_ustr, css::uno::Any(rItem.GetValue()));
358 }
359
360 void ChartAreaPanel::setFillStyleAndColor(const XFillStyleItem* pStyleItem,
361 const XFillColorItem& rColorItem)
362 {
363 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
364 if (!xPropSet.is())
365 return;
366
367 if (pStyleItem)
368 xPropSet->setPropertyValue(u"FillStyle"_ustr, css::uno::Any(pStyleItem->GetValue()));
369 xPropSet->setPropertyValue(u"FillColor"_ustr, css::uno::Any(rColorItem.GetValue()));
370 if (selectionIsDataSeries())
371 {
372 mxModel->clearColorPalette();
373 }
374 }
375
376 void ChartAreaPanel::setFillStyleAndGradient(const XFillStyleItem* pStyleItem,
377 const XFillGradientItem& rGradientItem)
378 {
379 PreventUpdate aProtector(mbUpdate);
380 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
381 if (!xPropSet.is())
382 return;
383
384 if (pStyleItem)
385 xPropSet->setPropertyValue(u"FillStyle"_ustr, css::uno::Any(pStyleItem->GetValue()));
386
387 const OUString& aName = rGradientItem.GetName();
388 css::uno::Any aGradientVal;
389 rGradientItem.QueryValue(aGradientVal, MID_FILLGRADIENT);
390 OUString aNewName = PropertyHelper::addGradientUniqueNameToTable(aGradientVal, mxModel, aName);
391 xPropSet->setPropertyValue(u"FillGradientName"_ustr, css::uno::Any(aNewName));
392 }
393
394 void ChartAreaPanel::setFillStyleAndHatch(const XFillStyleItem* pStyleItem,
395 const XFillHatchItem& rHatchItem)
396 {
397 PreventUpdate aProtector(mbUpdate);
398 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
399 if (!xPropSet.is())
400 return;
401
402 if (pStyleItem)
403 xPropSet->setPropertyValue(u"FillStyle"_ustr, css::uno::Any(pStyleItem->GetValue()));
404 xPropSet->setPropertyValue(u"FillHatchName"_ustr, css::uno::Any(rHatchItem.GetValue()));
405 }
406
407 void ChartAreaPanel::setFillStyleAndBitmap(const XFillStyleItem* pStyleItem,
408 const XFillBitmapItem& rBitmapItem)
409 {
410 PreventUpdate aProtector(mbUpdate);
411 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
412 if (!xPropSet.is())
413 return;
414
415 if (pStyleItem)
416 xPropSet->setPropertyValue(u"FillStyle"_ustr, css::uno::Any(pStyleItem->GetValue()));
417
418 css::uno::Any aBitmap;
419 rBitmapItem.QueryValue(aBitmap, MID_BITMAP);
420 const OUString& aPreferredName = rBitmapItem.GetName();
421 aBitmap <<= PropertyHelper::addBitmapUniqueNameToTable(aBitmap, mxModel, aPreferredName);
422 xPropSet->setPropertyValue(u"FillBitmapName"_ustr, aBitmap);
423 }
424
425 void ChartAreaPanel::setFillUseBackground(const XFillStyleItem* pStyleItem,
426 const XFillUseSlideBackgroundItem& /*rItem*/)
427 {
428 setFillStyle(*pStyleItem);
429 }
430
431 void ChartAreaPanel::updateData()
432 {
433 if (!mbUpdate || !mbModelValid)
434 return;
435
436 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
437 if (!xPropSet.is())
438 return;
439
440 css::uno::Reference<css::beans::XPropertySetInfo> xInfo(xPropSet->getPropertySetInfo());
441 if (!xInfo.is())
442 return;
443
444 SolarMutexGuard aGuard;
445 if (xInfo->hasPropertyByName(u"FillStyle"_ustr))
446 {
447 css::drawing::FillStyle eFillStyle = css::drawing::FillStyle_SOLID;
448 xPropSet->getPropertyValue(u"FillStyle"_ustr) >>= eFillStyle;
449 XFillStyleItem aFillStyleItem(eFillStyle);
450 updateFillStyle(false, true, &aFillStyleItem);
451 }
452
453 if (xInfo->hasPropertyByName(u"FillTransparence"_ustr))
454 {
455 sal_uInt16 nFillTransparence = 0;
456 xPropSet->getPropertyValue(u"FillTransparence"_ustr) >>= nFillTransparence;
457 XFillTransparenceItem aTransparenceItem(nFillTransparence);
458 updateFillTransparence(false, true, &aTransparenceItem);
459 }
460
461 if (xInfo->hasPropertyByName(u"FillGradientName"_ustr))
462 {
463 OUString aGradientName;
464 xPropSet->getPropertyValue(u"FillGradientName"_ustr) >>= aGradientName;
465 XFillGradientItem aGradientItem = getXGradientForName(mxModel, aGradientName);
466 updateFillGradient(false, true, &aGradientItem);
467 }
468
469 if (xInfo->hasPropertyByName(u"FillHatchName"_ustr))
470 {
471 OUString aHatchName;
472 xPropSet->getPropertyValue(u"FillHatchName"_ustr) >>= aHatchName;
473 XHatch aHatch = getXHatchFromName(mxModel, aHatchName);
474 XFillHatchItem aHatchItem(aHatchName, aHatch);
475 updateFillHatch(false, true, &aHatchItem);
476 }
477
478 if (xInfo->hasPropertyByName(u"FillBitmapName"_ustr))
479 {
480 OUString aBitmapName;
481 xPropSet->getPropertyValue(u"FillBitmapName"_ustr) >>= aBitmapName;
482 GraphicObject aBitmap = getXBitmapFromName(mxModel, aBitmapName);
483 XFillBitmapItem aBitmapItem(aBitmapName, aBitmap);
484 std::unique_ptr<XFillBitmapItem> pBitmapItem;
485 try
486 {
487 DrawModelWrapper* pModelWrapper = getDrawModelWrapper(mxModel);
488 if (pModelWrapper)
489 {
490 pBitmapItem = aBitmapItem.checkForUniqueItem(pModelWrapper->getSdrModel());
491 }
492 }
493 catch (...)
494 {
495 }
496 updateFillBitmap(false, true, pBitmapItem ? pBitmapItem.get() : &aBitmapItem);
497 }
498
499 if (xInfo->hasPropertyByName(u"FillTransparenceGradientName"_ustr))
500 {
501 OUString aFillFloatTransparenceName;
502 xPropSet->getPropertyValue(u"FillTransparenceGradientName"_ustr) >>= aFillFloatTransparenceName;
503 XFillFloatTransparenceItem aFillFloatTransparenceItem = getXTransparencyGradientForName(mxModel, aFillFloatTransparenceName);
504 updateFillFloatTransparence(false, true, &aFillFloatTransparenceItem);
505
506 maFillColorWrapper.updateData();
507 }
508
509 if (xInfo->hasPropertyByName(u"FillColor"_ustr))
510 {
511 sal_uInt32 nFillColor = 0;
512 xPropSet->getPropertyValue(u"FillColor"_ustr) >>= nFillColor;
513 XFillColorItem aFillColorItem(u""_ustr, Color(ColorTransparency, nFillColor));
514 updateFillColor(true, &aFillColorItem);
515 }
516 }
517
518 void ChartAreaPanel::modelInvalid()
519 {
520 mbModelValid = false;
521 }
522
523 void ChartAreaPanel::selectionChanged(bool bCorrectType)
524 {
525 if (!bCorrectType)
526 return;
527
528 // set the initial correct color for the color picker
529 if (comphelper::LibreOfficeKit::isActive())
530 {
531 css::uno::Reference<css::beans::XPropertySet> xPropSet = getPropSet(mxModel);
532 if (xPropSet.is())
533 {
534 css::uno::Reference<css::beans::XPropertySetInfo> xInfo(xPropSet->getPropertySetInfo());
535 if (xInfo.is())
536 {
537 SolarMutexGuard aGuard;
538 if (xInfo->hasPropertyByName(u"FillStyle"_ustr))
539 {
540 css::drawing::FillStyle eFillStyle = css::drawing::FillStyle::FillStyle_NONE;
541 xPropSet->getPropertyValue(u"FillStyle"_ustr) >>= eFillStyle;
542 if (eFillStyle == css::drawing::FillStyle_SOLID)
543 {
544 if (xInfo->hasPropertyByName(u"FillColor"_ustr))
545 {
546 sal_uInt32 nFillColor = static_cast<sal_uInt32>(-1);
547 xPropSet->getPropertyValue(u"FillColor"_ustr) >>= nFillColor;
548 if (nFillColor != static_cast<sal_uInt32>(-1))
549 {
550 if (SfxViewShell* pViewShell = SfxViewShell::Current())
551 {
552 const OString sCommand = ".uno:FillColor"_ostr;
553 pViewShell->libreOfficeKitViewCallback(
554 LOK_CALLBACK_STATE_CHANGED,
555 sCommand + "=" + OString::number(nFillColor));
556 }
557 }
558 }
559 }
560 }
561 }
562 }
563 }
564
565 updateData();
566 }
567
568 void ChartAreaPanel::doUpdateModel(const rtl::Reference<::chart::ChartModel>& xModel)
569 {
570 if (mbModelValid)
571 {
572 mxModel->removeModifyListener(mxListener);
573
574 css::uno::Reference<css::view::XSelectionSupplier> oldSelectionSupplier(
575 mxModel->getCurrentController(), css::uno::UNO_QUERY);
576 if (oldSelectionSupplier.is()) {
577 oldSelectionSupplier->removeSelectionChangeListener(mxSelectionListener);
578 }
579 }
580
581 mxModel = xModel;
582 mbModelValid = mxModel.is();
583
584 if (!mbModelValid)
585 return;
586
587 mxModel->addModifyListener(mxListener);
588
589 css::uno::Reference<css::view::XSelectionSupplier> xSelectionSupplier(mxModel->getCurrentController(), css::uno::UNO_QUERY);
590 if (xSelectionSupplier.is())
591 xSelectionSupplier->addSelectionChangeListener(mxSelectionListener);
592 }
593
594 void ChartAreaPanel::updateModel( css::uno::Reference<css::frame::XModel> xModel)
595 {
596 ::chart::ChartModel* pModel = dynamic_cast<::chart::ChartModel*>(xModel.get());
597 assert(!xModel || pModel);
598 doUpdateModel(pModel);
599 }
600
601
602 }
603
604 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */