From 2da5363448faaf737906e2d446fd66ed3d19b6a3 Mon Sep 17 00:00:00 2001 From: Gres Date: Sat, 28 Mar 2020 12:16:54 +0300 Subject: [PATCH] Move interaction with last result to representer --- src/manager.cpp | 30 ++++++++++-------------------- src/manager.h | 1 - src/represent/representer.cpp | 22 ++++++++++++++++++++++ src/represent/representer.h | 2 ++ src/represent/resultwidget.cpp | 7 +++++++ src/represent/resultwidget.h | 2 ++ 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/manager.cpp b/src/manager.cpp index 7753cdb..c2b42e3 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -11,7 +11,6 @@ #include "updates.h" #include -#include #include #include @@ -127,11 +126,9 @@ void Manager::finishTask(const TaskPtr &task) --activeTaskCount_; tray_->setActiveTaskCount(activeTaskCount_); - last_ = task; - tray_->setTaskActionsEnabled(last_->isValid()); - if (!task->isValid()) { tray_->showError(task->error); + tray_->setTaskActionsEnabled(false); return; } @@ -201,6 +198,7 @@ void Manager::translated(const TaskPtr &task) return; representer_->represent(task); + tray_->setTaskActionsEnabled(true); } void Manager::applySettings(const Settings &settings) @@ -232,14 +230,6 @@ void Manager::repeatCapture() capturer_->repeatCapture(); } -void Manager::showLast() -{ - if (!last_ || !last_->isValid()) - return; - SOFT_ASSERT(representer_, return ); - representer_->represent(last_); -} - void Manager::settings() { SettingsEditor editor(*this, *updater_); @@ -260,16 +250,16 @@ void Manager::settings() applySettings(edited); } +void Manager::showLast() +{ + SOFT_ASSERT(representer_, return ); + representer_->showLast(); +} + void Manager::copyLastToClipboard() { - if (!last_ || !last_->isValid()) - return; - - QClipboard *clipboard = QApplication::clipboard(); - clipboard->setText(last_->recognized + QLatin1String(" - ") + - last_->translated); - tray_->showInformation( - QObject::tr("The last result was copied to the clipboard.")); + SOFT_ASSERT(representer_, return ); + representer_->clipboardLast(); } void Manager::about() diff --git a/src/manager.h b/src/manager.h index 27f18e4..c5a1f9b 100644 --- a/src/manager.h +++ b/src/manager.h @@ -40,6 +40,5 @@ private: std::unique_ptr representer_; std::unique_ptr updater_; std::unique_ptr updateAutoChecker_; - TaskPtr last_; int activeTaskCount_{0}; }; diff --git a/src/represent/representer.cpp b/src/represent/representer.cpp index e39b40e..c3fc88c 100644 --- a/src/represent/representer.cpp +++ b/src/represent/representer.cpp @@ -1,10 +1,14 @@ #include "representer.h" +#include "debug.h" #include "manager.h" #include "resultwidget.h" #include "settings.h" #include "task.h" #include "trayicon.h" +#include +#include + Representer::Representer(Manager &manager, TrayIcon &tray, const Settings &settings) : manager_(manager) @@ -13,6 +17,24 @@ Representer::Representer(Manager &manager, TrayIcon &tray, { } +void Representer::showLast() +{ + SOFT_ASSERT(widget_, return ); + widget_->show(); +} + +void Representer::clipboardLast() +{ + SOFT_ASSERT(widget_, return ); + SOFT_ASSERT(widget_->task(), return ); + const auto task = widget_->task(); + QClipboard *clipboard = QApplication::clipboard(); + clipboard->setText(task->recognized + QLatin1String(" - ") + + task->translated); + tray_.showInformation( + QObject::tr("The last result was copied to the clipboard.")); +} + Representer::~Representer() = default; void Representer::represent(const TaskPtr &task) diff --git a/src/represent/representer.h b/src/represent/representer.h index 56b5fa0..094528e 100644 --- a/src/represent/representer.h +++ b/src/represent/representer.h @@ -11,6 +11,8 @@ public: Representer(Manager &manager, TrayIcon &tray, const Settings &settings); ~Representer(); + void showLast(); + void clipboardLast(); void represent(const TaskPtr &task); void updateSettings(); diff --git a/src/represent/resultwidget.cpp b/src/represent/resultwidget.cpp index 1b0a5c5..1878929 100644 --- a/src/represent/resultwidget.cpp +++ b/src/represent/resultwidget.cpp @@ -52,9 +52,16 @@ ResultWidget::ResultWidget(const Settings &settings, QWidget *parent) layout->setSpacing(1); } +const TaskPtr &ResultWidget::task() const +{ + return task_; +} + void ResultWidget::show(const TaskPtr &task) { + task_ = task; SOFT_ASSERT(task->isValid(), return ); + image_->setPixmap(task->captured); recognized_->setText(task->corrected); diff --git a/src/represent/resultwidget.h b/src/represent/resultwidget.h index 1d1e044..d31ea81 100644 --- a/src/represent/resultwidget.h +++ b/src/represent/resultwidget.h @@ -12,6 +12,7 @@ class ResultWidget : public QFrame public: explicit ResultWidget(const Settings& settings, QWidget* parent = nullptr); + const TaskPtr& task() const; void show(const TaskPtr& task); using QWidget::show; void updateSettings(); @@ -20,6 +21,7 @@ public: private: const Settings& settings_; + TaskPtr task_; QLabel* image_; QLabel* recognized_; QLabel* translated_;