From 969a2d594f9b1821417a3f8b61dd54781ae0ceb3 Mon Sep 17 00:00:00 2001 From: DannyP39 <98972125+Danipiza@users.noreply.github.com> Date: Thu, 25 Jun 2026 11:49:44 +0200 Subject: [PATCH] Add multi-select endpoint deletion and fixed 'Delete Endpoint' typo (#257) Signed-off-by: danipiza (cherry picked from commit 2113d28ed0f65a67aac1a8ac38625ed74648209c) # Conflicts: # include/eprosimashapesdemo/qt/mainwindow.h --- forms/mainwindow.ui | 4 +- include/eprosimashapesdemo/qt/mainwindow.h | 9 +- src/qt/mainwindow.cpp | 132 +++++++++++++-------- 3 files changed, 93 insertions(+), 52 deletions(-) diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index cfb9fad..13605a8 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -253,7 +253,7 @@ QAbstractItemView::NoEditTriggers - QAbstractItemView::SingleSelection + QAbstractItemView::ExtendedSelection QAbstractItemView::SelectRows @@ -355,7 +355,7 @@ - Delete Enpoint + Delete Endpoint diff --git a/include/eprosimashapesdemo/qt/mainwindow.h b/include/eprosimashapesdemo/qt/mainwindow.h index 2f44a2f..175daa1 100644 --- a/include/eprosimashapesdemo/qt/mainwindow.h +++ b/include/eprosimashapesdemo/qt/mainwindow.h @@ -120,9 +120,6 @@ private slots: void on_actionDelete_Enpoint_triggered(); - void on_tableEndpoint_clicked( - const QModelIndex& index); - void on_MainWindow_destroyed(); void closeEvent( @@ -147,7 +144,13 @@ public slots: UpdateThread* mp_writeThread; QStandardItemModel* m_pubsub; std::vector m_pubsub_pointers; +<<<<<<< HEAD int m_tableRow; +======= + AxisArrowOverlay* m_axisOverlay; + std::vector selectedEndpointRows() const; + void removeSelectedRows(); +>>>>>>> 2113d28 (Add multi-select endpoint deletion and fixed 'Delete Endpoint' typo (#257)) void removeRow( int row); }; diff --git a/src/qt/mainwindow.cpp b/src/qt/mainwindow.cpp index 9dbc573..3b1f586 100644 --- a/src/qt/mainwindow.cpp +++ b/src/qt/mainwindow.cpp @@ -35,6 +35,7 @@ #include #include +#include #include @@ -44,7 +45,6 @@ MainWindow::MainWindow( , ui(new Ui::MainWindow) , m_shapesDemo(this) , mp_writeThread(NULL) - , m_tableRow(-1) { setWindowIcon(QIcon(":images/eprosima_icon.png")); @@ -180,6 +180,7 @@ void MainWindow::on_actionStop_triggered() { this->m_shapesDemo.stop(); m_pubsub->removeRows(0, m_pubsub->rowCount()); + m_pubsub_pointers.clear(); update(); addMessageToOutput(QString("ShapesDemo stopped"), true); } @@ -378,13 +379,18 @@ void MainWindow::addSubscriberToTable( void MainWindow::on_tableEndpoint_customContextMenuRequested( const QPoint& pos) { - // cout <<"CONTEXT MENU REQUESTED"<ui->tableEndpoint->indexAt(pos); - this->ui->tableEndpoint->selectRow(index.row()); - // cout << index.column()<< " "<< index.row()<m_tableRow = index.row(); if (index.row() >= 0) { + QItemSelectionModel* selection_model = this->ui->tableEndpoint->selectionModel(); + if (selection_model != nullptr && + !selection_model->isRowSelected(index.row(), QModelIndex())) + { + selection_model->select( + index, + QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + } + QMenu* menu = new QMenu(this); menu->addAction(this->ui->actionDelete_Enpoint); menu->popup(this->ui->tableEndpoint->viewport()->mapToGlobal(pos)); @@ -393,49 +399,87 @@ void MainWindow::on_tableEndpoint_customContextMenuRequested( void MainWindow::on_actionDelete_Enpoint_triggered() { - //cout << "DELETE ENDPOINT" < MainWindow::selectedEndpointRows() const +{ + std::vector rows; + QItemSelectionModel* selection_model = this->ui->tableEndpoint->selectionModel(); + + if (selection_model == nullptr) + { + return rows; + } + + QModelIndexList selected_rows = selection_model->selectedRows(); + rows.reserve(selected_rows.size()); + + for (const QModelIndex& index : selected_rows) + { + rows.push_back(index.row()); + } + + std::sort(rows.begin(), rows.end(), [](int lhs, int rhs) + { + return lhs > rhs; + }); + rows.erase(std::unique(rows.begin(), rows.end()), rows.end()); + + return rows; +} + +void MainWindow::removeSelectedRows() +{ + for (int row : selectedEndpointRows()) + { + removeRow(row); + } } void MainWindow::removeRow( int row) { // cout << "REMOVING ROW **************************"<removeRow(row); - for (std::vector::iterator it = this->m_pubsub_pointers.begin(); - it != this->m_pubsub_pointers.end(); ++it) + if (row < 0 || row >= m_pubsub->rowCount()) { - if (row == it->pos) + return; + } + + std::vector::iterator endpoint_it = std::find_if( + m_pubsub_pointers.begin(), + m_pubsub_pointers.end(), + [row](const SD_Endpoint& endpoint) { - if (it->type == PUB) - { - // cout << "REMOVING PUBLISHER "<m_shapesDemo.removePublisher(it->pub); - addMessageToOutput(QString("Removed Publisher"), false); - } - else - { - // cout << "REMOVING SUBSCRIBER "<m_shapesDemo.removeSubscriber(it->sub); - addMessageToOutput(QString("Removed Subscriber"), false); - } + return endpoint.pos == row; + }); - for (std::vector::iterator it2 = it; it2 != this->m_pubsub_pointers.end(); ++it2) - { - it2->pos--; - // cout << "POSITION -1"<ui->tableEndpoint->selectRow(index.row()); + m_pubsub->removeRow(row); + + if (endpoint_it->type == PUB) + { + this->m_shapesDemo.removePublisher(endpoint_it->pub); + addMessageToOutput(QString("Removed Publisher"), false); + } + else + { + this->m_shapesDemo.removeSubscriber(endpoint_it->sub); + addMessageToOutput(QString("Removed Subscriber"), false); + } + + for (std::vector::iterator it = endpoint_it + 1; + it != this->m_pubsub_pointers.end(); it++) + { + it->pos--; + } + + m_pubsub_pointers.erase(endpoint_it); + //cout << "FINISH REMOVE ROW"<key() == Qt::Key_Delete) { - QItemSelectionModel* select = this->ui->tableEndpoint->selectionModel(); - if (select->hasSelection()) - { - QModelIndexList list = select->selectedRows(); - for (QModelIndexList::iterator it = list.begin(); it != list.end(); ++it) - { - removeRow(it->row()); - } - - } + removeSelectedRows(); + return; } + + QMainWindow::keyPressEvent(event); } void MainWindow::on_actionAbout_triggered()