diff --git a/effects/trackmouse/trackmouse.h b/effects/trackmouse/trackmouse.h --- a/effects/trackmouse/trackmouse.h +++ b/effects/trackmouse/trackmouse.h @@ -4,6 +4,7 @@ Copyright (C) 2006 Lubos Lunak Copyright (C) 2010 Jorge Mata +Copyright (C) 2018 Vlad Zagorodniy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -61,7 +62,7 @@ bool init(); void loadTexture(); QRect m_lastRect[2]; - bool m_active, m_mousePolling; + bool m_mousePolling; float m_angle; float m_angleBase; GLTexture* m_texture[2]; @@ -72,6 +73,13 @@ QAction* m_action; QImage m_image[2]; Qt::KeyboardModifiers m_modifiers; + + enum class State { + ActivatedByModifiers, + ActivatedByShortcut, + Inactive + }; + State m_state = State::Inactive; }; } // namespace diff --git a/effects/trackmouse/trackmouse.cpp b/effects/trackmouse/trackmouse.cpp --- a/effects/trackmouse/trackmouse.cpp +++ b/effects/trackmouse/trackmouse.cpp @@ -4,6 +4,7 @@ Copyright (C) 2006 Lubos Lunak Copyright (C) 2010 Jorge Mata +Copyright (C) 2018 Vlad Zagorodniy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -42,8 +43,7 @@ { TrackMouseEffect::TrackMouseEffect() - : m_active(false) - , m_angle(0) + : m_angle(0) { initConfig(); m_texture[0] = m_texture[1] = 0; @@ -107,21 +107,18 @@ void TrackMouseEffect::prePaintScreen(ScreenPrePaintData& data, int time) { - if (m_active) { - QTime t = QTime::currentTime(); - m_angle = ((t.second() % 4) * m_angleBase) + (t.msec() / 1000.0 * m_angleBase); - m_lastRect[0].moveCenter(cursorPos()); - m_lastRect[1].moveCenter(cursorPos()); - data.paint |= m_lastRect[0].adjusted(-1,-1,1,1); - } + QTime t = QTime::currentTime(); + m_angle = ((t.second() % 4) * m_angleBase) + (t.msec() / 1000.0 * m_angleBase); + m_lastRect[0].moveCenter(cursorPos()); + m_lastRect[1].moveCenter(cursorPos()); + data.paint |= m_lastRect[0].adjusted(-1,-1,1,1); + effects->prePaintScreen(data, time); } void TrackMouseEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data) { effects->paintScreen(mask, region, data); // paint normal screen - if (!m_active) - return; if ( effects->isOpenGLCompositing() && m_texture[0] && m_texture[1]) { ShaderBinder binder(ShaderTrait::MapTexture); @@ -191,9 +188,7 @@ void TrackMouseEffect::postPaintScreen() { - if (m_active) { - effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1)); - } + effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1)); effects->postPaintScreen(); } @@ -215,39 +210,68 @@ #endif m_lastRect[0].moveCenter(cursorPos()); m_lastRect[1].moveCenter(cursorPos()); - m_active = true; m_angle = 0; return true; } void TrackMouseEffect::toggle() { - if (m_mousePolling) - return; + switch (m_state) { + case State::ActivatedByModifiers: + case State::ActivatedByShortcut: + m_state = State::Inactive; + break; - if (m_active) { - m_active = false; - } else if (!init()) { - return; + case State::Inactive: + if (!init()) { + return; + } + m_state = State::ActivatedByShortcut; + break; + + default: + Q_UNREACHABLE(); + break; } - effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1)); + + effects->addRepaint(m_lastRect[0].adjusted(-1, -1, 1, 1)); } void TrackMouseEffect::slotMouseChanged(const QPoint&, const QPoint&, Qt::MouseButtons, Qt::MouseButtons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers) { - if (!m_mousePolling) // we didn't ask for it but maybe someone else did... + if (!m_mousePolling) { // we didn't ask for it but maybe someone else did... return; - if (m_modifiers && modifiers == m_modifiers) { - if (!m_active && !init()) { + } + + switch (m_state) { + case State::ActivatedByModifiers: + if (modifiers == m_modifiers) { return; } - effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1)); - } else if (m_active) { - m_active = false; - effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1)); + m_state = State::Inactive; + break; + + case State::ActivatedByShortcut: + return; + + case State::Inactive: + if (modifiers != m_modifiers) { + return; + } + if (!init()) { + return; + } + m_state = State::ActivatedByModifiers; + break; + + default: + Q_UNREACHABLE(); + break; } + + effects->addRepaint(m_lastRect[0].adjusted(-1, -1, 1, 1)); } void TrackMouseEffect::loadTexture() @@ -280,7 +304,7 @@ bool TrackMouseEffect::isActive() const { - return m_active; + return m_state != State::Inactive; } } // namespace diff --git a/effects/trackmouse/trackmouse_config.h b/effects/trackmouse/trackmouse_config.h --- a/effects/trackmouse/trackmouse_config.h +++ b/effects/trackmouse/trackmouse_config.h @@ -52,7 +52,6 @@ private Q_SLOTS: void shortcutChanged(const QKeySequence &seq); private: - void checkModifiers(); TrackMouseEffectConfigForm* m_ui; KActionCollection* m_actionCollection; }; diff --git a/effects/trackmouse/trackmouse_config.cpp b/effects/trackmouse/trackmouse_config.cpp --- a/effects/trackmouse/trackmouse_config.cpp +++ b/effects/trackmouse/trackmouse_config.cpp @@ -44,6 +44,8 @@ namespace KWin { +static const QString s_toggleTrackMouseActionName = QStringLiteral("TrackMouse"); + TrackMouseEffectConfigForm::TrackMouseEffectConfigForm(QWidget* parent) : QWidget(parent) { setupUi(this); @@ -64,7 +66,7 @@ m_actionCollection->setConfigGroup(QStringLiteral("TrackMouse")); m_actionCollection->setConfigGlobal(true); - QAction *a = m_actionCollection->addAction(QStringLiteral("TrackMouse")); + QAction *a = m_actionCollection->addAction(s_toggleTrackMouseActionName); a->setText(i18n("Track mouse")); a->setProperty("isConfigurationAction", true); @@ -81,20 +83,16 @@ { } -void TrackMouseEffectConfig::checkModifiers() -{ - const bool modifiers = m_ui->kcfg_Shift->isChecked() || m_ui->kcfg_Alt->isChecked() || - m_ui->kcfg_Control->isChecked() || m_ui->kcfg_Meta->isChecked(); - m_ui->modifierRadio->setChecked(modifiers); - m_ui->shortcutRadio->setChecked(!modifiers); -} - void TrackMouseEffectConfig::load() { KCModule::load(); - checkModifiers(); - emit changed(false); + if (QAction *a = m_actionCollection->action(s_toggleTrackMouseActionName)) { + auto shortcuts = KGlobalAccel::self()->shortcut(a); + if (!shortcuts.isEmpty()) { + m_ui->shortcut->setKeySequence(shortcuts.first()); + } + } } void TrackMouseEffectConfig::save() @@ -111,15 +109,13 @@ { KCModule::defaults(); m_ui->shortcut->clearKeySequence(); - checkModifiers(); } void TrackMouseEffectConfig::shortcutChanged(const QKeySequence &seq) { if (QAction *a = m_actionCollection->action(QStringLiteral("TrackMouse"))) { KGlobalAccel::self()->setShortcut(a, QList() << seq, KGlobalAccel::NoAutoloading); } -// m_actionCollection->writeSettings(); emit changed(true); } diff --git a/effects/trackmouse/trackmouse_config.ui b/effects/trackmouse/trackmouse_config.ui --- a/effects/trackmouse/trackmouse_config.ui +++ b/effects/trackmouse/trackmouse_config.ui @@ -6,8 +6,8 @@ 0 0 - 327 - 104 + 345 + 112 @@ -22,19 +22,35 @@ - + - Modifiers + Keyboard shortcut: - - - false + + + + + + Modifier keys: + + + + - + + 0 + + + 0 + + + 0 + + 0 @@ -68,20 +84,6 @@ - - - - Shortcut - - - - - - - false - - - @@ -97,38 +99,5 @@ - - - shortcutRadio - toggled(bool) - shortcut - setEnabled(bool) - - - 107 - 75 - - - 183 - 75 - - - - - modifierRadio - toggled(bool) - widget - setEnabled(bool) - - - 99 - 44 - - - 309 - 52 - - - - +