Krita Source Code Documentation
Loading...
Searching...
No Matches
KisQmlPopupWidgetManager.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Wolthera van Hovell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
8#include <QApplication>
9
10#include <optional>
11
14
15 qreal x;
16 qreal y;
17
18 qreal margins = 0;
19 std::optional<qreal> topMargin = std::nullopt;
20 std::optional<qreal> bottomMargin = std::nullopt;
21 std::optional<qreal> leftMargin = std::nullopt;
22 std::optional<qreal> rightMargin = std::nullopt;
23
24 QQuickItem *itemParent;
25};
26
28 : QObject(parent)
29 , d(new Private)
30{
31 d->popup = new KisQQuickPopupWidget();
32
33 connect(this, SIGNAL(marginsChanged()), this, SLOT(updateMargins()));
34 connect(this, SIGNAL(topMarginChanged()), this, SLOT(updateMargins()));
35 connect(this, SIGNAL(leftMarginChanged()), this, SLOT(updateMargins()));
36 connect(this, SIGNAL(rightMarginChanged()), this, SLOT(updateMargins()));
37 connect(this, SIGNAL(bottomMarginChanged()), this, SLOT(updateMargins()));
38 connect(d->popup, SIGNAL(signalRootObjectReady()), this, SIGNAL(rootControlChanged()));
39}
40
42{
43 d->popup->deleteLater();
44}
45
47{
48 return d->x;
49}
50
52{
53 if (qFuzzyCompare(d->x, value)) return;
54 d->x = value;
55 Q_EMIT xChanged();
56}
57
59{
60 return d->y;
61}
62
64{
65 if (qFuzzyCompare(d->y, value)) return;
66 d->y = value;
67 Q_EMIT yChanged();
68}
69
71{
72 return d->itemParent;
73}
74
76{
77 if(d->itemParent == item) return;
78 d->itemParent = item;
79 Q_EMIT itemParentChanged();
80}
81
83{
84 return d->popup->rootObject();
85}
86
88{
89 return d->margins;
90}
91
93{
94 if (qFuzzyCompare(d->margins, value)) return;
95 d->margins = value;
96 Q_EMIT marginsChanged();
97}
98
100{
101 return (d->topMargin)? d->topMargin.value(): d->margins;
102}
103
105{
106 return (d->bottomMargin)? d->bottomMargin.value(): d->margins;
107}
108
110{
111 return (d->leftMargin)? d->leftMargin.value(): d->margins;
112}
113
115{
116 return (d->rightMargin)? d->rightMargin.value(): d->margins;
117}
118
120{
121 if (d->topMargin) {
122 if (qFuzzyCompare(d->topMargin.value(), value)) return;
123 }
124 d->topMargin = std::make_optional(value);
125 Q_EMIT topMarginChanged();
126}
127
129{
130 if (d->rightMargin) {
131 if (qFuzzyCompare(d->rightMargin.value(), value)) return;
132 }
133 d->rightMargin = std::make_optional(value);
134 Q_EMIT rightMarginChanged();
135}
136
138{
139 if (d->leftMargin) {
140 if (qFuzzyCompare(d->leftMargin.value(), value)) return;
141 }
142 d->leftMargin = std::make_optional(value);
143 Q_EMIT leftMarginChanged();
144}
145
147{
148 if (d->bottomMargin) {
149 if (qFuzzyCompare(d->bottomMargin.value(), value)) return;
150 }
151 d->bottomMargin = std::make_optional(value);
152 Q_EMIT bottomMarginChanged();
153}
154
156{
157 d->topMargin = std::nullopt;
158}
159
161{
162 d->leftMargin = std::nullopt;
163}
164
166{
167 d->rightMargin = std::nullopt;
168}
169
171{
172 d->bottomMargin = std::nullopt;
173}
174
176{
177 return d->popup->isVisible();
178}
179
181{
182 if (!d->itemParent) {
183 qWarning() << "Parent not set! PopupWidget requires parent to be explicitely set!";
184 return;
185 }
186 d->popup->raise();
187 d->popup->show();
188 d->popup->move(d->itemParent->mapToGlobal(QPointF(d->x, d->y)).toPoint());
189 // The following is probably causing issues with keyboard focus :(
190 d->popup->activateWindow();
191 Q_EMIT visibleChanged();
192}
193
195{
196 d->popup->hide();
197 Q_EMIT visibleChanged();
198}
199
201{
202 d->popup->releaseKeyboard();
203}
204
206{
207 QMargins margins;
208 margins.setTop(qRound(topMargin()));
209 margins.setLeft(qRound(leftMargin()));
210 margins.setRight(qRound(rightMargin()));
211 margins.setBottom(qRound(bottomMargin()));
212 d->popup->setLayoutMargins(margins);
213}
float value(const T *src, size_t ch)
The KisQQuickPopupWidget class.
KisQmlPopupWidgetManager(QObject *parent=nullptr)
void setTopMargin(const qreal value)
void setBottomMargin(const qreal value)
QScopedPointer< Private > d
void setLeftMargin(const qreal value)
void setItemParent(QQuickItem *item)
void setRightMargin(const qreal value)
void setMargins(const qreal value)
static bool qFuzzyCompare(half p1, half p2)