ScreenTranslator/Manager.cpp

154 lines
4.7 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 <QThread>
2013-11-24 19:43:37 +07:00
#include <QSettings>
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-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),
captureAction_ (NULL)
2013-11-23 13:48:34 +07:00
{
GlobalActionHelper::init ();
2013-11-23 13:48:34 +07:00
selection_->setWindowIcon (trayIcon_->icon ());
connect (this, SIGNAL (showPixmap (QPixmap)),
selection_, SLOT (setPixmap (QPixmap)));
Recognizer* recognizer = new Recognizer;
2013-11-23 13:48:34 +07:00
connect (selection_, SIGNAL (selected (QPixmap)),
recognizer, SLOT (recognize (QPixmap)));
2013-11-24 19:43:37 +07:00
connect (recognizer, SIGNAL (error (QString)),
SLOT (showError (QString)));
QThread* recognizerThread = new QThread (this);
recognizer->moveToThread (recognizerThread);
recognizerThread->start ();
2013-11-23 13:48:34 +07:00
Translator* translator = new Translator;
connect (recognizer, SIGNAL (recognized (QString)),
translator, SLOT (translate (QString)));
2013-11-24 19:43:37 +07:00
connect (translator, SIGNAL (error (QString)),
SLOT (showError (QString)));
QThread* translatorThread = new QThread (this);
translator->moveToThread (translatorThread);
translatorThread->start ();
connect (translator, SIGNAL (translated (QString, QString)),
SLOT (showTranslation (QString, QString)));
2013-11-24 19:43:37 +07:00
connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ()));
connect (this, SIGNAL (settingsEdited ()), recognizer, SLOT (applySettings ()));
connect (this, SIGNAL (settingsEdited ()), translator, SLOT (applySettings ()));
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
}
QMenu*Manager::trayContextMenu()
{
QMenu* menu = new QMenu ();
2013-11-24 19:43:37 +07:00
captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ()));
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,
settings_values::captureHotkey).
toString ();
Q_CHECK_PTR (captureAction_);
GlobalActionHelper::removeGlobal (captureAction_);
captureAction_->setShortcut (captureHotkey);
GlobalActionHelper::makeGlobal (captureAction_);
}
2013-11-24 20:06:19 +07:00
void Manager::processTrayAction(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger)
{
if (!lastMessage_.isEmpty ())
{
trayIcon_->showMessage (tr ("Последний перевод"), lastMessage_,
QSystemTrayIcon::Information);
}
}
}
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"
"Автор: Gres (dariusiii@qip.ru)");
QMessageBox message (QMessageBox::Information, tr ("О программе"), text,
QMessageBox::Ok);
message.setIconPixmap (trayIcon_->icon ().pixmap (QSize (64, 64)));
message.exec ();
}
void Manager::showTranslation(QString sourceText, QString translatedText)
2013-11-23 13:48:34 +07:00
{
2013-11-24 20:06:19 +07:00
lastMessage_ = sourceText + " - " + translatedText;
2013-11-24 19:43:37 +07:00
qDebug () << sourceText << translatedText;
2013-11-24 20:06:19 +07:00
trayIcon_->showMessage (tr ("Перевод"), lastMessage_, 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
}