1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-19 04:52:12 +01:00

LP-438: fixed connection manager ugly side effects (triggered by LP-430 )

This commit is contained in:
Eric Price 2016-10-25 20:44:59 +02:00
parent e00c0fad63
commit 105219e714
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)
{
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<DevListItem>::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());

View File

@ -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<DevListItem> getAvailableDevices()
{

View File

@ -170,8 +170,11 @@ void ControllerPage::devicesChanged(QLinkedList<Core::DevListItem> 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();
}