diff --git a/src/ui/DebugConsole.cc b/src/ui/DebugConsole.cc index 838055d2ecc97261c293ed463e23a15a2d311706..0869347239bb977b0668de32a3007adef07f0c61 100644 --- a/src/ui/DebugConsole.cc +++ b/src/ui/DebugConsole.cc @@ -91,8 +91,7 @@ DebugConsole::DebugConsole(QWidget *parent) : // Connect to UAS manager to get notified about new UAS connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(uasCreated(UASInterface*))); - // Get a list of all existing links - links = QList(); + // Add all existing links foreach (LinkInterface* link, LinkManager::instance()->getLinks()) { addLink(link); } @@ -171,12 +170,16 @@ void DebugConsole::uasCreated(UASInterface* uas) */ void DebugConsole::addLink(LinkInterface* link) { - // Add link to link list - links.insert(link->getMavlinkChannel(), link); + // Add link to list + + foreach (SharedLinkInterface sharedLink, _links) { + Q_ASSERT(sharedLink.data() != link); + } + _links.append(LinkManager::instance()->sharedPointerForLink(link)); m_ui->linkComboBox->insertItem(link->getMavlinkChannel(), link->getName()); // Set new item as current - m_ui->linkComboBox->setCurrentIndex(qMax(0, links.size() - 1)); + m_ui->linkComboBox->setCurrentIndex(qMax(0, _links.size() - 1)); linkSelected(m_ui->linkComboBox->currentIndex()); // Register for name changes @@ -184,18 +187,24 @@ void DebugConsole::addLink(LinkInterface* link) connect(LinkManager::instance(), &LinkManager::linkDisconnected, this, &DebugConsole::removeLink, Qt::UniqueConnection); } -void DebugConsole::removeLink(LinkInterface* const linkInterface) +void DebugConsole::removeLink(LinkInterface* const link) { - // Add link to link list - if (links.contains(linkInterface)) { - int linkIndex = links.indexOf(linkInterface); - - links.removeAt(linkIndex); - - m_ui->linkComboBox->removeItem(linkIndex); + bool found = false; + int linkIndex; + for (linkIndex=0; linkIndex<_links.count(); linkIndex++) { + if (_links[linkIndex].data() == link) { + found = true; + _links.removeAt(linkIndex); + break; + } } + Q_UNUSED(found); + Q_ASSERT(found); + + m_ui->linkComboBox->removeItem(linkIndex); + // Now if this was the current link, clean up some stuff. - if (linkInterface == currLink) + if (link == currLink) { // Like disable the update time for the UI. snapShotTimer.stop(); @@ -211,7 +220,7 @@ void DebugConsole::linkStatusUpdate(const QString& name,const QString& text) m_ui->receiveText->ensureCursorVisible(); } -void DebugConsole::linkSelected(int linkId) +void DebugConsole::linkSelected(int linkIndex) { // Disconnect if (currLink) @@ -226,8 +235,8 @@ void DebugConsole::linkSelected(int linkId) m_ui->receiveText->clear(); // Connect new link - if (linkId != -1) { - currLink = links[linkId]; + if (linkIndex != -1) { + currLink = _links[linkIndex].data(); connect(currLink, SIGNAL(bytesReceived(LinkInterface*,QByteArray)), this, SLOT(receiveBytes(LinkInterface*, QByteArray))); disconnect(currLink, &LinkInterface::connected, this, &DebugConsole::_linkConnected); connect(currLink,SIGNAL(communicationUpdate(QString,QString)),this,SLOT(linkStatusUpdate(QString,QString))); @@ -241,13 +250,21 @@ void DebugConsole::linkSelected(int linkId) */ void DebugConsole::updateLinkName(QString name) { - // Set name if signal came from a link LinkInterface* link = qobject_cast(sender()); - if((link != NULL) && (links.contains(link))) - { - const qint16 &linkIndex(links.indexOf(link)); - m_ui->linkComboBox->setItemText(linkIndex,name); - } + if (link != NULL) { + bool found = false; + int linkIndex; + for (linkIndex=0; linkIndex<_links.count(); linkIndex++) { + if (_links[linkIndex].data() == link) { + found = true; + break; + } + } + + if (found) { + m_ui->linkComboBox->setItemText(linkIndex, name); + } + } } void DebugConsole::setAutoHold(bool hold) diff --git a/src/ui/DebugConsole.h b/src/ui/DebugConsole.h index 6fcadf94e51795d8558e89c6aefbec96a331e332..6a4849157abf3cd4ba72667bbd99e309ede65e65 100644 --- a/src/ui/DebugConsole.h +++ b/src/ui/DebugConsole.h @@ -116,7 +116,6 @@ protected: /** @brief Cycle through the command history */ void cycleCommandHistory(bool up); - QList links; LinkInterface* currLink; bool holdOn; ///< Hold current view, ignore new data @@ -150,6 +149,11 @@ private slots: private: /** @brief Set connection state of the current link */ void _setConnectionState(bool); + + /// List of all links we are keeping track of. We keep SharedLinkInterface objects + /// which are QSharedPointer's in order to maintain reference counts. Otherwise signals + /// can get out of order and the link may be deleted before we clean up our side. + QList _links; Ui::DebugConsole *m_ui; };