From 9e85af0eaf5a478270ca55633a7d00f361eb11c1 Mon Sep 17 00:00:00 2001 From: Gres Date: Tue, 29 Sep 2015 18:14:56 +0300 Subject: [PATCH] Added possibility to capture image from last used screenshot (SelectionDialog). --- Manager.cpp | 65 +++++++++++++++--- Manager.h | 4 +- Settings.h | 2 + SettingsEditor.cpp | 2 + SettingsEditor.ui | 162 ++++++++++++++++++++++----------------------- 5 files changed, 145 insertions(+), 90 deletions(-) diff --git a/Manager.cpp b/Manager.cpp index 8cdd67e..2dddab5 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -26,7 +26,8 @@ Manager::Manager (QObject *parent) : trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)), dictionary_ (new LanguageHelper), resultDialog_ (new ResultDialog), - captureAction_ (NULL), repeatAction_ (NULL), clipboardAction_ (NULL), + captureAction_ (NULL), repeatCaptureAction_ (NULL), + repeatAction_ (NULL), clipboardAction_ (NULL), useResultDialog_ (true) { GlobalActionHelper::init (); qRegisterMetaType(); @@ -72,6 +73,7 @@ Manager::Manager (QObject *parent) : SLOT (processTrayAction (QSystemTrayIcon::ActivationReason))); trayIcon_->setContextMenu (trayContextMenu ()); + updateActionsState (); trayIcon_->show (); applySettings (); @@ -80,6 +82,8 @@ Manager::Manager (QObject *parent) : QMenu * Manager::trayContextMenu () { QMenu *menu = new QMenu (); captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ())); + repeatCaptureAction_ = menu->addAction (tr ("Повторить захват"), + this, SLOT (repeatCapture ())); QMenu *translateMenu = menu->addMenu (tr ("Результат")); repeatAction_ = translateMenu->addAction (tr ("Показать"), this, SLOT (showLast ())); @@ -91,10 +95,29 @@ QMenu * Manager::trayContextMenu () { return menu; } -void Manager::setActionsEnabled (bool isEnabled) { +void Manager::updateActionsState (bool isEnabled) { +#ifdef Q_OS_LINUX + // Avoid unneeded tray blinking (required to update context menu). + QList actions; + actions << captureAction_ << repeatCaptureAction_ << repeatAction_ << clipboardAction_; + QList states; + foreach (const QAction * action, actions) { + states << action->isEnabled (); + } +#endif captureAction_->setEnabled (isEnabled); - repeatAction_->setEnabled (isEnabled); - clipboardAction_->setEnabled (isEnabled); + repeatCaptureAction_->setEnabled (isEnabled && !selections_.isEmpty ()); + repeatAction_->setEnabled (isEnabled && lastItem_.isValid ()); + clipboardAction_->setEnabled (isEnabled && lastItem_.isValid ()); +#ifdef Q_OS_LINUX + for (int i = 0, end = actions.size (); i < end; ++i) { + if (states.at (i) != actions.at (i)->isEnabled ()) { + trayIcon_->hide (); + trayIcon_->show (); + break; + } + } +#endif } void Manager::applySettings () { @@ -107,6 +130,11 @@ void Manager::applySettings () { captureAction_->setShortcut (GET (captureHotkey).toString ()); GlobalActionHelper::makeGlobal (captureAction_); + Q_CHECK_PTR (repeatCaptureAction_); + GlobalActionHelper::removeGlobal (repeatCaptureAction_); + repeatCaptureAction_->setShortcut (GET (repeatCaptureHotkey).toString ()); + GlobalActionHelper::makeGlobal (repeatCaptureAction_); + Q_CHECK_PTR (repeatAction_); GlobalActionHelper::removeGlobal (repeatAction_); repeatAction_->setShortcut (GET (repeatHotkey).toString ()); @@ -168,15 +196,32 @@ void Manager::capture () { SelectionDialog *selection = selections_[name]; selection->setPixmap (pixmap, geometry); } + updateActionsState (); +} + +void Manager::repeatCapture () { + if (selections_.isEmpty ()) { + return; + } + QList screens = QApplication::screens (); + foreach (QScreen * screen, screens) { + QString name = screen->name (); + if (!selections_.contains (name)) { + continue; + } + SelectionDialog *selection = selections_[name]; + selection->show (); + selection->activateWindow (); + } } void Manager::settings () { SettingsEditor editor (*dictionary_); editor.setWindowIcon (trayIcon_->icon ()); connect (&editor, SIGNAL (settingsEdited ()), SIGNAL (settingsEdited ())); - setActionsEnabled (false); + updateActionsState (false); editor.exec (); - setActionsEnabled (true); + updateActionsState (true); } void Manager::close () { @@ -198,12 +243,15 @@ void Manager::about () { } void Manager::processTrayAction (QSystemTrayIcon::ActivationReason reason) { - if (reason == QSystemTrayIcon::Trigger) { + if (reason == QSystemTrayIcon::Trigger && repeatAction_->isEnabled ()) { showLast (); } - else if (reason == QSystemTrayIcon::MiddleClick) { + else if (reason == QSystemTrayIcon::MiddleClick && clipboardAction_->isEnabled ()) { copyLastToClipboard (); } + else if (reason == QSystemTrayIcon::DoubleClick && repeatCaptureAction_->isEnabled ()) { + repeatCapture (); + } } void Manager::showLast () { @@ -236,6 +284,7 @@ void Manager::showResult (ProcessingItem item) { QString message = item.recognized + " - " + item.translated; trayIcon_->showMessage (tr ("Результат"), message, QSystemTrayIcon::Information); } + updateActionsState (); } void Manager::showError (QString text) { diff --git a/Manager.h b/Manager.h index 84f1774..be0ba46 100644 --- a/Manager.h +++ b/Manager.h @@ -30,6 +30,7 @@ class Manager : public QObject { private slots: void capture (); + void repeatCapture (); void settings (); void close (); void about (); @@ -45,7 +46,7 @@ class Manager : public QObject { private: QMenu * trayContextMenu (); - void setActionsEnabled (bool isEnabled); + void updateActionsState (bool isEnabled = true); private: QSystemTrayIcon *trayIcon_; @@ -54,6 +55,7 @@ class Manager : public QObject { QMap selections_; ResultDialog *resultDialog_; QAction *captureAction_; + QAction *repeatCaptureAction_; QAction *repeatAction_; QAction *clipboardAction_; ProcessingItem lastItem_; diff --git a/Settings.h b/Settings.h index dc056f9..b7cf46f 100644 --- a/Settings.h +++ b/Settings.h @@ -8,6 +8,7 @@ namespace settings_names { const QString guiGroup = "GUI"; const QString geometry = "geometry"; const QString captureHotkey = "captureHotkey"; + const QString repeatCaptureHotkey = "repeatCaptureHotkey"; const QString repeatHotkey = "repeatHotkey"; const QString clipboardHotkey = "clipboardHotkey"; const QString resultShowType = "resultShowType"; @@ -32,6 +33,7 @@ namespace settings_values { //! UI const QString captureHotkey = "Ctrl+Alt+Z"; + const QString repeatCaptureHotkey = "Ctrl+Alt+S"; const QString repeatHotkey = "Ctrl+Alt+X"; const QString clipboardHotkey = "Ctrl+Alt+C"; const QString resultShowType = "1";//dialog diff --git a/SettingsEditor.cpp b/SettingsEditor.cpp index 62eca4d..5c3c89c 100644 --- a/SettingsEditor.cpp +++ b/SettingsEditor.cpp @@ -43,6 +43,7 @@ void SettingsEditor::saveSettings () const { QSettings settings; settings.beginGroup (guiGroup); settings.setValue (captureHotkey, ui->captureEdit->keySequence ().toString ()); + settings.setValue (repeatCaptureHotkey, ui->repeatCaptureEdit->keySequence ().toString ()); settings.setValue (repeatHotkey, ui->repeatEdit->keySequence ().toString ()); settings.setValue (clipboardHotkey, ui->clipboardEdit->keySequence ().toString ()); settings.setValue (resultShowType, buttonGroup_->checkedId ()); @@ -84,6 +85,7 @@ void SettingsEditor::loadSettings () { settings.beginGroup (settings_names::guiGroup); ui->captureEdit->setKeySequence (QKeySequence (GET (captureHotkey).toString ())); + ui->repeatCaptureEdit->setKeySequence (QKeySequence (GET (repeatCaptureHotkey).toString ())); ui->repeatEdit->setKeySequence (QKeySequence (GET (repeatHotkey).toString ())); ui->clipboardEdit->setKeySequence (QKeySequence (GET (clipboardHotkey).toString ())); QAbstractButton *button = buttonGroup_->button (GET (resultShowType).toInt ()); diff --git a/SettingsEditor.ui b/SettingsEditor.ui index dab1918..4db4394 100644 --- a/SettingsEditor.ui +++ b/SettingsEditor.ui @@ -6,61 +6,26 @@ 0 0 - 518 - 274 + 603 + 269 Настройки - - - - Горячие клавиши + + + + Qt::Vertical - - - - - <html><head/><body><p>Сочетание клавиш для перехода в режим захвата.</p></body></html> - - - Захватить - - - - - - - - - - <html><head/><body><p>Сочетание клавиш для повторного отображения последнего результата.</p></body></html> - - - Показать - - - - - - - - - - <html><head/><body><p>Сочетание клавиш для копирования последнего результата в буфер обмена.</p></body></html> - - - Скопировать - - - - - - - - + + + 20 + 1 + + + @@ -126,20 +91,17 @@ - - + + Qt::Horizontal - - - 0 - 20 - + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok - + - + Вывод результата @@ -165,7 +127,68 @@ - + + + + Горячие клавиши + + + + + + <html><head/><body><p>Сочетание клавиш для перехода в режим захвата.</p></body></html> + + + Захватить + + + + + + + + + + <html><head/><body><p>Сочетание клавиш для перехода в режим захвата, но с использованием последнего использованного, а не текущего, изображения.</p></body></html> + + + Захватить повторно + + + + + + + + + + <html><head/><body><p>Сочетание клавиш для повторного отображения последнего результата.</p></body></html> + + + Показать + + + + + + + + + + <html><head/><body><p>Сочетание клавиш для копирования последнего результата в буфер обмена.</p></body></html> + + + Скопировать + + + + + + + + + + Перевод @@ -200,29 +223,6 @@ - - - - Qt::Vertical - - - - 20 - 1 - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - -