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_
: sourceLanguage_;
}
bool CaptureArea::isLocked() const
{
return isLocked_;
}

View File

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

View File

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

View File

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

View File

@ -35,6 +35,17 @@ void CaptureAreaSelector::activate()
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)
{
auto helpRect = fontMetrics().boundingRect({}, 0, help_);
@ -150,6 +161,7 @@ void CaptureAreaSelector::drawCaptureArea(QPainter &painter,
void CaptureAreaSelector::showEvent(QShowEvent * /*event*/)
{
editor_->hide();
if (area_ && !area_->isLocked())
area_.reset();
startSelectPos_ = currentSelectPos_ = QPoint();
}

View File

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

View File

@ -28,6 +28,19 @@ void Capturer::capture()
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()
{
const auto screens = QApplication::screens();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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