Add ability to lock capture areas and capture them by hotkey

This commit is contained in:
Gres 2020-03-30 20:03:00 +03:00
parent aac286df9d
commit 0738a88eb7
16 changed files with 109 additions and 18 deletions

View File

@ -48,3 +48,8 @@ QString CaptureArea::toolTip() const
return doTranslation_ ? sourceLanguage_ + "->" + targetLanguage_ return doTranslation_ ? sourceLanguage_ + "->" + targetLanguage_
: sourceLanguage_; : sourceLanguage_;
} }
bool CaptureArea::isLocked() const
{
return isLocked_;
}

View File

@ -14,6 +14,7 @@ public:
TaskPtr task(const QPixmap& pixmap) const; TaskPtr task(const QPixmap& pixmap) const;
bool isValid() const; bool isValid() const;
bool isLocked() const;
const QRect& rect() const; const QRect& rect() const;
void setRect(const QRect& rect); void setRect(const QRect& rect);
@ -24,6 +25,7 @@ private:
QRect rect_; QRect rect_;
bool doTranslation_; bool doTranslation_;
bool isLocked_{false};
LanguageId sourceLanguage_; LanguageId sourceLanguage_;
LanguageId targetLanguage_; LanguageId targetLanguage_;
QStringList translators_; QStringList translators_;

View File

