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->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)

View File

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

View File

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

View File

@@ -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();