ScreenTranslator/src/manager.cpp

285 lines
6.7 KiB
C++
Raw Normal View History

2020-01-27 02:01:08 +07:00
#include "manager.h"
2020-02-21 00:45:53 +07:00
#include "capturer.h"
#include "corrector.h"
#include "debug.h"
#include "recognizer.h"
#include "representer.h"
#include "settingseditor.h"
#include "task.h"
#include "translator.h"
#include "trayicon.h"
2020-03-15 18:10:26 +07:00
#include "updates.h"
2013-11-23 13:48:34 +07:00
#include <QApplication>
2013-11-24 20:06:19 +07:00
#include <QMessageBox>
2020-03-07 00:53:53 +07:00
#include <QNetworkProxy>
2013-11-23 13:48:34 +07:00
2020-03-15 18:10:26 +07:00
namespace
{
#ifdef DEVELOP
const auto updatesUrl = "http://localhost:8081/updates.json";
#else
const auto updatesUrl =
"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/"
"updates.json";
#endif
} // namespace
2020-02-21 00:45:53 +07:00
Manager::Manager()
2020-03-21 17:03:58 +07:00
: settings_(std::make_unique<Settings>())
, updater_(std::make_unique<update::Loader>(QUrl(updatesUrl)))
2020-02-21 00:45:53 +07:00
{
2020-03-21 17:03:58 +07:00
SOFT_ASSERT(settings_, return );
tray_ = std::make_unique<TrayIcon>(*this, *settings_);
capturer_ = std::make_unique<Capturer>(*this, *settings_);
recognizer_ = std::make_unique<Recognizer>(*this, *settings_);
translator_ = std::make_unique<Translator>(*this, *settings_);
corrector_ = std::make_unique<Corrector>(*this, *settings_);
representer_ = std::make_unique<Representer>(*this, *tray_, *settings_);
2013-11-23 13:48:34 +07:00
2020-02-21 00:45:53 +07:00
qRegisterMetaType<TaskPtr>();
2013-11-23 13:48:34 +07:00
2020-03-21 17:03:58 +07:00
settings_->load();
updateSettings();
2020-03-09 15:29:51 +07:00
2020-03-21 17:03:58 +07:00
if (settings_->showMessageOnStart)
2020-03-09 15:29:51 +07:00
tray_->showInformation(QObject::tr("Screen translator started"));
2020-03-15 18:10:26 +07:00
QObject::connect(updater_.get(), &update::Loader::error, //
tray_.get(), &TrayIcon::showError);
QObject::connect(updater_.get(), &update::Loader::updated, //
tray_.get(), [this] {
tray_->showInformation(QObject::tr("Update completed"));
});
2020-03-17 01:44:33 +07:00
QObject::connect(updater_.get(), &update::Loader::updatesAvailable, //
tray_.get(), [this] {
tray_->showInformation(QObject::tr("Updates available"));
});
}
2020-03-18 01:32:36 +07:00
Manager::~Manager()
{
2020-03-21 17:03:58 +07:00
SOFT_ASSERT(settings_, return );
if (updateAutoChecker_ && updateAutoChecker_->isLastCheckDateChanged()) {
settings_->lastUpdateCheck = updateAutoChecker_->lastCheckDate();
settings_->saveLastUpdateCheck();
}
2020-03-18 01:32:36 +07:00
}
2013-11-24 19:43:37 +07:00
2020-03-21 17:03:58 +07:00
void Manager::updateSettings()
2020-02-21 00:45:53 +07:00
{
LTRACE() << "updateSettings";
2020-03-21 17:03:58 +07:00
SOFT_ASSERT(settings_, return );
setupProxy(*settings_);
setupUpdates(*settings_);
2020-03-15 18:10:26 +07:00
2020-03-21 17:03:58 +07:00
tray_->updateSettings();
capturer_->updateSettings();
recognizer_->updateSettings();
translator_->updateSettings();
corrector_->updateSettings();
representer_->updateSettings();
2020-02-21 00:45:53 +07:00
}
2020-03-07 00:53:53 +07:00
void Manager::setupProxy(const Settings &settings)
{
if (settings.proxyType == ProxyType::System) {
QNetworkProxyFactory::setUseSystemConfiguration(true);
return;
}
QNetworkProxyFactory::setUseSystemConfiguration(false);
if (settings.proxyType == ProxyType::Disabled) {
QNetworkProxy::setApplicationProxy({});
return;
}
QNetworkProxy proxy;
using T = QNetworkProxy::ProxyType;
proxy.setType(settings.proxyType == ProxyType::Socks5 ? T::Socks5Proxy
: T::HttpProxy);
proxy.setHostName(settings.proxyHostName);
proxy.setPort(settings.proxyPort);
proxy.setUser(settings.proxyUser);
proxy.setPassword(settings.proxyPassword);
QNetworkProxy::setApplicationProxy(proxy);
}
void Manager::setupUpdates(const Settings &settings)
{
updater_->model()->setExpansions({
{"$translators$", settings.translatorsDir},
{"$tessdata$", settings.tessdataPath},
});
if (settings.autoUpdateIntervalDays > 0) {
updateAutoChecker_ = std::make_unique<update::AutoChecker>(*updater_);
updateAutoChecker_->setLastCheckDate(settings.lastUpdateCheck);
updateAutoChecker_->setCheckIntervalDays(settings.autoUpdateIntervalDays);
} else {
updateAutoChecker_.reset();
}
}
2020-02-21 00:45:53 +07:00
void Manager::finishTask(const TaskPtr &task)
{
SOFT_ASSERT(task, return );
LTRACE() << "finishTask" << task->captured << task->error;
2020-02-21 00:45:53 +07:00
--activeTaskCount_;
tray_->setActiveTaskCount(activeTaskCount_);
2020-02-21 00:45:53 +07:00
if (!task->isValid()) {
tray_->showError(task->error);
tray_->setTaskActionsEnabled(false);
2020-02-21 00:45:53 +07:00
return;
2015-10-10 18:45:57 +07:00
}
2020-02-21 00:45:53 +07:00
tray_->showSuccess();
}
2014-04-04 21:39:10 +07:00
2020-02-21 00:45:53 +07:00
void Manager::captured(const TaskPtr &task)
{
tray_->blockActions(false);
2020-02-21 00:45:53 +07:00
SOFT_ASSERT(task, return );
LTRACE() << "captured" << task->captured << task->error;
2020-02-21 00:45:53 +07:00
++activeTaskCount_;
tray_->setActiveTaskCount(activeTaskCount_);
2013-11-24 20:06:19 +07:00
2020-02-21 00:45:53 +07:00
if (!task->isValid()) {
finishTask(task);
return;
}
2020-02-21 00:45:53 +07:00
recognizer_->recognize(task);
}
2020-02-21 00:45:53 +07:00
void Manager::captureCanceled()
{
tray_->blockActions(false);
2013-11-24 19:43:37 +07:00
}
2020-02-21 00:45:53 +07:00
void Manager::recognized(const TaskPtr &task)
{
SOFT_ASSERT(task, return );
LTRACE() << "recognized" << task->recognized << task->error;
2020-02-21 00:45:53 +07:00
if (!task->isValid()) {
finishTask(task);
return;
}
2020-02-21 00:45:53 +07:00
corrector_->correct(task);
}
2020-02-21 00:45:53 +07:00
void Manager::corrected(const TaskPtr &task)
{
SOFT_ASSERT(task, return );
LTRACE() << "corrected" << task->recognized << task->error;
if (!task->isValid()) {
finishTask(task);
return;
}
2013-11-23 13:48:34 +07:00
2020-02-21 00:45:53 +07:00
if (!task->targetLanguage.isEmpty())
translator_->translate(task);
else
translated(task);
2013-11-23 13:48:34 +07:00
}
2020-02-21 00:45:53 +07:00
void Manager::translated(const TaskPtr &task)
{
SOFT_ASSERT(task, return );
LTRACE() << "translated" << task->recognized << task->error;
2013-11-23 13:48:34 +07:00
2020-02-21 00:45:53 +07:00
finishTask(task);
2013-11-24 20:06:19 +07:00
2020-02-21 00:45:53 +07:00
representer_->represent(task);
2020-03-28 16:45:13 +07:00
tray_->setTaskActionsEnabled(!task->isNull());
}
void Manager::applySettings(const Settings &settings)
{
2020-03-21 17:03:58 +07:00
SOFT_ASSERT(settings_, return );
*settings_ = settings;
settings_->save();
updateSettings();
}
2020-02-21 00:45:53 +07:00
void Manager::fatalError(const QString &text)
{
tray_->blockActions(false);
tray_->showFatalError(text);
}
2020-02-21 00:45:53 +07:00
void Manager::capture()
{
SOFT_ASSERT(capturer_, return );
tray_->blockActions(true);
capturer_->capture();
tray_->setRepeatCaptureEnabled(true);
}
2020-02-21 00:45:53 +07:00
void Manager::repeatCapture()
{
SOFT_ASSERT(capturer_, return );
tray_->blockActions(true);
capturer_->repeatCapture();
}
2020-02-21 00:45:53 +07:00
void Manager::settings()
{
SettingsEditor editor(*this, *updater_);
2020-02-21 00:45:53 +07:00
2020-03-21 17:03:58 +07:00
SOFT_ASSERT(settings_, return );
editor.setSettings(*settings_);
2020-02-21 00:45:53 +07:00
tray_->blockActions(true);
auto result = editor.exec();
tray_->blockActions(false);
if (result != QDialog::Accepted)
return;
2020-02-21 00:45:53 +07:00
tray_->resetFatalError();
2020-03-21 17:03:58 +07:00
const auto edited = editor.settings();
applySettings(edited);
2013-11-24 19:43:37 +07:00
}
2013-11-23 13:48:34 +07:00
void Manager::showLast()
2020-02-21 00:45:53 +07:00
{
SOFT_ASSERT(representer_, return );
representer_->showLast();
}
2020-02-21 00:45:53 +07:00
void Manager::copyLastToClipboard()
{
SOFT_ASSERT(representer_, return );
representer_->clipboardLast();
2013-11-23 13:48:34 +07:00
}
2020-02-21 00:45:53 +07:00
void Manager::about()
{
auto text =
QObject::tr(R"(Optical character recognition (OCR) and translation tool
Author: Gres (translator@gres.biz)
Version: %1)")
.arg(QApplication::applicationVersion());
QMessageBox message(QMessageBox::Information, QObject::tr("About"), text,
QMessageBox::Ok);
message.setIconPixmap(QIcon(":/icons/app.png").pixmap(QSize(64, 64)));
message.exec();
}
2020-02-21 00:45:53 +07:00
void Manager::quit()
{
QApplication::quit();
}