aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2018-02-01 16:33:34 +0100
committerEike Ziller <eike.ziller@qt.io>2018-02-06 08:07:31 +0000
commite8dbc168146aa81eb8ad2ee426f5f7b9c4f6cf10 (patch)
tree89fd7627bab2c8b8079e6d81222c42e3216b1bf6
parent34a2e61dca41c604ff7c0962ad8b09fad71cdbb8 (diff)
ModelEditor: Simplify creation of commands and tool buttons
Change-Id: I569d2894edb791be2393e6cb7a1878e88b373797 Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
-rw-r--r--src/plugins/modeleditor/actionhandler.cpp40
-rw-r--r--src/plugins/modeleditor/actionhandler.h11
-rw-r--r--src/plugins/modeleditor/modeleditor.cpp50
-rw-r--r--src/plugins/modeleditor/modeleditor.h6
4 files changed, 56 insertions, 51 deletions
diff --git a/src/plugins/modeleditor/actionhandler.cpp b/src/plugins/modeleditor/actionhandler.cpp
index 17de4270d5..69a9ed59a2 100644
--- a/src/plugins/modeleditor/actionhandler.cpp
+++ b/src/plugins/modeleditor/actionhandler.cpp
@@ -143,12 +143,12 @@ void ActionHandler::createActions()
d->copyAction = registerCommand(Core::Constants::COPY, &ModelEditor::copy, d->context)->action();
d->pasteAction = registerCommand(Core::Constants::PASTE, &ModelEditor::paste, d->context)->action();
Core::Command *removeCommand = registerCommand(
- Constants::REMOVE_SELECTED_ELEMENTS, &ModelEditor::removeSelectedElements, d->context, true,
+ Constants::REMOVE_SELECTED_ELEMENTS, &ModelEditor::removeSelectedElements, d->context,
tr("&Remove"), QKeySequence::Delete);
medit->addAction(removeCommand, Core::Constants::G_EDIT_COPYPASTE);
d->removeAction = removeCommand->action();
Core::Command *deleteCommand = registerCommand(
- Constants::DELETE_SELECTED_ELEMENTS, &ModelEditor::deleteSelectedElements, d->context, true,
+ Constants::DELETE_SELECTED_ELEMENTS, &ModelEditor::deleteSelectedElements, d->context,
tr("&Delete"), QKeySequence("Ctrl+D"));
medit->addAction(deleteCommand, Core::Constants::G_EDIT_COPYPASTE);
d->deleteAction = deleteCommand->action();
@@ -160,14 +160,14 @@ void ActionHandler::createActions()
menuTools->addMenu(menuModelEditor);
Core::Command *exportDiagramCommand = registerCommand(
- Constants::EXPORT_DIAGRAM, &ModelEditor::exportDiagram, d->context, true,
+ Constants::EXPORT_DIAGRAM, &ModelEditor::exportDiagram, d->context,
tr("Export Diagram..."));
exportDiagramCommand->setAttribute(Core::Command::CA_Hide);
mfile->addAction(exportDiagramCommand, Core::Constants::G_FILE_EXPORT);
d->exportDiagramAction = exportDiagramCommand->action();
Core::Command *exportSelectedElementsCommand = registerCommand(
- Constants::EXPORT_SELECTED_ELEMENTS, &ModelEditor::exportSelectedElements, d->context, true,
+ Constants::EXPORT_SELECTED_ELEMENTS, &ModelEditor::exportSelectedElements, d->context,
tr("Export Selected Elements..."));
exportSelectedElementsCommand->setAttribute(Core::Command::CA_Hide);
mfile->addAction(exportSelectedElementsCommand, Core::Constants::G_FILE_EXPORT);
@@ -186,18 +186,22 @@ void ActionHandler::createActions()
menuModelEditor->addAction(resetZoomCommand);
d->openParentDiagramAction = registerCommand(
- Constants::OPEN_PARENT_DIAGRAM, &ModelEditor::openParentDiagram, Core::Context(), true,
- tr("Open Parent Diagram"), QKeySequence("Ctrl+Shift+P"))->action();
- d->openParentDiagramAction->setIcon(QIcon(":/modeleditor/up.png"));
- registerCommand(Constants::ACTION_ADD_PACKAGE, nullptr, Core::Context(), true, tr("Add Package"));
- registerCommand(Constants::ACTION_ADD_COMPONENT, nullptr, Core::Context(), true, tr("Add Component"));
- registerCommand(Constants::ACTION_ADD_CLASS, nullptr, Core::Context(), true, tr("Add Class"));
- registerCommand(Constants::ACTION_ADD_CANVAS_DIAGRAM, nullptr, Core::Context(), true, tr("Add Canvas Diagram"));
+ Constants::OPEN_PARENT_DIAGRAM, &ModelEditor::openParentDiagram, Core::Context(),
+ tr("Open Parent Diagram"), QKeySequence("Ctrl+Shift+P"),
+ QIcon(":/modeleditor/up.png"))->action();
+ registerCommand(Constants::ACTION_ADD_PACKAGE, nullptr, Core::Context(), tr("Add Package"),
+ QKeySequence(), QIcon(":/modelinglib/48x48/package.png"));
+ registerCommand(Constants::ACTION_ADD_COMPONENT, nullptr, Core::Context(), tr("Add Component"),
+ QKeySequence(), QIcon(":/modelinglib/48x48/component.png"));
+ registerCommand(Constants::ACTION_ADD_CLASS, nullptr, Core::Context(), tr("Add Class"),
+ QKeySequence(), QIcon(":/modelinglib/48x48/class.png"));
+ registerCommand(Constants::ACTION_ADD_CANVAS_DIAGRAM, nullptr, Core::Context(), tr("Add Canvas Diagram"),
+ QKeySequence(), QIcon(":/modelinglib/48x48/canvas-diagram.png"));
d->synchronizeBrowserAction = registerCommand(
- Constants::ACTION_SYNC_BROWSER, nullptr, Core::Context(), true,
+ Constants::ACTION_SYNC_BROWSER, nullptr, Core::Context(),
tr("Synchronize Browser and Diagram") + "<br><i><small>"
- + tr("Press && Hold for options") + "</small></i>")->action();
- d->synchronizeBrowserAction->setIcon(Utils::Icons::LINK.icon());
+ + tr("Press && Hold for options") + "</small></i>", QKeySequence(),
+ Utils::Icons::LINK.icon())->action();
d->synchronizeBrowserAction->setCheckable(true);
auto editPropertiesAction = new QAction(tr("Edit Element Properties"), Core::ICore::mainWindow());
@@ -237,11 +241,13 @@ std::function<void()> invokeOnCurrentModelEditor(void (ModelEditor::*function)()
}
Core::Command *ActionHandler::registerCommand(const Core::Id &id, void (ModelEditor::*function)(),
- const Core::Context &context, bool scriptable, const QString &title,
- const QKeySequence &keySequence)
+ const Core::Context &context, const QString &title,
+ const QKeySequence &keySequence, const QIcon &icon)
{
auto action = new QAction(title, this);
- Core::Command *command = Core::ActionManager::registerAction(action, id, context, scriptable);
+ if (!icon.isNull())
+ action->setIcon(icon);
+ Core::Command *command = Core::ActionManager::registerAction(action, id, context, /*scriptable=*/true);
if (!keySequence.isEmpty())
command->setDefaultKeySequence(keySequence);
if (function)
diff --git a/src/plugins/modeleditor/actionhandler.h b/src/plugins/modeleditor/actionhandler.h
index f79a4f7d99..192a665839 100644
--- a/src/plugins/modeleditor/actionhandler.h
+++ b/src/plugins/modeleditor/actionhandler.h
@@ -25,10 +25,11 @@
#pragma once
-#include <QObject>
-
#include <coreplugin/icontext.h>
+#include <QIcon>
+#include <QObject>
+
#include <functional>
QT_BEGIN_NAMESPACE
@@ -79,9 +80,9 @@ private:
void onEditItem();
Core::Command *registerCommand(const Core::Id &id, void (ModelEditor::*function)(),
- const Core::Context &context,
- bool scriptable = true, const QString &title = QString(),
- const QKeySequence &keySequence = QKeySequence());
+ const Core::Context &context, const QString &title = QString(),
+ const QKeySequence &keySequence = QKeySequence(),
+ const QIcon &icon = QIcon());
private:
ActionHandlerPrivate *d;
diff --git a/src/plugins/modeleditor/modeleditor.cpp b/src/plugins/modeleditor/modeleditor.cpp
index 4744d69f10..3fa5aad9a9 100644
--- a/src/plugins/modeleditor/modeleditor.cpp
+++ b/src/plugins/modeleditor/modeleditor.cpp
@@ -69,6 +69,8 @@
#include "qmt/tasks/diagramscenecontroller.h"
#include "qmt/tasks/finddiagramvisitor.h"
+#include <coreplugin/actionmanager/actionmanager.h>
+#include <coreplugin/actionmanager/command.h>
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/minisplitter.h>
@@ -323,22 +325,18 @@ void ModelEditor::init(QWidget *parent)
toolbarLayout->addWidget(d->diagramSelector, 1);
toolbarLayout->addStretch(1);
- toolbarLayout->addWidget(
- createToolbarCommandButton(Constants::ACTION_ADD_PACKAGE, [this]() { onAddPackage(); },
- QIcon(":/modelinglib/48x48/package.png"),
- tr("Add Package"), d->toolbar));
- toolbarLayout->addWidget(
- createToolbarCommandButton(Constants::ACTION_ADD_COMPONENT, [this]() { onAddComponent(); },
- QIcon(":/modelinglib/48x48/component.png"),
- tr("Add Component"), d->toolbar));
- toolbarLayout->addWidget(
- createToolbarCommandButton(Constants::ACTION_ADD_CLASS, [this]() { onAddClass(); },
- QIcon(":/modelinglib/48x48/class.png"),
- tr("Add Class"), d->toolbar));
- toolbarLayout->addWidget(
- createToolbarCommandButton(Constants::ACTION_ADD_CANVAS_DIAGRAM, [this]() { onAddCanvasDiagram(); },
- QIcon(":/modelinglib/48x48/canvas-diagram.png"),
- tr("Add Canvas Diagram"), d->toolbar));
+ toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_PACKAGE,
+ [this]() { onAddPackage(); },
+ d->toolbar));
+ toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_COMPONENT,
+ [this]() { onAddComponent(); },
+ d->toolbar));
+ toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_CLASS,
+ [this]() { onAddClass(); },
+ d->toolbar));
+ toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_CANVAS_DIAGRAM,
+ [this]() { onAddCanvasDiagram(); },
+ d->toolbar));
toolbarLayout->addSpacing(20);
auto syncToggleButton = new Core::CommandButton(Constants::ACTION_SYNC_BROWSER, d->toolbar);
@@ -807,18 +805,18 @@ void ModelEditor::expandModelTreeToDepth(int depth)
d->modelTreeView->expandToDepth(depth);
}
-QToolButton *ModelEditor::createToolbarCommandButton(const Core::Id &id, const std::function<void()> &slot,
- const QIcon &icon, const QString &toolTipBase,
+QToolButton *ModelEditor::createToolbarCommandButton(const Core::Id &id,
+ const std::function<void()> &slot,
QWidget *parent)
{
- auto button = new Core::CommandButton(id, parent);
- auto action = new QAction(button);
- action->setIcon(icon);
- action->setToolTip(toolTipBase);
- button->setDefaultAction(action);
- //button->setIcon(icon);
- //button->setToolTipBase(toolTipBase);
- connect(button, &Core::CommandButton::clicked, this, slot);
+ Core::Command *command = Core::ActionManager::command(id);
+ QTC_CHECK(command);
+ const QString text = command ? command->description() : QString();
+ auto action = new QAction(text, this);
+ action->setIcon(command ? command->action()->icon() : QIcon());
+ auto button = Core::Command::toolButtonWithAppendedShortcut(action, command);
+ button->setParent(parent);
+ connect(button, &QToolButton::clicked, this, slot);
return button;
}
diff --git a/src/plugins/modeleditor/modeleditor.h b/src/plugins/modeleditor/modeleditor.h
index 427b5d47dd..639f7fab38 100644
--- a/src/plugins/modeleditor/modeleditor.h
+++ b/src/plugins/modeleditor/modeleditor.h
@@ -104,9 +104,9 @@ private:
void showProperties(qmt::MDiagram *diagram, const QList<qmt::DElement *> &diagramElements);
void clearProperties();
void expandModelTreeToDepth(int depth);
- QToolButton *createToolbarCommandButton(const Core::Id &id, const std::function<void()> &slot,
- const QIcon &icon,
- const QString &toolTipBase, QWidget *parent);
+ QToolButton *createToolbarCommandButton(const Core::Id &id,
+ const std::function<void()> &slot,
+ QWidget *parent);
bool updateButtonIconByTheme(QAbstractButton *button, const QString &name);
void showZoomIndicator();