From 41e945f1903390f010c58d1b4a4dedd2d85d6ff6 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 2 May 2022 10:07:18 +0100 Subject: [PATCH] New method to setup the menu. One lambda less --- .../QuickCommands/quickcommandswidget.cpp | 47 ++++++++++--------- .../QuickCommands/quickcommandswidget.h | 2 + 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/plugins/QuickCommands/quickcommandswidget.cpp b/src/plugins/QuickCommands/quickcommandswidget.cpp index 2dabcb16..18a0c229 100644 --- a/src/plugins/QuickCommands/quickcommandswidget.cpp +++ b/src/plugins/QuickCommands/quickcommandswidget.cpp @@ -44,28 +44,7 @@ QuickCommandsWidget::QuickCommandsWidget(QWidget *parent) connect(ui->commandsTreeView, &QTreeView::doubleClicked, this, &QuickCommandsWidget::invokeCommand); connect(ui->commandsTreeView, &QTreeView::clicked, this, &QuickCommandsWidget::indexSelected); - connect(ui->commandsTreeView, &QTreeView::customContextMenuRequested, [this](const QPoint &pos) { - QModelIndex idx = ui->commandsTreeView->indexAt(pos); - if (!idx.isValid()) - return; - auto sourceIdx = priv->filterModel->mapToSource(idx); - const bool isParent = sourceIdx.parent() == priv->model->invisibleRootItem()->index(); - QMenu *menu = new QMenu(this); - - if (!isParent) { - auto actionEdit = new QAction(QStringLiteral("Edit"), ui->commandsTreeView); - menu->addAction(actionEdit); - connect(actionEdit, &QAction::triggered, this, &QuickCommandsWidget::editMode); - } else { - auto actionRename = new QAction(QStringLiteral("Rename"), ui->commandsTreeView); - menu->addAction(actionRename); - connect(actionRename, &QAction::triggered, this, &QuickCommandsWidget::triggerRename); - } - auto actionDelete = new QAction(QStringLiteral("Delete"), ui->commandsTreeView); - menu->addAction(actionDelete); - connect(actionDelete, &QAction::triggered, this, &QuickCommandsWidget::triggerDelete); - menu->popup(ui->commandsTreeView->viewport()->mapToGlobal(pos)); - }); + connect(ui->commandsTreeView, &QTreeView::customContextMenuRequested, this, &QuickCommandsWidget::createMenu); viewMode(); } @@ -235,3 +214,27 @@ bool QuickCommandsWidget::valid() } return true; } + +void QuickCommandsWidget::createMenu(const QPoint &pos) +{ + QModelIndex idx = ui->commandsTreeView->indexAt(pos); + if (!idx.isValid()) + return; + auto sourceIdx = priv->filterModel->mapToSource(idx); + const bool isParent = sourceIdx.parent() == priv->model->invisibleRootItem()->index(); + QMenu *menu = new QMenu(this); + + if (!isParent) { + auto actionEdit = new QAction(QStringLiteral("Edit"), ui->commandsTreeView); + menu->addAction(actionEdit); + connect(actionEdit, &QAction::triggered, this, &QuickCommandsWidget::editMode); + } else { + auto actionRename = new QAction(QStringLiteral("Rename"), ui->commandsTreeView); + menu->addAction(actionRename); + connect(actionRename, &QAction::triggered, this, &QuickCommandsWidget::triggerRename); + } + auto actionDelete = new QAction(QStringLiteral("Delete"), ui->commandsTreeView); + menu->addAction(actionDelete); + connect(actionDelete, &QAction::triggered, this, &QuickCommandsWidget::triggerDelete); + menu->popup(ui->commandsTreeView->viewport()->mapToGlobal(pos)); +} diff --git a/src/plugins/QuickCommands/quickcommandswidget.h b/src/plugins/QuickCommands/quickcommandswidget.h index 84e6c493..bc97be5d 100644 --- a/src/plugins/QuickCommands/quickcommandswidget.h +++ b/src/plugins/QuickCommands/quickcommandswidget.h @@ -47,6 +47,8 @@ public: Q_SLOT void triggerDelete(); + Q_SLOT void createMenu(const QPoint &pos); + void setModel(QuickCommandsModel *model); void setCurrentController(Konsole::SessionController *controller);