-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapewidget.cpp
More file actions
72 lines (60 loc) · 2.34 KB
/
Copy pathshapewidget.cpp
File metadata and controls
72 lines (60 loc) · 2.34 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
#include "shapewidget.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPixmap>
#include <QWindow>
ShapeWidget::ShapeWidget(QWidget *parent)
: QWidget(parent)
{
setMouseTracking(true); // Track mouse movements
setAttribute(Qt::WA_TranslucentBackground); // Enable transparent background
setAttribute(Qt::WA_NoSystemBackground, false); // Disable system background
setWindowFlags(Qt::FramelessWindowHint); // Remove window frame
// Capture the desktop's screenshot
//QScreen *screen = QGuiApplication::primaryScreen();
// Get the screen where the mouse is located
QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
QPixmap desktopScreenshot = screen->grabWindow(0);
// Set the desktop screenshot as the background
setAutoFillBackground(true);
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(desktopScreenshot));
setPalette(palette);
showFullScreen();
//setWindowOpacity(0.1);
}
void ShapeWidget::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
// Start drawing a new rectangle when the left mouse button is pressed
currentRect = QRectF(event->pos(), QSize(0, 0));
update(); // Trigger the paintEvent
}
}
void ShapeWidget::mouseMoveEvent(QMouseEvent *event) {
if (event->buttons() & Qt::LeftButton) {
// Update the rectangle while the left mouse button is held down
currentRect.setSize(QSize(event->pos().x() - currentRect.x(), event->pos().y() - currentRect.y()));
update(); // Trigger the paintEvent
}
}
void ShapeWidget::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
// Finish drawing when the left mouse button is released
rectangles.append(currentRect);
currentRect = QRectF(); // Clear the current rectangle
update(); // Trigger the paintEvent
}
}
void ShapeWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::red, 5, Qt::SolidLine, Qt::RoundCap));
// Draw previously created rectangles
for (const QRectF &rect : rectangles) {
painter.drawRect(rect);
}
// Draw the currently being drawn rectangle
if (!currentRect.isNull()) {
painter.drawRect(currentRect);
}
}