2020-03-21 19:16:57 +07:00
|
|
|
#include "captureareaselector.h"
|
2020-02-21 00:45:53 +07:00
|
|
|
#include "capturer.h"
|
|
|
|
|
#include "task.h"
|
|
|
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QScreen>
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
CaptureAreaSelector::CaptureAreaSelector(Capturer &capturer)
|
2020-02-21 00:45:53 +07:00
|
|
|
: capturer_(capturer)
|
|
|
|
|
{
|
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
|
|
|
|
|
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
|
|
|
|
|
setCursor(Qt::CrossCursor);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::setScreen(QScreen &screen)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
const auto geometry = screen.availableGeometry();
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
|
const auto pixmap =
|
|
|
|
|
screen.grabWindow(0, 0, 0, geometry.width(), geometry.height());
|
|
|
|
|
#else
|
|
|
|
|
const auto pixmap = screen.grabWindow(0, geometry.x(), geometry.y(),
|
|
|
|
|
geometry.width(), geometry.height());
|
|
|
|
|
#endif
|
|
|
|
|
pixmap_ = pixmap;
|
|
|
|
|
|
|
|
|
|
auto palette = this->palette();
|
|
|
|
|
palette.setBrush(backgroundRole(), pixmap);
|
|
|
|
|
setPalette(palette);
|
|
|
|
|
setGeometry(geometry);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::showEvent(QShowEvent * /*event*/)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
startSelectPos_ = currentSelectPos_ = QPoint();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::keyPressEvent(QKeyEvent *event)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
if (event->key() == Qt::Key_Escape)
|
|
|
|
|
capturer_.canceled();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::paintEvent(QPaintEvent * /*event*/)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
auto selection = QRect(startSelectPos_, currentSelectPos_).normalized();
|
|
|
|
|
if (!selection.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
painter.setPen(Qt::red);
|
|
|
|
|
painter.drawRect(selection);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::mousePressEvent(QMouseEvent *event)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
if (startSelectPos_.isNull())
|
|
|
|
|
startSelectPos_ = event->pos();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::mouseMoveEvent(QMouseEvent *event)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
if (startSelectPos_.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
currentSelectPos_ = event->pos();
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:16:57 +07:00
|
|
|
void CaptureAreaSelector::mouseReleaseEvent(QMouseEvent *event)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
if (startSelectPos_.isNull() || pixmap_.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const auto endPos = event->pos();
|
|
|
|
|
const auto selection = QRect(startSelectPos_, endPos).normalized();
|
|
|
|
|
const auto selectedPixmap = pixmap_.copy(selection);
|
|
|
|
|
|
|
|
|
|
startSelectPos_ = currentSelectPos_ = QPoint();
|
|
|
|
|
|
|
|
|
|
if (selectedPixmap.width() < 3 || selectedPixmap.height() < 3) {
|
|
|
|
|
capturer_.canceled();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto task = std::make_shared<Task>();
|
|
|
|
|
task->captured = selectedPixmap;
|
|
|
|
|
task->capturePoint = pos() + selection.topLeft();
|
|
|
|
|
// TODO add customization menus
|
|
|
|
|
capturer_.captured(task);
|
|
|
|
|
}
|