Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:epopov:branches:openSUSE:Factory
spectacle
2001-fix-shortcuts.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 2001-fix-shortcuts.patch of Package spectacle
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 64b423217a7b01f0b1028936bd7234dbd8f22266..0be3e07718b281ae511fbc67e5d569d9419a6df9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ add_executable(spectacle Gui/SpectacleWindow.cpp Gui/SpectacleMenu.cpp Gui/ViewerWindow.cpp + Gui/Action.cpp Gui/Annotations/AnnotationDocument.cpp Gui/Annotations/AnnotationViewport.cpp Gui/Annotations/EditAction.cpp diff --git a/src/Gui/Action.cpp b/src/Gui/Action.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1454d986166da54fa34c166fd7040cf0ec4720b --- /dev/null +++ b/src/Gui/Action.cpp @@ -0,0 +1,24 @@ +#include "Action.h" + +#include <QKeyEvent> + +Action::~Action() +{ + parent()->removeEventFilter(this); +} + +bool Action::eventFilter(QObject *watched, QEvent *event) +{ + if (watched == parent() && event->type() == QEvent::KeyPress) { + const QKeyEvent *keyEvent = static_cast<const QKeyEvent *>(event); + for (const QKeySequence::StandardKey &shortcut : m_shortcuts) { + if (keyEvent->matches(shortcut)) { + Q_EMIT activated(); + + return true; + } + } + } + + return QObject::eventFilter(watched, event); +} diff --git a/src/Gui/Action.h b/src/Gui/Action.h new file mode 100644 index 0000000000000000000000000000000000000000..89fff9a70bab2586769b46891769b2b3793d08b4 --- /dev/null +++ b/src/Gui/Action.h @@ -0,0 +1,84 @@ +#ifndef ACTION_H +#define ACTION_H + +#include <QKeySequence> +#include <QObject> + +class Action : public QObject +{ + Q_OBJECT + +public: + template<typename Func> + explicit Action(QObject *parent, const QList<QKeySequence::StandardKey> &shortcuts, const typename QtPrivate::FunctionPointer<Func>::Object *receiver, Func slot, Qt::ConnectionType type = Qt::AutoConnection) + : QObject(parent) + , m_shortcuts(shortcuts) + { + parent->installEventFilter(this); + + connect(this, &Action::activated, receiver, std::move(slot), type); + } + + template<typename Func> + explicit Action(typename QtPrivate::FunctionPointer<Func>::Object *parent, const QList<QKeySequence::StandardKey> &shortcuts, Func slot, Qt::ConnectionType type = Qt::AutoConnection) + : QObject(parent) + , m_shortcuts(shortcuts) + { + parent->installEventFilter(this); + + connect(this, &Action::activated, parent, std::move(slot), type); + } + + template<typename Func> + explicit Action(QObject *parent, const QList<QKeySequence::StandardKey> &shortcuts, const QObject *context, Func slot, Qt::ConnectionType type = Qt::AutoConnection) + : QObject(parent) + , m_shortcuts(shortcuts) + { + parent->installEventFilter(this); + + connect(this, &Action::activated, context, std::move(slot), type); + } + + template<typename Func> + explicit Action(QObject *parent, QKeySequence::StandardKey shortcut, const typename QtPrivate::FunctionPointer<Func>::Object *receiver, Func slot, Qt::ConnectionType type = Qt::AutoConnection) + : Action(parent, QList<QKeySequence::StandardKey>() << shortcut, receiver, slot, type) + {} + + template<typename Func> + explicit Action(typename QtPrivate::FunctionPointer<Func>::Object *parent, QKeySequence::StandardKey shortcut, Func slot, Qt::ConnectionType type = Qt::AutoConnection) + : Action(parent, QList<QKeySequence::StandardKey>() << shortcut, slot, type) + {} + + template<typename Func> + explicit Action(QObject *parent, QKeySequence::StandardKey shortcut, const QObject *context, Func slot, Qt::ConnectionType type = Qt::AutoConnection) + : Action(parent, QList<QKeySequence::StandardKey>() << shortcut, context, slot, type) + {} + + template<typename Func> + explicit Action(QObject *parent, const QList<QKeySequence::StandardKey> &shortcuts, Func slot) + : QObject(parent) + , m_shortcuts(shortcuts) + { + parent->installEventFilter(this); + + connect(this, &Action::activated, std::move(slot)); + } + + template<typename Func> + explicit Action(QObject *parent, QKeySequence::StandardKey shortcut, Func slot) + : Action(parent, QList<QKeySequence::StandardKey>() << shortcut, slot) + {} + + ~Action() override; + +Q_SIGNALS: + void activated(); + +protected: + bool eventFilter(QObject *watched, QEvent *event) override; + +private: + const QList<QKeySequence::StandardKey> m_shortcuts; +}; + +#endif // ACTION_H diff --git a/src/Gui/CaptureWindow.cpp b/src/Gui/CaptureWindow.cpp index b0fa6740d84bf2b3b2e62add4270217210222fb9..b869451a193ea4c91bb65dfe7dd919970fb3421c 100644 --- a/src/Gui/CaptureWindow.cpp +++ b/src/Gui/CaptureWindow.cpp @@ -7,6 +7,7 @@ #include "CaptureWindow.h" +#include "Action.h" #include "Config.h" #include "SpectacleCore.h" #include "Gui/SelectionEditor.h" @@ -71,6 +72,27 @@ CaptureWindow::CaptureWindow(Mode mode, QScreen *screen, QQmlEngine *engine, QWi if (auto rootItem = rootObject()) { rootItem->installEventFilter(selectionEditor); } + + new Action(this, QKeySequence::Save, &CaptureWindow::save); + new Action(this, QKeySequence::SaveAs, &CaptureWindow::saveAs); + new Action(this, QKeySequence::Copy, &CaptureWindow::copyImage); + new Action(this, QKeySequence::Print, &CaptureWindow::showPrintDialog); + new Action(this, QKeySequence::Undo, [this]() { + if (isAnnotating()) { + auto document = SpectacleCore::instance()->annotationDocument(); + if (document->undoStackDepth() > 0) { + document->undo(); + } + } + }); + new Action(this, QKeySequence::Redo, [this]() { + if (isAnnotating()) { + auto document = SpectacleCore::instance()->annotationDocument(); + if (document->redoStackDepth() > 0) { + document->redo(); + } + } + }); } CaptureWindow::~CaptureWindow() @@ -153,37 +175,6 @@ void CaptureWindow::mousePressEvent(QMouseEvent *event) SpectacleWindow::mousePressEvent(event); } -void CaptureWindow::keyReleaseEvent(QKeyEvent *event) -{ - SpectacleWindow::keyReleaseEvent(event); - if (event->isAccepted()) { - return; - } - if (event->matches(QKeySequence::Save)) { - event->accept(); - save(); - } else if (event->matches(QKeySequence::SaveAs)) { - event->accept(); - saveAs(); - } else if (event->matches(QKeySequence::Copy)) { - event->accept(); - copyImage(); - } else if (event->matches(QKeySequence::Print)) { - event->accept(); - showPrintDialog(); - } - auto document = SpectacleCore::instance()->annotationDocument(); - if (!event->isAccepted() && document) { - if (document->undoStackDepth() > 0 && event->matches(QKeySequence::Undo)) { - event->accept(); - document->undo(); - } else if (document->redoStackDepth() > 0 && event->matches(QKeySequence::Redo)) { - event->accept(); - document->redo(); - } - } -} - void CaptureWindow::showEvent(QShowEvent *event) { SpectacleWindow::showEvent(event); diff --git a/src/Gui/CaptureWindow.h b/src/Gui/CaptureWindow.h index c8c62f027d29eb0c54730b9e5209cf72e0ed6075..b5e87a8340297d774e73dfce771023f47ba0db90 100644 --- a/src/Gui/CaptureWindow.h +++ b/src/Gui/CaptureWindow.h @@ -44,7 +44,6 @@ Q_SIGNALS: protected: void mousePressEvent(QMouseEvent *event) override; - void keyReleaseEvent(QKeyEvent *event) override; void showEvent(QShowEvent *event) override; private: diff --git a/src/Gui/SpectacleWindow.cpp b/src/Gui/SpectacleWindow.cpp index d648f3450d6521b84e4d1ee2045d78781914a9ef..4c66d9a943243ee4c8714a9c053abd2de07b7cd2 100644 --- a/src/Gui/SpectacleWindow.cpp +++ b/src/Gui/SpectacleWindow.cpp @@ -8,6 +8,7 @@ #include "SpectacleWindow.h" +#include "Action.h" #include "ExportManager.h" #include "SpectacleCore.h" #include "Geometry.h" @@ -58,6 +59,15 @@ SpectacleWindow::SpectacleWindow(QQmlEngine *engine, QWindow *parent) // set up QML setResizeMode(QQuickView::SizeRootObjectToView); m_context->setContextProperty(u"contextWindow"_s, this); + + new Action(this, { QKeySequence::Cancel, QKeySequence::Close, QKeySequence::Quit }, [this]() { + SpectacleCore::instance()->cancelScreenshot(); + }); + new Action(this, QKeySequence::Preferences, &SpectacleWindow::showPreferencesDialog); + new Action(this, QKeySequence::New, []() { + SpectacleCore::instance()->takeNewScreenshot(); + }); + new Action(this, QKeySequence::HelpContents, HelpMenu::instance(), &HelpMenu::showAppHelp); } SpectacleWindow::~SpectacleWindow() @@ -419,46 +429,4 @@ void SpectacleWindow::mousePressEvent(QMouseEvent *event) } } -void SpectacleWindow::keyPressEvent(QKeyEvent *event) -{ - // Events need to be processed normally first for events to reach items - QQuickView::keyPressEvent(event); - if (event->isAccepted()) { - return; - } - m_pressedKeys = event->key() | event->modifiers(); -} - -void SpectacleWindow::keyReleaseEvent(QKeyEvent *event) -{ - // Events need to be processed normally first for events to reach items - QQuickView::keyReleaseEvent(event); - if (event->isAccepted()) { - return; - } - // Cancel defaults to Escape in QPlatformTheme. - // Handling this here fixes https://bugs.kde.org/show_bug.cgi?id=428478 - if ((event->matches(QKeySequence::Quit) - || event->matches(QKeySequence::Close) - || event->matches(QKeySequence::Cancel)) - // We need to check if these were pressed previously or else pressing escape - // in a dialog will quit spectacle when you release the escape key. - && m_pressedKeys == event->key() | event->modifiers() - ) { - event->accept(); - auto spectacleCore = SpectacleCore::instance(); - spectacleCore->cancelScreenshot(); - } else if (event->matches(QKeySequence::Preferences)) { - event->accept(); - showPreferencesDialog(); - } else if (event->matches(QKeySequence::New)) { - event->accept(); - SpectacleCore::instance()->takeNewScreenshot(); - } else if (event->matches(QKeySequence::HelpContents)) { - event->accept(); - HelpMenu::instance()->showAppHelp(); - } - m_pressedKeys = {}; -} - #include "moc_SpectacleWindow.cpp" diff --git a/src/Gui/SpectacleWindow.h b/src/Gui/SpectacleWindow.h index 5ff19c2df719408f7be0cd58d51abd67981b27f7..79073783e3569717f2864388e5927ed405bb0af9 100644 --- a/src/Gui/SpectacleWindow.h +++ b/src/Gui/SpectacleWindow.h @@ -107,8 +107,6 @@ protected: void setSource(const QUrl &source, const QVariantMap &initialProperties); void mousePressEvent(QMouseEvent *event) override; - void keyPressEvent(QKeyEvent *event) override; - void keyReleaseEvent(QKeyEvent *event) override; static QList<SpectacleWindow *> s_spectacleWindowInstances; static bool s_synchronizingVisibility; @@ -120,6 +118,4 @@ protected: const std::unique_ptr<QQmlContext> m_context; std::unique_ptr<QQmlComponent> m_component; - - QKeySequence m_pressedKeys; }; diff --git a/src/Gui/ViewerWindow.cpp b/src/Gui/ViewerWindow.cpp index 701c3a41e20aa572f95d957507965096e8e7cd6c..67d95bd5f67e05c1017526d51b5a76a4de7767a2 100644 --- a/src/Gui/ViewerWindow.cpp +++ b/src/Gui/ViewerWindow.cpp @@ -7,6 +7,7 @@ #include "ViewerWindow.h" +#include "Action.h" #include "Config.h" #include "SpectacleCore.h" #include "Gui/ExportMenu.h" @@ -40,6 +41,27 @@ ViewerWindow::ViewerWindow(Mode mode, QQmlEngine *engine, QWindow *parent) setResizeMode(QQuickView::SizeRootObjectToView); setMode(mode); // sets source and other stuff based on mode. m_oldWindowStates = windowStates(); + + new Action(this, QKeySequence::Save, &ViewerWindow::save); + new Action(this, QKeySequence::SaveAs, &ViewerWindow::saveAs); + new Action(this, QKeySequence::Copy, &ViewerWindow::copyImage); + new Action(this, QKeySequence::Print, &ViewerWindow::showPrintDialog); + new Action(this, QKeySequence::Undo, [this]() { + if (isAnnotating()) { + auto document = SpectacleCore::instance()->annotationDocument(); + if (document->undoStackDepth() > 0) { + document->undo(); + } + } + }); + new Action(this, QKeySequence::Redo, [this]() { + if (isAnnotating()) { + auto document = SpectacleCore::instance()->annotationDocument(); + if (document->redoStackDepth() > 0) { + document->redo(); + } + } + }); } ViewerWindow::~ViewerWindow() @@ -288,35 +310,4 @@ void ViewerWindow::resizeEvent(QResizeEvent *event) } } -void ViewerWindow::keyReleaseEvent(QKeyEvent *event) -{ - SpectacleWindow::keyReleaseEvent(event); - if (event->isAccepted() || m_mode == Dialog) { - return; - } - if (event->matches(QKeySequence::Save)) { - event->accept(); - save(); - } else if (event->matches(QKeySequence::SaveAs)) { - event->accept(); - saveAs(); - } else if (event->matches(QKeySequence::Copy)) { - event->accept(); - copyImage(); - } else if (event->matches(QKeySequence::Print)) { - event->accept(); - showPrintDialog(); - } - auto document = SpectacleCore::instance()->annotationDocument(); - if (!event->isAccepted() && document) { - if (document->undoStackDepth() > 0 && event->matches(QKeySequence::Undo)) { - event->accept(); - document->undo(); - } else if (document->redoStackDepth() > 0 && event->matches(QKeySequence::Redo)) { - event->accept(); - document->redo(); - } - } -} - #include "moc_ViewerWindow.cpp" diff --git a/src/Gui/ViewerWindow.h b/src/Gui/ViewerWindow.h index 55003fd8fff922b9871e795ee0101a3e8840c869..6a715df84222c2d2b8cceb21eefe09fbf1d64737 100644 --- a/src/Gui/ViewerWindow.h +++ b/src/Gui/ViewerWindow.h @@ -46,7 +46,6 @@ public: protected: bool event(QEvent *event) override; void resizeEvent(QResizeEvent *event) override; - void keyReleaseEvent(QKeyEvent *event) override; private: explicit ViewerWindow(Mode mode, QQmlEngine *engine, QWindow *parent = nullptr);
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor