ModelEditor: Simplify creation of commands and tool buttons

Change-Id: I569d2894edb791be2393e6cb7a1878e88b373797
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2018-02-01 16:33:34 +01:00
parent 34a2e61dca
commit e8dbc16814
4 changed files with 56 additions and 51 deletions

View File

@@ -143,12 +143,12 @@ void ActionHandler::createActions()
d->copyAction = registerCommand(Core::Constants::COPY, &ModelEditor::copy, d->context)->action(); d->copyAction = registerCommand(Core::Constants::COPY, &ModelEditor::copy, d->context)->action();
d->pasteAction = registerCommand(Core::Constants::PASTE, &ModelEditor::paste, d->context)->action(); d->pasteAction = registerCommand(Core::Constants::PASTE, &ModelEditor::paste, d->context)->action();
Core::Command *removeCommand = registerCommand( 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); tr("&Remove"), QKeySequence::Delete);
medit->addAction(removeCommand, Core::Constants::G_EDIT_COPYPASTE); medit->addAction(removeCommand, Core::Constants::G_EDIT_COPYPASTE);
d->removeAction = removeCommand->action(); d->removeAction = removeCommand->action();
Core::Command *deleteCommand = registerCommand( 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")); tr("&Delete"), QKeySequence("Ctrl+D"));
medit->addAction(deleteCommand, Core::Constants::G_EDIT_COPYPASTE); medit->addAction(deleteCommand, Core::Constants::G_EDIT_COPYPASTE);
d->deleteAction = deleteCommand->action(); d->deleteAction = deleteCommand->action();
@@ -160,14 +160,14 @@ void ActionHandler::createActions()
menuTools->addMenu(menuModelEditor); menuTools->addMenu(menuModelEditor);
Core::Command *exportDiagramCommand = registerCommand( Core::Command *exportDiagramCommand = registerCommand(
Constants::EXPORT_DIAGRAM, &ModelEditor::exportDiagram, d->context, true, Constants::EXPORT_DIAGRAM, &ModelEditor::exportDiagram, d->context,
tr("Export Diagram...")); tr("Export Diagram..."));
exportDiagramCommand->setAttribute(Core::Command::CA_Hide); exportDiagramCommand->setAttribute(Core::Command::CA_Hide);
mfile->addAction(exportDiagramCommand, Core::Constants::G_FILE_EXPORT); mfile->addAction(exportDiagramCommand, Core::Constants::G_FILE_EXPORT);
d->exportDiagramAction = exportDiagramCommand->action(); d->exportDiagramAction = exportDiagramCommand->action();
Core::Command *exportSelectedElementsCommand = registerCommand( 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...")); tr("Export Selected Elements..."));
exportSelectedElementsCommand->setAttribute(Core::Command::CA_Hide); exportSelectedElementsCommand->setAttribute(Core::Command::CA_Hide);
mfile->addAction(exportSelectedElementsCommand, Core::Constants::G_FILE_EXPORT); mfile->addAction(exportSelectedElementsCommand, Core::Constants::G_FILE_EXPORT);
@@ -186,18 +186,22 @@ void ActionHandler::createActions()
menuModelEditor->addAction(resetZoomCommand); menuModelEditor->addAction(resetZoomCommand);
d->openParentDiagramAction = registerCommand( d->openParentDiagramAction = registerCommand(
Constants::OPEN_PARENT_DIAGRAM, &ModelEditor::openParentDiagram, Core::Context(), true, Constants::OPEN_PARENT_DIAGRAM, &ModelEditor::openParentDiagram, Core::Context(),
tr("Open Parent Diagram"), QKeySequence("Ctrl+Shift+P"))->action(); tr("Open Parent Diagram"), QKeySequence("Ctrl+Shift+P"),
d->openParentDiagramAction->setIcon(QIcon(":/modeleditor/up.png")); QIcon(":/modeleditor/up.png"))->action();
registerCommand(Constants::ACTION_ADD_PACKAGE, nullptr, Core::Context(), true, tr("Add Package")); registerCommand(Constants::ACTION_ADD_PACKAGE, nullptr, Core::Context(), tr("Add Package"),
registerCommand(Constants::ACTION_ADD_COMPONENT, nullptr, Core::Context(), true, tr("Add Component")); QKeySequence(), QIcon(":/modelinglib/48x48/package.png"));
registerCommand(Constants::ACTION_ADD_CLASS, nullptr, Core::Context(), true, tr("Add Class")); registerCommand(Constants::ACTION_ADD_COMPONENT, nullptr, Core::Context(), tr("Add Component"),
registerCommand(Constants::ACTION_ADD_CANVAS_DIAGRAM, nullptr, Core::Context(), true, tr("Add Canvas Diagram")); 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( 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("Synchronize Browser and Diagram") + "<br><i><small>"
+ tr("Press && Hold for options") + "</small></i>")->action(); + tr("Press && Hold for options") + "</small></i>", QKeySequence(),
d->synchronizeBrowserAction->setIcon(Utils::Icons::LINK.icon()); Utils::Icons::LINK.icon())->action();
d->synchronizeBrowserAction->setCheckable(true); d->synchronizeBrowserAction->setCheckable(true);
auto editPropertiesAction = new QAction(tr("Edit Element Properties"), Core::ICore::mainWindow()); 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)(), Core::Command *ActionHandler::registerCommand(const Core::Id &id, void (ModelEditor::*function)(),
const Core::Context &context, bool scriptable, const QString &title, const Core::Context &context, const QString &title,
const QKeySequence &keySequence) const QKeySequence &keySequence, const QIcon &icon)
{ {
auto action = new QAction(title, this); 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()) if (!keySequence.isEmpty())
command->setDefaultKeySequence(keySequence); command->setDefaultKeySequence(keySequence);
if (function) if (function)

View File

@@ -25,10 +25,11 @@
#pragma once #pragma once
#include <QObject>
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <QIcon>
#include <QObject>
#include <functional> #include <functional>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@@ -79,9 +80,9 @@ private:
void onEditItem(); void onEditItem();
Core::Command *registerCommand(const Core::Id &id, void (ModelEditor::*function)(), Core::Command *registerCommand(const Core::Id &id, void (ModelEditor::*function)(),
const Core::Context &context, const Core::Context &context, const QString &title = QString(),
bool scriptable = true, const QString &title = QString(), const QKeySequence &keySequence = QKeySequence(),
const QKeySequence &keySequence = QKeySequence()); const QIcon &icon = QIcon());
private: private:
ActionHandlerPrivate *d; ActionHandlerPrivate *d;

View File

@@ -69,6 +69,8 @@
#include "qmt/tasks/diagramscenecontroller.h" #include "qmt/tasks/diagramscenecontroller.h"
#include "qmt/tasks/finddiagramvisitor.h" #include "qmt/tasks/finddiagramvisitor.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/minisplitter.h> #include <coreplugin/minisplitter.h>
@@ -323,22 +325,18 @@ void ModelEditor::init(QWidget *parent)
toolbarLayout->addWidget(d->diagramSelector, 1); toolbarLayout->addWidget(d->diagramSelector, 1);
toolbarLayout->addStretch(1); toolbarLayout->addStretch(1);
toolbarLayout->addWidget( toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_PACKAGE,
createToolbarCommandButton(Constants::ACTION_ADD_PACKAGE, [this]() { onAddPackage(); }, [this]() { onAddPackage(); },
QIcon(":/modelinglib/48x48/package.png"), d->toolbar));
tr("Add Package"), d->toolbar)); toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_COMPONENT,
toolbarLayout->addWidget( [this]() { onAddComponent(); },
createToolbarCommandButton(Constants::ACTION_ADD_COMPONENT, [this]() { onAddComponent(); }, d->toolbar));
QIcon(":/modelinglib/48x48/component.png"), toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_CLASS,
tr("Add Component"), d->toolbar)); [this]() { onAddClass(); },
toolbarLayout->addWidget( d->toolbar));
createToolbarCommandButton(Constants::ACTION_ADD_CLASS, [this]() { onAddClass(); }, toolbarLayout->addWidget(createToolbarCommandButton(Constants::ACTION_ADD_CANVAS_DIAGRAM,
QIcon(":/modelinglib/48x48/class.png"), [this]() { onAddCanvasDiagram(); },
tr("Add Class"), d->toolbar)); 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->addSpacing(20); toolbarLayout->addSpacing(20);
auto syncToggleButton = new Core::CommandButton(Constants::ACTION_SYNC_BROWSER, d->toolbar); auto syncToggleButton = new Core::CommandButton(Constants::ACTION_SYNC_BROWSER, d->toolbar);
@@ -807,18 +805,18 @@ void ModelEditor::expandModelTreeToDepth(int depth)
d->modelTreeView->expandToDepth(depth); d->modelTreeView->expandToDepth(depth);
} }
QToolButton *ModelEditor::createToolbarCommandButton(const Core::Id &id, const std::function<void()> &slot, QToolButton *ModelEditor::createToolbarCommandButton(const Core::Id &id,
const QIcon &icon, const QString &toolTipBase, const std::function<void()> &slot,
QWidget *parent) QWidget *parent)
{ {
auto button = new Core::CommandButton(id, parent); Core::Command *command = Core::ActionManager::command(id);
auto action = new QAction(button); QTC_CHECK(command);
action->setIcon(icon); const QString text = command ? command->description() : QString();
action->setToolTip(toolTipBase); auto action = new QAction(text, this);
button->setDefaultAction(action); action->setIcon(command ? command->action()->icon() : QIcon());
//button->setIcon(icon); auto button = Core::Command::toolButtonWithAppendedShortcut(action, command);
//button->setToolTipBase(toolTipBase); button->setParent(parent);
connect(button, &Core::CommandButton::clicked, this, slot); connect(button, &QToolButton::clicked, this, slot);
return button; return button;
} }

View File

@@ -104,9 +104,9 @@ private:
void showProperties(qmt::MDiagram *diagram, const QList<qmt::DElement *> &diagramElements); void showProperties(qmt::MDiagram *diagram, const QList<qmt::DElement *> &diagramElements);
void clearProperties(); void clearProperties();
void expandModelTreeToDepth(int depth); void expandModelTreeToDepth(int depth);
QToolButton *createToolbarCommandButton(const Core::Id &id, const std::function<void()> &slot, QToolButton *createToolbarCommandButton(const Core::Id &id,
const QIcon &icon, const std::function<void()> &slot,
const QString &toolTipBase, QWidget *parent); QWidget *parent);
bool updateButtonIconByTheme(QAbstractButton *button, const QString &name); bool updateButtonIconByTheme(QAbstractButton *button, const QString &name);
void showZoomIndicator(); void showZoomIndicator();