From c7c9d72bd7e67df14064c4a8eeb9f7cc89507552 Mon Sep 17 00:00:00 2001 From: MSN Date: Tue, 26 Nov 2013 10:44:00 +0400 Subject: [PATCH] Use of ResultDialog and ProcessingItem --- Manager.cpp | 42 +++++++++------- Manager.h | 7 ++- ProcessingItem.h | 15 ++++++ Recognizer.cpp | 112 ++++++++++++++++++++++--------------------- Recognizer.h | 6 +-- ResultDialog.cpp | 62 ++++++++++++++++++++++++ ResultDialog.h | 31 ++++++++++++ ResultDialog.ui | 98 +++++++++++++++++++++++++++++++++++++ ScreenTranslator.pro | 14 ++++-- SelectionDialog.cpp | 5 +- SelectionDialog.h | 4 +- Translator.cpp | 19 +++++--- Translator.h | 7 ++- 13 files changed, 331 insertions(+), 91 deletions(-) create mode 100644 ProcessingItem.h create mode 100644 ResultDialog.cpp create mode 100644 ResultDialog.h create mode 100644 ResultDialog.ui diff --git a/Manager.cpp b/Manager.cpp index d7102f5..09000a8 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -16,44 +16,53 @@ #include "GlobalActionHelper.h" #include "Recognizer.h" #include "Translator.h" +#include "ResultDialog.h" Manager::Manager(QObject *parent) : QObject(parent), trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)), selection_ (new SelectionDialog), + resultDialog_ (new ResultDialog), captureAction_ (NULL) { GlobalActionHelper::init (); + qRegisterMetaType(); - selection_->setWindowIcon (trayIcon_->icon ()); - connect (this, SIGNAL (showPixmap (QPixmap)), - selection_, SLOT (setPixmap (QPixmap))); - + // Recognizer Recognizer* recognizer = new Recognizer; - connect (selection_, SIGNAL (selected (QPixmap)), - recognizer, SLOT (recognize (QPixmap))); + connect (selection_, SIGNAL (selected (ProcessingItem)), + recognizer, SLOT (recognize (ProcessingItem))); connect (recognizer, SIGNAL (error (QString)), SLOT (showError (QString))); + connect (this, SIGNAL (settingsEdited ()), + recognizer, SLOT (applySettings ())); QThread* recognizerThread = new QThread (this); recognizer->moveToThread (recognizerThread); recognizerThread->start (); + + // Translator Translator* translator = new Translator; - connect (recognizer, SIGNAL (recognized (QString)), - translator, SLOT (translate (QString))); + connect (recognizer, SIGNAL (recognized (ProcessingItem)), + translator, SLOT (translate (ProcessingItem))); connect (translator, SIGNAL (error (QString)), SLOT (showError (QString))); + connect (this, SIGNAL (settingsEdited ()), + translator, SLOT (applySettings ())); QThread* translatorThread = new QThread (this); translator->moveToThread (translatorThread); translatorThread->start (); - connect (translator, SIGNAL (translated (QString, QString)), - SLOT (showTranslation (QString, QString))); + connect (translator, SIGNAL (translated (ProcessingItem)), + SLOT (showResult (ProcessingItem))); + connect (this, SIGNAL (showPixmap (QPixmap)), + selection_, SLOT (setPixmap (QPixmap))); connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ())); - connect (this, SIGNAL (settingsEdited ()), recognizer, SLOT (applySettings ())); - connect (this, SIGNAL (settingsEdited ()), translator, SLOT (applySettings ())); + selection_->setWindowIcon (trayIcon_->icon ()); + resultDialog_->setWindowIcon (trayIcon_->icon ()); + connect (trayIcon_, SIGNAL (activated (QSystemTrayIcon::ActivationReason)), SLOT (processTrayAction (QSystemTrayIcon::ActivationReason))); @@ -151,11 +160,12 @@ void Manager::about() message.exec (); } -void Manager::showTranslation(QString sourceText, QString translatedText) +void Manager::showResult(ProcessingItem item) { - lastMessage_ = sourceText + " - " + translatedText; - qDebug () << sourceText << translatedText; - trayIcon_->showMessage (tr ("Перевод"), lastMessage_, QSystemTrayIcon::Information); + resultDialog_->showResult (item); +// lastMessage_ = sourceText + " - " + translatedText; +// qDebug () << sourceText << translatedText; +// trayIcon_->showMessage (tr ("Перевод"), lastMessage_, QSystemTrayIcon::Information); } void Manager::showError(QString text) diff --git a/Manager.h b/Manager.h index f1bfe46..2645311 100644 --- a/Manager.h +++ b/Manager.h @@ -4,10 +4,13 @@ #include #include +#include "ProcessingItem.h" + class QAction; class QMenu; class SelectionDialog; +class ResultDialog; class Manager : public QObject { @@ -18,7 +21,6 @@ class Manager : public QObject signals: void showPixmap (QPixmap pixmap); - void recognize (QPixmap pixmap); void settingsEdited (); private slots: @@ -31,7 +33,7 @@ class Manager : public QObject void processTrayAction (QSystemTrayIcon::ActivationReason reason); - void showTranslation (QString sourceText, QString translatedText); + void showResult (ProcessingItem item); void showError (QString text); private: @@ -40,6 +42,7 @@ class Manager : public QObject private: QSystemTrayIcon* trayIcon_; SelectionDialog* selection_; + ResultDialog* resultDialog_; QAction* captureAction_; QString lastMessage_; }; diff --git a/ProcessingItem.h b/ProcessingItem.h new file mode 100644 index 0000000..5ae701f --- /dev/null +++ b/ProcessingItem.h @@ -0,0 +1,15 @@ +#ifndef PROCESSINGITEM_H +#define PROCESSINGITEM_H + +#include + +struct ProcessingItem +{ + QPoint screenPos; + QPixmap source; + QString recognized; + QString translated; +}; +Q_DECLARE_METATYPE(ProcessingItem) + +#endif // PROCESSINGITEM_H diff --git a/Recognizer.cpp b/Recognizer.cpp index 4a53c4d..b342eb2 100644 --- a/Recognizer.cpp +++ b/Recognizer.cpp @@ -1,6 +1,6 @@ #include "Recognizer.h" -#include +//#include #include #include @@ -35,63 +35,67 @@ void Recognizer::applySettings() bool Recognizer::initEngine() { - if (tessDataDir_.isEmpty () || ocrLanguage_.isEmpty ()) - { - emit error (tr ("Неверные параметры для OCR")); - return false; - } - if (engine_ != NULL) - { - delete engine_; - } - engine_ = new tesseract::TessBaseAPI(); - int result = engine_->Init(qPrintable (tessDataDir_), qPrintable (ocrLanguage_), - tesseract::OEM_DEFAULT); - if (result != 0) - { - emit error (tr ("Ошибка инициализации OCR: %1").arg (result)); - delete engine_; - engine_ = NULL; - return false; - } - return true; +// if (tessDataDir_.isEmpty () || ocrLanguage_.isEmpty ()) +// { +// emit error (tr ("Неверные параметры для OCR")); +// return false; +// } +// if (engine_ != NULL) +// { +// delete engine_; +// } +// engine_ = new tesseract::TessBaseAPI(); +// int result = engine_->Init(qPrintable (tessDataDir_), qPrintable (ocrLanguage_), +// tesseract::OEM_DEFAULT); +// if (result != 0) +// { +// emit error (tr ("Ошибка инициализации OCR: %1").arg (result)); +// delete engine_; +// engine_ = NULL; +// return false; +// } +// return true; } -void Recognizer::recognize(QPixmap pixmap) +void Recognizer::recognize(ProcessingItem item) { - Q_ASSERT (!pixmap.isNull ()); - if (engine_ == NULL) - { - if (!initEngine ()) - { - return; - } - } + Q_ASSERT (!item.source.isNull ()); + item.recognized = "test rec"; + emit recognized (item); + return; +// if (engine_ == NULL) +// { +// if (!initEngine ()) +// { +// return; +// } +// } - QPixmap scaled = pixmap; - if (imageScale_ > 0) - { - scaled = pixmap.scaledToHeight (pixmap.height () * imageScale_, - Qt::SmoothTransformation); - } - QImage image = scaled.toImage (); - const int bytesPerPixel = image.depth () / 8; - engine_->SetImage (image.bits (), image.width (), image.height (), - bytesPerPixel, image.bytesPerLine ()); +// QPixmap scaled = pixmap; +// if (imageScale_ > 0) +// { +// scaled = pixmap.scaledToHeight (pixmap.height () * imageScale_, +// Qt::SmoothTransformation); +// } +// QImage image = scaled.toImage (); +// const int bytesPerPixel = image.depth () / 8; +// engine_->SetImage (image.bits (), image.width (), image.height (), +// bytesPerPixel, image.bytesPerLine ()); - char* outText = engine_->GetUTF8Text(); - engine_->Clear(); +// char* outText = engine_->GetUTF8Text(); +// engine_->Clear(); - QString result (outText); - result = result.trimmed(); - if (!result.isEmpty ()) - { - emit recognized (result); - emit recognized (pixmap, result); - } - else - { - emit error (tr ("Текст не распознан.")); - } - delete [] outText; +// QString result (outText); +// result = result.trimmed(); +// if (!result.isEmpty ()) +// { +// item.recognized = result; +// emit recognized (result); +// emit recognized (pixmap, result); +// } +// else +// { +// emit error (tr ("Текст не распознан.")); +// } +// delete [] outText; } diff --git a/Recognizer.h b/Recognizer.h index 4872b9f..cd01617 100644 --- a/Recognizer.h +++ b/Recognizer.h @@ -4,6 +4,7 @@ #include #include "QPixmap" +#include "ProcessingItem.h" namespace tesseract { @@ -17,12 +18,11 @@ class Recognizer : public QObject explicit Recognizer(QObject *parent = 0); signals: - void recognized (QString text); - void recognized (QPixmap pixmap, QString text); + void recognized (ProcessingItem item); void error (QString text); public slots: - void recognize (QPixmap pixmap); + void recognize (ProcessingItem item); void applySettings (); private: diff --git a/ResultDialog.cpp b/ResultDialog.cpp new file mode 100644 index 0000000..51f4960 --- /dev/null +++ b/ResultDialog.cpp @@ -0,0 +1,62 @@ +#include "ResultDialog.h" +#include "ui_ResultDialog.h" + +#include + +ResultDialog::ResultDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ResultDialog), + isShowAtCapturePos_ (true) +{ + ui->setupUi(this); + setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | + Qt::WindowStaysOnTopHint); + + installEventFilter (this); +} + +ResultDialog::~ResultDialog() +{ + delete ui; +} + +bool ResultDialog::eventFilter(QObject* object, QEvent* event) +{ + Q_UNUSED (object); + if (event->type () == QEvent::MouseButtonRelease) + { + hide (); + } + return QDialog::eventFilter (object, event); +} + +void ResultDialog::showResult(ProcessingItem item) +{ + Q_ASSERT (!item.source.isNull ()); + Q_ASSERT (!item.recognized.isEmpty ()); + Q_ASSERT (!item.translated.isEmpty ()); + Q_ASSERT (!item.screenPos.isNull ()); + + ui->sourceLabel->setPixmap (item.source); + ui->recognizeLabel->setText (item.recognized); + ui->translateLabel->setText (item.translated); + + adjustSize (); + + if (isShowAtCapturePos_) + { + QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ()); + move (item.screenPos - correction); + } + else + { + QDesktopWidget* desktop = QApplication::desktop (); + Q_CHECK_PTR (desktop); + QRect screenRect = desktop->availableGeometry (this); + Q_ASSERT (screenRect.isValid ()); + QPoint newPos (screenRect.width () - width (), screenRect.height () - height ()); + move (newPos); + } + + show (); +} diff --git a/ResultDialog.h b/ResultDialog.h new file mode 100644 index 0000000..f4bcd15 --- /dev/null +++ b/ResultDialog.h @@ -0,0 +1,31 @@ +#ifndef RESULTDIALOG_H +#define RESULTDIALOG_H + +#include + +#include "ProcessingItem.h" + +namespace Ui { + class ResultDialog; +} + +class ResultDialog : public QDialog +{ + Q_OBJECT + + public: + explicit ResultDialog(QWidget *parent = 0); + ~ResultDialog(); + + public: + bool eventFilter (QObject *object, QEvent *event); + + public slots: + void showResult (ProcessingItem item); + + private: + Ui::ResultDialog *ui; + bool isShowAtCapturePos_; +}; + +#endif // RESULTDIALOG_H diff --git a/ResultDialog.ui b/ResultDialog.ui new file mode 100644 index 0000000..92329f9 --- /dev/null +++ b/ResultDialog.ui @@ -0,0 +1,98 @@ + + + ResultDialog + + + + 0 + 0 + 260 + 222 + + + + Результат + + + + 0 + + + 0 + + + + + QFrame::StyledPanel + + + QFrame::Plain + + + + 0 + + + 0 + + + + + + + + Qt::AlignCenter + + + true + + + + + + + Qt::Horizontal + + + + + + + + + + Qt::AlignCenter + + + true + + + + + + + Qt::Horizontal + + + + + + + + + + Qt::AlignCenter + + + true + + + + + + + + + + + diff --git a/ScreenTranslator.pro b/ScreenTranslator.pro index 87b8ecc..b54b2b9 100644 --- a/ScreenTranslator.pro +++ b/ScreenTranslator.pro @@ -11,9 +11,9 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = ScreenTranslator TEMPLATE = app -INCLUDEPATH += C:/build/include +#INCLUDEPATH += C:/build/include -LIBS += -LC:/build/bin -ltesseract +#LIBS += -LC:/build/bin -ltesseract SOURCES += main.cpp\ Manager.cpp \ @@ -21,7 +21,8 @@ SOURCES += main.cpp\ SelectionDialog.cpp \ GlobalActionHelper.cpp \ Recognizer.cpp \ - Translator.cpp + Translator.cpp \ + ResultDialog.cpp HEADERS += \ Manager.h \ @@ -30,11 +31,14 @@ HEADERS += \ GlobalActionHelper.h \ Recognizer.h \ Translator.h \ - Settings.h + Settings.h \ + ProcessingItem.h \ + ResultDialog.h FORMS += \ SettingsEditor.ui \ - SelectionDialog.ui + SelectionDialog.ui \ + ResultDialog.ui RESOURCES += \ Recources.qrc diff --git a/SelectionDialog.cpp b/SelectionDialog.cpp index 418917d..7d5b69d 100644 --- a/SelectionDialog.cpp +++ b/SelectionDialog.cpp @@ -52,7 +52,10 @@ bool SelectionDialog::eventFilter(QObject* object, QEvent* event) QPixmap selectedPixmap = currentPixmap_.copy (selection); if (!selectedPixmap.isNull ()) { - emit selected (selectedPixmap); + ProcessingItem item; + item.source = selectedPixmap; + item.screenPos = selection.topLeft (); + emit selected (item); accept (); } } diff --git a/SelectionDialog.h b/SelectionDialog.h index 9cd957c..c0e2e59 100644 --- a/SelectionDialog.h +++ b/SelectionDialog.h @@ -4,6 +4,8 @@ #include #include +#include "ProcessingItem.h" + namespace Ui { class SelectionDialog; } @@ -19,7 +21,7 @@ class SelectionDialog : public QDialog bool eventFilter (QObject *object, QEvent *event); signals: - void selected (QPixmap pixmap); + void selected (ProcessingItem pixmap); public slots: void setPixmap (QPixmap pixmap); diff --git a/Translator.cpp b/Translator.cpp index c45ce01..02eac87 100644 --- a/Translator.cpp +++ b/Translator.cpp @@ -35,20 +35,26 @@ void Translator::applySettings() toString (); } -void Translator::translate(QString text) +void Translator::translate(ProcessingItem item) { - Q_ASSERT (!text.isEmpty ()); + Q_ASSERT (!item.recognized.isEmpty ()); + item.translated = "проверка"; + emit translated (item); + return; if (translationLanguage_.isEmpty ()) { emit error (tr ("Неверные парметры для перевода.")); return; } - QUrl url (translateBaseUrl.arg (text, translationLanguage_)); - network_.get (QNetworkRequest (url)); + QUrl url (translateBaseUrl.arg (item.recognized, translationLanguage_)); + QNetworkReply* reply = network_.get (QNetworkRequest (url)); + items_.insert (reply, item); } void Translator::replyFinished(QNetworkReply* reply) { + Q_ASSERT (items_.contains (reply)); + ProcessingItem item = items_.take (reply); Q_ASSERT (reply->isFinished ()); if (reply->error () != QNetworkReply::NoError) { @@ -73,7 +79,6 @@ void Translator::replyFinished(QNetworkReply* reply) } QJsonArray answerArray = document.array (); QJsonArray fullTranslation = answerArray.first ().toArray (); - QString source = ""; QString translation = ""; foreach (QJsonValue part, fullTranslation) { @@ -83,7 +88,7 @@ void Translator::replyFinished(QNetworkReply* reply) continue; } translation += partTranslation.at (0).toString (); - source += partTranslation.at (1).toString (); } - emit translated (source, translation); + item.translated = translation; + emit translated (item); } diff --git a/Translator.h b/Translator.h index 2c0c819..4e0571f 100644 --- a/Translator.h +++ b/Translator.h @@ -3,6 +3,8 @@ #include +#include "ProcessingItem.h" + class Translator : public QObject { Q_OBJECT @@ -10,11 +12,11 @@ class Translator : public QObject explicit Translator(QObject *parent = 0); signals: - void translated (QString sourceText, QString translatedText); + void translated (ProcessingItem item); void error (QString text); public slots: - void translate (QString text); + void translate (ProcessingItem item); void applySettings (); private slots: @@ -23,6 +25,7 @@ class Translator : public QObject private: QNetworkAccessManager network_; QString translationLanguage_; + QHash items_; };