Allow message_aggregator example to not join on he loopback interface

This commit is contained in:
Bill Somerville 2020-11-06 01:28:19 +00:00
parent 988fcfac5a
commit 742c180967
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
2 changed files with 38 additions and 5 deletions

View File

@ -107,12 +107,15 @@ 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 ())
, 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<QStandardItemModel *> (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"

View File

@ -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<ClientKey, ClientWidget *>;
@ -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_;