OpenToonz - An open-source full-featured 2D animation creation software
C
feat(ui): optional toggle panels with shortcut
Ahl7623a02c617bfd
diff --git a/toonz/sources/include/toonzqt/menubarcommand.h b/toonz/sources/include/toonzqt/menubarcommand.hindex d424fae90..dc74c512b 100644--- a/toonz/sources/include/toonzqt/menubarcommand.h+++ b/toonz/sources/include/toonzqt/menubarcommand.h@@ -9,6 +9,7 @@#include "tcommon.h"#include <QAction>+#include <QKeySequence>#include <QMenu>#undef DVAPI@@ -134,6 +135,8 @@ class DVAPI CommandManager { // singletonstd::map<std::string, Node *> m_idTable;std::map<QAction *, Node *> m_qactionTable;std::map<std::string, Node *> m_shortcutTable;+ bool m_executeTriggeredByShortcut;+ QAction *m_pendingShortcutAction;CommandManager();@@ -175,6 +178,11 @@ public:* otherwise recall execute(menuAction).*/void execute(QAction *action, QAction *menuAction);void execute(CommandId id);+ bool executeTriggeredByShortcut() const {+ return m_executeTriggeredByShortcut;+ }+ QAction *actionForShortcutKey(const QKeySequence &key) const;+ void setPendingShortcutAction(QAction *action);void enable(CommandId id, bool enabled);// if id is a toggle (e.g. a checkable menu item) then set its status;@@ -272,6 +280,9 @@ public:DVAction(const QString &text, QObject *parent);DVAction(const QIcon &icon, const QString &text, QObject *parent);+protected:+ bool event(QEvent *event) override;+public slots:void onTriggered();};diff --git a/toonz/sources/toonz/custompanelmanager.cpp b/toonz/sources/toonz/custompanelmanager.cppindex cea686268..1ac96372b 100644--- a/toonz/sources/toonz/custompanelmanager.cpp+++ b/toonz/sources/toonz/custompanelmanager.cpp@@ -436,14 +436,7 @@ void CustomPanelManager::registerCustomPanelCommands() {if (!currentRoom) return;std::string panelType = panelTypeFromPanelId(m_panelId).toStdString();- QList<TPanel*> panels = currentRoom->findChildren<TPanel*>();-- for (TPanel* panel : panels) {- if (panel->getPanelType() == panelType && !panel->isHidden()) {- panel->close();- return;- }- }+ if (OpenFloatingPanel::closeVisiblePanelsOfType(panelType)) return;OpenFloatingPanel::getOrOpenFloatingPanel(panelType);}diff --git a/toonz/sources/toonz/floatingpanelcommand.cpp b/toonz/sources/toonz/floatingpanelcommand.cppindex 0dbd37aa1..7035e6519 100644--- a/toonz/sources/toonz/floatingpanelcommand.cpp+++ b/toonz/sources/toonz/floatingpanelcommand.cpp@@ -9,6 +9,7 @@#include "toonz/preferences.h"#include "toonz/preferencesitemids.h"+#include "toonzqt/menubarcommand.h"#include "toonzqt/styleeditor.h"#include <QMainWindow>@@ -47,19 +48,9 @@ void OpenFloatingPanel::execute() {return;}- if (Preferences::instance()->getBoolValue(togglePanelWithShortcut)) {- TMainWindow *currentRoom = TApp::instance()->getCurrentRoom();- if (currentRoom) {- bool closedAny = false;- const QList<TPanel *> panels = currentRoom->findChildren<TPanel *>();- for (TPanel *panel : panels) {- if (panel->getPanelType() == m_panelType && !panel->isHidden()) {- panel->close();- closedAny = true;- }- }- if (closedAny) return;- }+ if (Preferences::instance()->getBoolValue(togglePanelWithShortcut) &&+ CommandManager::instance()->executeTriggeredByShortcut()) {+ if (closeVisiblePanelsOfType(m_panelType)) return;}getOrOpenFloatingPanel(m_panelType);@@ -118,6 +109,21 @@ panel->raise();*/}+bool OpenFloatingPanel::closeVisiblePanelsOfType(const std::string &panelType) {+ TMainWindow *currentRoom = TApp::instance()->getCurrentRoom();+ if (!currentRoom) return false;++ bool closedAny = false;+ const QList<TPanel *> panels = currentRoom->findChildren<TPanel *>();+ for (TPanel *panel : panels) {+ if (panel->getPanelType() == panelType && !panel->isHidden()) {+ panel->close();+ closedAny = true;+ }+ }+ return closedAny;+}+TPanel *OpenFloatingPanel::getOrOpenFloatingPanel(const std::string &panelType) {TMainWindow *currentRoom = TApp::instance()->getCurrentRoom();diff --git a/toonz/sources/toonz/floatingpanelcommand.h b/toonz/sources/toonz/floatingpanelcommand.hindex 5cdf7e85a..0fab32833 100644--- a/toonz/sources/toonz/floatingpanelcommand.h+++ b/toonz/sources/toonz/floatingpanelcommand.h@@ -20,6 +20,7 @@ public:OpenFloatingPanel(CommandId id, const std::string &panelType, QString title);void execute() override;+ static bool closeVisiblePanelsOfType(const std::string &panelType);static TPanel *getOrOpenFloatingPanel(const std::string &panelType);};diff --git a/toonz/sources/toonz/preferencespopup.cpp b/toonz/sources/toonz/preferencespopup.cppindex 2407c355d..88dc623cc 100644--- a/toonz/sources/toonz/preferencespopup.cpp+++ b/toonz/sources/toonz/preferencespopup.cpp@@ -1315,11 +1315,11 @@ QString PreferencesPopup::getUIString(PreferencesItemId id) {{displayIn30bit, tr("30bit Display*")},{showIconsInMenu, tr("Show Icons In Menu*")},{showRoomBindButtons, tr("Show Room Bind Buttons*")},+ {togglePanelWithShortcut, tr("Toggle Panels with Shortcut")},{customHelpLink, tr("Quicklink URL:")},{viewerIndicatorEnabled, tr("Show Viewer Indicators")},{restoreViewerViewFromLastSession,tr("Restore Viewer Zoom and Pan from Last Session")},- {togglePanelWithShortcut, tr("Toggle Panels with Shortcut")},// Visualization{show0ThickLines, tr("Show Lines with Thickness 0")},@@ -1946,12 +1946,12 @@ QWidget* PreferencesPopup::createInterfacePage() {lay->addWidget(check30bitBtn, row - 1, 2, Qt::AlignRight);insertUI(showIconsInMenu, lay);insertUI(showRoomBindButtons, lay);+ insertUI(togglePanelWithShortcut, lay);insertUI(customHelpLink, lay);getUI<FileField*>(customHelpLink)->setToolTip(tr("Leave blank to use the local OpenToonz documentation index. ""To open a PDF at a specific page, append #page=12."));- insertUI(togglePanelWithShortcut, lay);lay->setRowStretch(lay->rowCount(), 1);insertFootNote(lay);diff --git a/toonz/sources/toonzqt/menubarcommand.cpp b/toonz/sources/toonzqt/menubarcommand.cppindex 1351799f0..ed72fb1f1 100644--- a/toonz/sources/toonzqt/menubarcommand.cpp+++ b/toonz/sources/toonzqt/menubarcommand.cpp@@ -12,6 +12,8 @@#include <QSettings>#include <QKeySequence>#include <QApplication>+#include <QEvent>+#include <QShortcutEvent>#include <sys/types.h>@@ -28,6 +30,27 @@ void updateToolTip(QAction *action) {action->setToolTip(tooltip);}+class ShortcutTriggerFilter final : public QObject {+ CommandManager *m_manager;++public:+ explicit ShortcutTriggerFilter(CommandManager *manager)+ : m_manager(manager) {}++protected:+ bool eventFilter(QObject *watched, QEvent *event) override {+ if (event->type() != QEvent::Shortcut) return false;++ QShortcutEvent *shortcutEvent = static_cast<QShortcutEvent *>(event);+ if (shortcutEvent->isAmbiguous()) return false;++ QAction *action = m_manager->actionForShortcutKey(shortcutEvent->key());+ if (action) m_manager->setPendingShortcutAction(action);++ return false;+ }+};+} // namespace//=========================================================@@ -59,7 +82,10 @@ void AuxActionsCreatorManager::createAuxActions(QObject *parent) {//=========================================================-CommandManager::CommandManager() {}+CommandManager::CommandManager()+ : m_executeTriggeredByShortcut(false), m_pendingShortcutAction(nullptr) {+ if (qApp) qApp->installEventFilter(new ShortcutTriggerFilter(this));+}//---------------------------------------------------------@@ -150,6 +176,22 @@ void CommandManager::define(CommandId id, CommandType type,// set handler (id, handler)// possibly changes enable/disable qaction state//+QAction *CommandManager::actionForShortcutKey(const QKeySequence &key) const {+ std::map<QAction *, Node *>::const_iterator it;+ for (it = m_qactionTable.begin(); it != m_qactionTable.end(); ++it) {+ if (it->first->shortcut() == key) return it->first;+ }+ return nullptr;+}++//---------------------------------------------------------++void CommandManager::setPendingShortcutAction(QAction *action) {+ m_pendingShortcutAction = action;+}++//---------------------------------------------------------+void CommandManager::setHandler(CommandId id,CommandHandlerInterface *handler) {Node *node = getNode(id);@@ -174,7 +216,10 @@ void CommandManager::execute(QAction *qaction) {std::map<QAction *, Node *>::iterator it = m_qactionTable.find(qaction);assert(it != m_qactionTable.end());if (it != m_qactionTable.end() && it->second->m_handler) {+ m_executeTriggeredByShortcut = (qaction == m_pendingShortcutAction);+ m_pendingShortcutAction = nullptr;it->second->m_handler->execute();+ m_executeTriggeredByShortcut = false;}}@@ -198,6 +243,7 @@ void CommandManager::execute(CommandId id) {// principalmente per i toolaction->setChecked(true);}+ m_executeTriggeredByShortcut = false;node->m_handler->execute();}}@@ -334,7 +380,10 @@ void CommandManager::setChecked(CommandId id, bool checked) {if (!node) return;if (node->m_qaction) {node->m_qaction->setChecked(checked);- if (node->m_handler) node->m_handler->execute();+ if (node->m_handler) {+ m_executeTriggeredByShortcut = false;+ node->m_handler->execute();+ }}}@@ -516,6 +565,17 @@ DVAction::DVAction(const QIcon &icon, const QString &text, QObject *parent)//-----------------------------------------------------------------------------+bool DVAction::event(QEvent *event) {+ if (event->type() == QEvent::Shortcut) {+ QShortcutEvent *shortcutEvent = static_cast<QShortcutEvent *>(event);+ if (!shortcutEvent->isAmbiguous() && shortcut() == shortcutEvent->key())+ CommandManager::instance()->setPendingShortcutAction(this);+ }+ return QAction::event(event);+}++//-----------------------------------------------------------------------------+void DVAction::onTriggered() { CommandManager::instance()->execute(this); }//=============================================================================