Reset port widget current on portgroup disconnect

Failure to do so was causing a crash because port widget was trying to
disconnect signal from a non-existent port (corresponding to the current
index that was not reset) after the portgroup reconnected or another
portgroup came up and a port was selected in the port list.

This bug was a regression caused by the refactoring changes when
portwidget (and streamswidget) was extracted from portswindow.
This commit is contained in:
Srivats P 2022-12-08 16:22:39 +05:30
parent 1e50f9b095
commit f07cba39d5
6 changed files with 35 additions and 21 deletions

View File

@ -174,6 +174,7 @@ MainWindow::MainWindow(QWidget *parent)
this, SLOT(onNewVersion(QString))); this, SLOT(onNewVersion(QString)));
updater->checkForNewVersion(); updater->checkForNewVersion();
// TODO: If session file specified (and valid?), don't add local drone PG
// Add the "Local" Port Group // Add the "Local" Port Group
if (appParams.optLocalDrone()) { if (appParams.optLocalDrone()) {
PortGroup *pg = new PortGroup; PortGroup *pg = new PortGroup;

View File

@ -108,7 +108,8 @@ PortsWindow::PortsWindow(PortGroupList *pgl, QWidget *parent)
connect(this, connect(this,
SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)), SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)),
portWidget, SLOT(setCurrentPortIndex(const QModelIndex&))); portWidget,
SLOT(setCurrentPortIndex(const QModelIndex&, const QModelIndex&)));
connect(this, connect(this,
SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)), SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)),
streamsWidget, SLOT(setCurrentPortIndex(const QModelIndex&))); streamsWidget, SLOT(setCurrentPortIndex(const QModelIndex&)));

View File