@ -14,6 +14,7 @@ CaptureAreaEditor::CaptureAreaEditor(const CommonModels &models,
QWidget *parent) QWidget *parent)
: QWidget(parent) : QWidget(parent)
, doTranslation_(new QCheckBox(tr("Translate:"), this)) , doTranslation_(new QCheckBox(tr("Translate:"), this))
, isLocked_(new QCheckBox(tr("Save (can capture via hotkey)"), this))
, sourceLanguage_(new QComboBox(this)) , sourceLanguage_(new QComboBox(this))
, targetLanguage_(new QComboBox(this)) , targetLanguage_(new QComboBox(this))
{ {
@ -30,6 +31,9 @@ CaptureAreaEditor::CaptureAreaEditor(const CommonModels &models,
layout->addWidget(doTranslation_, row, 0); layout->addWidget(doTranslation_, row, 0);
layout->addWidget(targetLanguage_, row, 1); layout->addWidget(targetLanguage_, row, 1);
++row;
layout->addWidget(isLocked_, row, 0, 1, 2);
sourceLanguage_->setModel(models.sourceLanguageModel()); sourceLanguage_->setModel(models.sourceLanguageModel());
targetLanguage_->setModel(models.targetLanguageModel()); targetLanguage_->setModel(models.targetLanguageModel());
targetLanguage_->setEnabled(doTranslation_->isChecked()); targetLanguage_->setEnabled(doTranslation_->isChecked());
@ -59,6 +63,7 @@ void CaptureAreaEditor::swapLanguages()
void CaptureAreaEditor::set(const CaptureArea &area) void CaptureAreaEditor::set(const CaptureArea &area)
{ {
isLocked_->setChecked(area.isLocked());
doTranslation_->setChecked(area.doTranslation_); doTranslation_->setChecked(area.doTranslation_);
sourceLanguage_->setCurrentText(LanguageCodes::name(area.sourceLanguage_)); sourceLanguage_->setCurrentText(LanguageCodes::name(area.sourceLanguage_));
targetLanguage_->setCurrentText(LanguageCodes::name(area.targetLanguage_)); targetLanguage_->setCurrentText(LanguageCodes::name(area.targetLanguage_));
@ -66,6 +71,7 @@ void CaptureAreaEditor::set(const CaptureArea &area)
void CaptureAreaEditor::apply(CaptureArea &area) const void CaptureAreaEditor::apply(CaptureArea &area) const
{ {
area.isLocked_ = isLocked_->isChecked();
area.doTranslation_ = doTranslation_->isChecked(); area.doTranslation_ = doTranslation_->isChecked();
area.sourceLanguage_ = area.sourceLanguage_ =
LanguageCodes::idForName(sourceLanguage_->currentText()); LanguageCodes::idForName(sourceLanguage_->currentText());

View File

@ -22,6 +22,7 @@ private:
void swapLanguages(); void swapLanguages();
QCheckBox* doTranslation_; QCheckBox* doTranslation_;
QCheckBox* isLocked_;
QComboBox* sourceLanguage_; QComboBox* sourceLanguage_;
QComboBox* targetLanguage_; QComboBox* targetLanguage_;
}; };

View File

@ -35,6 +35,17 @@ void CaptureAreaSelector::activate()
activateWindow(); activateWindow();
} }
bool CaptureAreaSelector::hasLocked() const
{
return area_ && area_->isLocked();
}
void CaptureAreaSelector::captureLocked()
{
SOFT_ASSERT(hasLocked(), return );
capturer_.selected(*area_);
}
void CaptureAreaSelector::setScreenRects(const std::vector<QRect> &screens) void CaptureAreaSelector::setScreenRects(const std::vector<QRect> &screens)
{ {
auto helpRect = fontMetrics().boundingRect({}, 0, help_); auto helpRect = fontMetrics().boundingRect({}, 0, help_);
@ -150,6 +161,7 @@ void CaptureAreaSelector::drawCaptureArea(QPainter &painter,
void CaptureAreaSelector::showEvent(QShowEvent * /*event*/) void CaptureAreaSelector::showEvent(QShowEvent * /*event*/)
{ {
editor_->hide(); editor_->hide();
if (area_ && !area_->isLocked())
area_.reset(); area_.reset();
startSelectPos_ = currentSelectPos_ = QPoint(); startSelectPos_ = currentSelectPos_ = QPoint();
} }

View File

@ -14,6 +14,8 @@ public:
~CaptureAreaSelector(); ~CaptureAreaSelector();
void activate(); void activate();
bool hasLocked() const;
void captureLocked();
void setScreenRects(const std::vector<QRect> &screens); void setScreenRects(const std::vector<QRect> &screens);
void updateSettings(); void updateSettings();

View File

@ -28,6 +28,19 @@ void Capturer::capture()
selector_->activate(); selector_->activate();
} }
bool Capturer::canCaptureLocked()
{
SOFT_ASSERT(selector_, return false);
return selector_->hasLocked();
}
void Capturer::captureLocked()
{
updatePixmap();
SOFT_ASSERT(selector_, return );
selector_->captureLocked();
}
void Capturer::updatePixmap() void Capturer::updatePixmap()
{ {
const auto screens = QApplication::screens(); const auto screens = QApplication::screens();

View File

@ -12,6 +12,8 @@ public:
~Capturer(); ~Capturer();
void capture(); void capture();
bool canCaptureLocked();
void captureLocked();
void repeatCapture(); void repeatCapture();
void updateSettings(); void updateSettings();

View File

@ -83,6 +83,8 @@ void Manager::updateSettings()
recognizer_->updateSettings(); recognizer_->updateSettings();
translator_->updateSettings(); translator_->updateSettings();
representer_->updateSettings(); representer_->updateSettings();
tray_->setCaptureLockedEnabled(capturer_->canCaptureLocked());
} }
void Manager::setupProxy(const Settings &settings) void Manager::setupProxy(const Settings &settings)
@ -145,6 +147,7 @@ void Manager::finishTask(const TaskPtr &task)
void Manager::captured(const TaskPtr &task) void Manager::captured(const TaskPtr &task)
{ {
tray_->setCaptureLockedEnabled(capturer_->canCaptureLocked());
tray_->blockActions(false); tray_->blockActions(false);
SOFT_ASSERT(task, return ); SOFT_ASSERT(task, return );
@ -163,6 +166,7 @@ void Manager::captured(const TaskPtr &task)
void Manager::captureCanceled() void Manager::captureCanceled()
{ {
tray_->setCaptureLockedEnabled(capturer_->canCaptureLocked());
tray_->blockActions(false); tray_->blockActions(false);
} }
@ -235,6 +239,12 @@ void Manager::repeatCapture()
capturer_->repeatCapture(); capturer_->repeatCapture();
} }
void Manager::captureLocked()
{
SOFT_ASSERT(capturer_, return );
capturer_->captureLocked();
}
void Manager::settings() void Manager::settings()
{ {
SettingsEditor editor(*this, *updater_); SettingsEditor editor(*this, *updater_);

View File

@ -20,6 +20,7 @@ public:
void fatalError(const QString &text); void fatalError(const QString &text);
void capture(); void capture();
void repeatCapture(); void repeatCapture();
void captureLocked();
void showLast(); void showLast();
void settings(); void settings();
void copyLastToClipboard(); void copyLastToClipboard();

View File

@ -16,6 +16,7 @@ const QString qs_captureHotkey = "captureHotkey";
const QString qs_repeatCaptureHotkey = "repeatCaptureHotkey"; const QString qs_repeatCaptureHotkey = "repeatCaptureHotkey";
const QString qs_repeatHotkey = "repeatHotkey"; const QString qs_repeatHotkey = "repeatHotkey";
const QString qs_clipboardHotkey = "clipboardHotkey"; const QString qs_clipboardHotkey = "clipboardHotkey";
const QString qs_captureLockedHotkey = "captureLockedHotkey";
const QString qs_resultShowType = "resultShowType"; const QString qs_resultShowType = "resultShowType";
const QString qs_proxyType = "proxyType"; const QString qs_proxyType = "proxyType";
const QString qs_proxyHostName = "proxyHostName"; const QString qs_proxyHostName = "proxyHostName";
@ -145,6 +146,7 @@ void Settings::save() const
settings.setValue(qs_repeatCaptureHotkey, repeatCaptureHotkey); settings.setValue(qs_repeatCaptureHotkey, repeatCaptureHotkey);
settings.setValue(qs_repeatHotkey, showLastHotkey); settings.setValue(qs_repeatHotkey, showLastHotkey);
settings.setValue(qs_clipboardHotkey, clipboardHotkey); settings.setValue(qs_clipboardHotkey, clipboardHotkey);
settings.setValue(qs_captureLockedHotkey, captureLockedHotkey);
settings.setValue(qs_showMessageOnStart, showMessageOnStart); settings.setValue(qs_showMessageOnStart, showMessageOnStart);
@ -225,6 +227,8 @@ void Settings::load()
showLastHotkey = settings.value(qs_repeatHotkey, showLastHotkey).toString(); showLastHotkey = settings.value(qs_repeatHotkey, showLastHotkey).toString();
clipboardHotkey = clipboardHotkey =
settings.value(qs_clipboardHotkey, clipboardHotkey).toString(); settings.value(qs_clipboardHotkey, clipboardHotkey).toString();
captureLockedHotkey =
settings.value(qs_captureLockedHotkey, captureLockedHotkey).toString();
showMessageOnStart = showMessageOnStart =
settings.value(qs_showMessageOnStart, showMessageOnStart).toBool(); settings.value(qs_showMessageOnStart, showMessageOnStart).toBool();

View File

@ -33,6 +33,7 @@ public:
QString repeatCaptureHotkey{"Ctrl+Alt+S"}; QString repeatCaptureHotkey{"Ctrl+Alt+S"};
QString showLastHotkey{"Ctrl+Alt+X"}; QString showLastHotkey{"Ctrl+Alt+X"};
QString clipboardHotkey{"Ctrl+Alt+C"}; QString clipboardHotkey{"Ctrl+Alt+C"};
QString captureLockedHotkey{"Ctrl+Alt+Q"};
bool showMessageOnStart{true}; bool showMessageOnStart{true};
bool runAtSystemStart{false}; bool runAtSystemStart{false};

View File

@ -122,6 +122,8 @@ Settings SettingsEditor::settings() const
ui->repeatCaptureEdit->keySequence().toString(); ui->repeatCaptureEdit->keySequence().toString();
settings.showLastHotkey = ui->repeatEdit->keySequence().toString(); settings.showLastHotkey = ui->repeatEdit->keySequence().toString();
settings.clipboardHotkey = ui->clipboardEdit->keySequence().toString(); settings.clipboardHotkey = ui->clipboardEdit->keySequence().toString();
settings.captureLockedHotkey =
ui->captureLockedEdit->keySequence().toString();
settings.showMessageOnStart = ui->showOnStart->isChecked(); settings.showMessageOnStart = ui->showOnStart->isChecked();
@ -182,6 +184,7 @@ void SettingsEditor::setSettings(const Settings &settings)
ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey); ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey);
ui->repeatEdit->setKeySequence(settings.showLastHotkey); ui->repeatEdit->setKeySequence(settings.showLastHotkey);
ui->clipboardEdit->setKeySequence(settings.clipboardHotkey); ui->clipboardEdit->setKeySequence(settings.clipboardHotkey);
ui->captureLockedEdit->setKeySequence(settings.captureLockedHotkey);
ui->showOnStart->setChecked(settings.showMessageOnStart); ui->showOnStart->setChecked(settings.showMessageOnStart);

View File

@ -56,37 +56,30 @@
<string>Shortcuts</string> <string>Shortcuts</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0"> <item row="4" column="1">
<widget class="QLabel" name="label"> <widget class="QKeySequenceEdit" name="clipboardEdit"/>
<property name="text">
<string>Capture</string>
</property>
</widget>
</item> </item>
<item row="0" column="1"> <item row="2" column="0">
<widget class="QKeySequenceEdit" name="captureEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8"> <widget class="QLabel" name="label_8">
<property name="text"> <property name="text">
<string>Repeat capture</string> <string>Repeat capture</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="2" column="1">
<widget class="QKeySequenceEdit" name="repeatCaptureEdit"/> <widget class="QKeySequenceEdit" name="repeatCaptureEdit"/>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Show last result</string> <string>Show last result</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="0" column="1">
<widget class="QKeySequenceEdit" name="repeatEdit"/> <widget class="QKeySequenceEdit" name="captureEdit"/>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_7"> <widget class="QLabel" name="label_7">
<property name="text"> <property name="text">
<string>Copy result to clipboard</string> <string>Copy result to clipboard</string>
@ -94,7 +87,24 @@
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
<widget class="QKeySequenceEdit" name="clipboardEdit"/> <widget class="QKeySequenceEdit" name="repeatEdit"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Capture</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_22">
<property name="text">
<string>Capture saved areas</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QKeySequenceEdit" name="captureLockedEdit"/>
</item> </item>
</layout> </layout>
</widget> </widget>

View File

@ -44,6 +44,9 @@ void TrayIcon::updateSettings()
failedActions << settings_.showLastHotkey; failedActions << settings_.showLastHotkey;
if (!GlobalAction::update(clipboardAction_, settings_.clipboardHotkey)) if (!GlobalAction::update(clipboardAction_, settings_.clipboardHotkey))
failedActions << settings_.clipboardHotkey; failedActions << settings_.clipboardHotkey;
if (!GlobalAction::update(captureLockedAction_,
settings_.captureLockedHotkey))
failedActions << settings_.captureLockedHotkey;
if (!failedActions.isEmpty()) { if (!failedActions.isEmpty()) {
showError(tr("Failed to register global shortcuts:\n%1") showError(tr("Failed to register global shortcuts:\n%1")
@ -63,6 +66,12 @@ void TrayIcon::setTaskActionsEnabled(bool isEnabled)
updateActions(); updateActions();
} }
void TrayIcon::setCaptureLockedEnabled(bool isEnabled)
{
canCaptureLocked_ = isEnabled;
updateActions();
}
void TrayIcon::setRepeatCaptureEnabled(bool isEnabled) void TrayIcon::setRepeatCaptureEnabled(bool isEnabled)
{ {
canRepeatCapture_ = isEnabled; canRepeatCapture_ = isEnabled;
@ -73,7 +82,8 @@ void TrayIcon::updateActions()
{ {
if (isActionsBlocked_) { if (isActionsBlocked_) {
QVector<QAction *> blockable{captureAction_, repeatCaptureAction_, QVector<QAction *> blockable{captureAction_, repeatCaptureAction_,
showLastAction_, settingsAction_}; showLastAction_, settingsAction_,
captureLockedAction_};
for (auto &action : blockable) action->setEnabled(false); for (auto &action : blockable) action->setEnabled(false);
return; return;
} }
@ -85,6 +95,7 @@ void TrayIcon::updateActions()
for (auto &action : taskActions) action->setEnabled(gotTask_); for (auto &action : taskActions) action->setEnabled(gotTask_);
repeatCaptureAction_->setEnabled(canRepeatCapture_); repeatCaptureAction_->setEnabled(canRepeatCapture_);
captureLockedAction_->setEnabled(canCaptureLocked_);
} }
void TrayIcon::setIcon(TrayIcon::Icon icon, Duration duration) void TrayIcon::setIcon(TrayIcon::Icon icon, Duration duration)
@ -188,6 +199,11 @@ QMenu *TrayIcon::contextMenu()
connect(repeatCaptureAction_, &QAction::triggered, // connect(repeatCaptureAction_, &QAction::triggered, //
this, [this] { manager_.repeatCapture(); }); this, [this] { manager_.repeatCapture(); });
} }
{
captureLockedAction_ = menu->addAction(tr("Capture saved areas"));
connect(captureLockedAction_, &QAction::triggered, //
this, [this] { manager_.captureLocked(); });
}
{ {
QMenu *translateMenu = menu->addMenu(tr("Result")); QMenu *translateMenu = menu->addMenu(tr("Result"));

View File

@ -17,6 +17,7 @@ public:
void blockActions(bool block); void blockActions(bool block);
void setTaskActionsEnabled(bool isEnabled); void setTaskActionsEnabled(bool isEnabled);
void setCaptureLockedEnabled(bool isEnabled);
void setRepeatCaptureEnabled(bool isEnabled); void setRepeatCaptureEnabled(bool isEnabled);
void setActiveTaskCount(int count); void setActiveTaskCount(int count);
void resetFatalError(); void resetFatalError();
@ -40,6 +41,7 @@ private:
std::unique_ptr<QSystemTrayIcon> tray_; std::unique_ptr<QSystemTrayIcon> tray_;
QAction *captureAction_{nullptr}; QAction *captureAction_{nullptr};
QAction *captureLockedAction_{nullptr};
QAction *repeatCaptureAction_{nullptr}; QAction *repeatCaptureAction_{nullptr};
QAction *showLastAction_{nullptr}; QAction *showLastAction_{nullptr};
QAction *clipboardAction_{nullptr}; QAction *clipboardAction_{nullptr};
@ -53,4 +55,5 @@ private:
bool gotTask_{false}; bool gotTask_{false};
bool canRepeatCapture_{false}; bool canRepeatCapture_{false};
bool isActionsBlocked_{false}; bool isActionsBlocked_{false};
bool canCaptureLocked_{false};
}; };