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 <tim.jenssen@qt.io>
This commit is contained in:
Tim Jenssen
2020-03-03 17:14:07 +01:00
parent 4f55b9a0fa
commit e935291c85
3 changed files with 34 additions and 15 deletions

View File

@@ -43,6 +43,8 @@
#include <zoomaction.h>
#include <toolbox.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/icore.h>
#include <utils/fileutils.h>
@@ -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)) {

View File

@@ -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<FormEditorView> m_formEditorView;
QPointer<FormEditorGraphicsView> m_graphicsView;
@@ -115,6 +116,7 @@ private:
QPointer<Option3DAction> m_option3DAction;
QPointer<QAction> m_resetAction;
QPointer<DocumentWarningWidget> m_documentErrorWidget;
Core::IContext *m_context = nullptr;
};
} // namespace QmlDesigner

View File

@@ -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";