From 1f5d6c73bd28dd7080254cce4b8ca3126b39c1ff Mon Sep 17 00:00:00 2001 From: Gres Date: Wed, 30 Sep 2015 20:38:55 +0300 Subject: [PATCH] Added ability to repeat image recognition with another OCR language from ResultDialog. --- Manager.cpp | 6 +++++- ResultDialog.cpp | 38 ++++++++++++++++++++++++++++++++++---- ResultDialog.h | 15 ++++++++++++++- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Manager.cpp b/Manager.cpp index 8ca61bb..9c0ffb5 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -25,7 +25,7 @@ Manager::Manager (QObject *parent) : QObject (parent), trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)), dictionary_ (new LanguageHelper), - resultDialog_ (new ResultDialog), + resultDialog_ (new ResultDialog (*dictionary_)), captureAction_ (NULL), repeatCaptureAction_ (NULL), repeatAction_ (NULL), clipboardAction_ (NULL), useResultDialog_ (true) { @@ -66,7 +66,11 @@ Manager::Manager (QObject *parent) : connect (qApp, SIGNAL (aboutToQuit ()), translatorThread, SLOT (quit ())); connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ())); + resultDialog_->setWindowIcon (trayIcon_->icon ()); + connect (this, SIGNAL (settingsEdited ()), resultDialog_, SLOT (applySettings ())); + connect (resultDialog_, SIGNAL (requestRecognize (ProcessingItem)), + this, SIGNAL (requestRecognize (ProcessingItem))); connect (trayIcon_, SIGNAL (activated (QSystemTrayIcon::ActivationReason)), diff --git a/ResultDialog.cpp b/ResultDialog.cpp index 85c615f..8550037 100644 --- a/ResultDialog.cpp +++ b/ResultDialog.cpp @@ -1,28 +1,57 @@ #include "ResultDialog.h" #include "ui_ResultDialog.h" #include "StAssert.h" +#include "LanguageHelper.h" #include +#include +#include -ResultDialog::ResultDialog (QWidget *parent) : +ResultDialog::ResultDialog (const LanguageHelper &dictionary, QWidget *parent) : QDialog (parent), ui (new Ui::ResultDialog), - isShowAtCapturePos_ (true) { + dictionary_ (dictionary), isShowAtCapturePos_ (true), + contextMenu_ (NULL), recognizeSubMenu_ (NULL) { ui->setupUi (this); setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint); installEventFilter (this); + createContextMenu (); + applySettings (); } ResultDialog::~ResultDialog () { + delete contextMenu_; delete ui; } +void ResultDialog::applySettings () { + dictionary_.updateMenu (recognizeSubMenu_, dictionary_.availableOcrLanguagesUi ()); +} + +void ResultDialog::createContextMenu () { + contextMenu_ = new QMenu (); + recognizeSubMenu_ = contextMenu_->addMenu (tr ("Распознать другой язык")); +} + bool ResultDialog::eventFilter (QObject *object, QEvent *event) { Q_UNUSED (object); - if (event->type () == QEvent::MouseButtonRelease || - event->type () == QEvent::WindowDeactivate) { + if (event->type () == QEvent::MouseButtonPress) { + Qt::MouseButton button = static_cast(event)->button (); + if (button == Qt::RightButton) { + QAction *action = contextMenu_->exec (QCursor::pos ()); + QWidget *subMenu = action->parentWidget (); + if (recognizeSubMenu_->isAncestorOf (subMenu)) { + ProcessingItem item = lastItem_; + item.translated = item.recognized = QString (); + item.ocrLanguage = dictionary_.ocrUiToCode (action->text ()); + emit requestRecognize (item); + } + } + hide (); + } + else if (event->type () == QEvent::WindowDeactivate) { hide (); } return QDialog::eventFilter (object, event); @@ -30,6 +59,7 @@ bool ResultDialog::eventFilter (QObject *object, QEvent *event) { void ResultDialog::showResult (ProcessingItem item) { ST_ASSERT (item.isValid ()); + lastItem_ = item; ui->sourceLabel->setPixmap (item.source); ui->recognizeLabel->setText (item.recognized); ui->translateLabel->setText (item.translated); diff --git a/ResultDialog.h b/ResultDialog.h index 0a21645..ab2be8f 100644 --- a/ResultDialog.h +++ b/ResultDialog.h @@ -2,29 +2,42 @@ #define RESULTDIALOG_H #include +#include #include "ProcessingItem.h" namespace Ui { class ResultDialog; } +class LanguageHelper; class ResultDialog : public QDialog { Q_OBJECT public: - explicit ResultDialog (QWidget *parent = 0); + explicit ResultDialog (const LanguageHelper &dictionary, QWidget *parent = 0); ~ResultDialog (); + signals: + void requestRecognize (ProcessingItem item); + public: bool eventFilter (QObject *object, QEvent *event); public slots: void showResult (ProcessingItem item); + void applySettings (); + + private: + void createContextMenu (); private: Ui::ResultDialog *ui; + const LanguageHelper &dictionary_; bool isShowAtCapturePos_; + QMenu *contextMenu_; + QMenu *recognizeSubMenu_; + ProcessingItem lastItem_; }; #endif // RESULTDIALOG_H