forked from jlam55555/veikk-linux-driver-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qscreenmapscene.cpp
56 lines (48 loc) · 1.81 KB
/
qscreenmapscene.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
#include "qscreenmapscene.h"
#include <QGraphicsPixmapItem>
#include <QTimer>
#include <QScreen>
#include <QGraphicsSceneMouseEvent>
QScreenMapScene::QScreenMapScene(QScreen *screen)
: QGraphicsScene{}, screen{screen} {
QTimer *timer;
setSceneRect(screen->geometry());
// initialize screen pixmap
pixmapItem = new QGraphicsPixmapItem{QPixmap{0, 0}};
addItem(pixmapItem);
// update screen every 2000ms
updateScreenMapPreview();
timer = new QTimer{this};
connect(timer, &QTimer::timeout,
this, &QScreenMapScene::updateScreenMapPreview);
timer->start(2000);
// set up selection rectangle
isSelecting = false;
selectionRect = addRect(screen->geometry());
selectionRect->setBrush(QBrush{QColor{255, 0, 0, 50}});
}
void QScreenMapScene::updateScreenMapPreview() {
QPixmap pixmap = screen->grabWindow(0).scaled(sceneRect().size().toSize(),
Qt::KeepAspectRatio,
Qt::FastTransformation);
pixmapItem->setPixmap(pixmap);
}
void QScreenMapScene::updateScreenMapRect(QRect newScreenMap) {
selectionRect->setRect(newScreenMap);
}
void QScreenMapScene::mousePressEvent(QGraphicsSceneMouseEvent *evt) {
selectionStart = evt->scenePos();
selectionRect->setRect(QRectF{selectionStart, selectionStart});
isSelecting = true;
}
void QScreenMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent *evt) {
if(!isSelecting)
return;
selectionRect->setRect(QRectF{evt->scenePos(), selectionStart}
.normalized());
}
void QScreenMapScene::mouseReleaseEvent(__attribute__((unused))
QGraphicsSceneMouseEvent *evt) {
isSelecting = false;
emit updateScreenMapForm(selectionRect->rect().toRect());
}