@ -48,30 +48,34 @@ PortWidget::~PortWidget()
{ {
} }
void PortWidget::setCurrentPortIndex(const QModelIndex &portIndex) void PortWidget::setCurrentPortIndex(const QModelIndex &currentIndex,
const QModelIndex &previousIndex)
{ {
if (!plm) if (!plm)
return; return;
// XXX: We assume portIndex corresponds to sourceModel, not proxyModel qDebug("In %s", __PRETTY_FUNCTION__);
if (!plm->isPort(portIndex))
return;
qDebug("In %s", __FUNCTION__); // XXX: We assume indices corresponds to sourceModel, not proxyModel
// - caller/sender should ensure this
// Disconnect previous port // Disconnect previous port
if (plm->isPort(currentPortIndex_)) if (plm->isPort(previousIndex))
disconnect(&(plm->port(currentPortIndex_)), disconnect(&(plm->port(previousIndex)),
SIGNAL(portRateChanged(int, int)), SIGNAL(portRateChanged(int, int)),
this, SLOT(updatePortRates())); this, SLOT(updatePortRates()));
currentPortIndex_ = portIndex; if (!plm->isPort(currentIndex)) {
currentPortIndex_ = QModelIndex(); // set to invalid
return;
}
currentPortIndex_ = currentIndex;
// Connect current port // Connect current port
if (plm->isPort(currentPortIndex_)) connect(&(plm->port(currentPortIndex_)),
connect(&(plm->port(currentPortIndex_)), SIGNAL(portRateChanged(int, int)),
SIGNAL(portRateChanged(int, int)), this, SLOT(updatePortRates()));
this, SLOT(updatePortRates()));
double speed = plm->port(currentPortIndex_).speed(); double speed = plm->port(currentPortIndex_).speed();
portSpeed->setText(QString("Max %L1 Mbps").arg(speed)); portSpeed->setText(QString("Max %L1 Mbps").arg(speed));

View File

@ -37,7 +37,8 @@ public:
void setPortGroupList(PortGroupList *portGroups); void setPortGroupList(PortGroupList *portGroups);
public slots: public slots:
void setCurrentPortIndex(const QModelIndex &portIndex); void setCurrentPortIndex(const QModelIndex &currentIndex,
const QModelIndex &previousIndex);
private slots: private slots:

View File

@ -361,15 +361,13 @@ void StreamModel::setCurrentPortIndex(const QModelIndex &current)
else else
{ {
qDebug("change to valid port"); qDebug("change to valid port");
// Disconnect any existing connection to avoid duplication
// Qt 4.6 has Qt::UniqueConnection, but we want to remain compatible
// with earlier Qt versions
if (mCurrentPort) if (mCurrentPort)
{ {
disconnect(mCurrentPort, SIGNAL(streamListChanged(int, int)), disconnect(mCurrentPort, SIGNAL(streamListChanged(int, int)),
this, SLOT(when_mCurrentPort_streamListChanged(int, int))); this, SLOT(when_mCurrentPort_streamListChanged(int, int)));
} }
quint16 pg = current.internalId() >> 16; quint16 pg = current.internalId() >> 16;
// TODO: make mCurrentPort a smart weak pointer
mCurrentPort = pgl->mPortGroups[pgl->indexOfPortGroup(pg)]->mPorts[current.row()]; mCurrentPort = pgl->mPortGroups[pgl->indexOfPortGroup(pg)]->mPorts[current.row()];
connect(mCurrentPort, SIGNAL(streamListChanged(int, int)), connect(mCurrentPort, SIGNAL(streamListChanged(int, int)),
this, SLOT(when_mCurrentPort_streamListChanged(int, int))); this, SLOT(when_mCurrentPort_streamListChanged(int, int)));

View File

@ -126,6 +126,8 @@ void StreamsWidget::on_tvStreamList_activated(const QModelIndex & index)
qDebug("stream list activated\n"); qDebug("stream list activated\n");
Q_ASSERT(plm->isPort(currentPortIndex_));
Port &curPort = plm->port(currentPortIndex_); Port &curPort = plm->port(currentPortIndex_);
QList<Stream*> streams; QList<Stream*> streams;
@ -143,11 +145,10 @@ void StreamsWidget::setCurrentPortIndex(const QModelIndex &portIndex)
if (!plm) if (!plm)
return; return;
// XXX: We assume portIndex corresponds to sourceModel, not proxyModel // XXX: We assume portIndex corresponds to sourceModel, not proxyModel;
if (!plm->isPort(portIndex)) // caller should ensure this
return;
qDebug("In %s", __FUNCTION__); qDebug("In %s", __PRETTY_FUNCTION__);
currentPortIndex_ = portIndex; currentPortIndex_ = portIndex;
plm->getStreamModel()->setCurrentPortIndex(portIndex); plm->getStreamModel()->setCurrentPortIndex(portIndex);
@ -223,6 +224,8 @@ void StreamsWidget::on_actionNew_Stream_triggered()
count = selectionModel->selection().at(0).height(); count = selectionModel->selection().at(0).height();
} }
Q_ASSERT(plm->isPort(currentPortIndex_));
Port &curPort = plm->port(currentPortIndex_); Port &curPort = plm->port(currentPortIndex_);
QList<Stream*> streams; QList<Stream*> streams;
@ -243,6 +246,8 @@ void StreamsWidget::on_actionEdit_Stream_triggered()
if (!streamModel->hasSelection()) if (!streamModel->hasSelection())
return; return;
Q_ASSERT(plm->isPort(currentPortIndex_));
Port &curPort = plm->port(currentPortIndex_); Port &curPort = plm->port(currentPortIndex_);
QList<Stream*> streams; QList<Stream*> streams;
@ -262,6 +267,8 @@ void StreamsWidget::on_actionDuplicate_Stream_triggered()
qDebug("Duplicate Stream Action"); qDebug("Duplicate Stream Action");
Q_ASSERT(plm->isPort(currentPortIndex_));
if (model->hasSelection()) if (model->hasSelection())
{ {
bool isOk; bool isOk;
@ -304,6 +311,8 @@ void StreamsWidget::on_actionFind_Replace_triggered()
{ {
qDebug("Find & Replace Action"); qDebug("Find & Replace Action");
Q_ASSERT(plm->isPort(currentPortIndex_));
QItemSelectionModel* selectionModel = tvStreamList->selectionModel(); QItemSelectionModel* selectionModel = tvStreamList->selectionModel();
FindReplaceDialog::Action action; FindReplaceDialog::Action action;