refactor:move modifiers code at its own class

pull/4/head
Michail Vourlakos 6 years ago
parent f2f8f0eb8b
commit 608275cc6b

@ -2,7 +2,6 @@ set(lattedock-app_SRCS
../liblatte2/commontools.cpp
../liblatte2/types.cpp
alternativeshelper.cpp
globalshortcuts.cpp
importer.cpp
infoview.cpp
lattecorona.cpp
@ -22,6 +21,8 @@ set(lattedock-app_SRCS
settings/delegates/colorcmbboxdelegate.cpp
settings/delegates/colorcmbboxitemdelegate.cpp
settings/delegates/layoutnamedelegate.cpp
shortcuts/modifiertracker.cpp
shortcuts/globalshortcuts.cpp
view/contextmenu.cpp
view/effects.cpp
view/panelshadows.cpp

@ -22,12 +22,12 @@
// local
#include "alternativeshelper.h"
#include "globalshortcuts.h"
#include "importer.h"
#include "lattedockadaptor.h"
#include "launcherssignals.h"
#include "layoutmanager.h"
#include "screenpool.h"
#include "shortcuts/globalshortcuts.h"
#include "package/lattepackage.h"
#include "plasma/extended/screenpool.h"
#include "plasma/extended/theme.h"

@ -21,10 +21,11 @@
#include "globalshortcuts.h"
// local
#include "lattecorona.h"
#include "layoutmanager.h"
#include "settings/universalsettings.h"
#include "view/view.h"
#include "modifiertracker.h"
#include "../lattecorona.h"
#include "../layoutmanager.h"
#include "../settings/universalsettings.h"
#include "../view/view.h"
// C++
#include <array>
@ -54,6 +55,7 @@ GlobalShortcuts::GlobalShortcuts(QObject *parent)
: QObject(parent)
{
m_corona = qobject_cast<Latte::Corona *>(parent);
m_modifierTracker = new ShortcutsPart::ModifierTracker(this);
if (m_corona) {
init();
@ -75,6 +77,9 @@ GlobalShortcuts::GlobalShortcuts(QObject *parent)
GlobalShortcuts::~GlobalShortcuts()
{
if (m_modifierTracker) {
m_modifierTracker->deleteLater();
}
}
void GlobalShortcuts::init()
@ -95,7 +100,7 @@ void GlobalShortcuts::init()
settingsAction->setText(i18n("Show Dock Settings"));
KGlobalAccel::setGlobalShortcut(settingsAction, QKeySequence(Qt::META + Qt::Key_A));
connect(settingsAction, &QAction::triggered, this, [this] {
m_metaPressedTimer.stop();
m_modifierTracker->cancelMetaPressed();
showSettings();
});
@ -105,7 +110,7 @@ void GlobalShortcuts::init()
layoutsAction->setShortcut(QKeySequence(Qt::META + Qt::Key_W));
KGlobalAccel::setGlobalShortcut(layoutsAction, QKeySequence(Qt::META + Qt::Key_W));
connect(layoutsAction, &QAction::triggered, this, [this]() {
m_metaPressedTimer.stop();
m_modifierTracker->cancelMetaPressed();
m_corona->layoutManager()->showLatteSettingsDialog(Types::LayoutPage);
});
@ -115,7 +120,7 @@ void GlobalShortcuts::init()
universalSettingsAction->setShortcut(QKeySequence(Qt::META + Qt::Key_E));
KGlobalAccel::setGlobalShortcut(universalSettingsAction, QKeySequence(Qt::META + Qt::Key_E));
connect(universalSettingsAction, &QAction::triggered, this, [this]() {
m_metaPressedTimer.stop();
m_modifierTracker->cancelMetaPressed();
m_corona->layoutManager()->showLatteSettingsDialog(Types::PreferencesPage);
});
@ -178,95 +183,10 @@ void GlobalShortcuts::init()
m_singleMetaAction->setShortcut(QKeySequence(Qt::META));
//display shortcut badges while holding Meta
initModifiers();
m_metaPressedTimer.setInterval(700);
connect(&m_metaPressedTimer, &QTimer::timeout, this, [&]() {
connect(m_modifierTracker, &ShortcutsPart::ModifierTracker::metaModifierPressed, this, [&]() {
m_metaShowedViews = true;
showDocks();
});
connect(&m_keyInfo, &KModifierKeyInfo::keyPressed, this, [&](Qt::Key key, bool state) {
Qt::Key nKey = normalizeKey(key);
//! ignore modifiers that we do not take into account
if (!modifierIsTracked(nKey)) {
return;
}
m_pressed[nKey] = state;
if (nKey == Qt::Key_Super_L) {
bool singleKey{singleModifierPressed(Qt::Key_Super_L)};
if (state && singleKey) {
m_metaPressedTimer.start();
} else if (!state || !singleKey) {
m_metaPressedTimer.stop();
}
} else {
m_metaPressedTimer.stop();
}
emit modifiersChanged();
});
}
void GlobalShortcuts::initModifiers()
{
m_pressed[Qt::Key_Super_L] = false;
m_pressed[Qt::Key_Control] = false;
m_pressed[Qt::Key_Alt] = false;
m_pressed[Qt::Key_Shift] = false;
}
bool GlobalShortcuts::modifierIsTracked(Qt::Key key)
{
return(key == Qt::Key_Super_L || key == Qt::Key_Super_R || key == Qt::Key_Control || key == Qt::Key_Alt || key == Qt::Key_Shift);
}
bool GlobalShortcuts::noModifierPressed()
{
foreach(Qt::Key modifier, m_pressed.keys()) {
if ( m_pressed[modifier]) {
return false;
}
}
return true;
}
bool GlobalShortcuts::sequenceModifierPressed(const QKeySequence &seq)
{
if (seq.isEmpty()) {
return false;
}
int mod = seq[seq.count() - 1] & Qt::KeyboardModifierMask;
if ( ((mod & Qt::SHIFT) && m_pressed[Qt::Key_Shift])
|| ((mod & Qt::CTRL) && m_pressed[Qt::Key_Control])
|| ((mod & Qt::ALT) && m_pressed[Qt::Key_Alt])
|| ((mod & Qt::META) && m_pressed[Qt::Key_Super_L])) {
return true;
}
return false;
}
bool GlobalShortcuts::singleModifierPressed(Qt::Key key)
{
foreach(Qt::Key modifier, m_pressed.keys()) {
if ( (modifier != key && m_pressed[modifier])
|| (modifier == key && !m_pressed[modifier]) ) {
return false;
}
}
return true;
}
Qt::Key GlobalShortcuts::normalizeKey(Qt::Key key)
{
return key == Qt::Key_Super_L || key == key == Qt::Key_Super_R ? Qt::Key_Super_L : key;
}
//! Activate launcher menu through dbus interface
@ -375,7 +295,7 @@ bool GlobalShortcuts::activatePlasmaTaskManagerEntryAtContainment(const Plasma::
bool GlobalShortcuts::activateLatteEntryAtContainment(const Latte::View *view, int index, Qt::Key modifier)
{
m_metaPressedTimer.stop();
m_modifierTracker->cancelMetaPressed();
if (QQuickItem *containmentInterface = view->containment()->property("_plasma_graphicObject").value<QQuickItem *>()) {
const auto &childItems = containmentInterface->childItems();
@ -435,7 +355,7 @@ bool GlobalShortcuts::activateLatteEntryAtContainment(const Latte::View *view, i
//! Activate task manager entry
void GlobalShortcuts::activateEntry(int index, Qt::Key modifier)
{
m_metaPressedTimer.stop();
m_modifierTracker->cancelMetaPressed();
m_lastInvokedAction = dynamic_cast<QAction *>(sender());
@ -873,7 +793,7 @@ void GlobalShortcuts::hideDocksTimerSlot()
// qDebug() << "MEMORY ::: " << m_hideDocks.count() << " _ " << m_calledItems.count() << " _ " << m_methodsShowNumbers.count();
if (QX11Info::isPlatformX11()) {
if (!sequenceModifierPressed(m_lastInvokedAction->shortcut())) {
if (!m_modifierTracker->sequenceModifierPressed(m_lastInvokedAction->shortcut())) {
m_lastInvokedAction = Q_NULLPTR;
if (docksToHideAreValid()) {

@ -27,6 +27,7 @@
// Qt
#include <QMetaMethod>
#include <QQuickItem>
#include <QPointer>
#include <QTimer>
// KDE
@ -39,6 +40,9 @@ class Containment;
namespace Latte {
class Corona;
class View;
namespace ShortcutsPart{
class ModifierTracker;
}
}
namespace Latte {
@ -76,21 +80,6 @@ private:
int applicationLauncherId(const Plasma::Containment *c);
//! <key> modifier is tracked for changes
bool modifierIsTracked(Qt::Key key);
//! none of tracked modifiers is pressed
bool noModifierPressed();
//! at least one of the modifiers from KeySequence is pressed
bool sequenceModifierPressed(const QKeySequence &seq);
//! only <key> is pressed and no other modifier
bool singleModifierPressed(Qt::Key key);
//! adjust key in more general values, e.g. Super_L and Super_R both return Super_L
Qt::Key normalizeKey(Qt::Key key);
QList<Latte::View *> sortedViewsList(QHash<const Plasma::Containment *, Latte::View *> *views);
private:
@ -106,13 +95,8 @@ private:
QList<QQuickItem *> m_calledItems;
QList<QMetaMethod> m_methodsShowNumbers;
Latte::Corona *m_corona{nullptr};
KModifierKeyInfo m_keyInfo;
QTimer m_metaPressedTimer;
//! keep a record for modifiers
QHash<Qt::Key, bool> m_pressed;
QPointer<ShortcutsPart::ModifierTracker> m_modifierTracker;
QPointer<Latte::Corona> m_corona;
};
}

@ -0,0 +1,131 @@
/*
* Copyright 2019 Michail Vourlakos <mvourlakos@gmail.com>
*
* This file is part of Latte-Dock
*
* Latte-Dock is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Latte-Dock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "modifiertracker.h"
#include <QKeySequence>
namespace Latte {
namespace ShortcutsPart {
ModifierTracker::ModifierTracker(QObject *parent)
: QObject(parent)
{
init();
}
ModifierTracker::~ModifierTracker()
{
}
void ModifierTracker::init()
{
m_metaPressedTimer.setInterval(700);
m_pressed[Qt::Key_Super_L] = false;
m_pressed[Qt::Key_Control] = false;
m_pressed[Qt::Key_Alt] = false;
m_pressed[Qt::Key_Shift] = false;
connect(&m_metaPressedTimer, &QTimer::timeout, this, &ModifierTracker::metaModifierPressed);
connect(&m_modifierKeyInfo, &KModifierKeyInfo::keyPressed, this, [&](Qt::Key key, bool state) {
Qt::Key nKey = normalizeKey(key);
//! ignore modifiers that we do not take into account
if (!modifierIsTracked(nKey)) {
return;
}
m_pressed[nKey] = state;
if (nKey == Qt::Key_Super_L) {
bool singleKey{singleModifierPressed(Qt::Key_Super_L)};
if (state && singleKey) {
m_metaPressedTimer.start();
} else if (!state || !singleKey) {
cancelMetaPressed();
}
} else {
cancelMetaPressed();
}
emit modifiersChanged();
});
}
bool ModifierTracker::modifierIsTracked(Qt::Key key)
{
return(key == Qt::Key_Super_L || key == Qt::Key_Super_R || key == Qt::Key_Control || key == Qt::Key_Alt || key == Qt::Key_Shift);
}
bool ModifierTracker::noModifierPressed()
{
foreach(Qt::Key modifier, m_pressed.keys()) {
if ( m_pressed[modifier]) {
return false;
}
}
return true;
}
bool ModifierTracker::sequenceModifierPressed(const QKeySequence &seq)
{
if (seq.isEmpty()) {
return false;
}
int mod = seq[seq.count() - 1] & Qt::KeyboardModifierMask;
if ( ((mod & Qt::SHIFT) && m_pressed[Qt::Key_Shift])
|| ((mod & Qt::CTRL) && m_pressed[Qt::Key_Control])
|| ((mod & Qt::ALT) && m_pressed[Qt::Key_Alt])
|| ((mod & Qt::META) && m_pressed[Qt::Key_Super_L])) {
return true;
}
return false;
}
bool ModifierTracker::singleModifierPressed(Qt::Key key)
{
foreach(Qt::Key modifier, m_pressed.keys()) {
if ( (modifier != key && m_pressed[modifier])
|| (modifier == key && !m_pressed[modifier]) ) {
return false;
}
}
return true;
}
Qt::Key ModifierTracker::normalizeKey(Qt::Key key)
{
return key == Qt::Key_Super_L || key == key == Qt::Key_Super_R ? Qt::Key_Super_L : key;
}
void ModifierTracker::cancelMetaPressed()
{
m_metaPressedTimer.stop();
}
}
}

@ -0,0 +1,78 @@
/*
* Copyright 2019 Michail Vourlakos <mvourlakos@gmail.com>
*
* This file is part of Latte-Dock
*
* Latte-Dock is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Latte-Dock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MODIFIERTRACKER_H
#define MODIFIERTRACKER_H
// Qt
#include <QHash>
#include <QObject>
#include <QTimer>
// KDE
#include <KModifierKeyInfo>
namespace Latte {
namespace ShortcutsPart {
class ModifierTracker: public QObject {
Q_OBJECT
public:
ModifierTracker(QObject *parent);
~ModifierTracker() override;
//! cancel meta is pressed delayer
void cancelMetaPressed();
//! none of tracked modifiers is pressed
bool noModifierPressed();
//! at least one of the modifiers from KeySequence is pressed
bool sequenceModifierPressed(const QKeySequence &seq);
//! only <key> is pressed and no other modifier
bool singleModifierPressed(Qt::Key key);
signals:
void metaModifierPressed();
void modifiersChanged();
private:
void init();
//! <key> modifier is tracked for changes
bool modifierIsTracked(Qt::Key key);
//! adjust key in more general values, e.g. Super_L and Super_R both return Super_L
Qt::Key normalizeKey(Qt::Key key);
private:
KModifierKeyInfo m_modifierKeyInfo;
QTimer m_metaPressedTimer;
//! keep a record for modifiers
QHash<Qt::Key, bool> m_pressed;
};
}
}
#endif
Loading…
Cancel
Save