Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_color_selector_base.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev at xibo dot at>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QMouseEvent>
10#include <QApplication>
11#include <QScreen>
12#include <QScreen>
13#include <QTimer>
14#include <QCursor>
15#include <QPainter>
16#include <QMimeData>
17
18#include <kconfig.h>
19#include <kconfiggroup.h>
20#include <ksharedconfig.h>
21
22#include "KoColorSpace.h"
24
25#include "kis_canvas2.h"
27#include "kis_node.h"
28#include "KisViewManager.h"
29#include <KisView.h>
30#include "kis_image.h"
31#include "kis_global.h"
33
35
36// for HAVE_WAYLAND
37#include <KoConfig.h>
38
39#if defined HAVE_WAYLAND && QT_VERSION >= QT_VERSION_CHECK(6, 11, 0)
40#define HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
41#endif
42
43#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
44#include <QtWaylandClient/private/qwaylandwindow_p.h>
45#endif
46
47
48namespace {
49 QWidget* findNearestDockerOrNativeParent(QWidget *w) {
50 if (!w || w->inherits("QDockWidget") || w->windowHandle()) {
51 return w;
52 }
53
54 return findNearestDockerOrNativeParent(w->parentWidget());
55 }
56}
57
58class KisColorPreviewPopup : public QWidget {
59public:
61 : QWidget(parent), m_parent(parent)
62 {
63 setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::NoDropShadowWindowHint);
64 setAttribute(Qt::WA_TranslucentBackground);
65 setQColor(QColor(0,0,0));
66 m_baseColor = QColor(0,0,0,0);
67 m_previousColor = QColor(0,0,0,0);
68 m_lastUsedColor = QColor(0,0,0,0);
69 }
70
72 {
74 show();
75 }
76
78 {
79 resize(100, 150);
80
81 // make sure that all the platform-specific structures for the window
82 // are created (i.e. QWaylandWindow) before trying to request them
83 // via `windowHandle()->handle()`
84 create();
85
86#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
87 if (auto waylandWindow = dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(windowHandle()->handle())) {
88 QWidget *referenceWidget = findNearestDockerOrNativeParent(parentWidget());
89 if (!referenceWidget) {
90 referenceWidget = parentWidget();
91 }
92
93 const QPoint parentPos = parentWidget()->window()->mapFromGlobal(referenceWidget->mapToGlobal(QPoint()));
94 const QRect gravityRect(QRect(parentPos, QSize(referenceWidget->width(), 10)));
95
96 waylandWindow->setParentControlGeometry(gravityRect);
97
98 // well, it is technically not a "submenu", but this is the way
99 // to make qt position it "around" the docker widget
100 waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::SubMenu);
101 } else
102#endif /* HAVE_QT_PRIVATE_TOOLTIP_POSITION_API */
103 {
104 QPoint parentPos = m_parent->mapToGlobal(QPoint(0,0));
105 const QRect availRect = this->screen()->availableGeometry();
106 QPoint targetPos;
107 if ( parentPos.x() - 100 > availRect.x() ) {
108 targetPos = QPoint(parentPos.x() - 100, parentPos.y());
109 } else if ( parentPos.x() + m_parent->width() + 100 < availRect.right()) {
110 targetPos = m_parent->mapToGlobal(QPoint(m_parent->width(), 0));
111 } else if ( parentPos.y() - 100 > availRect.y() ) {
112 targetPos = QPoint(parentPos.x(), parentPos.y() - 100);
113 } else {
114 targetPos = QPoint(parentPos.x(), parentPos.y() + m_parent->height());
115 }
116 move(targetPos.x(), targetPos.y());
117 }
118 }
119
120 void initializeColors(const QColor& color, const QColor& lastUsedColor) {
121 m_color = color;
122 m_baseColor = color;
123 m_previousColor = color;
124 m_lastUsedColor = lastUsedColor;
125 update();
126 }
127
128 void setQColor(const QColor& color)
129 {
130 m_color = color;
131 update();
132 }
133
138
139 void setBaseColor(const QColor& color)
140 {
141 m_baseColor = color;
142 update();
143 }
144
145 void setLastUsedColor(const QColor& color)
146 {
147 m_lastUsedColor = color;
148 update();
149 }
150
151protected:
152 void paintEvent(QPaintEvent *e) override {
153 Q_UNUSED(e);
154 QPainter p(this);
155 p.fillRect(0, 0, width(), width(), m_color);
156 p.fillRect(50, width(), width(), height(), m_previousColor);
157 p.fillRect(0, width(), 50, height(), m_lastUsedColor);
158 }
159
160#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
161 void enterEvent(QEvent *e) override
162#else
163 void enterEvent(QEnterEvent *e) override
164#endif
165 {
167 QWidget::enterEvent(e);
168 }
169
170 void leaveEvent(QEvent *e) override {
172 QWidget::leaveEvent(e);
173 }
174
175private:
177 QColor m_color;
181};
182
184 QWidget(parent),
185 m_canvas(0),
186 m_popup(0),
187 m_parent(0),
188 m_colorUpdateAllowed(true),
189 m_colorUpdateSelf(false),
190 m_hideTimer(new QTimer(this)),
191 m_popupOnMouseOver(false),
192 m_popupOnMouseClick(true),
193 m_colorSpace(0),
194 m_isPopup(false),
195 m_hideOnMouseClick(false),
196 m_colorPreviewPopup(new KisColorPreviewPopup(this))
197{
198 m_hideTimer->setInterval(200);
199 m_hideTimer->setSingleShot(true);
200 connect(m_hideTimer, SIGNAL(timeout()), this, SLOT(tryHideMyself()));
201
202 using namespace std::placeholders; // For _1 placeholder
203 auto function = std::bind(&KisColorSelectorBase::slotUpdateColorAndPreview, this, _1);
204 m_updateColorCompressor.reset(new ColorCompressorType(25 /* ms */, function));
205
206 // Color selectors don't have context menus. Setting this prevents any
207 // long-presses from delaying inputs, see KisLongPressEventFilter.cpp.
208 setContextMenuPolicy(Qt::PreventContextMenu);
209}
210
216
217void KisColorSelectorBase::setPopupBehaviour(bool onMouseOver, bool onMouseClick)
218{
219 m_popupOnMouseClick = onMouseClick;
220 m_popupOnMouseOver = onMouseOver;
221 if(onMouseClick) {
222 m_popupOnMouseOver = false;
223 }
224
226 setMouseTracking(true);
227 }
228}
229
234
236{
237 if (m_canvas) {
238 m_canvas->disconnectCanvasObserver(this);
239 }
240 m_canvas = canvas;
241 if (m_canvas) {
242 connect(m_canvas->resourceManager(), SIGNAL(canvasResourceChanged(int,QVariant)),
243 SLOT(canvasResourceChanged(int,QVariant)), Qt::UniqueConnection);
244
245 connect(m_canvas->displayColorConverter(), SIGNAL(displayConfigurationChanged()),
246 SLOT(reset()), Qt::UniqueConnection);
247
248 connect(canvas->imageView()->resourceProvider(), SIGNAL(sigFGColorUsed(KoColor)),
249 this, SLOT(updateLastUsedColorPreview(KoColor)), Qt::UniqueConnection);
250
251 if (m_canvas->viewManager() && m_canvas->viewManager()->canvasResourceProvider()) {
252 const KoColor currentColor = Acs::currentColor(m_canvas->viewManager()->canvasResourceProvider(), Acs::Foreground);
253 setColor(currentColor);
254
255 auto colorHistory = m_canvas->viewManager()->canvasResourceProvider()->colorHistoryColors();
256 const KoColor lastUsedColor = !colorHistory.isEmpty() ? colorHistory.first() : currentColor;
257
258 const QColor currentQColor = converter()->toQColor(currentColor);
259 const QColor lastUsedQColor = converter()->toQColor(lastUsedColor);
260
261 m_colorPreviewPopup->initializeColors(currentQColor, lastUsedQColor);
262 }
263 }
264 if (m_popup) {
265 m_popup->setCanvas(canvas);
266 }
267
268 reset();
269}
270
272{
273 if (m_popup) {
275 }
276 m_canvas = 0;
277}
278
279
280
282{
283 event->accept();
284
286 event->button() == Qt::MiddleButton) {
287
289
290 m_colorUpdateSelf=false;
291 showPopup(event->globalPos());
292
293 } else if (m_isPopup && event->button() == Qt::MiddleButton) {
295 m_colorPreviewPopup->hide();
296 }
297 hide();
298 } else {
301 event->ignore();
302 }
303}
304
306
307 Q_UNUSED(e);
308
309 if (e->button() == Qt::MiddleButton) {
310 e->accept();
311 } else if (m_isPopup &&
313 !m_hideTimer->isActive()) {
315 m_colorPreviewPopup->hide();
316 }
317 hide();
318 }
319
320 m_colorUpdateSelf = false;
321}
322#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
324#else
326#endif
327{
328
329 if (m_hideTimer->isActive()) {
330 m_hideTimer->stop();
331 }
332
333 // do not show the popup when boxed in
334 // the configuration dialog (m_canvas == 0)
335
336 if (m_canvas &&
337 !m_isPopup && m_popupOnMouseOver &&
338 (!m_popup || m_popup->isHidden())) {
339
340 lazyCreatePopup();
341
342 const QPoint preferredCenter = mapToGlobal(rect().center());
343 showPopup(preferredCenter);
344 }
345
346#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
347 QWidget::enterEvent(e);
348#else
349 QEnterEvent *enterEvent = dynamic_cast<QEnterEvent*>(e);
350 QWidget::enterEvent(enterEvent);
351#endif
352
353}
354
356{
357 m_hideTimer->start();
358 QWidget::leaveEvent(e);
359 m_colorUpdateSelf = false;
360}
361
363{
365}
366
368{
369 if(e->mimeData()->hasColor())
370 e->acceptProposedAction();
371 if(e->mimeData()->hasText() && QColor(e->mimeData()->text()).isValid())
372 e->acceptProposedAction();
373}
374
376{
377 QColor color;
378 if(e->mimeData()->hasColor()) {
379 color = qvariant_cast<QColor>(e->mimeData()->colorData());
380 }
381 else if(e->mimeData()->hasText()) {
382#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
383 color.setNamedColor(e->mimeData()->text());
384#else
385 color.fromString(e->mimeData()->text());
386#endif
387 if(!color.isValid())
388 return;
389 }
390
391 KoColor kocolor(color , KoColorSpaceRegistry::instance()->rgb8());
392 updateColor(kocolor, Acs::Foreground, true);
393}
394
395void KisColorSelectorBase::updateColor(const KoColor &color, Acs::ColorRole role, bool needsExplicitColorReset)
396{
397 commitColor(color, role);
398
399 if (needsExplicitColorReset) {
400 setColor(color);
401 }
402}
403
405{
406 m_updateColorCompressor->start(qMakePair(color, role));
407}
408
409void KisColorSelectorBase::slotUpdateColorAndPreview(QPair<KoColor, Acs::ColorRole> color)
410{
411 updateColorPreview(color.first);
412 updateColor(color.first, color.second, false);
413}
414
416{
417 Q_UNUSED(color);
418}
419
421{
422 if (!m_popup) {
424 Q_ASSERT(m_popup);
425 m_popup->setParent(this);
426
427 // Setting Qt::BypassWindowManagerHint will prevent
428 // the WM from showing another taskbar entry,
429 // but will require that we handle window activation manually
430 m_popup->setWindowFlags(Qt::FramelessWindowHint |
431 Qt::Popup |
432 Qt::NoDropShadowWindowHint |
433 Qt::BypassWindowManagerHint);
434 m_popup->m_parent = this;
435 m_popup->m_isPopup = true;
436 }
439}
440
441void KisColorSelectorBase::showPopup(std::optional<QPoint> preferredCenter)
442{
443 // This slot may be called by some action,
444 // so we need to be able to handle it
446
447 const QPoint preferredGlobalCenterPos = preferredCenter ? *preferredCenter : QCursor::pos();
448
449 m_popup->create();
450
451 const QPoint preferredGlobalTopLeft =
452 QPoint(preferredGlobalCenterPos.x() - m_popup->width() / 2, preferredGlobalCenterPos.y() - m_popup->height() / 2);
453
454#ifdef HAVE_WAYLAND
455 if (qApp->platformName() == "wayland") {
456 m_popup->move(preferredGlobalTopLeft);
457 } else
458#endif
459 {
460 const QRect availRect = this->screen()->availableGeometry();
461 QRect rc(preferredGlobalTopLeft, m_popup->size());
462 rc = kisEnsureInRect(rc, availRect);
463 m_popup->move(rc.topLeft());
464 }
465
467 m_colorPreviewPopup->hide();
468 }
469
470 m_popup->show();
472}
473
478
480{
481 m_colorPreviewPopup->hide();
482
483 if (m_isPopup) {
484 hide();
485 }
486}
487
489{
490 if (!m_canvas)
491 return;
492
494
495 if (role == Acs::Foreground)
496 m_canvas->resourceManager()->setForegroundColor(color);
497 else
498 m_canvas->resourceManager()->setBackgroundColor(color);
499
501}
502
509
511{
512 m_colorPreviewPopup->setQColor(converter()->toQColor(color));
513}
514
515void KisColorSelectorBase::canvasResourceChanged(int key, const QVariant &v)
516{
518 KoColor realColor(v.value<KoColor>());
519 updateColorPreview(realColor);
521 setColor(realColor);
522 }
523 }
524}
525
530
532{
533 if(m_popup) {
535 }
536
537 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
538
539
540 int zoomSelectorOptions = (int) cfg.readEntry("zoomSelectorOptions", 0) ;
541 if (zoomSelectorOptions == 0) {
542 setPopupBehaviour(false, true); // middle mouse button click will open zoom selector
543 } else if (zoomSelectorOptions == 1) {
544 setPopupBehaviour(true, false); // move over will open the zoom selector
545 }
546 else
547 {
548 setPopupBehaviour(false, false); // do not show zoom selector
549 }
550
551 if(m_isPopup) {
552 m_hideOnMouseClick = cfg.readEntry("hidePopupOnClickCheck", false);
553 const int zoomSize = cfg.readEntry("zoomSize", 280);
554 resize(zoomSize, zoomSize);
555 }
556
557 reset();
558}
559
561{
562 update();
563}
564
569
574
579
586
588{
589 event->accept();
590}
591
593{
594 // hide the popup when another window becomes active, e.g. due to alt+tab
595 if(m_isPopup && event->type() == QEvent::ActivationChange && !isActiveWindow()) {
597 }
598
599 QWidget::changeEvent(event);
600}
601
602void KisColorSelectorBase::showEvent(QShowEvent *event)
603{
604 QWidget::showEvent(event);
605
606 // manual activation required due to Qt::BypassWindowManagerHint
607 if(m_isPopup) {
608 activateWindow();
609 }
610}
const Params2D p
qreal v
QPointer< KisView > imageView() const
void paintEvent(QPaintEvent *e) override
KisColorPreviewPopup(KisColorSelectorBase *parent)
void initializeColors(const QColor &color, const QColor &lastUsedColor)
void setLastUsedColor(const QColor &color)
void leaveEvent(QEvent *e) override
void enterEvent(QEnterEvent *e) override
KisColorSelectorBase * m_parent
void setQColor(const QColor &color)
void setBaseColor(const QColor &color)
Base class for all color selectors, that should support color management and zooming.
KisColorSelectorBase * m_popup
void showPopup(std::optional< QPoint > preferredCenter=std::nullopt)
void requestUpdateColorAndPreview(const KoColor &color, Acs::ColorRole role)
const KoColorSpace * m_colorSpace
void dropEvent(QDropEvent *) override
void updateColorPreview(const KoColor &color)
virtual void canvasResourceChanged(int key, const QVariant &v)
if you overwrite this, keep in mind, that you should set the color only, if m_colorUpdateAllowed is t...
void dragEnterEvent(QDragEnterEvent *) override
void updateLastUsedColorPreview(const KoColor &color)
void slotUpdateColorAndPreview(QPair< KoColor, Acs::ColorRole > color)
void changeEvent(QEvent *event) override
virtual void setCanvas(KisCanvas2 *canvas)
KisDisplayColorConverter * converter() const
const KoColorSpace * colorSpace() const
virtual void setColor(const KoColor &color)
void mouseReleaseEvent(QMouseEvent *) override
KisColorSelectorBase(QWidget *parent=0)
void mousePressEvent(QMouseEvent *) override
void enterEvent(QEnterEvent *e) override
KisColorPreviewPopup * m_colorPreviewPopup
void showEvent(QShowEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void commitColor(const KoColor &koColor, Acs::ColorRole role)
QScopedPointer< ColorCompressorType > m_updateColorCompressor
QPointer< KisCanvas2 > m_canvas
KisSignalCompressorWithParam< QPair< KoColor, Acs::ColorRole > > ColorCompressorType
void updateBaseColorPreview(const KoColor &color)
void setPopupBehaviour(bool onMouseOver, bool onMouseClick)
void setColorSpace(const KoColorSpace *colorSpace)
void leaveEvent(QEvent *e) override
virtual KisColorSelectorBase * createPopup() const =0
void updateColor(const KoColor &color, Acs::ColorRole role, bool needsExplicitColorReset)
void keyPressEvent(QKeyEvent *) override
const KoColorSpace * paintingColorSpace() const
static KisDisplayColorConverter * dumbConverterInstance()
QColor toQColor(const KoColor &c, bool proofToPaintColors=false) const
QRect kisEnsureInRect(QRect rc, const QRect &bounds)
Definition kis_global.h:291
KoColor currentColor(ResourceProvider *provider, ColorRole role)
@ Foreground
@ BackgroundColor
The active background color selected for this canvas.
@ ForegroundColor
The active foreground color selected for this canvas.
static KoColorSpaceRegistry * instance()