ScreenTranslator/src/represent/representer.cpp

176 lines
4.1 KiB
C++
Raw Normal View History

2020-02-21 00:45:53 +07:00
#include "representer.h"
#include "debug.h"
2020-03-29 15:09:55 +07:00
#include "geometryutils.h"
2020-02-21 00:45:53 +07:00
#include "manager.h"
2020-03-29 15:09:55 +07:00
#include "resulteditor.h"
2020-02-21 00:45:53 +07:00
#include "resultwidget.h"
#include "settings.h"
#include "task.h"
#include "trayicon.h"
#include <QApplication>
#include <QClipboard>
2020-04-05 01:03:27 +07:00
#include <QMouseEvent>
2020-03-29 15:09:55 +07:00
#include <QScreen>
2020-03-21 17:03:58 +07:00
Representer::Representer(Manager &manager, TrayIcon &tray,
2020-03-29 15:09:55 +07:00
const Settings &settings, const CommonModels &models)
2020-02-21 00:45:53 +07:00
: manager_(manager)
, tray_(tray)
2020-03-21 17:03:58 +07:00
, settings_(settings)
2020-03-29 15:09:55 +07:00
, models_(models)
2020-02-21 00:45:53 +07:00
{
}
2020-03-29 15:09:55 +07:00
Representer::~Representer() = default;
void Representer::showLast()
{
if (settings_.resultShowType == ResultMode::Tooltip) {
SOFT_ASSERT(lastTooltipTask_, return );
showTooltip(lastTooltipTask_);
return;
}
2020-04-05 01:03:27 +07:00
SOFT_ASSERT(!widgets_.empty(), return );
for (auto &widget : widgets_) {
SOFT_ASSERT(widget->task(), continue);
if (widget->task()->generation != generation_)
continue;
widget->show();
widget->activateWindow();
}
}
void Representer::clipboardLast()
{
if (settings_.resultShowType == ResultMode::Tooltip) {
SOFT_ASSERT(lastTooltipTask_, return );
clipboardText(lastTooltipTask_);
return;
}
2020-04-05 01:03:27 +07:00
SOFT_ASSERT(!widgets_.empty(), return );
SOFT_ASSERT(widgets_.front()->task(), return );
clipboardText(widgets_.front()->task());
tray_.showInformation(
QObject::tr("The last result was copied to the clipboard."));
}
2020-02-21 00:45:53 +07:00
void Representer::represent(const TaskPtr &task)
{
2020-03-21 17:03:58 +07:00
if (settings_.resultShowType == ResultMode::Tooltip)
2020-02-21 00:45:53 +07:00
showTooltip(task);
else
showWidget(task);
}
bool Representer::isVisible() const
{
2020-04-05 01:03:27 +07:00
if (widgets_.empty())
return false;
return std::any_of(widgets_.cbegin(), widgets_.cend(),
[](const auto &w) { return w->isVisible(); });
}
void Representer::hide()
{
2020-04-05 01:03:27 +07:00
if (widgets_.empty())
return;
for (auto &w : widgets_) w->hide();
}
2020-03-21 17:03:58 +07:00
void Representer::updateSettings()
2020-02-21 00:45:53 +07:00
{
lastTooltipTask_.reset();
2020-04-05 01:03:27 +07:00
if (widgets_.empty())
return;
for (auto &w : widgets_) w->updateSettings();
2020-02-21 00:45:53 +07:00
}
2020-03-29 15:09:55 +07:00
void Representer::clipboardText(const TaskPtr &task)
{
if (!task)
return;
QClipboard *clipboard = QApplication::clipboard();
auto text = task->recognized;
if (!task->translated.isEmpty())
text += QLatin1String(" - ") + task->translated;
clipboard->setText(text);
}
void Representer::clipboardImage(const TaskPtr &task)
{
if (!task)
return;
QClipboard *clipboard = QApplication::clipboard();
clipboard->setPixmap(task->captured);
}
void Representer::edit(const TaskPtr &task)
{
if (!editor_)
editor_ = std::make_unique<ResultEditor>(manager_, models_, settings_);
editor_->show(task);
const auto cursor = QCursor::pos();
const auto screen = QApplication::screenAt(cursor);
SOFT_ASSERT(screen, return );
editor_->move(service::geometry::cornerAtPoint(cursor, editor_->size(),
screen->geometry()));
}
2020-04-05 01:03:27 +07:00
bool Representer::eventFilter(QObject * /*watched*/, QEvent *event)
{
if (event->type() == QEvent::WindowDeactivate) {
for (const auto &w : widgets_) {
if (w->isActiveWindow())
return false;
}
hide();
} else if (event->type() == QEvent::MouseButtonPress) {
const auto casted = static_cast<QMouseEvent *>(event);
if (casted->button() == Qt::LeftButton)
hide();
}
return false;
}
2020-02-21 00:45:53 +07:00
void Representer::showTooltip(const TaskPtr &task)
{
SOFT_ASSERT(task, return );
lastTooltipTask_ = task;
auto message = task->recognized;
if (!task->translated.isEmpty())
message += QLatin1String(" - ") + task->translated;
2020-02-21 00:45:53 +07:00
tray_.showInformation(message);
}
void Representer::showWidget(const TaskPtr &task)
{
SOFT_ASSERT(task, return );
2020-04-05 01:03:27 +07:00
generation_ = task->generation;
auto index = 0u;
const auto count = widgets_.size();
for (; index < count; ++index) {
auto &widget = widgets_[index];
SOFT_ASSERT(widget->task(), continue);
if (widget->task()->generation != generation_)
break;
}
if (index == count) {
widgets_.emplace_back(
std::make_unique<ResultWidget>(manager_, *this, settings_));
2020-04-05 01:03:27 +07:00
widgets_.back()->installEventFilter(this);
}
auto &widget = widgets_[index];
widget->show(task);
2020-02-21 00:45:53 +07:00
}