Skip to content

Commit

Permalink
Added possibility to capture image from last used screenshot (Selecti…
Browse files Browse the repository at this point in the history
…onDialog).
  • Loading branch information
OneMoreGres committed Sep 29, 2015
1 parent dd3d92e commit 9e85af0
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 90 deletions.
65 changes: 57 additions & 8 deletions Manager.cpp
Expand Up @@ -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<ProcessingItem>();
Expand Down Expand Up @@ -72,6 +73,7 @@ Manager::Manager (QObject *parent) :
SLOT (processTrayAction (QSystemTrayIcon::ActivationReason)));

trayIcon_->setContextMenu (trayContextMenu ());
updateActionsState ();
trayIcon_->show ();

applySettings ();
Expand All @@ -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 ()));
Expand All @@ -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<QAction *> actions;
actions << captureAction_ << repeatCaptureAction_ << repeatAction_ << clipboardAction_;
QList<bool> 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 () {
Expand All @@ -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 ());
Expand Down Expand Up @@ -168,15 +196,32 @@ void Manager::capture () {
SelectionDialog *selection = selections_[name];
selection->setPixmap (pixmap, geometry);
}
updateActionsState ();
}

void Manager::repeatCapture () {
if (selections_.isEmpty ()) {
return;
}
QList<QScreen *> 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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion Manager.h
Expand Up @@ -30,6 +30,7 @@ class Manager : public QObject {

private slots:
void capture ();
void repeatCapture ();
void settings ();
void close ();
void about ();
Expand All @@ -45,7 +46,7 @@ class Manager : public QObject {

private:
QMenu * trayContextMenu ();
void setActionsEnabled (bool isEnabled);
void updateActionsState (bool isEnabled = true);

private:
QSystemTrayIcon *trayIcon_;
Expand All @@ -54,6 +55,7 @@ class Manager : public QObject {
QMap<QString, SelectionDialog *> selections_;
ResultDialog *resultDialog_;
QAction *captureAction_;
QAction *repeatCaptureAction_;
QAction *repeatAction_;
QAction *clipboardAction_;
ProcessingItem lastItem_;
Expand Down
2 changes: 2 additions & 0 deletions Settings.h
Expand Up @@ -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";
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions SettingsEditor.cpp
Expand Up @@ -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 ());
Expand Down Expand Up @@ -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 ());
Expand Down
162 changes: 81 additions & 81 deletions SettingsEditor.ui
Expand Up @@ -6,61 +6,26 @@
<rect>
<x>0</x>
<y>0</y>
<width>518</width>
<height>274</height>
<width>603</width>
<height>269</height>
</rect>
</property>
<property name="windowTitle">
<string>Настройки</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Горячие клавиши</string>
<item row="4" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Захватить</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QKeySequenceEdit" name="captureEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для повторного отображения последнего результата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Показать</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QKeySequenceEdit" name="repeatEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_7">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для копирования последнего результата в буфер обмена.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Скопировать</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QKeySequenceEdit" name="clipboardEdit"/>
</item>
</layout>
</widget>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_2">
Expand Down Expand Up @@ -126,20 +91,17 @@
</layout>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<item row="3" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>20</height>
</size>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</spacer>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QGroupBox" name="resultGroup">
<property name="title">
<string>Вывод результата</string>
Expand All @@ -165,7 +127,68 @@
</layout>
</widget>
</item>
<item row="1" column="1">
<item row="0" column="0" rowspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Горячие клавиши</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Захватить</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QKeySequenceEdit" name="captureEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата, но с использованием последнего использованного, а не текущего, изображения.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Захватить повторно</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QKeySequenceEdit" name="repeatCaptureEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для повторного отображения последнего результата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Показать</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QKeySequenceEdit" name="repeatEdit"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_7">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для копирования последнего результата в буфер обмена.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Скопировать</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QKeySequenceEdit" name="clipboardEdit"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="1" rowspan="2">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Перевод</string>
Expand Down Expand Up @@ -200,29 +223,6 @@
</layout>
</widget>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
Expand Down

0 comments on commit 9e85af0

Please sign in to comment.