Added ability to swap translation/ocr languages.

This commit is contained in:
Gres 2015-10-10 21:05:12 +03:00
parent a57e0b1af8
commit 027c7b46a0
5 changed files with 36 additions and 6 deletions

View File

@ -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);
}

View File

@ -1,5 +1,10 @@
#include "ProcessingItem.h"
ProcessingItem::ProcessingItem ()
: swapLanguages_ (false) {
}
bool ProcessingItem::isValid (bool checkOnlyInput) const {
bool valid = true;
valid &= (!screenPos.isNull ());

View File

@ -4,6 +4,7 @@
#include <QPixmap>
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;
};

View File

@ -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);
}

View File

@ -37,6 +37,7 @@ class SelectionDialog : public QDialog {
QPoint currentSelectPos_;
QPixmap currentPixmap_;
QMenu *languageMenu_;
QAction *swapLanguagesAction_;
};
#endif // SELECTIONDIALOG_H