-
Notifications
You must be signed in to change notification settings - Fork 3
/
OverlappedWidget.cpp
116 lines (95 loc) · 2.3 KB
/
OverlappedWidget.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
#include "OverlappedWidget.h"
#include <QPainter>
OverlappedWidget::OverlappedWidget(QWidget *parent)
: QDialog(parent)
{
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
m_dragging = false;
m_action = None;
}
OverlappedWidget::~OverlappedWidget()
{
}
void OverlappedWidget::setAction(Action action)
{
m_action = action;
}
void OverlappedWidget::setScreenImage(const QImage& image)
{
m_screenImage = image;
if (m_screenImage.format() != QImage::Format_RGB888)
m_screenImage = m_screenImage.convertToFormat(QImage::Format_RGB888);
m_darkMaskImage = m_screenImage;
uchar* bits = m_darkMaskImage.bits();
for (int i = 0; i < m_darkMaskImage.byteCount(); i++)
bits[i] /= 2;
}
QRect OverlappedWidget::selectedRegion()
{
return m_selectedRegion;
}
QImage OverlappedWidget::regionImage(const QRect& region)
{
return m_screenImage.copy(region);
}
void OverlappedWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawImage(rect(), m_darkMaskImage);
if (m_dragging)
{
QRect clipRect = QRect(m_pressedPos, m_draggingPos).normalized();
QImage clipImage = m_screenImage.copy(clipRect);
painter.drawImage(clipRect, clipImage);
static QPen borderPen(QBrush(QColor(Qt::green)), 1);
painter.setPen(borderPen);
painter.drawRect(clipRect);
}
QDialog::paintEvent(event);
}
void OverlappedWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_pressedPos = event->pos();
m_draggingPos = m_pressedPos;
m_dragging = true;
}
else if (event->button() == Qt::RightButton)
{
hide();
m_dragging = false;
}
QDialog::mousePressEvent(event);
}
void OverlappedWidget::mouseMoveEvent(QMouseEvent *event)
{
if (m_dragging)
{
m_draggingPos = event->pos();
repaint();
}
QDialog::mouseMoveEvent(event);
}
void OverlappedWidget::mouseReleaseEvent(QMouseEvent *event)
{
hide();
m_dragging = false;
if (m_draggingPos != m_pressedPos)
{
m_selectedRegion = QRect(m_pressedPos, m_draggingPos).normalized();
emit regionSelected(m_action, m_selectedRegion);
}
QDialog::mouseReleaseEvent(event);
}
void OverlappedWidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
{
hide();
m_dragging = false;
}
QDialog::keyPressEvent(event);
}