feat: reimplement contextmenu

Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
Sefa Eyeoglu 2022-10-01 19:36:08 +02:00
parent e786f2ea62
commit 6527604b93
No known key found for this signature in database
GPG Key ID: C10411294912A422
4 changed files with 27 additions and 19 deletions

View File

@ -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<QAction *> 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);
/*

View File

@ -164,7 +164,7 @@ private slots:
*/
void iconUpdated(QString);
void showInstanceContextMenu(const QPoint &);
void showInstanceContextMenu(const QPoint &pos, InstancePtr inst);
void updateMainToolBar();

View File

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

View File

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