From 105219e7146aa0d43df120ac69ef8e2a20796816 Mon Sep 17 00:00:00 2001 From: Eric Price Date: Tue, 25 Oct 2016 20:44:59 +0200 Subject: [PATCH] LP-438: fixed connection manager ugly side effects (triggered by LP-430 ) --- .../plugins/coreplugin/connectionmanager.cpp | 31 ++++++++----------- .../plugins/coreplugin/connectionmanager.h | 3 +- .../setupwizard/pages/controllerpage.cpp | 13 +++----- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/ground/gcs/src/plugins/coreplugin/connectionmanager.cpp b/ground/gcs/src/plugins/coreplugin/connectionmanager.cpp index 1e1cc0410..2831835ec 100644 --- a/ground/gcs/src/plugins/coreplugin/connectionmanager.cpp +++ b/ground/gcs/src/plugins/coreplugin/connectionmanager.cpp @@ -130,8 +130,7 @@ void ConnectionManager::addWidget(QWidget *widget) bool ConnectionManager::connectDevice(DevListItem device) { Q_UNUSED(device); - QString deviceName = m_availableDevList->itemData(m_availableDevList->currentIndex(), Qt::ToolTipRole).toString(); - DevListItem connection_device = findDevice(deviceName); + DevListItem connection_device = findDevice(m_availableDevList->currentIndex()); if (!connection_device.connection) { return false; @@ -279,8 +278,7 @@ void ConnectionManager::onConnectClicked() // Check if we have a ioDev already created: if (!m_ioDev) { // connecting to currently selected device - QString deviceName = m_availableDevList->itemData(m_availableDevList->currentIndex(), Qt::ToolTipRole).toString(); - DevListItem device = findDevice(deviceName); + DevListItem device = findDevice(m_availableDevList->currentIndex()); if (device.connection) { connectDevice(device); } @@ -343,15 +341,15 @@ void ConnectionManager::reconnectCheckSlot() /** * Find a device by its displayed (visible on screen) name */ -DevListItem ConnectionManager::findDevice(const QString &devName) +DevListItem ConnectionManager::findDevice(int devNumber) { foreach(DevListItem d, m_devList) { - if (d.getConName() == devName) { + if (d.displayNumber == devNumber) { return d; } } - qDebug() << "findDevice: cannot find " << devName << " in device list"; + qDebug() << "findDevice: cannot find item in device list"; DevListItem d; d.connection = NULL; @@ -480,28 +478,25 @@ void ConnectionManager::devChanged(IConnection *connection) void ConnectionManager::updateConnectionDropdown() { // add all the list again to the combobox - foreach(DevListItem d, m_devList) { - m_availableDevList->addItem(d.getConName()); - m_availableDevList->setItemData(m_availableDevList->count() - 1, d.getConDescription(), Qt::ToolTipRole); - if (!m_ioDev && d.getConName().startsWith("USB")) { + for (QLinkedList::iterator iter = m_devList.begin(); iter != m_devList.end(); ++iter) { + m_availableDevList->addItem(iter->getConName()); + // record position in the box in the device + iter->displayNumber = m_availableDevList->count() - 1; + m_availableDevList->setItemData(m_availableDevList->count() - 1, iter->getConDescription(), Qt::ToolTipRole); + if (!m_ioDev && iter->getConName().startsWith("USB")) { if (m_mainWindow->generalSettings()->autoConnect() || m_mainWindow->generalSettings()->autoSelect()) { m_availableDevList->setCurrentIndex(m_availableDevList->count() - 1); } if (m_mainWindow->generalSettings()->autoConnect() && polling) { qDebug() << "Automatically opening device"; - connectDevice(d); + connectDevice(*iter); qDebug() << "ConnectionManager::updateConnectionDropdown autoconnected USB device"; } } } if (m_ioDev) { // if a device is connected make it the one selected on the dropbox - for (int i = 0; i < m_availableDevList->count(); i++) { - QString deviceName = m_availableDevList->itemData(i, Qt::ToolTipRole).toString(); - if (m_connectionDevice.getConName() == deviceName) { - m_availableDevList->setCurrentIndex(i); - } - } + m_availableDevList->setCurrentIndex(m_connectionDevice.displayNumber); } // update combo box tooltip onDeviceSelectionChanged(m_availableDevList->currentIndex()); diff --git a/ground/gcs/src/plugins/coreplugin/connectionmanager.h b/ground/gcs/src/plugins/coreplugin/connectionmanager.h index 50a9257a4..f46af6a31 100644 --- a/ground/gcs/src/plugins/coreplugin/connectionmanager.h +++ b/ground/gcs/src/plugins/coreplugin/connectionmanager.h @@ -65,6 +65,7 @@ public: return connection == rhs.connection && device == rhs.device; } + int displayNumber = -1; IConnection *connection; IConnection::device device; }; @@ -83,7 +84,7 @@ public: { return m_connectionDevice; } - DevListItem findDevice(const QString &devName); + DevListItem findDevice(int devNumber); QLinkedList getAvailableDevices() { diff --git a/ground/gcs/src/plugins/setupwizard/pages/controllerpage.cpp b/ground/gcs/src/plugins/setupwizard/pages/controllerpage.cpp index 2235220b4..f84833e96 100644 --- a/ground/gcs/src/plugins/setupwizard/pages/controllerpage.cpp +++ b/ground/gcs/src/plugins/setupwizard/pages/controllerpage.cpp @@ -170,8 +170,11 @@ void ControllerPage::devicesChanged(QLinkedList devices) // Loop and fill the combo with items from connectionmanager foreach(Core::DevListItem deviceItem, devices) { ui->deviceCombo->addItem(deviceItem.getConName()); + // TODO - have tooltips similar to how connection manager does QString deviceName = (const QString)deviceItem.getConName(); ui->deviceCombo->setItemData(ui->deviceCombo->count() - 1, deviceName, Qt::ToolTipRole); + // we fill a combobox with items in the same order as the connectionmanager, so they should have the same numerical ids. if not, things break. + Q_ASSERT(ui->deviceCombo->count() - 1 == deviceItem.displayNumber); if (!deviceName.startsWith("USB:", Qt::CaseInsensitive)) { ui->deviceCombo->setItemData(ui->deviceCombo->count() - 1, QVariant(0), Qt::UserRole - 1); } @@ -194,13 +197,7 @@ void ControllerPage::connectionStatusChanged() ui->deviceCombo->setEnabled(false); ui->connectButton->setText(tr("Disconnect")); ui->boardTypeCombo->setEnabled(false); - QString connectedDeviceName = m_connectionManager->getCurrentDevice().getConName(); - for (int i = 0; i < ui->deviceCombo->count(); ++i) { - if (connectedDeviceName == ui->deviceCombo->itemData(i, Qt::ToolTipRole).toString()) { - ui->deviceCombo->setCurrentIndex(i); - break; - } - } + ui->deviceCombo->setCurrentIndex(m_connectionManager->getCurrentDevice().displayNumber); SetupWizard::CONTROLLER_TYPE type = getControllerType(); setControllerType(type); @@ -256,7 +253,7 @@ void ControllerPage::connectDisconnect() if (m_connectionManager->isConnected()) { m_connectionManager->disconnectDevice(); } else { - m_connectionManager->connectDevice(m_connectionManager->findDevice(ui->deviceCombo->itemData(ui->deviceCombo->currentIndex(), Qt::ToolTipRole).toString())); + m_connectionManager->connectDevice(m_connectionManager->findDevice(ui->deviceCombo->currentIndex())); } emit completeChanged(); }