From 65616052e54e07c8424ed33918c2d1af3ffc8b4c Mon Sep 17 00:00:00 2001 From: Palo Kisa Date: Wed, 19 Apr 2017 02:06:54 +0200 Subject: [PATCH] Use KGlobalAccel for shorcut handling ..and drop usage of lxqt-globalkeys --- CMakeLists.txt | 6 ++-- configuredialog/configuredialog.cpp | 43 ++++++++++++++++++++++------- configuredialog/configuredialog.h | 6 ++-- configuredialog/configuredialog.ui | 19 +------------ dialog.cpp | 31 +++++++++------------ dialog.h | 10 ++----- providers.cpp | 2 -- 7 files changed, 55 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee9ce26..a5aa429 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,8 +27,7 @@ find_package(Qt5Widgets REQUIRED) find_package(Qt5Xml REQUIRED) find_package(Qt5LinguistTools REQUIRED QUIET) find_package(lxqt REQUIRED) -find_package(lxqt-globalkeys REQUIRED) -find_package(lxqt-globalkeys-ui REQUIRED) +find_package(KF5GlobalAccel REQUIRED) message(STATUS "Building with Qt${Qt5Core_VERSION_STRING}") include(LXQtCompilerSettings NO_POLICY_SCOPE) @@ -78,8 +77,7 @@ set(QRC_FILES set(lxqt-runner_LIBRARIES lxqt - lxqt-globalkeys - lxqt-globalkeys-ui + KF5::GlobalAccel ${MENUCACHE_LIBRARIES} ${QTX_LIBRARIES} ${MUPARSER_LDFLAGS} diff --git a/configuredialog/configuredialog.cpp b/configuredialog/configuredialog.cpp index b9f7c06..d45ce24 100644 --- a/configuredialog/configuredialog.cpp +++ b/configuredialog/configuredialog.cpp @@ -42,15 +42,15 @@ +const QKeySequence ConfigureDialog::DEFAULT_SHORTCUT{Qt::ALT + Qt::Key_F2}; /************************************************ ************************************************/ -ConfigureDialog::ConfigureDialog(QSettings *settings, const QString &defaultShortcut, QWidget *parent) : +ConfigureDialog::ConfigureDialog(QSettings *settings, QWidget *parent) : QDialog(parent), ui(new Ui::ConfigureDialog), mSettings(settings), - mOldSettings(new LXQt::SettingsCache(settings)), - mDefaultShortcut(defaultShortcut) + mOldSettings(new LXQt::SettingsCache(settings)) { ui->setupUi(this); @@ -75,9 +75,10 @@ ConfigureDialog::ConfigureDialog(QSettings *settings, const QString &defaultShor // Shortcut ................................. - connect(ui->shortcutEd, SIGNAL(shortcutGrabbed(QString)), this, SLOT(shortcutChanged(QString))); + connect(ui->shortcutEd, &QKeySequenceEdit::editingFinished, this, &ConfigureDialog::shortcutChanged); - connect(ui->shortcutEd->addMenuAction(tr("Reset")), SIGNAL(triggered()), this, SLOT(shortcutReset())); + //TODO:? + //connect(ui->shortcutEd->addMenuAction(tr("Reset")), SIGNAL(triggered()), this, SLOT(shortcutReset())); settingsChanged(); @@ -96,7 +97,7 @@ void ConfigureDialog::settingsChanged() ui->positionCbx->setCurrentIndex(1); ui->monitorCbx->setCurrentIndex(mSettings->value("dialog/monitor", -1).toInt() + 1); - ui->shortcutEd->setText(mSettings->value("dialog/shortcut", "Alt+F2").toString()); + ui->shortcutEd->setKeySequence(mSettings->value("dialog/shortcut", DEFAULT_SHORTCUT).toString()); ui->historyCb->setChecked(mSettings->value("dialog/history_first", true).toBool()); } @@ -114,10 +115,31 @@ ConfigureDialog::~ConfigureDialog() /************************************************ ************************************************/ -void ConfigureDialog::shortcutChanged(const QString &text) +void ConfigureDialog::shortcutChanged() { - ui->shortcutEd->setText(text); - mSettings->setValue("dialog/shortcut", text); + QKeySequence shortcut = ui->shortcutEd->keySequence(); + if (shortcut.isEmpty()) + shortcut = mSettings->value("dialog/shortcut", DEFAULT_SHORTCUT).toString(); + else + { + // use only the first key (+modifiers) + if (shortcut.count() > 1) + shortcut = shortcut[0]; + + const int modifiers = shortcut[0] & Qt::MODIFIER_MASK; + const int key = shortcut[0] & ~Qt::MODIFIER_MASK; + // do not allow plain printable keys + if (modifiers + && !(modifiers == Qt::SHIFT && key > Qt::Key_Space && key <= Qt::Key_AsciiTilde)) + { + mSettings->setValue("dialog/shortcut", shortcut); + } else + { + shortcut = mSettings->value("dialog/shortcut", DEFAULT_SHORTCUT).toString(); + } + } + ui->shortcutEd->setKeySequence(shortcut); + ui->shortcutEd->clearFocus(); } @@ -126,7 +148,8 @@ void ConfigureDialog::shortcutChanged(const QString &text) ************************************************/ void ConfigureDialog::shortcutReset() { - shortcutChanged(mDefaultShortcut); + ui->shortcutEd->setKeySequence(DEFAULT_SHORTCUT); + shortcutChanged(); } diff --git a/configuredialog/configuredialog.h b/configuredialog/configuredialog.h index e19a98f..e0454a4 100644 --- a/configuredialog/configuredialog.h +++ b/configuredialog/configuredialog.h @@ -51,8 +51,9 @@ class ConfigureDialog : public QDialog PositionCenter }; + static const QKeySequence DEFAULT_SHORTCUT; - explicit ConfigureDialog(QSettings *settings, const QString &defaultShortcut, QWidget *parent = 0); + explicit ConfigureDialog(QSettings *settings, QWidget *parent = 0); ~ConfigureDialog(); protected: @@ -62,10 +63,9 @@ class ConfigureDialog : public QDialog Ui::ConfigureDialog *ui; QSettings *mSettings; LXQt::SettingsCache *mOldSettings; - QString mDefaultShortcut; private slots: - void shortcutChanged(const QString &text); + void shortcutChanged(); void shortcutReset(); void settingsChanged(); void positionCbxChanged(int index); diff --git a/configuredialog/configuredialog.ui b/configuredialog/configuredialog.ui index 0ac88fc..140329e 100644 --- a/configuredialog/configuredialog.ui +++ b/configuredialog/configuredialog.ui @@ -65,17 +65,7 @@ - - - - 81 - 0 - - - - - - + @@ -119,13 +109,6 @@ - - - ShortcutSelector - QToolButton -
LXQtGlobalKeysUi/ShortcutSelector
-
-
diff --git a/dialog.cpp b/dialog.cpp index aa23e68..239558b 100644 --- a/dialog.cpp +++ b/dialog.cpp @@ -37,8 +37,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -53,7 +52,6 @@ #include -#define DEFAULT_SHORTCUT "Alt+F2" /************************************************ @@ -62,7 +60,7 @@ Dialog::Dialog(QWidget *parent) : QDialog(parent, Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint), ui(new Ui::Dialog), mSettings(new LXQt::Settings("lxqt-runner", this)), - mGlobalShortcut(0), + mGlobalShortcut(new QAction("lxqt-runner action", this)), mLockCascadeChanges(false), mDesktopChanged(false), mConfigureDialog(0) @@ -111,12 +109,15 @@ Dialog::Dialog(QWidget *parent) : ui->actionButton->setMenu(menu); // End of popup menu ........................ + mGlobalShortcut->setObjectName("show_hide_dialog"); + mGlobalShortcut->setProperty("componentName", "org.lxqt.runner"); + mGlobalShortcut->setProperty("componentDisplayName", tr("Show/hide runner dialog")); applySettings(); connect(QApplication::desktop(), SIGNAL(screenCountChanged(int)), SLOT(realign())); connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(realign())); - connect(mGlobalShortcut, SIGNAL(activated()), this, SLOT(showHide())); - connect(mGlobalShortcut, SIGNAL(shortcutChanged(QString,QString)), this, SLOT(shortcutChanged(QString,QString))); + connect(mGlobalShortcut, &QAction::triggered, this, &Dialog::showHide); + connect(KGlobalAccel::self(), &KGlobalAccel::globalShortcutChanged, this, &Dialog::shortcutChanged); connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(onActiveWindowChanged(WId))); connect(KWindowSystem::self(), &KWindowSystem::currentDesktopChanged, this, &Dialog::onCurrentDesktopChanged); @@ -343,14 +344,10 @@ void Dialog::applySettings() return; // Shortcut ................................. - QString shortcut = mSettings->value("dialog/shortcut", DEFAULT_SHORTCUT).toString(); + QKeySequence shortcut = mSettings->value("dialog/shortcut", ConfigureDialog::DEFAULT_SHORTCUT).toString(); if (shortcut.isEmpty()) - shortcut = DEFAULT_SHORTCUT; - - if (!mGlobalShortcut) - mGlobalShortcut = GlobalKeyShortcut::Client::instance()->addAction(shortcut, "/runner/show_hide_dialog", tr("Show/hide runner dialog"), this); - else if (mGlobalShortcut->shortcut() != shortcut) - mGlobalShortcut->changeShortcut(shortcut); + shortcut = ConfigureDialog::DEFAULT_SHORTCUT; + KGlobalAccel::self()->setShortcut(mGlobalShortcut, {shortcut}, KGlobalAccel::NoAutoloading); mShowOnTop = mSettings->value("dialog/show_on_top", true).toBool(); @@ -366,9 +363,9 @@ void Dialog::applySettings() /************************************************ ************************************************/ -void Dialog::shortcutChanged(const QString &/*oldShortcut*/, const QString &newShortcut) +void Dialog::shortcutChanged(QAction * action, const QKeySequence & newShortcut) { - if (!newShortcut.isEmpty()) + if (mGlobalShortcut == action && !newShortcut.isEmpty()) { mLockCascadeChanges = true; @@ -483,8 +480,6 @@ void Dialog::runCommand() void Dialog::showConfigDialog() { if (!mConfigureDialog) - mConfigureDialog = new ConfigureDialog(mSettings, DEFAULT_SHORTCUT, this); + mConfigureDialog = new ConfigureDialog(mSettings, this); mConfigureDialog->exec(); } - -#undef DEFAULT_SHORTCUT diff --git a/dialog.h b/dialog.h index a1c7611..2f0ec51 100644 --- a/dialog.h +++ b/dialog.h @@ -43,11 +43,7 @@ namespace LXQt { class CommandListView; class CommandItemModel; class ConfigureDialog; - -namespace GlobalKeyShortcut -{ -class Action; -} +class QAction; class Dialog : public QDialog @@ -70,7 +66,7 @@ class Dialog : public QDialog private: Ui::Dialog *ui; LXQt::Settings *mSettings; - GlobalKeyShortcut::Action *mGlobalShortcut; + QAction *mGlobalShortcut; CommandItemModel *mCommandItemModel; bool mShowOnTop; int mMonitor; @@ -90,7 +86,7 @@ private slots: void dataChanged(); void runCommand(); void showConfigDialog(); - void shortcutChanged(const QString &oldShortcut, const QString &newShortcut); + void shortcutChanged(QAction * action, const QKeySequence & newShortcut); void onActiveWindowChanged(WId id); void onCurrentDesktopChanged(int desktop); }; diff --git a/providers.cpp b/providers.cpp index 3daad5b..a1deff8 100644 --- a/providers.cpp +++ b/providers.cpp @@ -43,11 +43,9 @@ #include #include #include -#include #include #include #include "providers.h" -#include #include #include