2020-03-21 19:16:57 +07:00
|
|
|
#include "captureareaselector.h"
|
2020-02-21 00:45:53 +07:00
|
|
|
#include "capturer.h"
|
2020-03-21 19:22:29 +07:00
|
|
|
#include "languagecodes.h"
|
|
|
|
|
#include "settings.h"
|
2020-02-21 00:45:53 +07:00
|
|
|
#include "task.h"
|
|
|
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QScreen>
|
|
|
|
|
|
2020-03-21 19:22:29 +07:00
|
|
|
CaptureAreaSelector::CaptureAreaSelector(Capturer &capturer,
|
|
|
|
|
const Settings &settings)
|
2020-02-21 00:45:53 +07:00
|
|
|
: capturer_(capturer)
|
2020-03-21 19:22:29 +07:00
|
|
|
, settings_(settings)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
|
|
|
|
|
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
|
|
|
|
|
setCursor(Qt::CrossCursor);
|
2020-03-21 19:22:29 +07:00
|
|
|
setMouseTracking(true);
|
2020-02-21 00:45:53 +07:00
|
|
|
}
|
|
|
|
|
|
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:22:29 +07:00
|
|
|
|
|
|
|
|
updateHelp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CaptureAreaSelector::updateHelp()
|
|
|
|
|
{
|
|
|
|
|
LanguageCodes languages;
|
|
|
|
|
const auto source = languages.findById(settings_.sourceLanguage);
|
|
|
|
|
const auto sourceName =
|
|
|
|
|
source ? QObject::tr(source->name) : settings_.sourceLanguage;
|
|
|
|
|
const auto target = languages.findById(settings_.targetLanguage);
|
|
|
|
|
const auto targetName =
|
|
|
|
|
target ? QObject::tr(target->name) : settings_.targetLanguage;
|
|
|
|
|
|
|
|
|
|
help_ = tr(R"(Recognition language: %1
|
|
|
|
|
Translation language: %2)")
|
|
|
|
|
.arg(sourceName, targetName);
|
|
|
|
|
|
|
|
|
|
const auto rect = this->rect();
|
|
|
|
|
auto helpRect = fontMetrics().boundingRect({}, 0, help_);
|
|
|
|
|
helpRect.setSize(helpRect.size() * 1.4);
|
|
|
|
|
helpRects_ = std::vector<QRect>(2, helpRect);
|
|
|
|
|
helpRects_[0].moveTopLeft(rect.topLeft());
|
|
|
|
|
helpRects_[1].moveTopRight(rect.topRight());
|
|
|
|
|
currentHelpRect_ = helpRects_[0];
|
2020-02-21 00:45:53 +07:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2020-03-21 19:22:29 +07:00
|
|
|
QPainter painter(this);
|
|
|
|
|
|
|
|
|
|
const auto cursor = mapFromGlobal(QCursor::pos());
|
|
|
|
|
if (currentHelpRect_.contains(cursor)) {
|
|
|
|
|
for (const auto &rect : helpRects_) {
|
|
|
|
|
if (!rect.contains(cursor)) {
|
|
|
|
|
currentHelpRect_ = rect;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
painter.setBrush(QBrush(QColor(200, 200, 200, 200)));
|
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
|
painter.drawRect(currentHelpRect_);
|
|
|
|
|
painter.setBrush({});
|
|
|
|
|
painter.setPen(Qt::black);
|
|
|
|
|
painter.drawText(currentHelpRect_, Qt::AlignCenter, help_);
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
auto selection = QRect(startSelectPos_, currentSelectPos_).normalized();
|
|
|
|
|
if (!selection.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-03-21 19:22:29 +07:00
|
|
|
painter.setBrush({});
|
2020-02-21 00:45:53 +07:00
|
|
|
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
|
|
|
{
|
2020-03-21 19:22:29 +07:00
|
|
|
if (startSelectPos_.isNull()) {
|
|
|
|
|
if (currentHelpRect_.contains(event->pos()))
|
|
|
|
|
update();
|
2020-02-21 00:45:53 +07:00
|
|
|
return;
|
2020-03-21 19:22:29 +07:00
|
|
|
}
|
2020-02-21 00:45:53 +07:00
|
|
|
|
|
|
|
|
currentSelectPos_ = event->pos();
|
2020-03-21 19:22:29 +07:00
|
|
|
update();
|
2020-02-21 00:45:53 +07:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|