1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-15 07:29:15 +01:00

Merged in corvusvcorax/librepilot (pull request #349)

LP-438: fixed connection manager ugly side effects (triggered by LP-430 )
This commit is contained in:
Lalanne Laurent 2016-10-26 20:15:16 +00:00
commit b05f7d96a5
3 changed files with 20 additions and 27 deletions

View File

@ -130,8 +130,7 @@ void ConnectionManager::addWidget(QWidget *widget)
bool ConnectionManager::connectDevice(DevListItem device) bool ConnectionManager::connectDevice(DevListItem device)
{ {
Q_UNUSED(device); Q_UNUSED(device);
QString deviceName = m_availableDevList->itemData(m_availableDevList->currentIndex(), Qt::ToolTipRole).toString(); DevListItem connection_device = findDevice(m_availableDevList->currentIndex());
DevListItem connection_device = findDevice(deviceName);
if (!connection_device.connection) { if (!connection_device.connection) {
return false; return false;
@ -279,8 +278,7 @@ void ConnectionManager::onConnectClicked()
// Check if we have a ioDev already created: // Check if we have a ioDev already created:
if (!m_ioDev) { if (!m_ioDev) {
// connecting to currently selected device // connecting to currently selected device
QString deviceName = m_availableDevList->itemData(m_availableDevList->currentIndex(), Qt::ToolTipRole).toString(); DevListItem device = findDevice(m_availableDevList->currentIndex());
DevListItem device = findDevice(deviceName);
if (device.connection) { if (device.connection) {
connectDevice(device); connectDevice(device);
} }
@ -343,15 +341,15 @@ void ConnectionManager::reconnectCheckSlot()
/** /**
* Find a device by its displayed (visible on screen) name * 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) { foreach(DevListItem d, m_devList) {
if (d.getConName() == devName) { if (d.displayNumber == devNumber) {
return d; return d;
} }
} }
qDebug() << "findDevice: cannot find " << devName << " in device list"; qDebug() << "findDevice: cannot find item in device list";
DevListItem d; DevListItem d;
d.connection = NULL; d.connection = NULL;
@ -480,28 +478,25 @@ void ConnectionManager::devChanged(IConnection *connection)
void ConnectionManager::updateConnectionDropdown() void ConnectionManager::updateConnectionDropdown()
{ {
// add all the list again to the combobox // add all the list again to the combobox
foreach(DevListItem d, m_devList) { for (QLinkedList<DevListItem>::iterator iter = m_devList.begin(); iter != m_devList.end(); ++iter) {
m_availableDevList->addItem(d.getConName()); m_availableDevList->addItem(iter->getConName());
m_availableDevList->setItemData(m_availableDevList->count() - 1, d.getConDescription(), Qt::ToolTipRole); // record position in the box in the device
if (!m_ioDev && d.getConName().startsWith("USB")) { 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()) { if (m_mainWindow->generalSettings()->autoConnect() || m_mainWindow->generalSettings()->autoSelect()) {
m_availableDevList->setCurrentIndex(m_availableDevList->count() - 1); m_availableDevList->setCurrentIndex(m_availableDevList->count() - 1);
} }
if (m_mainWindow->generalSettings()->autoConnect() && polling) { if (m_mainWindow->generalSettings()->autoConnect() && polling) {
qDebug() << "Automatically opening device"; qDebug() << "Automatically opening device";
connectDevice(d); connectDevice(*iter);
qDebug() << "ConnectionManager::updateConnectionDropdown autoconnected USB device"; qDebug() << "ConnectionManager::updateConnectionDropdown autoconnected USB device";
} }
} }
} }
if (m_ioDev) { if (m_ioDev) {
// if a device is connected make it the one selected on the dropbox // if a device is connected make it the one selected on the dropbox
for (int i = 0; i < m_availableDevList->count(); i++) { m_availableDevList->setCurrentIndex(m_connectionDevice.displayNumber);
QString deviceName = m_availableDevList->itemData(i, Qt::ToolTipRole).toString();
if (m_connectionDevice.getConName() == deviceName) {
m_availableDevList->setCurrentIndex(i);
}
}
} }
// update combo box tooltip // update combo box tooltip
onDeviceSelectionChanged(m_availableDevList->currentIndex()); onDeviceSelectionChanged(m_availableDevList->currentIndex());

View File

@ -65,6 +65,7 @@ public:
return connection == rhs.connection && device == rhs.device; return connection == rhs.connection && device == rhs.device;
} }
int displayNumber = -1;
IConnection *connection; IConnection *connection;
IConnection::device device; IConnection::device device;
}; };
@ -83,7 +84,7 @@ public:
{ {
return m_connectionDevice; return m_connectionDevice;
} }
DevListItem findDevice(const QString &devName); DevListItem findDevice(int devNumber);
QLinkedList<DevListItem> getAvailableDevices() QLinkedList<DevListItem> getAvailableDevices()
{ {

View File

@ -170,8 +170,11 @@ void ControllerPage::devicesChanged(QLinkedList<Core::DevListItem> devices)
// Loop and fill the combo with items from connectionmanager // Loop and fill the combo with items from connectionmanager
foreach(Core::DevListItem deviceItem, devices) { foreach(Core::DevListItem deviceItem, devices) {
ui->deviceCombo->addItem(deviceItem.getConName()); ui->deviceCombo->addItem(deviceItem.getConName());
// TODO - have tooltips similar to how connection manager does
QString deviceName = (const QString)deviceItem.getConName(); QString deviceName = (const QString)deviceItem.getConName();
ui->deviceCombo->setItemData(ui->deviceCombo->count() - 1, deviceName, Qt::ToolTipRole); 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)) { if (!deviceName.startsWith("USB:", Qt::CaseInsensitive)) {
ui->deviceCombo->setItemData(ui->deviceCombo->count() - 1, QVariant(0), Qt::UserRole - 1); ui->deviceCombo->setItemData(ui->deviceCombo->count() - 1, QVariant(0), Qt::UserRole - 1);
} }
@ -194,13 +197,7 @@ void ControllerPage::connectionStatusChanged()
ui->deviceCombo->setEnabled(false); ui->deviceCombo->setEnabled(false);
ui->connectButton->setText(tr("Disconnect")); ui->connectButton->setText(tr("Disconnect"));
ui->boardTypeCombo->setEnabled(false); ui->boardTypeCombo->setEnabled(false);
QString connectedDeviceName = m_connectionManager->getCurrentDevice().getConName(); ui->deviceCombo->setCurrentIndex(m_connectionManager->getCurrentDevice().displayNumber);
for (int i = 0; i < ui->deviceCombo->count(); ++i) {
if (connectedDeviceName == ui->deviceCombo->itemData(i, Qt::ToolTipRole).toString()) {
ui->deviceCombo->setCurrentIndex(i);
break;
}
}
SetupWizard::CONTROLLER_TYPE type = getControllerType(); SetupWizard::CONTROLLER_TYPE type = getControllerType();
setControllerType(type); setControllerType(type);
@ -256,7 +253,7 @@ void ControllerPage::connectDisconnect()
if (m_connectionManager->isConnected()) { if (m_connectionManager->isConnected()) {
m_connectionManager->disconnectDevice(); m_connectionManager->disconnectDevice();
} else { } 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(); emit completeChanged();
} }