ModelEditor: Remove code duplication in action handler

Change-Id: I7d4beff0c0408c0ef8655953434c9d324fb452cc
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Eike Ziller
2018-01-31 13:15:45 +01:00
parent ebc975e4a4
commit 9c68afa08c
4 changed files with 41 additions and 129 deletions

View File

@@ -136,22 +136,22 @@ void ActionHandler::createActions()
{ {
Core::ActionContainer *medit = Core::ActionManager::actionContainer(Core::Constants::M_EDIT); Core::ActionContainer *medit = Core::ActionManager::actionContainer(Core::Constants::M_EDIT);
d->undoAction = registerCommand(Core::Constants::UNDO, [this]() { undo(); }, d->context)->action(); d->undoAction = registerCommand(Core::Constants::UNDO, &ModelEditor::undo, d->context)->action();
d->redoAction = registerCommand(Core::Constants::REDO, [this]() { redo(); }, d->context)->action(); d->redoAction = registerCommand(Core::Constants::REDO, &ModelEditor::redo, d->context)->action();
d->cutAction = registerCommand(Core::Constants::CUT, [this]() { cut(); }, d->context)->action(); d->cutAction = registerCommand(Core::Constants::CUT, &ModelEditor::cut, d->context)->action();
d->copyAction = registerCommand(Core::Constants::COPY, [this]() { copy(); }, d->context)->action(); d->copyAction = registerCommand(Core::Constants::COPY, &ModelEditor::copy, d->context)->action();
d->pasteAction = registerCommand(Core::Constants::PASTE, [this]() { 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, [this]() { removeSelectedElements(); }, d->context, true, Constants::REMOVE_SELECTED_ELEMENTS, &ModelEditor::removeSelectedElements, d->context, true,
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, [this]() { deleteSelectedElements(); }, d->context, true, Constants::DELETE_SELECTED_ELEMENTS, &ModelEditor::deleteSelectedElements, d->context, true,
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();
d->selectAllAction = registerCommand(Core::Constants::SELECTALL, [this]() { selectAll(); }, d->context)->action(); d->selectAllAction = registerCommand(Core::Constants::SELECTALL, &ModelEditor::selectAll, d->context)->action();
Core::ActionContainer *menuModelEditor = Core::ActionManager::createMenu(Constants::MENU_ID); Core::ActionContainer *menuModelEditor = Core::ActionManager::createMenu(Constants::MENU_ID);
menuModelEditor->menu()->setTitle(tr("Model Editor")); menuModelEditor->menu()->setTitle(tr("Model Editor"));
@@ -159,13 +159,13 @@ void ActionHandler::createActions()
menuTools->addMenu(menuModelEditor); menuTools->addMenu(menuModelEditor);
Core::Command *exportDiagramCommand = registerCommand( Core::Command *exportDiagramCommand = registerCommand(
Constants::EXPORT_DIAGRAM, [this]() { exportDiagram(); }, d->context, true, Constants::EXPORT_DIAGRAM, &ModelEditor::exportDiagram, d->context, true,
tr("Export Diagram...")); tr("Export Diagram..."));
menuModelEditor->addAction(exportDiagramCommand); menuModelEditor->addAction(exportDiagramCommand);
d->exportDiagramAction = exportDiagramCommand->action(); d->exportDiagramAction = exportDiagramCommand->action();
Core::Command *exportSelectedElementsCommand = registerCommand( Core::Command *exportSelectedElementsCommand = registerCommand(
Constants::EXPORT_SELECTED_ELEMENTS, [this]() { exportSelectedElements(); }, d->context, true, Constants::EXPORT_SELECTED_ELEMENTS, &ModelEditor::exportSelectedElements, d->context, true,
tr("Export Selected Elements...")); tr("Export Selected Elements..."));
menuModelEditor->addAction(exportSelectedElementsCommand); menuModelEditor->addAction(exportSelectedElementsCommand);
d->exportSelectedElementsAction = exportSelectedElementsCommand->action(); d->exportSelectedElementsAction = exportSelectedElementsCommand->action();
@@ -173,22 +173,22 @@ void ActionHandler::createActions()
menuModelEditor->addSeparator(d->context); menuModelEditor->addSeparator(d->context);
Core::Command *zoomInCommand = registerCommand( Core::Command *zoomInCommand = registerCommand(
Constants::ZOOM_IN, [this]() { zoomIn(); }, d->context, true, Constants::ZOOM_IN, &ModelEditor::zoomIn, d->context, true,
tr("Zoom In"), QKeySequence("Ctrl++")); tr("Zoom In"), QKeySequence("Ctrl++"));
menuModelEditor->addAction(zoomInCommand); menuModelEditor->addAction(zoomInCommand);
Core::Command *zoomOutCommand = registerCommand( Core::Command *zoomOutCommand = registerCommand(
Constants::ZOOM_OUT, [this]() { zoomOut(); }, d->context, true, Constants::ZOOM_OUT, &ModelEditor::zoomOut, d->context, true,
tr("Zoom Out"), QKeySequence("Ctrl+-")); tr("Zoom Out"), QKeySequence("Ctrl+-"));
menuModelEditor->addAction(zoomOutCommand); menuModelEditor->addAction(zoomOutCommand);
Core::Command *resetZoomCommand = registerCommand( Core::Command *resetZoomCommand = registerCommand(
Constants::RESET_ZOOM, [this]() { resetZoom(); }, d->context, true, Constants::RESET_ZOOM, &ModelEditor::resetZoom, d->context, true,
tr("Reset Zoom"), QKeySequence("Ctrl+0")); tr("Reset Zoom"), QKeySequence("Ctrl+0"));
menuModelEditor->addAction(resetZoomCommand); menuModelEditor->addAction(resetZoomCommand);
d->openParentDiagramAction = registerCommand( d->openParentDiagramAction = registerCommand(
Constants::OPEN_PARENT_DIAGRAM, [this]() { openParentDiagram(); }, Core::Context(), true, Constants::OPEN_PARENT_DIAGRAM, &ModelEditor::openParentDiagram, Core::Context(), true,
tr("Open Parent Diagram"), QKeySequence("Ctrl+Shift+P"))->action(); tr("Open Parent Diagram"), QKeySequence("Ctrl+Shift+P"))->action();
d->openParentDiagramAction->setIcon(QIcon(":/modeleditor/up.png")); d->openParentDiagramAction->setIcon(QIcon(":/modeleditor/up.png"));
registerCommand(Constants::ACTION_ADD_PACKAGE, nullptr, Core::Context(), true, tr("Add Package")); registerCommand(Constants::ACTION_ADD_PACKAGE, nullptr, Core::Context(), true, tr("Add Package"));
@@ -215,69 +215,6 @@ void ActionHandler::createActions()
connect(editItemAction, &QAction::triggered, this, &ActionHandler::onEditItem); connect(editItemAction, &QAction::triggered, this, &ActionHandler::onEditItem);
} }
void ActionHandler::undo()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->undo();
}
void ActionHandler::redo()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->redo();
}
void ActionHandler::cut()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->cut();
}
void ActionHandler::copy()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->copy();
}
void ActionHandler::paste()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->paste();
}
void ActionHandler::removeSelectedElements()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->removeSelectedElements();
}
void ActionHandler::deleteSelectedElements()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->deleteSelectedElements();
}
void ActionHandler::selectAll()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->selectAll();
}
void ActionHandler::openParentDiagram()
{
auto editor = dynamic_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->openParentDiagram();
}
void ActionHandler::onEditProperties() void ActionHandler::onEditProperties()
{ {
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor()); auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
@@ -292,42 +229,16 @@ void ActionHandler::onEditItem()
editor->editSelectedItem(); editor->editSelectedItem();
} }
void ActionHandler::exportDiagram() std::function<void()> invokeOnCurrentModelEditor(void (ModelEditor::*function)())
{ {
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor()); return [function] {
if (editor) auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
editor->exportDiagram(false); if (editor)
(editor->*function)();
};
} }
void ActionHandler::exportSelectedElements() Core::Command *ActionHandler::registerCommand(const Core::Id &id, void (ModelEditor::*function)(),
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->exportDiagram(true);
}
void ActionHandler::zoomIn()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->zoomIn();
}
void ActionHandler::zoomOut()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->zoomOut();
}
void ActionHandler::resetZoom()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->resetZoom();
}
Core::Command *ActionHandler::registerCommand(const Core::Id &id, const std::function<void()> &slot,
const Core::Context &context, bool scriptable, const QString &title, const Core::Context &context, bool scriptable, const QString &title,
const QKeySequence &keySequence) const QKeySequence &keySequence)
{ {
@@ -335,8 +246,8 @@ Core::Command *ActionHandler::registerCommand(const Core::Id &id, const std::fun
Core::Command *command = Core::ActionManager::registerAction(action, id, context, scriptable); Core::Command *command = Core::ActionManager::registerAction(action, id, context, scriptable);
if (!keySequence.isEmpty()) if (!keySequence.isEmpty())
command->setDefaultKeySequence(keySequence); command->setDefaultKeySequence(keySequence);
if (slot) if (function)
connect(action, &QAction::triggered, this, slot); connect(action, &QAction::triggered, this, invokeOnCurrentModelEditor(function));
return command; return command;
} }

View File

@@ -43,6 +43,8 @@ class Command;
namespace ModelEditor { namespace ModelEditor {
namespace Internal { namespace Internal {
class ModelEditor;
class ActionHandler : class ActionHandler :
public QObject public QObject
{ {
@@ -73,24 +75,10 @@ public:
void createActions(); void createActions();
private: private:
void undo();
void redo();
void cut();
void copy();
void paste();
void removeSelectedElements();
void deleteSelectedElements();
void selectAll();
void openParentDiagram();
void onEditProperties(); void onEditProperties();
void onEditItem(); void onEditItem();
void exportDiagram();
void exportSelectedElements();
void zoomIn();
void zoomOut();
void resetZoom();
Core::Command *registerCommand(const Core::Id &id, const std::function<void()> &slot, Core::Command *registerCommand(const Core::Id &id, void (ModelEditor::*function)(),
const Core::Context &context, const Core::Context &context,
bool scriptable = true, const QString &title = QString(), bool scriptable = true, const QString &title = QString(),
const QKeySequence &keySequence = QKeySequence()); const QKeySequence &keySequence = QKeySequence());

View File

@@ -569,7 +569,17 @@ void ModelEditor::editSelectedItem()
onEditSelectedElement(); onEditSelectedElement();
} }
void ModelEditor::exportDiagram(bool selectedElements) void ModelEditor::exportDiagram()
{
exportToImage(/*selectedElements=*/false);
}
void ModelEditor::exportSelectedElements()
{
exportToImage(/*selectedElements=*/true);
}
void ModelEditor::exportToImage(bool selectedElements)
{ {
qmt::MDiagram *diagram = currentDiagram(); qmt::MDiagram *diagram = currentDiagram();
if (diagram) { if (diagram) {

View File

@@ -87,7 +87,8 @@ public:
void openParentDiagram(); void openParentDiagram();
void editProperties(); void editProperties();
void editSelectedItem(); void editSelectedItem();
void exportDiagram(bool selectedElements); void exportDiagram();
void exportSelectedElements();
void zoomIn(); void zoomIn();
void zoomOut(); void zoomOut();
void resetZoom(); void resetZoom();
@@ -158,6 +159,8 @@ private:
void synchronizeDiagramWithBrowser(); void synchronizeDiagramWithBrowser();
void synchronizeBrowserWithDiagram(const qmt::MDiagram *diagram); void synchronizeBrowserWithDiagram(const qmt::MDiagram *diagram);
void exportToImage(bool selectedElements);
private: private:
ModelEditorPrivate *d; ModelEditorPrivate *d;
}; };