From 742c180967cefc15413ba03ddaf9778e931dbe89 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Fri, 6 Nov 2020 01:28:19 +0000 Subject: [PATCH] Allow message_aggregator example to not join on he loopback interface --- UDPExamples/MessageAggregatorMainWindow.cpp | 41 ++++++++++++++++++--- UDPExamples/MessageAggregatorMainWindow.hpp | 2 + 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/UDPExamples/MessageAggregatorMainWindow.cpp b/UDPExamples/MessageAggregatorMainWindow.cpp index a0fe90c47..63b1e45b5 100644 --- a/UDPExamples/MessageAggregatorMainWindow.cpp +++ b/UDPExamples/MessageAggregatorMainWindow.cpp @@ -107,13 +107,16 @@ MessageAggregatorMainWindow::MessageAggregatorMainWindow () auto flags = QNetworkInterface::IsRunning | QNetworkInterface::CanMulticast; if ((net_if.flags () & flags) == flags) { - auto is_loopback = net_if.flags () & QNetworkInterface::IsLoopBack; + if (net_if.flags () & QNetworkInterface::IsLoopBack) + { + loopback_interface_name_ = net_if.name (); + } auto item = network_interfaces_combo_box_->addCheckItem (net_if.humanReadableName () , net_if.name () - , is_loopback ? Qt::Checked : Qt::Unchecked); - item->setEnabled (!is_loopback); - auto tip = QString {"name(index): %1(%2) - %3"}.arg (net_if.name ()).arg (net_if.index ()) - .arg (net_if.flags () & QNetworkInterface::IsUp ? "Up" : "Down"); + , Qt::Unchecked); + auto tip = QString {"name(index): %1(%2) - %3"} + .arg (net_if.name ()).arg (net_if.index ()) + .arg (net_if.flags () & QNetworkInterface::IsUp ? "Up" : "Down"); auto hw_addr = net_if.hardwareAddress (); if (hw_addr.size ()) { @@ -131,6 +134,8 @@ MessageAggregatorMainWindow::MessageAggregatorMainWindow () item->setToolTip (tip); } } + connect (network_interfaces_combo_box_, &QComboBox::currentTextChanged, this, &MessageAggregatorMainWindow::validate_network_interfaces); + validate_network_interfaces (QString {}); log_table_view_->setModel (log_); log_table_view_->verticalHeader ()->hide (); @@ -369,4 +374,30 @@ void MessageAggregatorMainWindow::change_highlighting (QString const& call, QCol } } +void MessageAggregatorMainWindow::validate_network_interfaces (QString const& /*text*/) +{ + auto model = static_cast (network_interfaces_combo_box_->model ()); + bool has_checked {false}; + int loopback_row {-1}; + for (int row = 0; row < model->rowCount (); ++row) + { + if (model->item (row)->data ().toString () == loopback_interface_name_) + { + loopback_row = row; + } + else if (Qt::Checked == model->item (row)->checkState ()) + { + has_checked = true; + } + } + if (loopback_row >= 0) + { + if (!has_checked) + { + model->item (loopback_row)->setCheckState (Qt::Checked); + } + model->item (loopback_row)->setEnabled (has_checked); + } +} + #include "moc_MessageAggregatorMainWindow.cpp" diff --git a/UDPExamples/MessageAggregatorMainWindow.hpp b/UDPExamples/MessageAggregatorMainWindow.hpp index 1bff0d692..71ab89ffe 100644 --- a/UDPExamples/MessageAggregatorMainWindow.hpp +++ b/UDPExamples/MessageAggregatorMainWindow.hpp @@ -46,6 +46,7 @@ private: void remove_client (ClientKey const&); void change_highlighting (QString const& call, QColor const& bg = QColor {}, QColor const& fg = QColor {}, bool last_only = false); + Q_SLOT void validate_network_interfaces (QString const&); // maps client id to widgets using ClientsDictionary = QHash; @@ -59,6 +60,7 @@ private: QSpinBox * port_spin_box_; QLineEdit * multicast_group_line_edit_; CheckableItemComboBox * network_interfaces_combo_box_; + QString loopback_interface_name_; QLabel * network_interfaces_form_label_widget_; QTableView * log_table_view_; QListWidget * calls_of_interest_;