2013-11-23 13:48:34 +07:00
|
|
|
|
#include "Manager.h"
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
|
#include <QScreen>
|
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>
|
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"
|
|
|
|
|
#include "Translator.h"
|
2013-11-26 13:44:00 +07:00
|
|
|
|
#include "ResultDialog.h"
|
2013-11-23 13:48:34 +07:00
|
|
|
|
|
|
|
|
|
Manager::Manager(QObject *parent) :
|
|
|
|
|
QObject(parent),
|
|
|
|
|
trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)),
|
2013-11-24 19:43:37 +07:00
|
|
|
|
selection_ (new SelectionDialog),
|
2013-11-26 13:44:00 +07:00
|
|
|
|
resultDialog_ (new ResultDialog),
|
2013-11-26 23:59:47 +07:00
|
|
|
|
captureAction_ (NULL), repeatAction_ (NULL), clipboardAction_ (NULL),
|
|
|
|
|
useResultDialog_ (true)
|
2013-11-23 13:48:34 +07:00
|
|
|
|
{
|
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
|
2013-11-23 14:00:22 +07:00
|
|
|
|
Recognizer* recognizer = new Recognizer;
|
2013-11-26 13:44:00 +07:00
|
|
|
|
connect (selection_, SIGNAL (selected (ProcessingItem)),
|
|
|
|
|
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 ()));
|
2013-11-23 14:00:22 +07:00
|
|
|
|
QThread* recognizerThread = new QThread (this);
|
|
|
|
|
recognizer->moveToThread (recognizerThread);
|
|
|
|
|
recognizerThread->start ();
|
2013-11-23 13:48:34 +07:00
|
|
|
|
|
2013-11-26 13:44:00 +07:00
|
|
|
|
|
|
|
|
|
// Translator
|
2013-11-23 14:00:22 +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 ()));
|
2013-11-23 14:00:22 +07:00
|
|
|
|
QThread* translatorThread = new QThread (this);
|
|
|
|
|
translator->moveToThread (translatorThread);
|
|
|
|
|
translatorThread->start ();
|
|
|
|
|
|
2013-11-26 13:44:00 +07:00
|
|
|
|
connect (translator, SIGNAL (translated (ProcessingItem)),
|
|
|
|
|
SLOT (showResult (ProcessingItem)));
|
2013-11-23 14:00:22 +07:00
|
|
|
|
|
2013-11-26 13:44:00 +07:00
|
|
|
|
connect (this, SIGNAL (showPixmap (QPixmap)),
|
|
|
|
|
selection_, SLOT (setPixmap (QPixmap)));
|
2013-11-24 19:43:37 +07:00
|
|
|
|
|
|
|
|
|
connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ()));
|
2013-11-26 13:44:00 +07:00
|
|
|
|
selection_->setWindowIcon (trayIcon_->icon ());
|
|
|
|
|
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)));
|
|
|
|
|
|
2013-11-23 14:00:22 +07:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMenu*Manager::trayContextMenu()
|
|
|
|
|
{
|
|
|
|
|
QMenu* menu = new QMenu ();
|
2013-11-24 19:43:37 +07:00
|
|
|
|
captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ()));
|
2013-11-26 23:59:47 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-24 19:43:37 +07:00
|
|
|
|
void Manager::applySettings()
|
|
|
|
|
{
|
|
|
|
|
QSettings settings;
|
|
|
|
|
settings.beginGroup (settings_names::guiGroup);
|
|
|
|
|
QString captureHotkey = settings.value (settings_names::captureHotkey,
|
2013-11-26 23:59:47 +07:00
|
|
|
|
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_);
|
|
|
|
|
|
2013-11-26 23:59:47 +07:00
|
|
|
|
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 ();
|
2013-11-24 20:06:19 +07:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-24 19:43:37 +07:00
|
|
|
|
Manager::~Manager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-23 13:48:34 +07:00
|
|
|
|
void Manager::capture()
|
|
|
|
|
{
|
|
|
|
|
QList<QScreen*> screens = QApplication::screens ();
|
|
|
|
|
Q_ASSERT (!screens.isEmpty ());
|
|
|
|
|
QScreen* screen = screens.first ();
|
|
|
|
|
Q_CHECK_PTR (screen);
|
|
|
|
|
WId desktopId = QApplication::desktop ()->winId ();
|
|
|
|
|
QPixmap pixmap = screen->grabWindow (desktopId);
|
|
|
|
|
Q_ASSERT (!pixmap.isNull ());
|
|
|
|
|
emit showPixmap (pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Manager::settings()
|
|
|
|
|
{
|
|
|
|
|
SettingsEditor editor;
|
|
|
|
|
editor.setWindowIcon (trayIcon_->icon ());
|
2013-11-24 19:43:37 +07:00
|
|
|
|
connect (&editor, SIGNAL (settingsEdited ()), SIGNAL (settingsEdited ()));
|
2013-11-23 13:48:34 +07:00
|
|
|
|
editor.exec ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Manager::close()
|
|
|
|
|
{
|
|
|
|
|
QApplication::quit ();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-24 20:06:19 +07:00
|
|
|
|
void Manager::about()
|
|
|
|
|
{
|
|
|
|
|
QString text = tr ("Программа для распознавания текста на экране.\n"\
|
|
|
|
|
"Создана с использованием Qt, tesseract-ocr, Google Translate.\n"
|
2013-11-25 00:23:21 +07:00
|
|
|
|
"Автор: Gres (onemoregres@gmail.com)");
|
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 ();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 23:59:47 +07:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 13:44:00 +07:00
|
|
|
|
void Manager::showResult(ProcessingItem item)
|
2013-11-23 13:48:34 +07:00
|
|
|
|
{
|
2013-11-26 23:59:47 +07:00
|
|
|
|
Q_ASSERT (item.isValid ());
|
|
|
|
|
lastItem_ = item;
|
|
|
|
|
if (useResultDialog_)
|
|
|
|
|
{
|
|
|
|
|
resultDialog_->showResult (item);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2013-11-24 19:43:37 +07:00
|
|
|
|
void Manager::showError(QString text)
|
|
|
|
|
{
|
|
|
|
|
qCritical () << text;
|
|
|
|
|
trayIcon_->showMessage (tr ("Ошибка"), text, QSystemTrayIcon::Critical);
|
2013-11-23 13:48:34 +07:00
|
|
|
|
}
|