-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregiongrabber.cpp
More file actions
398 lines (341 loc) · 13.3 KB
/
Copy pathregiongrabber.cpp
File metadata and controls
398 lines (341 loc) · 13.3 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* From KSnapshot (KDE4). Straight ported to XCB by kernel1024.
*
* Copyright (C) 2007 Luca Gugelmann <lucag@student.ethz.ch>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "regiongrabber.h"
#include <QPainter>
#include <QMouseEvent>
#include <QApplication>
#include <QToolTip>
#include <QTimer>
#include "funcs.h"
#include "xcbtools.h"
RegionGrabber::RegionGrabber(QWidget *parent, const QRect &startSelection, bool includePointer)
: QWidget(parent, Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool ),
selection(startSelection),
TLHandle(0,0,handleSize,handleSize), TRHandle(0,0,handleSize,handleSize),
BLHandle(0,0,handleSize,handleSize), BRHandle(0,0,handleSize,handleSize),
LHandle(0,0,handleSize,handleSize), THandle(0,0,handleSize,handleSize),
RHandle(0,0,handleSize,handleSize), BHandle(0,0,handleSize,handleSize)
{
const int captureTimeout = 200;
handles << &TLHandle << &TRHandle << &BLHandle << &BRHandle
<< &LHandle << &THandle << &RHandle << &BHandle;
setMouseTracking(true);
QTimer::singleShot(captureTimeout, this, [this,includePointer](){
init(includePointer);
});
}
RegionGrabber::~RegionGrabber() = default;
void RegionGrabber::init(bool includePointer)
{
pixmap = ZXCBTools::getWindowPixmap(ZXCBTools::appRootWindow(), includePointer);
resize(pixmap.size());
move(0, 0);
setCursor(Qt::CrossCursor);
show();
grabMouse();
grabKeyboard();
}
static void drawRect(QPainter *painter, const QRect &r, const QColor &outline, const QColor &fill = QColor())
{
QRegion clip(r);
clip = clip.subtracted(r.adjusted(1, 1, -1, -1));
painter->save();
painter->setClipRegion(clip);
painter->setPen(Qt::NoPen);
painter->setBrush(outline);
painter->drawRect(r);
if (fill.isValid()) {
painter->setClipping(false);
painter->setBrush(fill);
painter->drawRect(r.adjusted(1, 1, -1, -1));
}
painter->restore();
}
void RegionGrabber::paintEvent(QPaintEvent* event)
{
const int handleAlpha = 160;
const int handleFillAlpha = 60;
Q_UNUSED(event);
if (grabbing) return; // grabWindow() should just get the background
QPainter painter(this);
QPalette pal(QToolTip::palette());
QFont font = QToolTip::font();
QColor handleColor = pal.color(QPalette::Active, QPalette::Highlight);
handleColor.setAlpha(handleAlpha);
QColor overlayColor(0, 0, 0, handleAlpha);
QColor textColor = pal.color(QPalette::Active, QPalette::Text);
QColor textBackgroundColor = pal.color(QPalette::Active, QPalette::Base);
painter.drawPixmap(0, 0, pixmap);
painter.setFont(font);
QRect r = selection;
if (!selection.isNull()) {
QRegion grey(rect());
grey = grey.subtracted(r);
painter.setClipRegion(grey);
painter.setPen(Qt::NoPen);
painter.setBrush(overlayColor);
painter.drawRect(rect());
painter.setClipRect(rect());
drawRect(&painter, r, handleColor);
}
if (showHelp) {
painter.setPen(textColor);
painter.setBrush(textBackgroundColor);
const QString helpText = tr("Select a region using the mouse. To take the snapshot, "
"press the Enter key or double click. Press Esc to quit.");
helpTextRect = painter.boundingRect(rect().adjusted(2, 2, -2, -2), Qt::TextWordWrap, helpText);
helpTextRect.adjust(-2, -2, 4, 2);
drawRect(&painter, helpTextRect, textColor, textBackgroundColor);
painter.drawText(helpTextRect.adjusted(3, 3, -3, -3 ), helpText);
}
if (selection.isNull()) return;
// The grabbed region is everything which is covered by the drawn
// rectangles (border included). This means that there is no 0px
// selection, since a 0px wide rectangle will always be drawn as a line.
const QString txt = QSL( "%1x%2" )
.arg(selection.width())
.arg(selection.height());
QRect textRect = painter.boundingRect(rect(), Qt::AlignLeft, txt);
QRect boundingRect = textRect.adjusted(-4, 0, 0, 0);
if ((textRect.width() < r.width() - 2*handleSize ) &&
(textRect.height() < r.height() - 2*handleSize) &&
(r.width() > 100 && r.height() > 100 )) { // center, unsuitable for small selections
boundingRect.moveCenter(r.center());
textRect.moveCenter(r.center());
} else if ((r.y() - 3 > textRect.height()) &&
(r.x() + textRect.width() < rect().right())) { // on top, left aligned
boundingRect.moveBottomLeft(QPoint(r.x(), r.y() - 3));
textRect.moveBottomLeft(QPoint(r.x() + 2, r.y() - 3));
} else if (r.x() - 3 > textRect.width()) { // left, top aligned
boundingRect.moveTopRight(QPoint(r.x() - 3, r.y()));
textRect.moveTopRight(QPoint( r.x() - 5, r.y()));
} else if (( r.bottom() + 3 + textRect.height() < rect().bottom()) &&
(r.right() > textRect.width())) { // at bottom, right aligned
boundingRect.moveTopRight(QPoint(r.right(), r.bottom() + 3));
textRect.moveTopRight(QPoint(r.right() - 2, r.bottom() + 3));
} else if (r.right() + textRect.width() + 3 < rect().width()) { // right, bottom aligned
boundingRect.moveBottomLeft(QPoint(r.right() + 3, r.bottom()));
textRect.moveBottomLeft(QPoint(r.right() + 5, r.bottom()));
}
// if the above didn't catch it, you are running on a very tiny screen...
drawRect(&painter, boundingRect, textColor, textBackgroundColor);
painter.drawText(textRect, txt);
if ((r.height() > handleSize*2 && r.width() > handleSize*2) || !mouseDown ) {
updateHandles();
painter.setPen(Qt::NoPen);
painter.setBrush(handleColor);
painter.setClipRegion(handleMask(StrokeMask));
painter.drawRect(rect());
handleColor.setAlpha(handleFillAlpha);
painter.setBrush(handleColor);
painter.setClipRegion(handleMask(FillMask));
painter.drawRect(rect());
}
}
void RegionGrabber::resizeEvent(QResizeEvent* event)
{
Q_UNUSED(event);
if (selection.isNull()) return;
QRect r = selection;
r.setTopLeft(limitPointToRect(r.topLeft(), rect()));
r.setBottomRight( limitPointToRect(r.bottomRight(), rect()));
if (r.width() <= 1 || r.height() <= 1) { //this just results in ugly drawing...
selection = QRect();
} else {
selection = normalizeSelection(r);
}
}
void RegionGrabber::mousePressEvent(QMouseEvent* event)
{
showHelp = !helpTextRect.contains(event->pos());
if (event->button() == Qt::LeftButton) {
mouseDown = true;
dragStartPoint = event->pos();
selectionBeforeDrag = selection;
if (!selection.contains(event->pos())) {
newSelection = true;
selection = QRect();
} else {
setCursor(Qt::ClosedHandCursor);
}
} else if (event->button() == Qt::RightButton) {
newSelection = false;
selection = QRect();
setCursor(Qt::CrossCursor);
}
update();
}
void RegionGrabber::mouseMoveEvent(QMouseEvent* event)
{
bool shouldShowHelp = !helpTextRect.contains(event->pos());
if (shouldShowHelp != showHelp) {
showHelp = shouldShowHelp;
update();
}
if (mouseDown)
{
if (newSelection)
{
QPoint p = event->pos();
QRect r = rect();
selection = normalizeSelection(QRect(dragStartPoint, limitPointToRect(p, r)));
} else if (mouseOverHandle == nullptr) { // moving the whole selection
QRect r = rect().normalized();
QRect s = selectionBeforeDrag.normalized();
QPoint p = s.topLeft() + event->pos() - dragStartPoint;
r.setBottomRight(r.bottomRight() - QPoint(s.width(), s.height()) + QPoint(1, 1));
if (!r.isNull() && r.isValid())
selection.moveTo(limitPointToRect(p, r));
} else { // dragging a handle
QRect r = selectionBeforeDrag;
QPoint offset = event->pos() - dragStartPoint;
if (mouseOverHandle == &TLHandle || mouseOverHandle == &THandle
|| mouseOverHandle == &TRHandle) { // dragging one of the top handles
r.setTop(r.top() + offset.y());
}
if (mouseOverHandle == &TLHandle || mouseOverHandle == &LHandle
|| mouseOverHandle == &BLHandle) { // dragging one of the left handles
r.setLeft(r.left() + offset.x());
}
if (mouseOverHandle == &BLHandle || mouseOverHandle == &BHandle
|| mouseOverHandle == &BRHandle) { // dragging one of the bottom handles
r.setBottom(r.bottom() + offset.y());
}
if (mouseOverHandle == &TRHandle || mouseOverHandle == &RHandle
|| mouseOverHandle == &BRHandle) { // dragging one of the right handles
r.setRight(r.right() + offset.x());
}
r.setTopLeft(limitPointToRect(r.topLeft(), rect()));
r.setBottomRight(limitPointToRect(r.bottomRight(), rect()));
selection = normalizeSelection(r);
}
update();
} else {
if (selection.isNull()) return;
bool found = false;
for (const auto &r : std::as_const(handles)) {
if (r->contains(event->pos())) {
mouseOverHandle = r;
found = true;
break;
}
}
if (!found) {
mouseOverHandle = nullptr;
if (selection.contains(event->pos())) {
setCursor(Qt::OpenHandCursor);
} else {
setCursor(Qt::CrossCursor);
}
} else {
if (mouseOverHandle == &TLHandle || mouseOverHandle == &BRHandle)
setCursor(Qt::SizeFDiagCursor);
if (mouseOverHandle == &TRHandle || mouseOverHandle == &BLHandle)
setCursor(Qt::SizeBDiagCursor);
if (mouseOverHandle == &LHandle || mouseOverHandle == &RHandle)
setCursor(Qt::SizeHorCursor);
if (mouseOverHandle == &THandle || mouseOverHandle == &BHandle)
setCursor(Qt::SizeVerCursor);
}
}
}
void RegionGrabber::mouseReleaseEvent(QMouseEvent* event)
{
mouseDown = false;
newSelection = false;
if (mouseOverHandle == nullptr && selection.contains( event->pos() ) )
setCursor(Qt::OpenHandCursor);
update();
}
void RegionGrabber::mouseDoubleClickEvent(QMouseEvent* event)
{
Q_UNUSED(event);
grabRect();
}
void RegionGrabber::keyPressEvent(QKeyEvent* event)
{
QRect r = selection;
if (event->key() == Qt::Key_Escape) {
Q_EMIT regionGrabbed(QPixmap(), r);
} else if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return ) {
grabRect();
} else {
event->ignore();
}
}
void RegionGrabber::grabRect()
{
QRect r = selection;
if (!r.isNull() && r.isValid()) {
grabbing = true;
Q_EMIT regionGrabbed(pixmap.copy(r), r);
}
}
void RegionGrabber::updateHandles()
{
QRect r = selection;
int s2 = handleSize / 2;
TLHandle.moveTopLeft( r.topLeft() );
TRHandle.moveTopRight( r.topRight() );
BLHandle.moveBottomLeft( r.bottomLeft() );
BRHandle.moveBottomRight( r.bottomRight() );
LHandle.moveTopLeft( QPoint( r.x(), r.y() + r.height() / 2 - s2) );
THandle.moveTopLeft( QPoint( r.x() + r.width() / 2 - s2, r.y() ) );
RHandle.moveTopRight( QPoint( r.right(), r.y() + r.height() / 2 - s2 ) );
BHandle.moveBottomLeft( QPoint( r.x() + r.width() / 2 - s2, r.bottom() ) );
}
QRegion RegionGrabber::handleMask(MaskType type) const
{
// note: not normalized QRects are bad here, since they will not be drawn
QRegion mask;
for (const auto &rect : std::as_const(handles)) {
if (type == StrokeMask) {
QRegion r(*rect );
mask += r.subtracted(rect->adjusted( 1, 1, -1, -1 ));
} else {
mask += QRegion(rect->adjusted( 1, 1, -1, -1 ));
}
}
return mask;
}
QPoint RegionGrabber::limitPointToRect(const QPoint &p, const QRect &r) const
{
QPoint q;
q.setX(p.x() < r.x() ? r.x() : p.x() < r.right() ? p.x() : r.right());
q.setY(p.y() < r.y() ? r.y() : p.y() < r.bottom() ? p.y() : r.bottom());
return q;
}
QRect RegionGrabber::normalizeSelection(const QRect &s) const
{
QRect r = s;
if (r.width() <= 0) {
int l = r.left();
int w = r.width();
r.setLeft(l + w - 1);
r.setRight(l);
}
if (r.height() <= 0) {
int t = r.top();
int h = r.height();
r.setTop(t + h - 1);
r.setBottom(t);
}
return r;
}