2020-02-21 00:45:53 +07:00
|
|
|
#include "trayicon.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "globalaction.h"
|
|
|
|
#include "manager.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2020-03-22 15:39:36 +07:00
|
|
|
using GlobalAction = service::GlobalAction;
|
|
|
|
|
2020-03-21 17:03:58 +07:00
|
|
|
TrayIcon::TrayIcon(Manager &manager, const Settings &settings)
|
2020-02-21 00:45:53 +07:00
|
|
|
: manager_(manager)
|
2020-03-21 17:03:58 +07:00
|
|
|
, settings_(settings)
|
2020-02-21 00:45:53 +07:00
|
|
|
, tray_(std::make_unique<QSystemTrayIcon>())
|
|
|
|
, iconUpdateTimer_(std::make_unique<QTimer>())
|
|
|
|
{
|
|
|
|
GlobalAction::init();
|
|
|
|
|
|
|
|
connect(tray_.get(), &QSystemTrayIcon::activated, //
|
|
|
|
this, &TrayIcon::handleIconClick);
|
|
|
|
|
|
|
|
iconUpdateTimer_->setSingleShot(true);
|
|
|
|
connect(iconUpdateTimer_.get(), &QTimer::timeout, //
|
|
|
|
this, &TrayIcon::updateIcon);
|
|
|
|
|
|
|
|
tray_->setContextMenu(contextMenu());
|
|
|
|
setIcon(Icon::Idle, Duration::Permanent);
|
|
|
|
updateActions();
|
|
|
|
tray_->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
TrayIcon::~TrayIcon() = default;
|
|
|
|
|
2020-03-21 17:03:58 +07:00
|
|
|
void TrayIcon::updateSettings()
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
QStringList failedActions;
|
2020-03-21 17:03:58 +07:00
|
|
|
if (!GlobalAction::update(captureAction_, settings_.captureHotkey))
|
|
|
|
failedActions << settings_.captureHotkey;
|
|
|
|
if (!GlobalAction::update(repeatCaptureAction_,
|
|
|
|
settings_.repeatCaptureHotkey))
|
|
|
|
failedActions << settings_.repeatCaptureHotkey;
|
|
|
|
if (!GlobalAction::update(showLastAction_, settings_.showLastHotkey))
|
|
|
|
failedActions << settings_.showLastHotkey;
|
|
|
|
if (!GlobalAction::update(clipboardAction_, settings_.clipboardHotkey))
|
|
|
|
failedActions << settings_.clipboardHotkey;
|
2020-03-31 00:03:00 +07:00
|
|
|
if (!GlobalAction::update(captureLockedAction_,
|
|
|
|
settings_.captureLockedHotkey))
|
|
|
|
failedActions << settings_.captureLockedHotkey;
|
2020-02-21 00:45:53 +07:00
|
|
|
|
|
|
|
if (!failedActions.isEmpty()) {
|
2020-05-09 17:10:49 +07:00
|
|
|
showError(tr("Failed to register global shortcuts:\n%1"
|
|
|
|
"\nMost likely they are already in use by another program")
|
2020-02-21 00:45:53 +07:00
|
|
|
.arg(failedActions.join('\n')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::blockActions(bool block)
|
|
|
|
{
|
|
|
|
isActionsBlocked_ = block;
|
|
|
|
updateActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::setTaskActionsEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
gotTask_ = isEnabled;
|
|
|
|
updateActions();
|
|
|
|
}
|
|
|
|
|
2020-03-31 00:03:00 +07:00
|
|
|
void TrayIcon::setCaptureLockedEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
canCaptureLocked_ = isEnabled;
|
|
|
|
updateActions();
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
void TrayIcon::setRepeatCaptureEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
canRepeatCapture_ = isEnabled;
|
|
|
|
updateActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::updateActions()
|
|
|
|
{
|
|
|
|
if (isActionsBlocked_) {
|
|
|
|
QVector<QAction *> blockable{captureAction_, repeatCaptureAction_,
|
2020-03-31 00:03:00 +07:00
|
|
|
showLastAction_, settingsAction_,
|
|
|
|
captureLockedAction_};
|
2020-02-21 00:45:53 +07:00
|
|
|
for (auto &action : blockable) action->setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
captureAction_->setEnabled(true);
|
|
|
|
settingsAction_->setEnabled(true);
|
|
|
|
|
|
|
|
QVector<QAction *> taskActions{showLastAction_, clipboardAction_};
|
|
|
|
for (auto &action : taskActions) action->setEnabled(gotTask_);
|
|
|
|
|
|
|
|
repeatCaptureAction_->setEnabled(canRepeatCapture_);
|
2020-03-31 00:03:00 +07:00
|
|
|
captureLockedAction_->setEnabled(canCaptureLocked_);
|
2020-02-21 00:45:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::setIcon(TrayIcon::Icon icon, Duration duration)
|
|
|
|
{
|
|
|
|
QMap<Icon, QString> icons{
|
|
|
|
{Icon::Idle, QStringLiteral(":icons/app.png")},
|
|
|
|
{Icon::Success, QStringLiteral(":icons/st_success.png")},
|
|
|
|
{Icon::Busy, QStringLiteral(":icons/st_busy.png")},
|
|
|
|
{Icon::Error, QStringLiteral(":icons/st_error.png")},
|
|
|
|
};
|
|
|
|
|
|
|
|
tray_->setIcon(QIcon(icons.value(icon)));
|
|
|
|
|
|
|
|
if (duration == Duration::Permanent) {
|
|
|
|
permanentIcon_ = icon;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto durationMsec = 3000;
|
|
|
|
iconUpdateTimer_->start(durationMsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::setActiveTaskCount(int count)
|
|
|
|
{
|
|
|
|
activeTaskCount_ = count;
|
|
|
|
updateIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::resetFatalError()
|
|
|
|
{
|
|
|
|
isFatalError_ = false;
|
|
|
|
updateIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::updateIcon()
|
|
|
|
{
|
|
|
|
if (iconUpdateTimer_->isActive())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isFatalError_) {
|
|
|
|
setIcon(Icon::Error, Duration::Permanent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIcon(activeTaskCount_ > 0 ? Icon::Busy : Icon::Idle, Duration::Permanent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::showInformation(const QString &text)
|
|
|
|
{
|
|
|
|
tray_->showMessage({}, text, QSystemTrayIcon::Information);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::showError(const QString &text)
|
|
|
|
{
|
|
|
|
LERROR() << text;
|
|
|
|
setIcon(Icon::Error, Duration::Temporal);
|
|
|
|
tray_->showMessage(tr("Error"), text, QSystemTrayIcon::Warning);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::showFatalError(const QString &text)
|
|
|
|
{
|
|
|
|
LERROR() << text;
|
|
|
|
isFatalError_ = true;
|
|
|
|
tray_->showMessage(tr("Error"), text, QSystemTrayIcon::Critical);
|
|
|
|
updateIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::showSuccess()
|
|
|
|
{
|
|
|
|
setIcon(Icon::Success, Duration::Temporal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIcon::handleIconClick(QSystemTrayIcon::ActivationReason reason)
|
|
|
|
{
|
|
|
|
if (reason == QSystemTrayIcon::Trigger && showLastAction_->isEnabled()) {
|
|
|
|
manager_.showLast();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reason == QSystemTrayIcon::MiddleClick && clipboardAction_->isEnabled()) {
|
|
|
|
manager_.copyLastToClipboard();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reason == QSystemTrayIcon::DoubleClick &&
|
|
|
|
repeatCaptureAction_->isEnabled()) {
|
|
|
|
manager_.repeatCapture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMenu *TrayIcon::contextMenu()
|
|
|
|
{
|
|
|
|
QMenu *menu = new QMenu();
|
|
|
|
{
|
|
|
|
captureAction_ = menu->addAction(tr("Capture"));
|
|
|
|
connect(captureAction_, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.capture(); });
|
|
|
|
}
|
|
|
|
{
|
|
|
|
repeatCaptureAction_ = menu->addAction(tr("Repeat capture"));
|
|
|
|
connect(repeatCaptureAction_, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.repeatCapture(); });
|
|
|
|
}
|
2020-03-31 00:03:00 +07:00
|
|
|
{
|
|
|
|
captureLockedAction_ = menu->addAction(tr("Capture saved areas"));
|
|
|
|
connect(captureLockedAction_, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.captureLocked(); });
|
|
|
|
}
|
2020-02-21 00:45:53 +07:00
|
|
|
|
|
|
|
{
|
|
|
|
QMenu *translateMenu = menu->addMenu(tr("Result"));
|
|
|
|
{
|
|
|
|
showLastAction_ = translateMenu->addAction(tr("Show"));
|
|
|
|
connect(showLastAction_, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.showLast(); });
|
|
|
|
}
|
|
|
|
{
|
|
|
|
clipboardAction_ = translateMenu->addAction(tr("To clipboard"));
|
|
|
|
connect(clipboardAction_, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.copyLastToClipboard(); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:54:54 +07:00
|
|
|
{
|
|
|
|
auto action = menu->addAction(tr("Show translator"));
|
|
|
|
connect(action, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.showTranslator(); });
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
settingsAction_ = menu->addAction(tr("Settings"));
|
|
|
|
connect(settingsAction_, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.settings(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto action = menu->addAction(tr("Quit"));
|
|
|
|
connect(action, &QAction::triggered, //
|
|
|
|
this, [this] { manager_.quit(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|