From e935291c85441cbe1cb548850f3ca86aee3b8ea4 Mon Sep 17 00:00:00 2001 From: Tim Jenssen Date: Tue, 3 Mar 2020 17:14:07 +0100 Subject: [PATCH] QmlDesigner: Use ActionManager in FormEditor We assign an ICore context to the FormEditorWidget and associate it with a Context (C_QMLFORMEDITOR). This guarantees that the action are only active when the FormEditorWidget has focus. The actions are then registered as Commands. The shortcuts are set on the command and are configurable in the options of Qt Creator. Task-number: QDS-1712 Change-Id: I327f35736205c9337386cf70e6f058d4b19be50e Reviewed-by: Tim Jenssen --- .../formeditor/formeditorwidget.cpp | 42 ++++++++++++------- .../components/formeditor/formeditorwidget.h | 2 + .../qmldesigner/qmldesignerconstants.h | 5 +++ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp index ec7e1d782d8..24177ccc6c9 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp @@ -43,6 +43,8 @@ #include #include +#include +#include #include #include @@ -59,6 +61,11 @@ namespace QmlDesigner { FormEditorWidget::FormEditorWidget(FormEditorView *view) : m_formEditorView(view) { + Core::Context context(Constants::C_QMLFORMEDITOR); + m_context = new Core::IContext(this); + m_context->setContext(context); + m_context->setWidget(this); + auto fillLayout = new QVBoxLayout(this); fillLayout->setContentsMargins(0, 0, 0, 0); fillLayout->setSpacing(0); @@ -77,21 +84,19 @@ FormEditorWidget::FormEditorWidget(FormEditorView *view) : m_noSnappingAction->setCheckable(true); m_noSnappingAction->setChecked(true); m_noSnappingAction->setIcon(Icons::NO_SNAPPING.icon()); + registerActionAsCommand(m_noSnappingAction, Constants::FORMEDITOR_NO_SNAPPING, QKeySequence(Qt::Key_T)); - m_snappingAndAnchoringAction = layoutActionGroup->addAction(tr("Snap to parent or sibling items and generate anchors (W).")); - m_snappingAndAnchoringAction->setShortcut(Qt::Key_W); - m_snappingAndAnchoringAction->setShortcutContext(Qt::WidgetWithChildrenShortcut); + m_snappingAndAnchoringAction = layoutActionGroup->addAction(tr("Snap to parent or sibling items and generate anchors")); m_snappingAndAnchoringAction->setCheckable(true); m_snappingAndAnchoringAction->setChecked(true); m_snappingAndAnchoringAction->setIcon(Icons::NO_SNAPPING_AND_ANCHORING.icon()); + registerActionAsCommand(m_snappingAndAnchoringAction, Constants::FORMEDITOR_NO_SNAPPING_AND_ANCHORING, QKeySequence(Qt::Key_W)); - m_snappingAction = layoutActionGroup->addAction(tr("Snap to parent or sibling items but do not generate anchors (E).")); - m_snappingAction->setShortcut(Qt::Key_E); - m_snappingAction->setShortcutContext(Qt::WidgetWithChildrenShortcut); + m_snappingAction = layoutActionGroup->addAction(tr("Snap to parent or sibling items but do not generate anchors")); m_snappingAction->setCheckable(true); m_snappingAction->setChecked(true); m_snappingAction->setIcon(Icons::SNAPPING.icon()); - + registerActionAsCommand(m_snappingAction, Constants::FORMEDITOR_SNAPPING, QKeySequence(Qt::Key_E)); addActions(layoutActionGroup->actions()); upperActions.append(layoutActionGroup->actions()); @@ -101,12 +106,12 @@ FormEditorWidget::FormEditorWidget(FormEditorView *view) : addAction(separatorAction); upperActions.append(separatorAction); - m_showBoundingRectAction = new QAction(tr("Show bounding rectangles and stripes for empty items (A)."), this); - m_showBoundingRectAction->setShortcut(Qt::Key_A); - m_showBoundingRectAction->setShortcutContext(Qt::WidgetWithChildrenShortcut); + m_showBoundingRectAction = new QAction(Utils::Icons::BOUNDING_RECT.icon(), + tr("Show bounding rectangles and stripes for empty items"), + this); m_showBoundingRectAction->setCheckable(true); m_showBoundingRectAction->setChecked(false); - m_showBoundingRectAction->setIcon(Utils::Icons::BOUNDING_RECT.icon()); + registerActionAsCommand(m_showBoundingRectAction, Constants::FORMEDITOR_NO_SHOW_BOUNDING_RECTANGLE, QKeySequence(Qt::Key_A)); addAction(m_showBoundingRectAction.data()); upperActions.append(m_showBoundingRectAction.data()); @@ -155,10 +160,8 @@ FormEditorWidget::FormEditorWidget(FormEditorView *view) : upperActions.append(m_zoomAction.data()); m_toolBox->addRightSideAction(m_zoomAction.data()); - m_resetAction = new QAction(tr("Reset view (R)."), this); - m_resetAction->setShortcut(Qt::Key_R); - m_resetAction->setShortcutContext(Qt::WidgetWithChildrenShortcut); - m_resetAction->setIcon(Utils::Icons::RESET_TOOLBAR.icon()); + m_resetAction = new QAction(Utils::Icons::RESET_TOOLBAR.icon(), tr("Reset view"), this); + registerActionAsCommand(m_resetAction, Constants::FORMEDITOR_REFRESH, QKeySequence(Qt::Key_R)); addAction(m_resetAction.data()); upperActions.append(m_resetAction.data()); @@ -207,6 +210,15 @@ void FormEditorWidget::changeBackgound(const QColor &color) m_graphicsView->activateColoredBackground(color); } +void FormEditorWidget::registerActionAsCommand(QAction *action, Core::Id id, const QKeySequence &keysequence) +{ + Core::Context context(Constants::C_QMLFORMEDITOR); + + Core::Command *command = Core::ActionManager::registerAction(action, id, context); + command->setDefaultKeySequence(keysequence); + command->augmentActionWithShortcutToolTip(action); +} + void FormEditorWidget::wheelEvent(QWheelEvent *event) { if (event->modifiers().testFlag(Qt::ControlModifier)) { diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h index f7efbf60c05..f8cd0cd1a8d 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h +++ b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h @@ -98,6 +98,7 @@ private: void changeRootItemWidth(const QString &widthText); void changeRootItemHeight(const QString &heightText); void changeBackgound(const QColor &color); + void registerActionAsCommand(QAction *action, Core::Id id, const QKeySequence &keysequence); QPointer m_formEditorView; QPointer m_graphicsView; @@ -115,6 +116,7 @@ private: QPointer m_option3DAction; QPointer m_resetAction; QPointer m_documentErrorWidget; + Core::IContext *m_context = nullptr; }; } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/qmldesignerconstants.h b/src/plugins/qmldesigner/qmldesignerconstants.h index 5dcb014e58f..89c5b643967 100644 --- a/src/plugins/qmldesigner/qmldesignerconstants.h +++ b/src/plugins/qmldesigner/qmldesignerconstants.h @@ -48,6 +48,11 @@ const char TOGGLE_RIGHT_SIDEBAR[] = "QmlDesigner.ToggleRightSideBar"; const char TOGGLE_STATES_EDITOR[] = "QmlDesigner.ToggleStatesEditor"; const char GO_INTO_COMPONENT[] = "QmlDesigner.GoIntoComponent"; const char EXPORT_AS_IMAGE[] = "QmlDesigner.ExportAsImage"; +const char FORMEDITOR_REFRESH[] = "QmlDesigner.FormEditor.Refresh"; +const char FORMEDITOR_SNAPPING[] = "QmlDesigner.FormEditor.Snapping"; +const char FORMEDITOR_NO_SNAPPING[] = "QmlDesigner.FormEditor.NoSnapping"; +const char FORMEDITOR_NO_SNAPPING_AND_ANCHORING[] = "QmlDesigner.FormEditor.NoSnappingAndAnchoring"; +const char FORMEDITOR_NO_SHOW_BOUNDING_RECTANGLE[] = "QmlDesigner.FormEditor.ShowBoundingRectangle"; const char QML_DESIGNER_SUBFOLDER[] = "/designer/"; const char QUICK_3D_ASSETS_FOLDER[] = "/Quick3DAssets";