Debian Codegraph @

chart2/source/controller/sidebar/ChartColorPaletteControl.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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <memory>
21 #include <vcl/svapp.hxx>
22 #include <vcl/toolbox.hxx>
23 #include <vcl/virdev.hxx>
24
25 #include <ChartColorPaletteHelper.hxx>
26 #include "ChartColorPaletteControl.hxx"
27
28 #include <vcl/graph.hxx>
29
30 using namespace css;
31
32 namespace chart::sidebar
33 {
34 constexpr tools::Long BORDER = ChartColorPaletteLayout::ItemBorder;
35 constexpr tools::Long SIZE = ChartColorPaletteLayout::ItemSize;
36 constexpr tools::Long constElementNumber = ChartColorPaletteSize;
37
38 ChartColorPaletteControl::ChartColorPaletteControl(
39 const uno::Reference<uno::XComponentContext>& rContext)
40 : PopupWindowController(rContext, nullptr, OUString())
41 , meColorPaletteType(ChartColorPaletteType::Unknown)
42 , mnColorPaletteIndex(0)
43 {
44 }
45
46 ChartColorPaletteControl::~ChartColorPaletteControl() = default;
47
48 void ChartColorPaletteControl::initialize(const uno::Sequence<uno::Any>& rArguments)
49 {
50 svt::PopupWindowController::initialize(rArguments);
51
52 if (m_pToolbar)
53 {
54 mxPopoverContainer = std::make_unique<ToolbarPopupContainer>(m_pToolbar);
55 m_pToolbar->set_item_popover(m_aCommandURL, mxPopoverContainer->getTopLevel());
56 m_pToolbar->set_item_sensitive(m_aCommandURL, true);
57 }
58
59 ToolBox* pToolBox = nullptr;
60 ToolBoxItemId nId;
61 if (getToolboxId(nId, &pToolBox))
62 {
63 pToolBox->SetItemBits(nId, pToolBox->GetItemBits(nId) | ToolBoxItemBits::DROPDOWNONLY);
64 pToolBox->EnableItem(nId, true);
65 }
66 }
67
68 void ChartColorPaletteControl::execute(sal_Int16 /*nKeyModifier*/)
69 {
70 if (m_pToolbar)
71 {
72 // Toggle the popup also when toolbutton is activated
73 m_pToolbar->set_menu_item_active(m_aCommandURL,
74 !m_pToolbar->get_menu_item_active(m_aCommandURL));
75 }
76 else
77 {
78 // Open the popup also when Enter key is pressed.
79 createPopupWindow();
80 }
81 }
82
83 void ChartColorPaletteControl::statusChanged(const frame::FeatureStateEvent& rEvent)
84 {
85 ToolBox* pToolBox = nullptr;
86 ToolBoxItemId nId;
87 if (!getToolboxId(nId, &pToolBox) && !m_pToolbar)
88 return;
89
90 if (rEvent.FeatureURL.Complete == m_aCommandURL)
91 {
92 updateStatus();
93 }
94 }
95
96 void ChartColorPaletteControl::updateStatus(bool bForce)
97 {
98 if (!mpHandler)
99 return;
100
101 if (bForce || meColorPaletteType != getColorPaletteType()
102 || mnColorPaletteIndex != getColorPaletteIndex())
103 {
104 meColorPaletteType = getColorPaletteType();
105 mnColorPaletteIndex = getColorPaletteIndex();
106
107 ToolBox* pToolBox = nullptr;
108 ToolBoxItemId nId;
109 if (!getToolboxId(nId, &pToolBox) && !m_pToolbar)
110 return;
111
112 auto pDev = VclPtr<VirtualDevice>::Create();
113 renderSelectedColorPalette(pDev);
114 auto aSelItemImg(pDev->GetBitmap(Point(), pDev->GetOutputSizePixel()));
115 if (m_pToolbar)
116 {
117 m_pToolbar->set_item_image(m_aCommandURL, Graphic(aSelItemImg).GetXGraphic());
118 }
119 else
120 {
121 pToolBox->SetItemImage(nId, Image(aSelItemImg));
122 }
123 }
124 }
125
126 void ChartColorPaletteControl::createDiagramSnapshot() const
127 {
128 if (mpHandler)
129 mpHandler->createDiagramSnapshot();
130 }
131
132 void ChartColorPaletteControl::restoreOriginalDiagram() const
133 {
134 if (mpHandler)
135 mpHandler->restoreOriginalDiagram();
136 }
137
138 void ChartColorPaletteControl::renderSelectedColorPalette(const VclPtr<VirtualDevice>& pDev) const
139 {
140 if (!pDev)
141 return;
142
143 const auto pHelper = getColorPaletteHelper();
144 if (!pHelper)
145 return;
146
147 static constexpr Point aPosition(0, 0);
148 static constexpr Size aSize
149 = { BORDER * 6 + SIZE * constElementNumber / 2, BORDER * 5 + SIZE * 2 };
150 static constexpr tools::Rectangle aDrawArea(aPosition, aSize);
151
152 pDev->SetOutputSizePixel(aSize, /*bErase*/ true, /*bAlphaMaskTransparent*/ true);
153
154 if (getColorPaletteType() == ChartColorPaletteType::Unknown)
155 {
156 ChartColorPaletteHelper::renderNoPalette(pDev, aDrawArea);
157 return;
158 }
159
160 const ChartColorPalette aColorPalette
161 = pHelper->getColorPalette(getColorPaletteType(), getColorPaletteIndex());
162 ChartColorPaletteHelper::renderColorPalette(pDev, aDrawArea, aColorPalette, true);
163 }
164
165 void ChartColorPaletteControl::setColorPaletteHandler(
166 const std::shared_ptr<IColorPaletteHandler>& rColorPaletteHandler)
167 {
168 if (!mpHandler)
169 {
170 mpHandler = rColorPaletteHandler;
171 updateStatus(true);
172 }
173 }
174
175 std::shared_ptr<ChartColorPaletteHelper> ChartColorPaletteControl::getColorPaletteHelper() const
176 {
177 if (mpHandler)
178 return mpHandler->getHelper();
179 return nullptr;
180 }
181
182 ChartColorPaletteType ChartColorPaletteControl::getColorPaletteType() const
183 {
184 if (mpHandler)
185 return mpHandler->getType();
186 return ChartColorPaletteType::Unknown;
187 }
188
189 sal_uInt32 ChartColorPaletteControl::getColorPaletteIndex() const
190 {
191 if (mpHandler)
192 return mpHandler->getIndex();
193 return 0;
194 }
195
196 void ChartColorPaletteControl::dispatchColorPaletteCommand(const ChartColorPaletteType eType,
197 const sal_uInt32 nIndex) const
198 {
199 if (mpHandler)
200 mpHandler->select(eType, nIndex);
201 }
202
203 void ChartColorPaletteControl::applyColorPalette(const ChartColorPalette* pColorPalette) const
204 {
205 if (mpHandler)
206 mpHandler->apply(pColorPalette);
207 }
208
209 std::unique_ptr<WeldToolbarPopup> ChartColorPaletteControl::weldPopupWindow()
210 {
211 return std::make_unique<ChartColorPalettePopup>(this, m_pToolbar);
212 }
213
214 VclPtr<vcl::Window> ChartColorPaletteControl::createVclPopupWindow(vcl::Window* pParent)
215 {
216 mxInterimPopover = VclPtr<InterimToolbarPopup>::Create(
217 getFrameInterface(), pParent,
218 std::make_unique<ChartColorPalettePopup>(this, pParent->GetFrameWeld()));
219
220 mxInterimPopover->Show();
221
222 return mxInterimPopover;
223 }
224
225 OUString ChartColorPaletteControl::getImplementationName()
226 {
227 return "com.sun.star.comp.chart2.ChartColorPaletteControl";
228 }
229
230 uno::Sequence<OUString> ChartColorPaletteControl::getSupportedServiceNames()
231 {
232 return { "com.sun.star.frame.ToolbarController" };
233 }
234
235 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
236 com_sun_star_comp_chart2_ChartColorPaletteControl_get_implementation(
237 uno::XComponentContext* rContext, uno::Sequence<uno::Any> const&)
238 {
239 return cppu::acquire(new ChartColorPaletteControl(rContext));
240 }
241
242 ChartColorPalettePopup::ChartColorPalettePopup(ChartColorPaletteControl* pControl,
243 weld::Widget* pParent)
244 : WeldToolbarPopup(pControl->getFrameInterface(), pParent,
245 "modules/schart/ui/chartcolorpalettepopup.ui", "ColorPaletteWindow")
246 , mxControl(pControl)
247 , mxColorfulPalettes(new ChartColorPalettes(*m_xBuilder, "colorful_palettes", "colorfulwin"))
248 , mxMonoPalettes(
249 new ChartColorPalettes(*m_xBuilder, "monochromatic_palettes", "monochromaticwin"))
250 , meHighlightedItemType(mxControl->getColorPaletteType())
251 , mnHighlightedItemId(mxControl->getColorPaletteIndex())
252 , mbItemSelected(false)
253 {
254 mxColorfulPalettes->SetSelectHdl(LINK(this, ChartColorPalettePopup, SelectColorfulPaletteHdl));
255 mxColorfulPalettes->setMouseMoveHdl(LINK(this, ChartColorPalettePopup, ColorfulMouseMoveHdl));
256
257 mxMonoPalettes->SetSelectHdl(LINK(this, ChartColorPalettePopup, SelectMonoPaletteHdl));
258 mxMonoPalettes->setMouseMoveHdl(LINK(this, ChartColorPalettePopup, MonoMouseMoveHdl));
259
260 initColorPalettes();
261
262 selectItem(mxControl->getColorPaletteType(), mxControl->getColorPaletteIndex() + 1);
263
264 mxControl->createDiagramSnapshot();
265 }
266
267 ChartColorPalettePopup::~ChartColorPalettePopup()
268 {
269 if (!mbItemSelected)
270 mxControl->restoreOriginalDiagram();
271 }
272
273 void ChartColorPalettePopup::selectItem(const ChartColorPaletteType eType,
274 const sal_uInt32 nIndex) const
275 {
276 switch (eType)
277 {
278 default:
279 case ChartColorPaletteType::Unknown:
280 mxColorfulPalettes->SetNoSelection();
281 mxMonoPalettes->SetNoSelection();
282 break;
283 case ChartColorPaletteType::Colorful:
284 mxColorfulPalettes->SelectItem(nIndex);
285 break;
286 case ChartColorPaletteType::Monochromatic:
287 mxMonoPalettes->SelectItem(nIndex);
288 break;
289 }
290 }
291
292 void ChartColorPalettePopup::initColorPalettes() const
293 {
294 const auto pColorPaletteHelper = mxControl->getColorPaletteHelper();
295 if (!pColorPaletteHelper)
296 return;
297 // colorful palettes
298 for (size_t i = 0; i < ChartColorPaletteHelper::ColorfulPaletteSize; ++i)
299 mxColorfulPalettes->insert(
300 pColorPaletteHelper->getColorPalette(ChartColorPaletteType::Colorful, i));
301 mxColorfulPalettes->Fill();
302 // monotonic palettes
303 for (size_t i = 0; i < ChartColorPaletteHelper::MonotonicPaletteSize; ++i)
304 mxMonoPalettes->insert(
305 pColorPaletteHelper->getColorPalette(ChartColorPaletteType::Monochromatic, i));
306 mxMonoPalettes->Fill();
307 }
308
309 void ChartColorPalettePopup::GrabFocus()
310 {
311 if (mxMonoPalettes->IsNoSelection())
312 mxColorfulPalettes->GrabFocus();
313 else
314 mxMonoPalettes->GrabFocus();
315 }
316
317 IMPL_LINK_NOARG(ChartColorPalettePopup, SelectColorfulPaletteHdl, weld::IconView&, bool)
318 {
319 sal_uInt32 nIndex = SelectPaletteHdl(mxColorfulPalettes);
320 if (nIndex != static_cast<sal_uInt32>(-1))
321 {
322 mxControl->dispatchColorPaletteCommand(ChartColorPaletteType::Colorful, nIndex);
323 mxMonoPalettes->SetNoSelection();
324 mxControl->updateStatus();
325 }
326 mxControl->EndPopupMode();
327 return true;
328 }
329
330 IMPL_LINK_NOARG(ChartColorPalettePopup, SelectMonoPaletteHdl, weld::IconView&, bool)
331 {
332 sal_uInt32 nIndex = SelectPaletteHdl(mxMonoPalettes);
333 if (nIndex != static_cast<sal_uInt32>(-1))
334 {
335 mxControl->dispatchColorPaletteCommand(ChartColorPaletteType::Monochromatic, nIndex);
336 mxColorfulPalettes->SetNoSelection();
337 mxControl->updateStatus();
338 }
339 mxControl->EndPopupMode();
340 return true;
341 }
342
343 sal_uInt32
344 ChartColorPalettePopup::SelectPaletteHdl(const std::unique_ptr<ChartColorPalettes>& xPalettes)
345 {
346 const sal_uInt32 nItemId = xPalettes->GetSelectedItemId();
347 if (!nItemId)
348 return static_cast<sal_uInt32>(-1);
349
350 const sal_uInt32 nIndex = nItemId - 1;
351
352 if (const ChartColorPalette* pPalette = xPalettes->getPalette(nIndex))
353 {
354 mxControl->applyColorPalette(pPalette);
355 mbItemSelected = true;
356 return nIndex;
357 }
358 return static_cast<sal_uInt32>(-1);
359 }
360
361 IMPL_LINK_NOARG(ChartColorPalettePopup, ColorfulMouseMoveHdl, const MouseEvent&, bool)
362 {
363 MouseMoveHdl(mxColorfulPalettes, ChartColorPaletteType::Colorful);
364 return true;
365 }
366
367 IMPL_LINK_NOARG(ChartColorPalettePopup, MonoMouseMoveHdl, const MouseEvent&, bool)
368 {
369 MouseMoveHdl(mxMonoPalettes, ChartColorPaletteType::Monochromatic);
370 return true;
371 }
372
373 void ChartColorPalettePopup::MouseMoveHdl(const std::unique_ptr<ChartColorPalettes>& xPalettes,
374 const ChartColorPaletteType eHlItemType)
375 {
376 const sal_uInt16 nHlId = xPalettes->GetHighlightedItemId();
377 if (eHlItemType == meHighlightedItemType && nHlId == mnHighlightedItemId)
378 return;
379
380 if (nHlId > 0)
381 {
382 if (const ChartColorPalette* pPalette = xPalettes->getPalette(nHlId - 1))
383 {
384 mxControl->applyColorPalette(pPalette);
385 }
386 }
387 else
388 {
389 mxControl->restoreOriginalDiagram();
390 }
391
392 meHighlightedItemType = eHlItemType;
393 mnHighlightedItemId = nHlId;
394 }
395
396 } // end namespace chart::sidebar
397
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */