From 476893a7cf8a20a27fba4d2617b061bcb08ffa67 Mon Sep 17 00:00:00 2001 From: msn Date: Tue, 26 Nov 2013 20:59:47 +0400 Subject: [PATCH] ProcessingItem added ResultDialog added --- Manager.cpp | 98 +++++++++++++++++++++++++++------------ Manager.h | 7 ++- ProcessingItem.cpp | 11 +++++ ProcessingItem.h | 2 + Recognizer.cpp | 108 +++++++++++++++++++++---------------------- ResultDialog.cpp | 11 ++++- ScreenTranslator.pro | 7 +-- Settings.h | 6 +++ SettingsEditor.cpp | 20 +++++++- SettingsEditor.h | 2 + SettingsEditor.ui | 104 ++++++++++++++++++++++++++++++++--------- Translator.cpp | 3 -- 12 files changed, 261 insertions(+), 118 deletions(-) create mode 100644 ProcessingItem.cpp diff --git a/Manager.cpp b/Manager.cpp index 09000a8..2350075 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -23,7 +23,8 @@ Manager::Manager(QObject *parent) : trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)), selection_ (new SelectionDialog), resultDialog_ (new ResultDialog), - captureAction_ (NULL) + captureAction_ (NULL), repeatAction_ (NULL), clipboardAction_ (NULL), + useResultDialog_ (true) { GlobalActionHelper::init (); qRegisterMetaType(); @@ -77,6 +78,11 @@ QMenu*Manager::trayContextMenu() { QMenu* menu = new QMenu (); captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ())); + QMenu* translateMenu = menu->addMenu (tr ("Перевод")); + repeatAction_ = translateMenu->addAction (tr ("Повторить"), this, + SLOT (showLast ())); + clipboardAction_ = translateMenu->addAction (tr ("Скопировать"), this, + SLOT (copyLastToClipboard ())); menu->addAction (tr ("Настройки"), this, SLOT (settings ())); menu->addAction (tr ("О программе"), this, SLOT (about ())); menu->addAction (tr ("Выход"), this, SLOT (close ())); @@ -88,35 +94,29 @@ void Manager::applySettings() QSettings settings; settings.beginGroup (settings_names::guiGroup); QString captureHotkey = settings.value (settings_names::captureHotkey, - settings_values::captureHotkey). - toString (); + settings_values::captureHotkey).toString (); Q_CHECK_PTR (captureAction_); GlobalActionHelper::removeGlobal (captureAction_); captureAction_->setShortcut (captureHotkey); GlobalActionHelper::makeGlobal (captureAction_); -} -void Manager::processTrayAction(QSystemTrayIcon::ActivationReason reason) -{ - if (reason == QSystemTrayIcon::Trigger) - { - if (!lastMessage_.isEmpty ()) - { - trayIcon_->showMessage (tr ("Последний перевод"), lastMessage_, - QSystemTrayIcon::Information); - } - } - else if (reason == QSystemTrayIcon::MiddleClick) - { - if (!lastMessage_.isEmpty ()) - { - QClipboard* clipboard = QApplication::clipboard (); - clipboard->setText (lastMessage_); - trayIcon_->showMessage (tr ("Последний перевод"), - tr ("Последний перевод был скопирован в буфер обмена."), - QSystemTrayIcon::Information); - } - } + QString repeatHotkey = settings.value (settings_names::repeatHotkey, + settings_values::repeatHotkey).toString (); + Q_CHECK_PTR (repeatAction_); + GlobalActionHelper::removeGlobal (repeatAction_); + repeatAction_->setShortcut (repeatHotkey); + GlobalActionHelper::makeGlobal (repeatAction_); + + QString clipboardHotkey = settings.value (settings_names::clipboardHotkey, + settings_values::clipboardHotkey).toString (); + Q_CHECK_PTR (clipboardAction_); + GlobalActionHelper::removeGlobal (clipboardAction_); + clipboardAction_->setShortcut (clipboardHotkey); + GlobalActionHelper::makeGlobal (clipboardAction_); + + // Depends on SettingsEditor button indexes. 1==dialog + useResultDialog_ = settings.value (settings_names::resultShowType, + settings_values::resultShowType).toBool (); } Manager::~Manager() @@ -160,12 +160,52 @@ void Manager::about() message.exec (); } +void Manager::processTrayAction(QSystemTrayIcon::ActivationReason reason) +{ + if (reason == QSystemTrayIcon::Trigger) + { + showLast (); + } + else if (reason == QSystemTrayIcon::MiddleClick) + { + copyLastToClipboard (); + } +} + +void Manager::showLast() +{ + if (lastItem_.isValid ()) + { + showResult (lastItem_); + } +} + +void Manager::copyLastToClipboard() +{ + if (lastItem_.isValid ()) + { + QClipboard* clipboard = QApplication::clipboard (); + QString message = lastItem_.recognized + " - " + lastItem_.translated; + clipboard->setText (message); + trayIcon_->showMessage (tr ("Перевод"), + tr ("Последний перевод был скопирован в буфер обмена."), + QSystemTrayIcon::Information); + } +} + void Manager::showResult(ProcessingItem item) { - resultDialog_->showResult (item); -// lastMessage_ = sourceText + " - " + translatedText; -// qDebug () << sourceText << translatedText; -// trayIcon_->showMessage (tr ("Перевод"), lastMessage_, QSystemTrayIcon::Information); + Q_ASSERT (item.isValid ()); + lastItem_ = item; + if (useResultDialog_) + { + resultDialog_->showResult (item); + } + else + { + QString message = item.recognized + " - " + item.translated; + trayIcon_->showMessage (tr ("Перевод"), message, QSystemTrayIcon::Information); + } } void Manager::showError(QString text) diff --git a/Manager.h b/Manager.h index 2645311..d9b76a7 100644 --- a/Manager.h +++ b/Manager.h @@ -28,6 +28,8 @@ class Manager : public QObject void settings (); void close (); void about (); + void showLast (); + void copyLastToClipboard (); void applySettings (); @@ -44,7 +46,10 @@ class Manager : public QObject SelectionDialog* selection_; ResultDialog* resultDialog_; QAction* captureAction_; - QString lastMessage_; + QAction* repeatAction_; + QAction* clipboardAction_; + ProcessingItem lastItem_; + bool useResultDialog_; }; #endif // MANAGER_H diff --git a/ProcessingItem.cpp b/ProcessingItem.cpp new file mode 100644 index 0000000..f0f4102 --- /dev/null +++ b/ProcessingItem.cpp @@ -0,0 +1,11 @@ +#include "ProcessingItem.h" + +bool ProcessingItem::isValid() const +{ + bool valid = true; + valid &= (!screenPos.isNull ()); + valid &= (!source.isNull ()); + valid &= (!recognized.isEmpty ()); + valid &= (!translated.isEmpty ()); + return valid; +} diff --git a/ProcessingItem.h b/ProcessingItem.h index 5ae701f..d6b063f 100644 --- a/ProcessingItem.h +++ b/ProcessingItem.h @@ -9,6 +9,8 @@ struct ProcessingItem QPixmap source; QString recognized; QString translated; + + bool isValid () const; }; Q_DECLARE_METATYPE(ProcessingItem) diff --git a/Recognizer.cpp b/Recognizer.cpp index b342eb2..44ddbe9 100644 --- a/Recognizer.cpp +++ b/Recognizer.cpp @@ -1,6 +1,6 @@ #include "Recognizer.h" -//#include +#include #include #include @@ -35,67 +35,63 @@ 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(ProcessingItem item) { Q_ASSERT (!item.source.isNull ()); - item.recognized = "test rec"; - emit recognized (item); - return; -// if (engine_ == NULL) -// { -// if (!initEngine ()) -// { -// 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 = item.source; + if (imageScale_ > 0) + { + scaled = scaled.scaledToHeight (scaled.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 ()) -// { -// item.recognized = result; -// 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 (item); + } + else + { + emit error (tr ("Текст не распознан.")); + } + delete [] outText; } diff --git a/ResultDialog.cpp b/ResultDialog.cpp index 51f4960..950e671 100644 --- a/ResultDialog.cpp +++ b/ResultDialog.cpp @@ -43,15 +43,22 @@ void ResultDialog::showResult(ProcessingItem item) adjustSize (); + QDesktopWidget* desktop = QApplication::desktop (); + Q_CHECK_PTR (desktop); if (isShowAtCapturePos_) { QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ()); move (item.screenPos - correction); + QRect screenRect = desktop->screenGeometry (this); + int minY = screenRect.bottom () - height (); + if (y () > minY) + { + move (x (), minY); + } } 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 ()); diff --git a/ScreenTranslator.pro b/ScreenTranslator.pro index b54b2b9..c2a63ad 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 \ @@ -22,7 +22,8 @@ SOURCES += main.cpp\ GlobalActionHelper.cpp \ Recognizer.cpp \ Translator.cpp \ - ResultDialog.cpp + ResultDialog.cpp \ + ProcessingItem.cpp HEADERS += \ Manager.h \ diff --git a/Settings.h b/Settings.h index 27f40c9..b8475ee 100644 --- a/Settings.h +++ b/Settings.h @@ -9,6 +9,9 @@ namespace settings_names const QString guiGroup = "GUI"; const QString geometry = "geometry"; const QString captureHotkey = "captureHotkey"; + const QString repeatHotkey = "repeatHotkey"; + const QString clipboardHotkey = "clipboardHotkey"; + const QString resultShowType = "resultShowType"; //! Recognition const QString recogntionGroup = "Recognition"; @@ -29,6 +32,9 @@ namespace settings_values //! UI const QString captureHotkey = "Ctrl+Alt+Z"; + const QString repeatHotkey = "Ctrl+Alt+X"; + const QString clipboardHotkey = "Ctrl+Alt+C"; + const QString resultShowType = "1";//dialog //! Recognition const QString tessDataPlace = "./"; diff --git a/SettingsEditor.cpp b/SettingsEditor.cpp index 687a8a4..9730920 100644 --- a/SettingsEditor.cpp +++ b/SettingsEditor.cpp @@ -10,10 +10,14 @@ SettingsEditor::SettingsEditor(QWidget *parent) : QDialog(parent), - ui(new Ui::SettingsEditor) + ui(new Ui::SettingsEditor), + buttonGroup_ (new QButtonGroup (this)) { ui->setupUi(this); + buttonGroup_->addButton (ui->trayRadio, 0); + buttonGroup_->addButton (ui->dialogRadio, 1); + connect (ui->tessdataButton, SIGNAL (clicked ()), SLOT (openTessdataDialog ())); connect (ui->tessdataEdit, SIGNAL (textChanged (const QString&)), SLOT (initOcrLangCombo ())); @@ -44,6 +48,9 @@ void SettingsEditor::saveSettings() const QSettings settings; settings.beginGroup (settings_names::guiGroup); settings.setValue (settings_names::captureHotkey, ui->captureEdit->text ()); + settings.setValue (settings_names::repeatHotkey, ui->repeatEdit->text ()); + settings.setValue (settings_names::clipboardHotkey, ui->clipboardEdit->text ()); + settings.setValue (settings_names::resultShowType, buttonGroup_->checkedId ()); settings.endGroup (); @@ -82,6 +89,17 @@ void SettingsEditor::loadSettings() QString captureHotkey = settings.value (settings_names::captureHotkey, settings_values::captureHotkey).toString (); ui->captureEdit->setText (captureHotkey); + QString repeatHotkey = settings.value (settings_names::repeatHotkey, + settings_values::repeatHotkey).toString (); + ui->repeatEdit->setText (repeatHotkey); + QString clipboardHotkey = settings.value (settings_names::clipboardHotkey, + settings_values::clipboardHotkey).toString (); + ui->clipboardEdit->setText (clipboardHotkey); + int resultShowType = settings.value (settings_names::resultShowType, + settings_values::resultShowType).toInt (); + QAbstractButton* button = buttonGroup_->button (resultShowType); + Q_CHECK_PTR (button); + button->setChecked (true); settings.endGroup (); diff --git a/SettingsEditor.h b/SettingsEditor.h index 15bd40d..de47f82 100644 --- a/SettingsEditor.h +++ b/SettingsEditor.h @@ -2,6 +2,7 @@ #define SETTINGSEDITOR_H #include +#include namespace Ui { class SettingsEditor; @@ -34,6 +35,7 @@ class SettingsEditor : public QDialog private: Ui::SettingsEditor *ui; + QButtonGroup* buttonGroup_; }; #endif // SETTINGSEDITOR_H diff --git a/SettingsEditor.ui b/SettingsEditor.ui index ae51d16..3f43331 100644 --- a/SettingsEditor.ui +++ b/SettingsEditor.ui @@ -6,14 +6,14 @@ 0 0 - 456 - 166 + 435 + 221 Настройки - + @@ -26,20 +26,52 @@ <html><head/><body><p>Сочетание клавиш для перехода в режим захвата.</p></body></html> - Захват + Захватить captureEdit - + + + + + <html><head/><body><p>Сочетание клавиш для перехода в режим захвата.</p></body></html> + + + Скопировать + + + captureEdit + + + + + + + + + + <html><head/><body><p>Сочетание клавиш для перехода в режим захвата.</p></body></html> + + + Повторить + + + captureEdit + + + + + + - + Распознавание @@ -103,7 +135,46 @@ + + + + Qt::Horizontal + + + + 0 + 20 + + + + + + + Вывод результата + + + + + + Трей + + + + + + + Окно + + + true + + + + + + + Перевод @@ -128,33 +199,20 @@ - - - - Qt::Horizontal - - - - 24 - 20 - - - - - - + + Qt::Vertical 20 - 37 + 1 - + Qt::Horizontal diff --git a/Translator.cpp b/Translator.cpp index 02eac87..3799604 100644 --- a/Translator.cpp +++ b/Translator.cpp @@ -38,9 +38,6 @@ void Translator::applySettings() void Translator::translate(ProcessingItem item) { Q_ASSERT (!item.recognized.isEmpty ()); - item.translated = "проверка"; - emit translated (item); - return; if (translationLanguage_.isEmpty ()) { emit error (tr ("Неверные парметры для перевода."));