-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragger.cpp
executable file
·357 lines (333 loc) · 9.01 KB
/
dragger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2022-present Contributors to the colorpicker project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/colorpicker
#include "dragger.h"
#include "mac.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QPointer>
class DraggerPrivate : public QObject
{
Q_OBJECT
public:
DraggerPrivate();
void init();
void mapToGeometry();
QSize mapToSize() const;
QRect dragRect() const;
bool eventFilter(QObject* object, QEvent* event);
void activate();
void deactivate();
public:
class State
{
public:
QPoint position;
QRect rect;
QRect unitedRect;
bool dragging;
};
QPixmap paintCross();
QPixmap paintSweep();
QColor color;
QPoint position;
QSize baseSize;
QRect baseRect;
qreal scale;
State state;
QPointer<Dragger> widget;
};
DraggerPrivate::DraggerPrivate()
: color(Qt::black)
, baseSize(256, 256)
, baseRect(0, 0, 256, 256)
, scale(0.8)
{
}
void
DraggerPrivate::init()
{
widget->setAttribute(Qt::WA_TranslucentBackground);
widget->resize(mapToSize());
widget->installEventFilter(this);
mac::setTopLevel(widget->winId());
deactivate(); // need to be initialized
}
void
DraggerPrivate::mapToGeometry()
{
QScreen* screen;
if (state.dragging) {
screen = QGuiApplication::screenAt(state.position);
QRect clipGeometry = screen->geometry();
position.setX(qMax(clipGeometry.left(), qMin(position.x(), clipGeometry.right() - 1)));
position.setY(qMax(clipGeometry.top(), qMin(position.y(), clipGeometry.bottom() - 1)));
} else {
screen = QGuiApplication::screenAt(position);
}
QSize size = mapToSize();
int x = position.x() - size.width() / 2;
int y = position.y() - size.height() / 2;
int width;
int height;
QRect screenGeometry = screen->geometry();
// left
if (x < screenGeometry.left()) {
width = (x + size.width()) - screenGeometry.left();
x = screenGeometry.left() - 2;
}
// right
else if (x + size.width() > screenGeometry.right()) {
width = (size.width() - ((x + size.width()) - screenGeometry.right()));
x = (screenGeometry.right() + 1) - width;
}
else {
width = size.width();
}
// top
if (y < screenGeometry.top()) {
height = size.height() + y;
y = screenGeometry.top();
}
// bottom
else if (y + size.height() > screenGeometry.bottom()) {
height = size.height() - ((y + size.height()) - screenGeometry.bottom());
y = (screenGeometry.bottom() + 1) - height;
}
else {
height = size.height() ;
}
baseRect = QRect(x, y, width, height);
if (state.dragging) {
QRect rect = baseRect.united(state.rect);
state.unitedRect = rect;
widget->setGeometry(rect);
widget->setFixedSize(rect.size());
widget->QWidget::update();
} else {
widget->setGeometry(x, y, width, height);
widget->setFixedSize(width, height);
widget->QWidget::update(); // no repaint needed, single cross
}
}
QSize
DraggerPrivate::mapToSize() const
{
return baseSize;
}
QRect
DraggerPrivate::dragRect() const
{
if (state.dragging) {
return QRect(state.position, position).normalized();
} else {
return QRect();
}
}
QPixmap
DraggerPrivate::paintCross()
{
QScreen* screen = QGuiApplication::screenAt(position);
qreal dpr = screen->devicePixelRatio();
QPoint cursor = widget->mapFromGlobal(position);
// size
QSize size = mapToSize();
// pixmap
QPixmap pixmap = QPixmap(size * dpr);
pixmap.fill(Qt::transparent);
pixmap.setDevicePixelRatio(dpr);
// painter
QPainter p(&pixmap);
qreal diameter = std::min(size.width(), size.height()) * scale;
qreal radius = diameter/2.0;
QBrush brush = QBrush(Qt::white);
p.translate(cursor.x(), cursor.y());
// shadow
{
qreal length = qMax(radius * 0.1, 0.0);
qreal origin = length * 0.4;
p.setPen(QPen(Qt::black, 2));
p.translate(2, 2); // offset shadow
p.drawLine(origin, 0, length, 0);
p.drawLine(-length, 0, -origin, 0);
p.drawLine(0, length, 0, origin);
p.drawLine(0, -origin, 0, -length);
p.translate(-1, -1);
}
// cross
{
qreal length = qMax(radius * 0.1, 0.0);
qreal origin = length * 0.4;
p.setPen(QPen(Qt::white, 2));
p.drawLine(origin, 0, length, 0);
p.drawLine(-length, 0, -origin, 0);
p.drawLine(0, length, 0, origin);
p.drawLine(0, -origin, 0, -length);
}
p.end();
return pixmap;
}
QPixmap
DraggerPrivate::paintSweep()
{
QScreen* screen = QGuiApplication::screenAt(position);
qreal dpr = screen->devicePixelRatio();
// size
QSize size = state.unitedRect.size();
// pixmap
QPixmap pixmap = QPixmap(size * dpr);
pixmap.fill(Qt::transparent);
pixmap.setDevicePixelRatio(dpr);
// sweep
QPainter p(&pixmap);
{
QPoint from = widget->mapFromGlobal(state.position);
QPoint to = widget->mapFromGlobal(position);
// sweep
{
p.save();
QRect rectangle = QRect(from, to);
QColor shadow = Qt::black;
shadow.setAlpha(20);
p.fillRect(rectangle, shadow);
p.restore();
}
// cross
{
size = mapToSize();
QPoint cursor = widget->mapFromGlobal(position);
qreal diameter = std::min(size.width(), size.height()) * scale;
qreal radius = diameter/2.0;
p.save();
p.translate(cursor.x(), cursor.y());
// shadow
{
qreal length = qMax(radius * 0.1, 0.0);
qreal origin = length * 0.4;
p.setPen(QPen(Qt::black, 2));
p.translate(2, 2); // offset shadow
p.drawLine(origin, 0, length, 0);
p.drawLine(-length, 0, -origin, 0);
p.drawLine(0, length, 0, origin);
p.drawLine(0, -origin, 0, -length);
p.translate(-1, -1);
}
// cross
{
qreal length = qMax(radius * 0.1, 0.0);
qreal origin = length * 0.4;
p.setPen(QPen(Qt::white, 2));
p.drawLine(origin, 0, length, 0);
p.drawLine(-length, 0, -origin, 0);
p.drawLine(0, length, 0, origin);
p.drawLine(0, -origin, 0, -length);
}
p.restore();
}
}
p.end();
return pixmap;
}
bool
DraggerPrivate::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Show) {
mac::hideCursor();
}
if (event->type() == QEvent::Hide) {
mac::showCursor();
widget->closed();
}
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = (QKeyEvent*)event;
if (keyEvent->key() == Qt::Key_Escape) {
widget->releaseMouse();
widget->hide();
deactivate();
return true;
}
}
if (event->type() == QEvent::QEvent::MouseButtonPress) {
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if (mouseEvent->button() == Qt::LeftButton) {
widget->grabMouse(); // needed for mouse move events
activate();
}
if (mouseEvent->button() == Qt::RightButton) {
widget->releaseMouse();
widget->hide();
deactivate();
}
return true;
}
if (event->type() == QEvent::QEvent::MouseButtonRelease) {
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if (mouseEvent->button() == Qt::LeftButton) {
widget->releaseMouse();
widget->triggered();
deactivate();
}
return true;
}
return false;
}
void
DraggerPrivate::activate()
{
state = State {
position,
widget->geometry(),
QRect(),
true
};
mapToGeometry();
}
void
DraggerPrivate::deactivate()
{
state = State {
QPoint(),
QRect(),
QRect(),
false
};
mapToGeometry();
}
#include "dragger.moc"
Dragger::Dragger(QWidget* parent)
: QWidget(parent,
Qt::Dialog |
Qt::FramelessWindowHint |
Qt::NoDropShadowWindowHint)
, p(new DraggerPrivate())
{
p->widget = this;
p->init();
}
Dragger::~Dragger()
{
}
void
Dragger::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.fillRect(rect(), QColor(0, 0, 0, 1)); // needed for mouse cursor update
if (p->state.dragging) {
painter.drawPixmap(0, 0, p->paintSweep());
} else {
painter.drawPixmap(0, 0, p->paintCross());
}
painter.end();
}
QRect
Dragger::dragRect() const
{
return p->dragRect();
}
void
Dragger::update(const QPoint& position)
{
p->position = position;
p->mapToGeometry();
}