From 6527604b93c950a9274078934a47d658a8221e1a Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Sat, 1 Oct 2022 19:36:08 +0200 Subject: [PATCH] feat: reimplement contextmenu Signed-off-by: Sefa Eyeoglu --- launcher/ui/MainWindow.cpp | 31 ++++++++++------------- launcher/ui/MainWindow.h | 2 +- launcher/ui/instanceview/InstanceView.cpp | 11 ++++++++ launcher/ui/instanceview/InstanceView.h | 2 ++ 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 1845a0eb2..229ca470a 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -872,9 +872,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow { view = new InstanceView(ui->centralWidget, APPLICATION->instances().get()); - view->installEventFilter(this); - view->setContextMenuPolicy(Qt::CustomContextMenu); - connect(view, &QWidget::customContextMenuRequested, this, &MainWindow::showInstanceContextMenu); + connect(view, &InstanceView::showContextMenu, this, &MainWindow::showInstanceContextMenu); ui->horizontalLayout->addWidget(view); } @@ -1047,30 +1045,27 @@ void MainWindow::konamiTriggered() qDebug() << "Super Secret Mode ACTIVATED!"; } -void MainWindow::showInstanceContextMenu(const QPoint &pos) +void MainWindow::showInstanceContextMenu(const QPoint &pos, InstancePtr inst) { QList actions; QAction *actionSep = new QAction("", this); actionSep->setSeparator(true); - bool onInstance = view->currentView()->indexAt(pos).isValid(); - if (onInstance) - { - actions = ui->instanceToolBar->actions(); + actions = ui->instanceToolBar->actions(); - // replace the change icon widget with an actual action - actions.replace(0, ui->actionChangeInstIcon); + // replace the change icon widget with an actual action + actions.replace(0, ui->actionChangeInstIcon); - // replace the rename widget with an actual action - actions.replace(1, ui->actionRenameInstance); + // replace the rename widget with an actual action + actions.replace(1, ui->actionRenameInstance); + + // add header + actions.prepend(actionSep); + QAction *actionVoid = new QAction(m_selectedInstance->name(), this); + actionVoid->setEnabled(false); + actions.prepend(actionVoid); - // add header - actions.prepend(actionSep); - QAction *actionVoid = new QAction(m_selectedInstance->name(), this); - actionVoid->setEnabled(false); - actions.prepend(actionVoid); - } QMenu myMenu; myMenu.addActions(actions); /* diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 3a8f5c421..e438f8fe1 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -164,7 +164,7 @@ private slots: */ void iconUpdated(QString); - void showInstanceContextMenu(const QPoint &); + void showInstanceContextMenu(const QPoint &pos, InstancePtr inst); void updateMainToolBar(); diff --git a/launcher/ui/instanceview/InstanceView.cpp b/launcher/ui/instanceview/InstanceView.cpp index d0a58300b..fef8f40bd 100644 --- a/launcher/ui/instanceview/InstanceView.cpp +++ b/launcher/ui/instanceview/InstanceView.cpp @@ -89,6 +89,7 @@ void InstanceView::createTable() { connect(m_table, &QTableView::doubleClicked, this, &InstanceView::activateInstance); connect(m_table->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &InstanceView::currentRowChanged); connect(m_table->selectionModel(), &QItemSelectionModel::currentColumnChanged, this, &InstanceView::selectNameColumn); + connect(m_table, &QWidget::customContextMenuRequested, this, &InstanceView::contextMenuRequested); } InstancePtr InstanceView::currentInstance() { @@ -137,6 +138,16 @@ void InstanceView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bo } } +void InstanceView::contextMenuRequested(const QPoint pos) { + QModelIndex index = m_table->indexAt(pos); + + if (index.isValid()) { + int row = mappedIndex(index).row(); + InstancePtr inst = m_instances->at(row); + emit showContextMenu(m_table->mapToParent(pos), inst); + } +} + QModelIndex InstanceView::mappedIndex(const QModelIndex& index) const { return m_proxy->mapToSource(index); } diff --git a/launcher/ui/instanceview/InstanceView.h b/launcher/ui/instanceview/InstanceView.h index bf185e007..73619cea9 100644 --- a/launcher/ui/instanceview/InstanceView.h +++ b/launcher/ui/instanceview/InstanceView.h @@ -44,6 +44,7 @@ public: signals: void instanceActivated(InstancePtr inst); void currentInstanceChanged(InstancePtr current, InstancePtr previous); + void showContextMenu(const QPoint pos, InstancePtr inst); private slots: void activateInstance(const QModelIndex& index); @@ -51,6 +52,7 @@ private slots: void selectNameColumn(const QModelIndex& current, const QModelIndex& previous); // emits currentRowChanged if a data update affected the current instance void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); + void contextMenuRequested(const QPoint pos); private: void createTable();