|
| 1 | +#include <QApplication> |
| 2 | +#include <QScreen> |
| 3 | +#include <QPainter> |
| 4 | +#include <QDebug> |
| 5 | +#include <QRect> |
| 6 | + |
| 7 | +#include "ScreenshotWidget.h" |
| 8 | + |
| 9 | + |
| 10 | +ScreenshotWidget::ScreenshotWidget(QWidget *parent) |
| 11 | + : QWidget(parent), selecting(false) { |
| 12 | + setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); |
| 13 | + setCursor(Qt::CrossCursor); |
| 14 | + hide(); |
| 15 | +} |
| 16 | + |
| 17 | +void ScreenshotWidget::mousePressEvent(QMouseEvent *event) { |
| 18 | + startPos = event->pos(); |
| 19 | +} |
| 20 | + |
| 21 | +void ScreenshotWidget::mouseMoveEvent(QMouseEvent *event) { |
| 22 | + endPos = event->pos(); |
| 23 | + update(); |
| 24 | +} |
| 25 | + |
| 26 | +void ScreenshotWidget::mouseReleaseEvent(QMouseEvent *event) { |
| 27 | + endPos = event->pos(); |
| 28 | + cropScreenshot(); |
| 29 | + startPos = QPoint(-1, -1); |
| 30 | + endPos = QPoint(-1, -1); |
| 31 | + update(); |
| 32 | +} |
| 33 | + |
| 34 | +void ScreenshotWidget::paintEvent(QPaintEvent *) { |
| 35 | + QPainter painter(this); |
| 36 | + painter.setPen(Qt::blue); |
| 37 | + painter.setBrush(QColor(0, 0, 255, 50)); // Semi-transparent blue |
| 38 | + QRect rect(startPos, endPos); |
| 39 | + painter.drawRect(rect.normalized()); |
| 40 | +} |
| 41 | + |
| 42 | +void ScreenshotWidget::cropScreenshot() { |
| 43 | + QScreen *screen = QGuiApplication::primaryScreen(); |
| 44 | + QPixmap originalPixmap = screen->grabWindow(0); |
| 45 | + |
| 46 | + QRect cropRect(startPos, endPos); |
| 47 | + QPixmap croppedPixmap = originalPixmap.copy(cropRect.normalized()); |
| 48 | + crop_signal(croppedPixmap); |
| 49 | + hide(); |
| 50 | +} |
| 51 | + |
0 commit comments