Debian Codegraph @

plugins/dockers/widegamutcolorselector/WGSelectorPopup.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2020 Mathias Wein <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6 #include "WGSelectorPopup.h"
7
8 #include <QApplication>
9 #include <QBoxLayout>
10 #include <QCursor>
11 #include <QScreen>
12 #include <QPainter>
13 #include <QScreen>
14
15 #include <kis_global.h>
16 #include <KisVisualColorSelector.h>
17 #include <WGSelectorWidgetBase.h>
18 #include <WGShadeSelector.h>
19
20 WGSelectorPopup::WGSelectorPopup(QWidget *parent)
21 : QWidget(parent, Qt::Popup | Qt::FramelessWindowHint), m_hideTimer(new QTimer(this))
22 {
23 setAttribute(Qt::WA_TranslucentBackground);
24 QBoxLayout *lo = new QBoxLayout(QBoxLayout::LeftToRight, this);
25 lo->setObjectName("WGSelectorPopupLayout");
26 lo->setSizeConstraint(QLayout::SetFixedSize);
27 lo->setContentsMargins(m_margin, m_margin, m_margin, m_margin);
28 m_hideTimer->setSingleShot(true);
29 m_hideTimer->setInterval(50);
30 connect(m_hideTimer, SIGNAL(timeout()), this, SLOT(hide()));
31
32 }
33
34 void WGSelectorPopup::setSelectorWidget(KisVisualColorSelector *selector)
35 {
36 replaceCentranWidget(selector);
37 connect(selector, SIGNAL(sigInteraction(bool)), SLOT(slotInteraction(bool)));
38 m_selectorWidget = 0;
39 }
40
41 void WGSelectorPopup::setSelectorWidget(WGSelectorWidgetBase *selector)
42 {
43 replaceCentranWidget(selector);
44 connect(selector, SIGNAL(sigColorInteraction(bool)), SLOT(slotInteraction(bool)));
45 m_selectorWidget = selector;
46 }
47
48 WGSelectorWidgetBase *WGSelectorPopup::selectorWidget() const
49 {
50 return m_selectorWidget;
51 }
52
53 void WGSelectorPopup::slotShowPopup()
54 {
55 QPoint cursorPos = QCursor::pos();
56 const QRect availRect = this->screen()->availableGeometry();
57
58 QRect popupRect(geometry());
59 QPoint offset = m_selectorWidget ? m_selectorWidget->pos() + m_selectorWidget->popupOffset()
60 : popupRect.center() - popupRect.topLeft();
61
62 popupRect.moveTopLeft(cursorPos - offset);
63 popupRect = kisEnsureInRect(popupRect, availRect);
64
65
66 move(popupRect.topLeft());
67
68 show();
69 //m_colorPreviewPopup->show();
70 }
71
72 void WGSelectorPopup::paintEvent(QPaintEvent *event)
73 {
74 Q_UNUSED(event);
75 QPainter painter(this);
76 painter.setRenderHint(QPainter::Antialiasing);
77 painter.setPen(Qt::NoPen);
78 painter.setBrush(palette().window());
79 painter.drawRoundedRect(QRect(0, 0, width(), height()), m_margin, m_margin);
80 }
81
82 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
83 void WGSelectorPopup::enterEvent(QEvent *event)
84 #else
85 void WGSelectorPopup::enterEvent(QEnterEvent *event)
86 #endif
87 {
88 Q_UNUSED(event);
89 if (m_hideTimer->isActive()) {
90 m_hideTimer->stop();
91 }
92 }
93
94 void WGSelectorPopup::leaveEvent(QEvent *event)
95 {
96 Q_UNUSED(event);
97
98 if (!m_isInteracting) {
99 m_hideTimer->start();
100 }
101 }
102
103 void WGSelectorPopup::keyPressEvent(QKeyEvent *event)
104 {
105 Q_UNUSED(event);
106 hide();
107 }
108
109 void WGSelectorPopup::hideEvent(QHideEvent *event)
110 {
111 QWidget::hideEvent(event);
112 Q_EMIT sigPopupClosed(this);
113 }
114
115 void WGSelectorPopup::slotInteraction(bool active)
116 {
117 m_isInteracting = active;
118 if (!active && !underMouse()) {
119 hide();
120 }
121 }
122
123 void WGSelectorPopup::replaceCentranWidget(QWidget *widget)
124 {
125 widget->setParent(this);
126 while (QLayoutItem *child = layout()->takeAt(0)) {
127 delete child->widget();
128 delete child;
129 }
130 layout()->addWidget(widget);
131 widget->show();
132 layout()->update();
133 adjustSize();
134 }
135
136