diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2db79a0..97ae7fe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ add_library( commonROS STATIC dialog_select_ros_topics.cpp dialog_with_itemlist.h publisher_select_dialog.h + publisher_select_dialog.cpp parser_configuration.cpp parser_configuration.h ros_parsers/ros2_parser.cpp diff --git a/src/TopicPublisherROS2/publisher_ros2.cpp b/src/TopicPublisherROS2/publisher_ros2.cpp index 7126d2d..f51c46b 100644 --- a/src/TopicPublisherROS2/publisher_ros2.cpp +++ b/src/TopicPublisherROS2/publisher_ros2.cpp @@ -2,18 +2,11 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include #include -#include -#include -#include -#include +#include #include #include #include @@ -153,34 +146,55 @@ void TopicPublisherROS2::filterDialog() std::map checkbox; - for (const TopicInfo& info : _topics_info) + dialog->ui()->listTopics->setRowCount(sorted_topics.size()); + + for (size_t i = 0; i < sorted_topics.size(); i++) { - const std::string topic_name = info.topic_name; - auto cb = new QCheckBox(dialog); + const std::string topic_name = sorted_topics[i].topic_name; + auto item = new QTableWidgetItem(QString::fromStdString(topic_name)); + item->setFlags(item->flags() & ~Qt::ItemIsEditable); + dialog->ui()->listTopics->setItem(i, 0, item); + auto filter_it = _topics_to_publish.find(topic_name); - if (filter_it == _topics_to_publish.end()) + if (filter_it == _topics_to_publish.end() || filter_it->second) { - cb->setChecked(true); + dialog->ui()->listTopics->selectRow(i); } - else + } + + dialog->ui()->listTopics->sortByColumn(0, Qt::AscendingOrder); + + connect(dialog->ui()->pushButtonSelect, &QPushButton::pressed, [dialog]() { + for (int row = 0; row < dialog->ui()->listTopics->rowCount(); row++) { - cb->setChecked(filter_it->second); + if (!dialog->ui()->listTopics->isRowHidden(row)) + { + dialog->ui()->listTopics->selectRow(row); + } } - cb->setFocusPolicy(Qt::NoFocus); - dialog->ui()->formLayout->addRow(new QLabel(QString::fromStdString(topic_name)), cb); - checkbox.insert(std::make_pair(topic_name, cb)); - connect(dialog->ui()->pushButtonSelect, &QPushButton::pressed, [cb]() { cb->setChecked(true); }); - connect(dialog->ui()->pushButtonDeselect, &QPushButton::pressed, [cb]() { cb->setChecked(false); }); - } + }); + connect(dialog->ui()->pushButtonDeselect, &QPushButton::pressed, dialog->ui()->listTopics, + &QAbstractItemView::clearSelection); dialog->exec(); if (dialog->result() == QDialog::Accepted) { _topics_to_publish.clear(); - for (const auto& it : checkbox) + QModelIndexList selected_indexes = dialog->ui()->listTopics->selectionModel()->selectedIndexes(); + + for (const auto& info : sorted_topics) { - _topics_to_publish.insert({ it.first, it.second->isChecked() }); + _topics_to_publish.insert({ info.topic_name, false }); + } + + for (const QModelIndex& index : selected_indexes) + { + if (index.column() == 0) + { + std::string topic_name = index.data(Qt::DisplayRole).toString().toStdString(); + _topics_to_publish[topic_name] = true; + } } updatePublishers(); diff --git a/src/publisher_select_dialog.cpp b/src/publisher_select_dialog.cpp new file mode 100644 index 0000000..89627cc --- /dev/null +++ b/src/publisher_select_dialog.cpp @@ -0,0 +1,32 @@ +#include "publisher_select_dialog.h" +#include +#include +#include + +void PublisherSelectDialog::on_lineEditFilter_textChanged(const QString& search_string) +{ + QStringList spaced_items = search_string.split(' '); + + for (int row = 0; row < _ui->listTopics->rowCount(); row++) + { + auto item = _ui->listTopics->item(row, 0); + QString name = item->text(); + bool toHide = false; + + for (const auto& item : spaced_items) + { + if (!name.contains(item, Qt::CaseInsensitive)) + { + toHide = true; + break; + } + } + _ui->listTopics->setRowHidden(row, toHide); + } +} + +void PublisherSelectDialog::on_listTopics_itemSelectionChanged() +{ + QModelIndexList indexes = _ui->listTopics->selectionModel()->selectedIndexes(); + _ui->buttonBox->setEnabled(indexes.size() > 0); +} diff --git a/src/publisher_select_dialog.h b/src/publisher_select_dialog.h index 417f57b..5e0520c 100644 --- a/src/publisher_select_dialog.h +++ b/src/publisher_select_dialog.h @@ -1,9 +1,6 @@ #pragma once #include -#include -#include -#include -#include +#include #include "ui_publisher_select_dialog.h" @@ -22,6 +19,10 @@ class PublisherSelectDialog : public QDialog explicit PublisherSelectDialog(QWidget* parent = nullptr) : QDialog(parent), _ui(new Ui::PublisherSelect) { _ui->setupUi(this); + _ui->listTopics->verticalHeader()->setVisible(false); + QStringList labels; + labels.push_back("Topic name"); + _ui->listTopics->setHorizontalHeaderLabels(labels); } Ui::PublisherSelect* ui() diff --git a/src/publisher_select_dialog.ui b/src/publisher_select_dialog.ui index c93ec04..ba2b111 100644 --- a/src/publisher_select_dialog.ui +++ b/src/publisher_select_dialog.ui @@ -1,160 +1,221 @@ - PublisherSelect - - - - 0 - 0 - 400 - 300 - - - - - 400 - 300 - - - - Select topics to be published - - - - - - true - - - - + PublisherSelect + + + 0 0 - 380 - 158 - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - - - - Qt::NoFocus - - - <html><head/><body><p><span style=" color:#2e3436;">Publish the topic [/clock].</span><span style=" font-family:'monospace'; color:#2e3436;"/></p><p><span style=" font-family:'monospace'; color:#2e3436;">You should set rosparam use_sim_time = true</span></p></body></html> - - - Keep original timestamp and publish [/clock] - - - true - - - - - - - Qt::NoFocus - - - <html><head/><body><p><span style=" color:#2e3436;">Pretend it is a new message.</span></p><p><span style=" font-family:'monospace'; color:#2e3436;">The timestamp of the original message will be overwritten with ros::Time::Now()</span></p></body></html> - - - Overwrite timestamp [std_msgs/Header/stamp] - - - - - - - - - Qt::NoFocus - - - SelectAll - - - - - - - Qt::NoFocus - - - Deselect All - - - + 400 + 300 + + + + + 400 + 300 + + + + Select topics to be published + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Filter: + + + + + + + + 300 + 16777215 + + + + true + + + + + + + + + + 0 + 0 + + + + QAbstractScrollArea::AdjustIgnored + + + QAbstractItemView::NoEditTriggers + + + false + + + false + + + QAbstractItemView::MultiSelection + + + QAbstractItemView::SelectRows + + + Qt::ElideRight + + + true + + + 1 + + + 300 + + + true + + + true + + + 18 + + + 21 + + + + + + + + Qt::NoFocus + + + <html><head/><body><p><span style=" + color:#2e3436;">Publish the topic [/clock].</span><span style=" + font-family:'monospace'; color:#2e3436;"/></p><p><span + style=" font-family:'monospace'; color:#2e3436;">You should set rosparam + use_sim_time = true</span></p></body></html> + + + Keep original timestamp and publish [/clock] + + + true + + + + + + + Qt::NoFocus + + + <html><head/><body><p><span style=" + color:#2e3436;">Pretend it is a new + message.</span></p><p><span style=" font-family:'monospace'; + color:#2e3436;">The timestamp of the original message will be overwritten with + ros::Time::Now()</span></p></body></html> + + + Overwrite timestamp [std_msgs/Header/stamp] + + + + + + + + + Qt::NoFocus + + + SelectAll + + + + + + + Qt::NoFocus + + + Deselect All + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Ok + + + - - - - - Qt::Horizontal - - - QDialogButtonBox::Ok - - - - - - - - - buttonBox - accepted() - PublisherSelect - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - PublisherSelect - reject() - - - 316 - 260 - - - 286 - 274 - - - - + + + + + buttonBox + accepted() + PublisherSelect + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + PublisherSelect + reject() + + + 316 + 260 + + + 286 + 274 + + + +