ScreenTranslator/Manager.cpp

223 lines
8.0 KiB
C++
Raw Normal View History

2013-11-23 13:48:34 +07:00
#include "Manager.h"
#include <QDebug>
#include <QMenu>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <QDesktopWidget>
#include <QThread>
2013-11-24 19:43:37 +07:00
#include <QSettings>
2013-11-25 00:23:21 +07:00
#include <QClipboard>
2013-11-24 20:06:19 +07:00
#include <QMessageBox>
2013-11-23 13:48:34 +07:00
2013-11-24 19:43:37 +07:00
#include "Settings.h"
2013-11-23 13:48:34 +07:00
#include "SettingsEditor.h"
#include "SelectionDialog.h"
#include "GlobalActionHelper.h"
#include "Recognizer.h"
#include "Translator.h"
2013-11-26 13:44:00 +07:00
#include "ResultDialog.h"
2014-04-04 21:39:10 +07:00
#include "LanguageHelper.h"
2015-06-30 00:26:33 +07:00
#include "StAssert.h"
2013-11-23 13:48:34 +07:00
2015-09-23 01:41:08 +07:00
Manager::Manager (QObject *parent) :
QObject (parent),
2013-11-23 13:48:34 +07:00
trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)),
2014-04-04 21:39:10 +07:00
dictionary_ (new LanguageHelper),
2013-11-26 13:44:00 +07:00
resultDialog_ (new ResultDialog),
captureAction_ (NULL), repeatAction_ (NULL), clipboardAction_ (NULL),
2015-09-23 01:41:08 +07:00
useResultDialog_ (true) {
GlobalActionHelper::init ();
2013-11-26 13:44:00 +07:00
qRegisterMetaType<ProcessingItem>();
2013-11-23 13:48:34 +07:00
2013-11-26 13:44:00 +07:00
// Recognizer
2015-09-23 01:41:08 +07:00
Recognizer *recognizer = new Recognizer;
connect (this, SIGNAL (selected (ProcessingItem)),
2013-11-26 13:44:00 +07:00
recognizer, SLOT (recognize (ProcessingItem)));
2013-11-24 19:43:37 +07:00
connect (recognizer, SIGNAL (error (QString)),
SLOT (showError (QString)));
2013-11-26 13:44:00 +07:00
connect (this, SIGNAL (settingsEdited ()),
recognizer, SLOT (applySettings ()));
2015-09-23 01:41:08 +07:00
QThread *recognizerThread = new QThread (this);
recognizer->moveToThread (recognizerThread);
recognizerThread->start ();
2014-04-04 22:03:53 +07:00
connect (qApp, SIGNAL (aboutToQuit ()), recognizerThread, SLOT (quit ()));
2013-11-23 13:48:34 +07:00
2013-11-26 13:44:00 +07:00
// Translator
2015-09-23 01:41:08 +07:00
Translator *translator = new Translator;
2013-11-26 13:44:00 +07:00
connect (recognizer, SIGNAL (recognized (ProcessingItem)),
translator, SLOT (translate (ProcessingItem)));
2013-11-24 19:43:37 +07:00
connect (translator, SIGNAL (error (QString)),
SLOT (showError (QString)));
2013-11-26 13:44:00 +07:00
connect (this, SIGNAL (settingsEdited ()),
translator, SLOT (applySettings ()));
2015-09-23 01:41:08 +07:00
QThread *translatorThread = new QThread (this);
translator->moveToThread (translatorThread);
translatorThread->start ();
2014-04-04 22:03:53 +07:00
connect (qApp, SIGNAL (aboutToQuit ()), translatorThread, SLOT (quit ()));
2013-11-26 13:44:00 +07:00
connect (translator, SIGNAL (translated (ProcessingItem)),
SLOT (showResult (ProcessingItem)));
2013-11-24 19:43:37 +07:00
connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ()));
2013-11-26 13:44:00 +07:00
resultDialog_->setWindowIcon (trayIcon_->icon ());
2013-11-24 19:43:37 +07:00
2013-11-24 20:06:19 +07:00
connect (trayIcon_, SIGNAL (activated (QSystemTrayIcon::ActivationReason)),
SLOT (processTrayAction (QSystemTrayIcon::ActivationReason)));
trayIcon_->setContextMenu (trayContextMenu ());
trayIcon_->show ();
2013-11-23 13:48:34 +07:00
2013-11-24 19:43:37 +07:00
applySettings ();
2013-11-23 13:48:34 +07:00
}
2015-09-23 01:41:08 +07:00
QMenu * Manager::trayContextMenu () {
QMenu *menu = new QMenu ();
2013-11-24 19:43:37 +07:00
captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ()));
2015-09-23 01:41:08 +07:00
QMenu *translateMenu = menu->addMenu (tr ("Перевод"));
repeatAction_ = translateMenu->addAction (tr ("Повторить"), this,
SLOT (showLast ()));
clipboardAction_ = translateMenu->addAction (tr ("Скопировать"), this,
SLOT (copyLastToClipboard ()));
2013-11-23 13:48:34 +07:00
menu->addAction (tr ("Настройки"), this, SLOT (settings ()));
2013-11-24 20:06:19 +07:00
menu->addAction (tr ("О программе"), this, SLOT (about ()));
2013-11-23 13:48:34 +07:00
menu->addAction (tr ("Выход"), this, SLOT (close ()));
return menu;
}
void Manager::setActionsEnabled (bool isEnabled) {
captureAction_->setEnabled (isEnabled);
repeatAction_->setEnabled (isEnabled);
clipboardAction_->setEnabled (isEnabled);
}
2015-09-23 01:41:08 +07:00
void Manager::applySettings () {
2013-11-24 19:43:37 +07:00
QSettings settings;
settings.beginGroup (settings_names::guiGroup);
QString captureHotkey = settings.value (settings_names::captureHotkey,
settings_values::captureHotkey).toString ();
2013-11-24 19:43:37 +07:00
Q_CHECK_PTR (captureAction_);
GlobalActionHelper::removeGlobal (captureAction_);
captureAction_->setShortcut (captureHotkey);
GlobalActionHelper::makeGlobal (captureAction_);
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 ();
2014-04-04 21:39:10 +07:00
Q_CHECK_PTR (dictionary_);
dictionary_->updateAvailableOcrLanguages ();
2013-11-24 20:06:19 +07:00
}
2015-09-23 01:41:08 +07:00
Manager::~Manager () {
2013-11-24 19:43:37 +07:00
}
2015-09-23 01:41:08 +07:00
void Manager::capture () {
QList<QScreen *> screens = QApplication::screens ();
foreach (QScreen * screen, screens) {
QRect geometry = screen->availableGeometry ();
QPixmap pixmap = screen->grabWindow (0, geometry.x (), geometry.y (),
geometry.width (), geometry.height ());
QString name = screen->name ();
if (!selections_.contains (name)) {
SelectionDialog *selection = new SelectionDialog (*dictionary_);
selection->setWindowIcon (trayIcon_->icon ());
connect (this, SIGNAL (closeSelections ()), selection, SLOT (close ()));
connect (this, SIGNAL (settingsEdited ()), selection, SLOT (updateMenu ()));
connect (selection, SIGNAL (selected (ProcessingItem)), SIGNAL (selected (ProcessingItem)));
connect (selection, SIGNAL (selected (ProcessingItem)), SIGNAL (closeSelections ()));
connect (selection, SIGNAL (rejected ()), SIGNAL (closeSelections ()));
selections_[name] = selection;
}
SelectionDialog *selection = selections_[name];
selection->setPixmap (pixmap, geometry);
}
2013-11-23 13:48:34 +07:00
}
2015-09-23 01:41:08 +07:00
void Manager::settings () {
2014-04-04 21:39:10 +07:00
SettingsEditor editor (*dictionary_);
2013-11-23 13:48:34 +07:00
editor.setWindowIcon (trayIcon_->icon ());
2013-11-24 19:43:37 +07:00
connect (&editor, SIGNAL (settingsEdited ()), SIGNAL (settingsEdited ()));
setActionsEnabled (false);
2013-11-23 13:48:34 +07:00
editor.exec ();
setActionsEnabled (true);
2013-11-23 13:48:34 +07:00
}
2015-09-23 01:41:08 +07:00
void Manager::close () {
2013-11-23 13:48:34 +07:00
QApplication::quit ();
}
2015-09-23 01:41:08 +07:00
void Manager::about () {
2015-06-30 00:26:57 +07:00
QString version = "1.2.3";
2015-09-23 01:41:08 +07:00
QString text = tr ("Программа для распознавания текста на экране.\n" \
2013-11-24 20:06:19 +07:00
"Создана с использованием Qt, tesseract-ocr, Google Translate.\n"
2015-06-30 00:26:57 +07:00
"Автор: Gres (translator@gres.biz)\n"
"Версия: %1 от %2 %3").arg (version)
2015-09-23 01:41:08 +07:00
.arg (__DATE__).arg (__TIME__);
2013-11-24 20:06:19 +07:00
QMessageBox message (QMessageBox::Information, tr ("О программе"), text,
QMessageBox::Ok);
message.setIconPixmap (trayIcon_->icon ().pixmap (QSize (64, 64)));
message.exec ();
}
2015-09-23 01:41:08 +07:00
void Manager::processTrayAction (QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
showLast ();
}
2015-09-23 01:41:08 +07:00
else if (reason == QSystemTrayIcon::MiddleClick) {
copyLastToClipboard ();
}
}
2015-09-23 01:41:08 +07:00
void Manager::showLast () {
if (lastItem_.isValid ()) {
showResult (lastItem_);
}
}
2015-09-23 01:41:08 +07:00
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);
}
}
2015-09-23 01:41:08 +07:00
void Manager::showResult (ProcessingItem item) {
2015-06-30 00:26:33 +07:00
ST_ASSERT (item.isValid ());
lastItem_ = item;
2015-09-23 01:41:08 +07:00
if (useResultDialog_) {
resultDialog_->showResult (item);
}
2015-09-23 01:41:08 +07:00
else {
QString message = item.recognized + " - " + item.translated;
trayIcon_->showMessage (tr ("Перевод"), message, QSystemTrayIcon::Information);
}
2013-11-24 19:43:37 +07:00
}
2013-11-23 13:48:34 +07:00
2015-09-23 01:41:08 +07:00
void Manager::showError (QString text) {
2013-11-24 19:43:37 +07:00
qCritical () << text;
trayIcon_->showMessage (tr ("Ошибка"), text, QSystemTrayIcon::Critical);
2013-11-23 13:48:34 +07:00
}