2013-11-23 13:48:34 +07:00
|
|
|
|
#include "Manager.h"
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
|
#include <QScreen>
|
2015-09-28 00:50:19 +07:00
|
|
|
|
#include <QDesktopWidget>
|
2013-11-23 14:00:22 +07:00
|
|
|
|
#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>
|
2015-10-02 00:49:53 +07:00
|
|
|
|
#include <QInputDialog>
|
2015-10-10 18:45:57 +07:00
|
|
|
|
#include <QNetworkProxy>
|
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"
|
2013-11-23 14:00:22 +07:00
|
|
|
|
#include "Recognizer.h"
|
2015-10-08 22:38:16 +07:00
|
|
|
|
#include "WebTranslator.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"
|
2015-10-10 18:45:57 +07:00
|
|
|
|
#include "Utils.h"
|
2013-11-23 13:48:34 +07:00
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
Manager::Manager (QObject *parent) :
|
|
|
|
|
QObject (parent),
|
2015-10-10 23:07:25 +07:00
|
|
|
|
trayIcon_ (new QSystemTrayIcon (this)),
|
2014-04-04 21:39:10 +07:00
|
|
|
|
dictionary_ (new LanguageHelper),
|
2015-10-01 00:38:55 +07:00
|
|
|
|
resultDialog_ (new ResultDialog (*dictionary_)),
|
2015-09-29 22:14:56 +07:00
|
|
|
|
captureAction_ (NULL), repeatCaptureAction_ (NULL),
|
|
|
|
|
repeatAction_ (NULL), clipboardAction_ (NULL),
|
2015-10-10 23:07:25 +07:00
|
|
|
|
useResultDialog_ (true), doTranslation_ (true), itemProcessingCount_ (0) {
|
|
|
|
|
updateNormalIcon ();
|
2013-11-23 14:00:22 +07:00
|
|
|
|
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;
|
2015-09-30 23:49:10 +07:00
|
|
|
|
connect (this, SIGNAL (requestRecognize (ProcessingItem)),
|
2013-11-26 13:44:00 +07:00
|
|
|
|
recognizer, SLOT (recognize (ProcessingItem)));
|
2015-09-29 19:30:04 +07:00
|
|
|
|
connect (recognizer, SIGNAL (recognized (ProcessingItem)),
|
2015-10-10 03:56:48 +07:00
|
|
|
|
this, SIGNAL (requestTranslate (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);
|
2015-09-29 02:17:24 +07:00
|
|
|
|
threads_ << recognizerThread;
|
2013-11-23 14:00:22 +07:00
|
|
|
|
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-10-08 22:38:16 +07:00
|
|
|
|
WebTranslator *translator = new WebTranslator;
|
2015-09-29 19:30:04 +07:00
|
|
|
|
connect (this, SIGNAL (requestTranslate (ProcessingItem)),
|
2013-11-26 13:44:00 +07:00
|
|
|
|
translator, SLOT (translate (ProcessingItem)));
|
2015-09-29 19:30:04 +07:00
|
|
|
|
connect (translator, SIGNAL (translated (ProcessingItem)),
|
|
|
|
|
SLOT (showResult (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 ()));
|
2013-11-23 14:00:22 +07:00
|
|
|
|
|
2013-11-24 19:43:37 +07:00
|
|
|
|
connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ()));
|
2015-10-01 00:38:55 +07:00
|
|
|
|
|
2013-11-26 13:44:00 +07:00
|
|
|
|
resultDialog_->setWindowIcon (trayIcon_->icon ());
|
2015-10-01 00:38:55 +07:00
|
|
|
|
connect (this, SIGNAL (settingsEdited ()), resultDialog_, SLOT (applySettings ()));
|
|
|
|
|
connect (resultDialog_, SIGNAL (requestRecognize (ProcessingItem)),
|
|
|
|
|
this, SIGNAL (requestRecognize (ProcessingItem)));
|
2015-10-01 01:25:45 +07:00
|
|
|
|
connect (resultDialog_, SIGNAL (requestTranslate (ProcessingItem)),
|
|
|
|
|
this, SIGNAL (requestTranslate (ProcessingItem)));
|
2015-10-01 01:04:25 +07:00
|
|
|
|
connect (resultDialog_, SIGNAL (requestClipboard ()), SLOT (copyLastToClipboard ()));
|
2015-10-10 04:20:14 +07:00
|
|
|
|
connect (resultDialog_, SIGNAL (requestImageClipboard ()),
|
|
|
|
|
SLOT (copyLastImageToClipboard ()));
|
2015-10-02 00:49:53 +07:00
|
|
|
|
connect (resultDialog_, SIGNAL (requestEdition (ProcessingItem)),
|
|
|
|
|
this, SLOT (editRecognized (ProcessingItem)));
|
2013-11-26 13:44:00 +07:00
|
|
|
|
|
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)));
|
|
|
|
|
|
2013-11-23 14:00:22 +07:00
|
|
|
|
trayIcon_->setContextMenu (trayContextMenu ());
|
2015-09-29 22:14:56 +07:00
|
|
|
|
updateActionsState ();
|
2013-11-23 14:00:22 +07:00
|
|
|
|
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-29 22:14:56 +07:00
|
|
|
|
repeatCaptureAction_ = menu->addAction (tr ("Повторить захват"),
|
|
|
|
|
this, SLOT (repeatCapture ()));
|
2015-09-29 19:37:47 +07:00
|
|
|
|
QMenu *translateMenu = menu->addMenu (tr ("Результат"));
|
|
|
|
|
repeatAction_ = translateMenu->addAction (tr ("Показать"), this,
|
2013-11-26 23:59:47 +07:00
|
|
|
|
SLOT (showLast ()));
|
2015-09-29 19:37:47 +07:00
|
|
|
|
clipboardAction_ = translateMenu->addAction (tr ("В буфер"), this,
|
2013-11-26 23:59:47 +07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-29 22:14:56 +07:00
|
|
|
|
void Manager::updateActionsState (bool isEnabled) {
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
// Avoid unneeded tray blinking (required to update context menu).
|
|
|
|
|
QList<QAction *> actions;
|
|
|
|
|
actions << captureAction_ << repeatCaptureAction_ << repeatAction_ << clipboardAction_;
|
|
|
|
|
QList<bool> states;
|
|
|
|
|
foreach (const QAction * action, actions) {
|
|
|
|
|
states << action->isEnabled ();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-09-27 22:58:50 +07:00
|
|
|
|
captureAction_->setEnabled (isEnabled);
|
2015-09-29 22:14:56 +07:00
|
|
|
|
repeatCaptureAction_->setEnabled (isEnabled && !selections_.isEmpty ());
|
2015-10-01 00:49:05 +07:00
|
|
|
|
const ProcessingItem &lastItem = resultDialog_->item ();
|
|
|
|
|
repeatAction_->setEnabled (isEnabled && lastItem.isValid ());
|
|
|
|
|
clipboardAction_->setEnabled (isEnabled && lastItem.isValid ());
|
2015-09-29 22:14:56 +07:00
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
for (int i = 0, end = actions.size (); i < end; ++i) {
|
|
|
|
|
if (states.at (i) != actions.at (i)->isEnabled ()) {
|
|
|
|
|
trayIcon_->hide ();
|
|
|
|
|
trayIcon_->show ();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-09-27 22:58:50 +07:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
void Manager::applySettings () {
|
2015-09-29 02:57:51 +07:00
|
|
|
|
#define GET(NAME) settings.value (settings_names::NAME, settings_values::NAME)
|
2013-11-24 19:43:37 +07:00
|
|
|
|
QSettings settings;
|
|
|
|
|
settings.beginGroup (settings_names::guiGroup);
|
2015-09-29 02:57:51 +07:00
|
|
|
|
|
2013-11-24 19:43:37 +07:00
|
|
|
|
Q_CHECK_PTR (captureAction_);
|
|
|
|
|
GlobalActionHelper::removeGlobal (captureAction_);
|
2015-09-29 02:57:51 +07:00
|
|
|
|
captureAction_->setShortcut (GET (captureHotkey).toString ());
|
2013-11-24 19:43:37 +07:00
|
|
|
|
GlobalActionHelper::makeGlobal (captureAction_);
|
|
|
|
|
|
2015-09-29 22:14:56 +07:00
|
|
|
|
Q_CHECK_PTR (repeatCaptureAction_);
|
|
|
|
|
GlobalActionHelper::removeGlobal (repeatCaptureAction_);
|
|
|
|
|
repeatCaptureAction_->setShortcut (GET (repeatCaptureHotkey).toString ());
|
|
|
|
|
GlobalActionHelper::makeGlobal (repeatCaptureAction_);
|
|
|
|
|
|
2013-11-26 23:59:47 +07:00
|
|
|
|
Q_CHECK_PTR (repeatAction_);
|
|
|
|
|
GlobalActionHelper::removeGlobal (repeatAction_);
|
2015-09-29 02:57:51 +07:00
|
|
|
|
repeatAction_->setShortcut (GET (repeatHotkey).toString ());
|
2013-11-26 23:59:47 +07:00
|
|
|
|
GlobalActionHelper::makeGlobal (repeatAction_);
|
|
|
|
|
|
|
|
|
|
Q_CHECK_PTR (clipboardAction_);
|
|
|
|
|
GlobalActionHelper::removeGlobal (clipboardAction_);
|
2015-09-29 02:57:51 +07:00
|
|
|
|
clipboardAction_->setShortcut (GET (clipboardHotkey).toString ());
|
2013-11-26 23:59:47 +07:00
|
|
|
|
GlobalActionHelper::makeGlobal (clipboardAction_);
|
|
|
|
|
|
|
|
|
|
// Depends on SettingsEditor button indexes. 1==dialog
|
2015-09-29 02:57:51 +07:00
|
|
|
|
useResultDialog_ = GET (resultShowType).toBool ();
|
2015-10-10 18:45:57 +07:00
|
|
|
|
|
|
|
|
|
QNetworkProxy proxy = QNetworkProxy::applicationProxy ();
|
2015-10-11 22:33:19 +07:00
|
|
|
|
QList<int> proxyTypes = proxyTypeOrder ();
|
|
|
|
|
int proxyTypeIndex = std::min (GET (proxyType).toInt (), proxyTypes.size ());
|
|
|
|
|
proxy.setType (QNetworkProxy::ProxyType (proxyTypes.at (std::max (proxyTypeIndex, 0))));
|
2015-10-10 18:45:57 +07:00
|
|
|
|
proxy.setHostName (GET (proxyHostName).toString ());
|
|
|
|
|
proxy.setPort (GET (proxyPort).toInt ());
|
|
|
|
|
proxy.setUser (GET (proxyUser).toString ());
|
|
|
|
|
if (GET (proxySavePassword).toBool ()) {
|
|
|
|
|
proxy.setPassword (encode (GET (proxyPassword).toString ()));
|
|
|
|
|
}
|
|
|
|
|
QNetworkProxy::setApplicationProxy (proxy);
|
2015-09-29 19:30:04 +07:00
|
|
|
|
settings.endGroup ();
|
2014-04-04 21:39:10 +07:00
|
|
|
|
|
2015-10-11 01:04:28 +07:00
|
|
|
|
settings.beginGroup (settings_names::recogntionGroup);
|
|
|
|
|
defaultOrcLanguage_ = GET (ocrLanguage).toString ();
|
|
|
|
|
settings.endGroup ();
|
|
|
|
|
|
2015-10-08 22:38:16 +07:00
|
|
|
|
settings.beginGroup (settings_names::translationGroup);
|
|
|
|
|
defaultTranslationLanguage_ = GET (translationLanguage).toString ();
|
2015-10-10 03:56:48 +07:00
|
|
|
|
doTranslation_ = GET (doTranslation).toBool ();
|
2015-10-08 22:38:16 +07:00
|
|
|
|
settings.endGroup ();
|
|
|
|
|
|
2014-04-04 21:39:10 +07:00
|
|
|
|
Q_CHECK_PTR (dictionary_);
|
|
|
|
|
dictionary_->updateAvailableOcrLanguages ();
|
2015-09-29 02:57:51 +07:00
|
|
|
|
#undef GET
|
2013-11-24 20:06:19 +07:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
Manager::~Manager () {
|
2015-10-11 22:33:55 +07:00
|
|
|
|
foreach (SelectionDialog * selection, selections_.values ()) {
|
|
|
|
|
selection->hide ();
|
|
|
|
|
delete selection;
|
|
|
|
|
}
|
|
|
|
|
trayIcon_->hide ();
|
|
|
|
|
delete trayIcon_->contextMenu ();
|
2015-09-29 02:17:24 +07:00
|
|
|
|
foreach (QThread * thread, threads_) {
|
|
|
|
|
thread->quit ();
|
|
|
|
|
thread->wait (1000000);
|
|
|
|
|
}
|
2013-11-24 19:43:37 +07:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
void Manager::capture () {
|
|
|
|
|
QList<QScreen *> screens = QApplication::screens ();
|
2015-09-28 00:50:19 +07:00
|
|
|
|
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 ()));
|
2015-10-01 00:20:33 +07:00
|
|
|
|
connect (this, SIGNAL (settingsEdited ()), selection, SLOT (applySettings ()));
|
2015-09-30 23:49:10 +07:00
|
|
|
|
connect (selection, SIGNAL (selected (ProcessingItem)),
|
|
|
|
|
SLOT (handleSelection (ProcessingItem)));
|
2015-09-28 00:50:19 +07:00
|
|
|
|
connect (selection, SIGNAL (rejected ()), SIGNAL (closeSelections ()));
|
|
|
|
|
selections_[name] = selection;
|
|
|
|
|
}
|
|
|
|
|
SelectionDialog *selection = selections_[name];
|
|
|
|
|
selection->setPixmap (pixmap, geometry);
|
|
|
|
|
}
|
2015-09-29 22:14:56 +07:00
|
|
|
|
updateActionsState ();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-30 23:49:10 +07:00
|
|
|
|
void Manager::handleSelection (ProcessingItem item) {
|
2015-10-10 03:56:48 +07:00
|
|
|
|
bool altMod = item.modifiers & Qt::AltModifier;
|
2015-10-11 01:05:12 +07:00
|
|
|
|
bool doTranslation = (doTranslation_ && !altMod) || (!doTranslation_ && altMod);
|
|
|
|
|
if (doTranslation) {
|
2015-10-08 22:38:16 +07:00
|
|
|
|
item.translateLanguage = defaultTranslationLanguage_;
|
|
|
|
|
}
|
2015-10-11 01:04:28 +07:00
|
|
|
|
if (item.ocrLanguage.isEmpty ()) {
|
|
|
|
|
item.ocrLanguage = defaultOrcLanguage_;
|
|
|
|
|
}
|
2015-10-11 01:05:12 +07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-11 01:04:28 +07:00
|
|
|
|
if (item.sourceLanguage.isEmpty ()) {
|
|
|
|
|
item.sourceLanguage = dictionary_->ocrToTranslateCodes (item.ocrLanguage);
|
|
|
|
|
}
|
2015-09-30 23:49:10 +07:00
|
|
|
|
emit requestRecognize (item);
|
2015-10-10 23:07:25 +07:00
|
|
|
|
++itemProcessingCount_;
|
|
|
|
|
updateNormalIcon ();
|
2015-09-30 23:49:10 +07:00
|
|
|
|
if (!(item.modifiers & Qt::ControlModifier)) {
|
|
|
|
|
emit closeSelections ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-29 22:14:56 +07:00
|
|
|
|
void Manager::repeatCapture () {
|
|
|
|
|
if (selections_.isEmpty ()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QList<QScreen *> screens = QApplication::screens ();
|
|
|
|
|
foreach (QScreen * screen, screens) {
|
|
|
|
|
QString name = screen->name ();
|
|
|
|
|
if (!selections_.contains (name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SelectionDialog *selection = selections_[name];
|
|
|
|
|
selection->show ();
|
|
|
|
|
selection->activateWindow ();
|
|
|
|
|
}
|
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 ()));
|
2015-09-29 22:14:56 +07:00
|
|
|
|
updateActionsState (false);
|
2013-11-23 13:48:34 +07:00
|
|
|
|
editor.exec ();
|
2015-09-29 22:14:56 +07:00
|
|
|
|
updateActionsState (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-10-11 22:34:55 +07:00
|
|
|
|
QString version = "2.0.0";
|
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__);
|
2015-10-11 01:05:28 +07:00
|
|
|
|
QString tips = tr ("\n\nПодсказки.\n"
|
|
|
|
|
"Клик по иконке в трее:\n"
|
|
|
|
|
"* левой кнопкой - отобразить последний результат\n"
|
|
|
|
|
"* средней кнопкой - скопировать последний результат в буфер обмена\n"
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
"* двойной клик - повторный захват последнего экрана\n"
|
|
|
|
|
#endif
|
|
|
|
|
"\n"
|
|
|
|
|
"Захвата изображения при зажатых кнопках:\n"
|
|
|
|
|
"* Ctrl - не выходить из режима захвата\n"
|
|
|
|
|
"* Alt - выполнить перевод, если в настройках он выключен "
|
|
|
|
|
"(и наоборот, не выполнять, если включен)\n"
|
|
|
|
|
"");
|
|
|
|
|
|
|
|
|
|
QMessageBox message (QMessageBox::Information, tr ("О программе"), text + tips,
|
2013-11-24 20:06:19 +07:00
|
|
|
|
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) {
|
2015-09-29 22:14:56 +07:00
|
|
|
|
if (reason == QSystemTrayIcon::Trigger && repeatAction_->isEnabled ()) {
|
2013-11-26 23:59:47 +07:00
|
|
|
|
showLast ();
|
|
|
|
|
}
|
2015-09-29 22:14:56 +07:00
|
|
|
|
else if (reason == QSystemTrayIcon::MiddleClick && clipboardAction_->isEnabled ()) {
|
2013-11-26 23:59:47 +07:00
|
|
|
|
copyLastToClipboard ();
|
2015-10-01 01:05:02 +07:00
|
|
|
|
trayIcon_->showMessage (tr ("Результат"),
|
|
|
|
|
tr ("Последний результат был скопирован в буфер обмена."),
|
|
|
|
|
QSystemTrayIcon::Information);
|
2013-11-26 23:59:47 +07:00
|
|
|
|
}
|
2015-09-29 22:14:56 +07:00
|
|
|
|
else if (reason == QSystemTrayIcon::DoubleClick && repeatCaptureAction_->isEnabled ()) {
|
|
|
|
|
repeatCapture ();
|
|
|
|
|
}
|
2013-11-26 23:59:47 +07:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-02 00:49:53 +07:00
|
|
|
|
void Manager::editRecognized (ProcessingItem item) {
|
|
|
|
|
QString fixed = QInputDialog::getMultiLineText (
|
|
|
|
|
NULL, tr ("Правка"), tr ("Исправьте распознанный текст"), item.recognized);
|
|
|
|
|
if (!fixed.isEmpty ()) {
|
|
|
|
|
item.recognized = fixed;
|
2015-10-10 04:06:10 +07:00
|
|
|
|
emit requestTranslate (item);
|
2015-10-02 00:49:53 +07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
void Manager::showLast () {
|
2015-10-01 00:49:05 +07:00
|
|
|
|
const ProcessingItem &item = resultDialog_->item ();
|
|
|
|
|
if (item.isValid ()) {
|
2015-10-11 23:51:07 +07:00
|
|
|
|
++itemProcessingCount_;
|
2015-10-01 00:49:05 +07:00
|
|
|
|
showResult (item);
|
2013-11-26 23:59:47 +07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
void Manager::copyLastToClipboard () {
|
2015-10-01 00:49:05 +07:00
|
|
|
|
const ProcessingItem &item = resultDialog_->item ();
|
|
|
|
|
if (item.isValid ()) {
|
2015-09-23 01:41:08 +07:00
|
|
|
|
QClipboard *clipboard = QApplication::clipboard ();
|
2015-10-01 00:49:05 +07:00
|
|
|
|
QString message = item.recognized;
|
|
|
|
|
if (!item.translated.isEmpty ()) {
|
|
|
|
|
message += " - " + item.translated;
|
2015-09-29 19:37:47 +07:00
|
|
|
|
}
|
2013-11-26 23:59:47 +07:00
|
|
|
|
clipboard->setText (message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 04:20:14 +07:00
|
|
|
|
void Manager::copyLastImageToClipboard () {
|
|
|
|
|
const ProcessingItem &item = resultDialog_->item ();
|
|
|
|
|
if (item.isValid ()) {
|
|
|
|
|
QClipboard *clipboard = QApplication::clipboard ();
|
|
|
|
|
clipboard->setPixmap (item.source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 01:41:08 +07:00
|
|
|
|
void Manager::showResult (ProcessingItem item) {
|
2015-10-10 23:07:25 +07:00
|
|
|
|
--itemProcessingCount_;
|
|
|
|
|
if (!item.isValid ()) {
|
|
|
|
|
// delay because it can show error
|
|
|
|
|
QTimer::singleShot (3000, this, SLOT (updateNormalIcon ()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
changeIcon (IconTypeSuccess);
|
2015-09-23 01:41:08 +07:00
|
|
|
|
if (useResultDialog_) {
|
2013-11-26 23:59:47 +07:00
|
|
|
|
resultDialog_->showResult (item);
|
|
|
|
|
}
|
2015-09-23 01:41:08 +07:00
|
|
|
|
else {
|
2013-11-26 23:59:47 +07:00
|
|
|
|
QString message = item.recognized + " - " + item.translated;
|
2015-09-29 19:37:47 +07:00
|
|
|
|
trayIcon_->showMessage (tr ("Результат"), message, QSystemTrayIcon::Information);
|
2013-11-26 23:59:47 +07:00
|
|
|
|
}
|
2015-09-29 22:14:56 +07:00
|
|
|
|
updateActionsState ();
|
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;
|
2015-10-10 23:07:25 +07:00
|
|
|
|
changeIcon (IconTypeError);
|
2013-11-24 19:43:37 +07:00
|
|
|
|
trayIcon_->showMessage (tr ("Ошибка"), text, QSystemTrayIcon::Critical);
|
2013-11-23 13:48:34 +07:00
|
|
|
|
}
|
2015-10-10 23:07:25 +07:00
|
|
|
|
|
|
|
|
|
void Manager::changeIcon (int iconType, int timeoutMsec) {
|
|
|
|
|
QString fileName;
|
|
|
|
|
switch (iconType) {
|
|
|
|
|
case IconTypeSuccess:
|
|
|
|
|
fileName = ":/images/STIconGreen.png";
|
|
|
|
|
break;
|
|
|
|
|
case IconTypeError:
|
|
|
|
|
fileName = ":/images/STIconRed.png";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
trayIcon_->setIcon (QIcon (fileName));
|
|
|
|
|
if (timeoutMsec > 0) {
|
|
|
|
|
QTimer::singleShot (timeoutMsec, this, SLOT (updateNormalIcon ()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Manager::updateNormalIcon () {
|
|
|
|
|
QString fileName = itemProcessingCount_ > 0
|
|
|
|
|
? ":/images/STIconOrange.png" : ":/images/STIconBlue.png";
|
|
|
|
|
trayIcon_->setIcon (QIcon (fileName));
|
|
|
|
|
}
|