Skip to content

Commit 2113d28

Browse files
authored
Add multi-select endpoint deletion and fixed 'Delete Endpoint' typo (#257)
Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent f0a8431 commit 2113d28

3 files changed

Lines changed: 89 additions & 53 deletions

File tree

forms/mainwindow.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
<set>QAbstractItemView::NoEditTriggers</set>
254254
</property>
255255
<property name="selectionMode">
256-
<enum>QAbstractItemView::SingleSelection</enum>
256+
<enum>QAbstractItemView::ExtendedSelection</enum>
257257
</property>
258258
<property name="selectionBehavior">
259259
<enum>QAbstractItemView::SelectRows</enum>
@@ -355,7 +355,7 @@
355355
</action>
356356
<action name="actionDelete_Enpoint">
357357
<property name="text">
358-
<string>Delete Enpoint</string>
358+
<string>Delete Endpoint</string>
359359
</property>
360360
</action>
361361
<action name="actionAbout">

include/eprosimashapesdemo/qt/mainwindow.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ private slots:
124124

125125
void on_actionDelete_Enpoint_triggered();
126126

127-
void on_tableEndpoint_clicked(
128-
const QModelIndex& index);
129-
130127
void on_MainWindow_destroyed();
131128

132129
void closeEvent(
@@ -152,8 +149,9 @@ public slots:
152149
UpdateThread* mp_writeThread;
153150
QStandardItemModel* m_pubsub;
154151
std::vector<SD_Endpoint> m_pubsub_pointers;
155-
int m_tableRow;
156152
AxisArrowOverlay* m_axisOverlay;
153+
std::vector<int> selectedEndpointRows() const;
154+
void removeSelectedRows();
157155
void removeRow(
158156
int row);
159157
};

src/qt/mainwindow.cpp

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <QDir>
3636
#include <QProcess>
3737

38+
#include <algorithm>
3839
#include <iostream>
3940

4041

@@ -44,7 +45,6 @@ MainWindow::MainWindow(
4445
, ui(new Ui::MainWindow)
4546
, m_shapesDemo(this)
4647
, mp_writeThread(NULL)
47-
, m_tableRow(-1)
4848
{
4949
setWindowIcon(QIcon(":images/eprosima_icon.png"));
5050

@@ -192,6 +192,7 @@ void MainWindow::on_actionStop_triggered()
192192
{
193193
this->m_shapesDemo.stop();
194194
m_pubsub->removeRows(0, m_pubsub->rowCount());
195+
m_pubsub_pointers.clear();
195196
update();
196197
addMessageToOutput(QString("ShapesDemo stopped"), true);
197198
}
@@ -390,13 +391,18 @@ void MainWindow::addSubscriberToTable(
390391
void MainWindow::on_tableEndpoint_customContextMenuRequested(
391392
const QPoint& pos)
392393
{
393-
// cout <<"CONTEXT MENU REQUESTED"<<endl;
394394
QModelIndex index = this->ui->tableEndpoint->indexAt(pos);
395-
this->ui->tableEndpoint->selectRow(index.row());
396-
// cout << index.column()<< " "<< index.row()<<endl;
397-
this->m_tableRow = index.row();
398395
if (index.row() >= 0)
399396
{
397+
QItemSelectionModel* selection_model = this->ui->tableEndpoint->selectionModel();
398+
if (selection_model != nullptr &&
399+
!selection_model->isRowSelected(index.row(), QModelIndex()))
400+
{
401+
selection_model->select(
402+
index,
403+
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
404+
}
405+
400406
QMenu* menu = new QMenu(this);
401407
menu->addAction(this->ui->actionDelete_Enpoint);
402408
menu->popup(this->ui->tableEndpoint->viewport()->mapToGlobal(pos));
@@ -405,67 +411,99 @@ void MainWindow::on_tableEndpoint_customContextMenuRequested(
405411

406412
void MainWindow::on_actionDelete_Enpoint_triggered()
407413
{
408-
//cout << "DELETE ENDPOINT" <<endl;
409-
removeRow(m_tableRow);
414+
removeSelectedRows();
415+
}
416+
417+
std::vector<int> MainWindow::selectedEndpointRows() const
418+
{
419+
std::vector<int> rows;
420+
QItemSelectionModel* selection_model = this->ui->tableEndpoint->selectionModel();
421+
422+
if (selection_model == nullptr)
423+
{
424+
return rows;
425+
}
426+
427+
QModelIndexList selected_rows = selection_model->selectedRows();
428+
rows.reserve(selected_rows.size());
429+
430+
for (const QModelIndex& index : selected_rows)
431+
{
432+
rows.push_back(index.row());
433+
}
434+
435+
std::sort(rows.begin(), rows.end(), [](int lhs, int rhs)
436+
{
437+
return lhs > rhs;
438+
});
439+
rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
440+
441+
return rows;
442+
}
443+
444+
void MainWindow::removeSelectedRows()
445+
{
446+
for (int row : selectedEndpointRows())
447+
{
448+
removeRow(row);
449+
}
410450
}
411451

412452
void MainWindow::removeRow(
413453
int row)
414454
{
415455
// cout << "REMOVING ROW **************************"<<endl;
416-
m_pubsub->removeRow(row);
417-
for (std::vector<SD_Endpoint>::iterator it = this->m_pubsub_pointers.begin();
418-
it != this->m_pubsub_pointers.end(); ++it)
456+
if (row < 0 || row >= m_pubsub->rowCount())
419457
{
420-
if (row == it->pos)
458+
return;
459+
}
460+
461+
std::vector<SD_Endpoint>::iterator endpoint_it = std::find_if(
462+
m_pubsub_pointers.begin(),
463+
m_pubsub_pointers.end(),
464+
[row](const SD_Endpoint& endpoint)
421465
{
422-
if (it->type == PUB)
423-
{
424-
// cout << "REMOVING PUBLISHER "<<endl;
425-
this->m_shapesDemo.removePublisher(it->pub);
426-
addMessageToOutput(QString("Removed Publisher"), false);
427-
}
428-
else
429-
{
430-
// cout << "REMOVING SUBSCRIBER "<<endl;
431-
this->m_shapesDemo.removeSubscriber(it->sub);
432-
addMessageToOutput(QString("Removed Subscriber"), false);
433-
}
466+
return endpoint.pos == row;
467+
});
434468

435-
for (std::vector<SD_Endpoint>::iterator it2 = it; it2 != this->m_pubsub_pointers.end(); ++it2)
436-
{
437-
it2->pos--;
438-
// cout << "POSITION -1"<<endl;
439-
}
440-
m_pubsub_pointers.erase(it);
441-
break;
442-
}
469+
if (endpoint_it == m_pubsub_pointers.end())
470+
{
471+
return;
443472
}
444-
//cout << "FINISH REMOVE ROW"<<endl;
445-
}
446473

447-
void MainWindow::on_tableEndpoint_clicked(
448-
const QModelIndex& index)
449-
{
450-
this->ui->tableEndpoint->selectRow(index.row());
474+
m_pubsub->removeRow(row);
475+
476+
if (endpoint_it->type == PUB)
477+
{
478+
this->m_shapesDemo.removePublisher(endpoint_it->pub);
479+
addMessageToOutput(QString("Removed Publisher"), false);
480+
}
481+
else
482+
{
483+
this->m_shapesDemo.removeSubscriber(endpoint_it->sub);
484+
addMessageToOutput(QString("Removed Subscriber"), false);
485+
}
486+
487+
for (std::vector<SD_Endpoint>::iterator it = endpoint_it + 1;
488+
it != this->m_pubsub_pointers.end(); it++)
489+
{
490+
it->pos--;
491+
}
492+
493+
m_pubsub_pointers.erase(endpoint_it);
494+
//cout << "FINISH REMOVE ROW"<<endl;
451495
}
452496

453497
void MainWindow::keyPressEvent(
454498
QKeyEvent* event)
455499
{
456500
if (event->key() == Qt::Key_Delete)
457501
{
458-
QItemSelectionModel* select = this->ui->tableEndpoint->selectionModel();
459-
if (select->hasSelection())
460-
{
461-
QModelIndexList list = select->selectedRows();
462-
for (QModelIndexList::iterator it = list.begin(); it != list.end(); ++it)
463-
{
464-
removeRow(it->row());
465-
}
466-
467-
}
502+
removeSelectedRows();
503+
return;
468504
}
505+
506+
QMainWindow::keyPressEvent(event);
469507
}
470508

471509
void MainWindow::on_actionAbout_triggered()

0 commit comments

Comments
 (0)