2020-02-21 00:45:53 +07:00
|
|
|
#include "resultwidget.h"
|
|
|
|
|
#include "debug.h"
|
2020-03-29 15:09:55 +07:00
|
|
|
#include "representer.h"
|
2020-03-21 17:03:58 +07:00
|
|
|
#include "settings.h"
|
2020-02-21 00:45:53 +07:00
|
|
|
#include "task.h"
|
|
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QBoxLayout>
|
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
|
#include <QLabel>
|
2020-03-29 15:09:55 +07:00
|
|
|
#include <QMenu>
|
2020-02-21 00:45:53 +07:00
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
2020-03-29 15:09:55 +07:00
|
|
|
ResultWidget::ResultWidget(Representer &representer, const Settings &settings,
|
|
|
|
|
QWidget *parent)
|
2020-02-21 00:45:53 +07:00
|
|
|
: QFrame(parent)
|
2020-03-29 15:09:55 +07:00
|
|
|
, representer_(representer)
|
2020-03-21 17:03:58 +07:00
|
|
|
, settings_(settings)
|
2020-02-21 00:45:53 +07:00
|
|
|
, image_(new QLabel(this))
|
|
|
|
|
, recognized_(new QLabel(this))
|
2020-03-29 15:55:03 +07:00
|
|
|
, separator_(new QLabel(this))
|
2020-02-21 00:45:53 +07:00
|
|
|
, translated_(new QLabel(this))
|
2020-03-29 15:09:55 +07:00
|
|
|
, contextMenu_(new QMenu(this))
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
|
|
|
|
|
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
|
|
|
|
|
|
|
|
|
|
setLineWidth(1);
|
|
|
|
|
setFrameShape(QFrame::StyledPanel);
|
|
|
|
|
setFrameShadow(QFrame::Plain);
|
|
|
|
|
|
|
|
|
|
image_->setAlignment(Qt::AlignCenter);
|
|
|
|
|
|
|
|
|
|
recognized_->setAlignment(Qt::AlignCenter);
|
|
|
|
|
recognized_->setWordWrap(true);
|
|
|
|
|
|
2020-03-29 15:55:03 +07:00
|
|
|
separator_->setFixedHeight(1);
|
|
|
|
|
separator_->setAutoFillBackground(true);
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
translated_->setAlignment(Qt::AlignCenter);
|
|
|
|
|
translated_->setWordWrap(true);
|
|
|
|
|
|
2020-03-29 15:09:55 +07:00
|
|
|
{
|
|
|
|
|
auto clipboardText = contextMenu_->addAction(tr("Copy text"));
|
|
|
|
|
connect(clipboardText, &QAction::triggered, //
|
|
|
|
|
this, &ResultWidget::copyText);
|
|
|
|
|
auto clipboardImage = contextMenu_->addAction(tr("Copy image"));
|
|
|
|
|
connect(clipboardImage, &QAction::triggered, //
|
|
|
|
|
this, &ResultWidget::copyImage);
|
|
|
|
|
auto edit = contextMenu_->addAction(tr("Edit..."));
|
|
|
|
|
connect(edit, &QAction::triggered, //
|
|
|
|
|
this, &ResultWidget::edit);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
installEventFilter(this);
|
|
|
|
|
|
|
|
|
|
auto layout = new QVBoxLayout(this);
|
|
|
|
|
layout->addWidget(image_);
|
|
|
|
|
layout->addWidget(recognized_);
|
2020-03-29 15:55:03 +07:00
|
|
|
layout->addWidget(separator_);
|
2020-02-21 00:45:53 +07:00
|
|
|
layout->addWidget(translated_);
|
|
|
|
|
|
|
|
|
|
layout->setMargin(0);
|
2020-03-29 15:55:03 +07:00
|
|
|
layout->setSpacing(0);
|
|
|
|
|
|
|
|
|
|
updateSettings();
|
2020-02-21 00:45:53 +07:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 16:16:54 +07:00
|
|
|
const TaskPtr &ResultWidget::task() const
|
|
|
|
|
{
|
|
|
|
|
return task_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
void ResultWidget::show(const TaskPtr &task)
|
|
|
|
|
{
|
2020-03-28 16:16:54 +07:00
|
|
|
task_ = task;
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
image_->setPixmap(task->captured);
|
2020-03-09 17:47:40 +07:00
|
|
|
|
|
|
|
|
recognized_->setText(task->corrected);
|
|
|
|
|
const auto tooltip = task->recognized == task->corrected
|
|
|
|
|
? ""
|
|
|
|
|
: tr("Without correction:\n") + task->recognized;
|
|
|
|
|
recognized_->setToolTip(tooltip);
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
translated_->setText(task->translated);
|
2020-04-05 16:51:39 +07:00
|
|
|
translated_->setToolTip(task->usedTranslator);
|
2020-02-21 00:45:53 +07:00
|
|
|
|
|
|
|
|
const auto gotTranslation = !task->translated.isEmpty();
|
|
|
|
|
translated_->setVisible(gotTranslation);
|
2020-03-29 15:55:03 +07:00
|
|
|
separator_->setVisible(gotTranslation);
|
2020-02-21 00:45:53 +07:00
|
|
|
|
2020-03-21 17:03:58 +07:00
|
|
|
const auto mustShowRecognized = settings_.showRecognized || !gotTranslation;
|
2020-03-20 01:55:32 +07:00
|
|
|
recognized_->setVisible(mustShowRecognized);
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
show();
|
|
|
|
|
adjustSize();
|
|
|
|
|
|
2020-03-29 15:55:03 +07:00
|
|
|
if (!image_->isVisible())
|
|
|
|
|
resize(std::max(width(), task->captured.width()),
|
|
|
|
|
std::max(height(), task->captured.height()));
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
QDesktopWidget *desktop = QApplication::desktop();
|
|
|
|
|
Q_CHECK_PTR(desktop);
|
2020-03-27 01:26:20 +07:00
|
|
|
const auto correction =
|
|
|
|
|
QPoint((width() - task->captured.width()) / 2, lineWidth());
|
|
|
|
|
auto rect = QRect(task->capturePoint - correction, size());
|
|
|
|
|
|
|
|
|
|
const auto screenRect = desktop->screenGeometry(this);
|
|
|
|
|
const auto shouldTextOnTop = rect.bottom() > screenRect.bottom();
|
|
|
|
|
if (shouldTextOnTop)
|
|
|
|
|
rect.moveBottom(rect.top() + task->captured.height() + lineWidth());
|
|
|
|
|
|
|
|
|
|
auto layout = static_cast<QBoxLayout *>(this->layout());
|
|
|
|
|
SOFT_ASSERT(layout, return );
|
|
|
|
|
const auto isTextOnTop = layout->indexOf(recognized_) == 0;
|
|
|
|
|
if (isTextOnTop != shouldTextOnTop) {
|
|
|
|
|
layout->removeWidget(recognized_);
|
2020-03-29 15:55:03 +07:00
|
|
|
layout->removeWidget(separator_);
|
2020-03-27 01:26:20 +07:00
|
|
|
layout->removeWidget(translated_);
|
2020-03-29 15:55:03 +07:00
|
|
|
auto index = shouldTextOnTop ? 0 : 1;
|
|
|
|
|
layout->insertWidget(index, recognized_);
|
|
|
|
|
layout->insertWidget(++index, separator_);
|
|
|
|
|
layout->insertWidget(++index, translated_);
|
2020-02-21 00:45:53 +07:00
|
|
|
}
|
|
|
|
|
|
2020-03-27 01:26:20 +07:00
|
|
|
move(rect.topLeft());
|
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
activateWindow();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 17:03:58 +07:00
|
|
|
void ResultWidget::updateSettings()
|
2020-03-20 01:47:46 +07:00
|
|
|
{
|
2020-03-21 17:03:58 +07:00
|
|
|
QFont font(settings_.fontFamily, settings_.fontSize);
|
2020-03-29 15:55:03 +07:00
|
|
|
setFont(font);
|
|
|
|
|
|
|
|
|
|
auto palette = this->palette();
|
|
|
|
|
const auto &backgroundColor = settings_.backgroundColor;
|
|
|
|
|
palette.setColor(QPalette::Window, backgroundColor);
|
|
|
|
|
palette.setColor(QPalette::WindowText, settings_.fontColor);
|
|
|
|
|
setPalette(palette);
|
|
|
|
|
|
|
|
|
|
const auto separatorColor = backgroundColor.lightness() > 150
|
|
|
|
|
? backgroundColor.darker()
|
|
|
|
|
: backgroundColor.lighter();
|
|
|
|
|
palette.setColor(QPalette::Window, separatorColor);
|
|
|
|
|
separator_->setPalette(palette);
|
2020-03-20 01:55:32 +07:00
|
|
|
|
2020-03-21 17:03:58 +07:00
|
|
|
image_->setVisible(settings_.showCaptured);
|
2020-03-20 01:47:46 +07:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 19:03:39 +07:00
|
|
|
void ResultWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
const auto button = event->button();
|
|
|
|
|
if (button == Qt::RightButton) {
|
|
|
|
|
contextMenu_->exec(QCursor::pos());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (button == Qt::MiddleButton) {
|
|
|
|
|
lastPos_ = event->pos();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResultWidget::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (!(event->buttons() & Qt::MiddleButton))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
move(pos() + event->pos() - lastPos_);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 15:09:55 +07:00
|
|
|
void ResultWidget::edit()
|
|
|
|
|
{
|
|
|
|
|
representer_.edit(task_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResultWidget::copyText()
|
|
|
|
|
{
|
|
|
|
|
representer_.clipboardText(task_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResultWidget::copyImage()
|
|
|
|
|
{
|
|
|
|
|
representer_.clipboardImage(task_);
|
|
|
|
|
}
|