From 027c7b46a009be44a0b7dee76f9f67cd74fd6aa0 Mon Sep 17 00:00:00 2001 From: Gres Date: Sat, 10 Oct 2015 21:05:12 +0300 Subject: [PATCH] Added ability to swap translation/ocr languages. --- Manager.cpp | 16 +++++++++++++++- ProcessingItem.cpp | 5 +++++ ProcessingItem.h | 2 ++ SelectionDialog.cpp | 18 +++++++++++++----- SelectionDialog.h | 1 + 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Manager.cpp b/Manager.cpp index 9171a69..4bca036 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -216,12 +216,26 @@ void Manager::capture () { void Manager::handleSelection (ProcessingItem item) { bool altMod = item.modifiers & Qt::AltModifier; - if ((doTranslation_ && !altMod) || (!doTranslation_ && altMod)) { + bool doTranslation = (doTranslation_ && !altMod) || (!doTranslation_ && altMod); + if (doTranslation) { item.translateLanguage = defaultTranslationLanguage_; } if (item.ocrLanguage.isEmpty ()) { item.ocrLanguage = defaultOrcLanguage_; } + if (item.swapLanguages_) { + QString translate = (item.translateLanguage.isEmpty ()) + ? defaultTranslationLanguage_ : item.translateLanguage; + if (doTranslation) { + item.translateLanguage = dictionary_->ocrToTranslateCodes (item.ocrLanguage); + } + item.sourceLanguage.clear (); + item.ocrLanguage = dictionary_->translateToOcrCodes (translate); + if (item.ocrLanguage.isEmpty ()) { + showError (tr ("Не найден подходящий язык распознавания.")); + return; + } + } if (item.sourceLanguage.isEmpty ()) { item.sourceLanguage = dictionary_->ocrToTranslateCodes (item.ocrLanguage); } diff --git a/ProcessingItem.cpp b/ProcessingItem.cpp index 396357e..df08fb1 100644 --- a/ProcessingItem.cpp +++ b/ProcessingItem.cpp @@ -1,5 +1,10 @@ #include "ProcessingItem.h" +ProcessingItem::ProcessingItem () + : swapLanguages_ (false) { + +} + bool ProcessingItem::isValid (bool checkOnlyInput) const { bool valid = true; valid &= (!screenPos.isNull ()); diff --git a/ProcessingItem.h b/ProcessingItem.h index 2f603b0..c5b7576 100644 --- a/ProcessingItem.h +++ b/ProcessingItem.h @@ -4,6 +4,7 @@ #include struct ProcessingItem { + ProcessingItem (); QPoint screenPos; QPixmap source; QString recognized; @@ -14,6 +15,7 @@ struct ProcessingItem { QString translateLanguage; Qt::KeyboardModifiers modifiers; + bool swapLanguages_; bool isValid (bool checkOnlyInput = false) const; }; diff --git a/SelectionDialog.cpp b/SelectionDialog.cpp index 7024055..8aa0084 100644 --- a/SelectionDialog.cpp +++ b/SelectionDialog.cpp @@ -11,7 +11,7 @@ SelectionDialog::SelectionDialog (const LanguageHelper &dictionary, QWidget *parent) : QDialog (parent), ui (new Ui::SelectionDialog), dictionary_ (dictionary), - languageMenu_ (new QMenu) { + languageMenu_ (new QMenu), swapLanguagesAction_ (NULL) { ui->setupUi (this); setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint); @@ -28,6 +28,9 @@ SelectionDialog::~SelectionDialog () { void SelectionDialog::applySettings () { dictionary_.updateMenu (languageMenu_, dictionary_.availableOcrLanguagesUi ()); + if (!languageMenu_->isEmpty ()) { + swapLanguagesAction_ = languageMenu_->addAction (tr ("Поменять язык текста и перевода")); + } } bool SelectionDialog::eventFilter (QObject *object, QEvent *event) { @@ -88,10 +91,15 @@ bool SelectionDialog::eventFilter (QObject *object, QEvent *event) { reject (); return QDialog::eventFilter (object, event); } - item.ocrLanguage = dictionary_.ocrUiToCode (action->text ()); - ST_ASSERT (!item.ocrLanguage.isEmpty ()); - item.sourceLanguage = dictionary_.ocrToTranslateCodes (item.ocrLanguage); - ST_ASSERT (!item.sourceLanguage.isEmpty ()); + if (action == swapLanguagesAction_) { + item.swapLanguages_ = true; + } + else { + item.ocrLanguage = dictionary_.ocrUiToCode (action->text ()); + ST_ASSERT (!item.ocrLanguage.isEmpty ()); + item.sourceLanguage = dictionary_.ocrToTranslateCodes (item.ocrLanguage); + ST_ASSERT (!item.sourceLanguage.isEmpty ()); + } } emit selected (item); } diff --git a/SelectionDialog.h b/SelectionDialog.h index 58dda6c..b126658 100644 --- a/SelectionDialog.h +++ b/SelectionDialog.h @@ -37,6 +37,7 @@ class SelectionDialog : public QDialog { QPoint currentSelectPos_; QPixmap currentPixmap_; QMenu *languageMenu_; + QAction *swapLanguagesAction_; }; #endif // SELECTIONDIALOG_H