Commit f053edc8 authored by Don Gagne's avatar Don Gagne

Merge pull request #1403 from DonLakeFlyer/DebugConsole

Track links using shared pointed
parents 8dd7b72d f712a7f4
...@@ -91,8 +91,7 @@ DebugConsole::DebugConsole(QWidget *parent) : ...@@ -91,8 +91,7 @@ DebugConsole::DebugConsole(QWidget *parent) :
// Connect to UAS manager to get notified about new UAS // Connect to UAS manager to get notified about new UAS
connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(uasCreated(UASInterface*))); connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(uasCreated(UASInterface*)));
// Get a list of all existing links // Add all existing links
links = QList<LinkInterface*>();
foreach (LinkInterface* link, LinkManager::instance()->getLinks()) { foreach (LinkInterface* link, LinkManager::instance()->getLinks()) {
addLink(link); addLink(link);
} }
...@@ -171,12 +170,16 @@ void DebugConsole::uasCreated(UASInterface* uas) ...@@ -171,12 +170,16 @@ void DebugConsole::uasCreated(UASInterface* uas)
*/ */
void DebugConsole::addLink(LinkInterface* link) void DebugConsole::addLink(LinkInterface* link)
{ {
// Add link to link list // Add link to list
links.insert(link->getMavlinkChannel(), link);
foreach (SharedLinkInterface sharedLink, _links) {
Q_ASSERT(sharedLink.data() != link);
}
_links.append(LinkManager::instance()->sharedPointerForLink(link));
m_ui->linkComboBox->insertItem(link->getMavlinkChannel(), link->getName()); m_ui->linkComboBox->insertItem(link->getMavlinkChannel(), link->getName());
// Set new item as current // 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()); linkSelected(m_ui->linkComboBox->currentIndex());
// Register for name changes // Register for name changes
...@@ -184,18 +187,24 @@ void DebugConsole::addLink(LinkInterface* link) ...@@ -184,18 +187,24 @@ void DebugConsole::addLink(LinkInterface* link)
connect(LinkManager::instance(), &LinkManager::linkDisconnected, this, &DebugConsole::removeLink, Qt::UniqueConnection); 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 bool found = false;
if (links.contains(linkInterface)) { int linkIndex;
int linkIndex = links.indexOf(linkInterface); for (linkIndex=0; linkIndex<_links.count(); linkIndex++) {
if (_links[linkIndex].data() == link) {
links.removeAt(linkIndex); found = true;
_links.removeAt(linkIndex);
m_ui->linkComboBox->removeItem(linkIndex); break;
}
} }
Q_UNUSED(found);
Q_ASSERT(found);
m_ui->linkComboBox->removeItem(linkIndex);
// Now if this was the current link, clean up some stuff. // 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. // Like disable the update time for the UI.
snapShotTimer.stop(); snapShotTimer.stop();
...@@ -211,7 +220,7 @@ void DebugConsole::linkStatusUpdate(const QString& name,const QString& text) ...@@ -211,7 +220,7 @@ void DebugConsole::linkStatusUpdate(const QString& name,const QString& text)
m_ui->receiveText->ensureCursorVisible(); m_ui->receiveText->ensureCursorVisible();
} }
void DebugConsole::linkSelected(int linkId) void DebugConsole::linkSelected(int linkIndex)
{ {
// Disconnect // Disconnect
if (currLink) if (currLink)
...@@ -226,8 +235,8 @@ void DebugConsole::linkSelected(int linkId) ...@@ -226,8 +235,8 @@ void DebugConsole::linkSelected(int linkId)
m_ui->receiveText->clear(); m_ui->receiveText->clear();
// Connect new link // Connect new link
if (linkId != -1) { if (linkIndex != -1) {
currLink = links[linkId]; currLink = _links[linkIndex].data();
connect(currLink, SIGNAL(bytesReceived(LinkInterface*,QByteArray)), this, SLOT(receiveBytes(LinkInterface*, QByteArray))); connect(currLink, SIGNAL(bytesReceived(LinkInterface*,QByteArray)), this, SLOT(receiveBytes(LinkInterface*, QByteArray)));
disconnect(currLink, &LinkInterface::connected, this, &DebugConsole::_linkConnected); disconnect(currLink, &LinkInterface::connected, this, &DebugConsole::_linkConnected);
connect(currLink,SIGNAL(communicationUpdate(QString,QString)),this,SLOT(linkStatusUpdate(QString,QString))); connect(currLink,SIGNAL(communicationUpdate(QString,QString)),this,SLOT(linkStatusUpdate(QString,QString)));
...@@ -241,13 +250,21 @@ void DebugConsole::linkSelected(int linkId) ...@@ -241,13 +250,21 @@ void DebugConsole::linkSelected(int linkId)
*/ */
void DebugConsole::updateLinkName(QString name) void DebugConsole::updateLinkName(QString name)
{ {
// Set name if signal came from a link
LinkInterface* link = qobject_cast<LinkInterface*>(sender()); LinkInterface* link = qobject_cast<LinkInterface*>(sender());
if((link != NULL) && (links.contains(link))) if (link != NULL) {
{ bool found = false;
const qint16 &linkIndex(links.indexOf(link)); int linkIndex;
m_ui->linkComboBox->setItemText(linkIndex,name); 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) void DebugConsole::setAutoHold(bool hold)
......
...@@ -116,7 +116,6 @@ protected: ...@@ -116,7 +116,6 @@ protected:
/** @brief Cycle through the command history */ /** @brief Cycle through the command history */
void cycleCommandHistory(bool up); void cycleCommandHistory(bool up);
QList<LinkInterface*> links;
LinkInterface* currLink; LinkInterface* currLink;
bool holdOn; ///< Hold current view, ignore new data bool holdOn; ///< Hold current view, ignore new data
...@@ -150,6 +149,11 @@ private slots: ...@@ -150,6 +149,11 @@ private slots:
private: private:
/** @brief Set connection state of the current link */ /** @brief Set connection state of the current link */
void _setConnectionState(bool); 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<SharedLinkInterface> _links;
Ui::DebugConsole *m_ui; Ui::DebugConsole *m_ui;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment