1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

GCS ConnectionManager: Get rid of all the copies of various forms of the names and

store a handle to the IConnection::device and generate the appropriate names from
consistent functions.
This commit is contained in:
James Cotton 2012-09-08 15:14:06 -05:00
parent 1ca8abb3c8
commit 65ef8a59b4
3 changed files with 65 additions and 67 deletions

View File

@ -45,10 +45,10 @@ namespace Core {
ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, QTabWidget *modeStack) : ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, QTabWidget *modeStack) :
QWidget(mainWindow), QWidget(mainWindow),
m_availableDevList(0), m_availableDevList(0),
m_connectBtn(0), m_connectBtn(0),
m_ioDev(NULL), m_ioDev(NULL),
m_mainWindow(mainWindow) m_mainWindow(mainWindow)
{ {
QHBoxLayout *layout = new QHBoxLayout; QHBoxLayout *layout = new QHBoxLayout;
layout->setSpacing(5); layout->setSpacing(5);
@ -69,7 +69,7 @@ ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, QTabWidge
modeStack->setCornerWidget(this, Qt::TopRightCorner); modeStack->setCornerWidget(this, Qt::TopRightCorner);
QObject::connect(m_connectBtn, SIGNAL(pressed()), this, SLOT(onConnectPressed())); QObject::connect(m_connectBtn, SIGNAL(pressed()), this, SLOT(onConnectPressed()));
} }
ConnectionManager::~ConnectionManager() ConnectionManager::~ConnectionManager()
@ -82,8 +82,8 @@ void ConnectionManager::init()
{ {
//register to the plugin manager so we can receive //register to the plugin manager so we can receive
//new connection object from plugins //new connection object from plugins
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject*)), this, SLOT(objectAdded(QObject*))); QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject*)), this, SLOT(objectAdded(QObject*)));
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject*)), this, SLOT(aboutToRemoveObject(QObject*))); QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject*)), this, SLOT(aboutToRemoveObject(QObject*)));
} }
/** /**
@ -95,7 +95,7 @@ bool ConnectionManager::connectDevice()
if (!connection_device.connection) if (!connection_device.connection)
return false; return false;
QIODevice *io_dev = connection_device.connection->openDevice(connection_device.Name); QIODevice *io_dev = connection_device.connection->openDevice(connection_device.device.name);
if (!io_dev) if (!io_dev)
return false; return false;
@ -105,15 +105,6 @@ bool ConnectionManager::connectDevice()
if (!io_dev->isOpen()) { if (!io_dev->isOpen()) {
qDebug() << "Error: io_dev->isOpen() returned FALSE .. could not open connection to " << connection_device.devName qDebug() << "Error: io_dev->isOpen() returned FALSE .. could not open connection to " << connection_device.devName
<< ": " << io_dev->errorString(); << ": " << io_dev->errorString();
// close the device
// EDOUARD: why do we close if we could not open ???
try {
connection_device.connection->closeDevice(connection_device.devName);
}
catch (...) { // handle exception
qDebug() << "Exception: connection_device.connection->closeDevice(" << connection_device.devName << ")";
}
return false; return false;
} }
@ -150,8 +141,9 @@ bool ConnectionManager::disconnectDevice()
emit deviceAboutToDisconnect(); emit deviceAboutToDisconnect();
try { try {
if (m_connectionDevice.connection) if (m_connectionDevice.connection) {
m_connectionDevice.connection->closeDevice(m_connectionDevice.devName); m_connectionDevice.connection->closeDevice(m_connectionDevice.getConName());
}
} catch (...) { // handle exception } catch (...) { // handle exception
qDebug() << "Exception: m_connectionDevice.connection->closeDevice(" << m_connectionDevice.devName << ")"; qDebug() << "Exception: m_connectionDevice.connection->closeDevice(" << m_connectionDevice.devName << ")";
} }
@ -172,7 +164,7 @@ void ConnectionManager::objectAdded(QObject *obj)
{ {
//Check if a plugin added a connection object to the pool //Check if a plugin added a connection object to the pool
IConnection *connection = Aggregation::query<IConnection>(obj); IConnection *connection = Aggregation::query<IConnection>(obj);
if (!connection) return; if (!connection) return;
//qDebug() << "Connection object registered:" << connection->connectionName(); //qDebug() << "Connection object registered:" << connection->connectionName();
//qDebug() << connection->availableDevices(); //qDebug() << connection->availableDevices();
@ -184,24 +176,24 @@ void ConnectionManager::objectAdded(QObject *obj)
// to do things // to do things
m_connectionsList.append(connection); m_connectionsList.append(connection);
QObject::connect(connection, SIGNAL(availableDevChanged(IConnection *)), this, SLOT(devChanged(IConnection *))); QObject::connect(connection, SIGNAL(availableDevChanged(IConnection *)), this, SLOT(devChanged(IConnection *)));
} }
void ConnectionManager::aboutToRemoveObject(QObject *obj) void ConnectionManager::aboutToRemoveObject(QObject *obj)
{ {
//Check if a plugin added a connection object to the pool //Check if a plugin added a connection object to the pool
IConnection *connection = Aggregation::query<IConnection>(obj); IConnection *connection = Aggregation::query<IConnection>(obj);
if (!connection) return; if (!connection) return;
if (m_connectionDevice.connection && m_connectionDevice.connection == connection) if (m_connectionDevice.connection && m_connectionDevice.connection == connection)
{ // we are currently using the one that is about to be removed { // we are currently using the one that is about to be removed
disconnectDevice(); disconnectDevice();
m_connectionDevice.connection = NULL; m_connectionDevice.connection = NULL;
m_ioDev = NULL; m_ioDev = NULL;
} }
if (m_connectionsList.contains(connection)) if (m_connectionsList.contains(connection))
m_connectionsList.removeAt(m_connectionsList.indexOf(connection)); m_connectionsList.removeAt(m_connectionsList.indexOf(connection));
} }
@ -233,9 +225,9 @@ void ConnectionManager::onConnectPressed()
*/ */
devListItem ConnectionManager::findDevice(const QString &devName) devListItem ConnectionManager::findDevice(const QString &devName)
{ {
foreach (devListItem d, m_devList) foreach (devListItem d, m_devList)
{ {
if (d.devName == devName) if (d.getConName() == devName)
return d; return d;
} }
@ -253,13 +245,13 @@ devListItem ConnectionManager::findDevice(const QString &devName)
*/ */
void ConnectionManager::suspendPolling() void ConnectionManager::suspendPolling()
{ {
foreach (IConnection *cnx, m_connectionsList) foreach (IConnection *cnx, m_connectionsList)
{ {
cnx->suspendPolling(); cnx->suspendPolling();
} }
m_connectBtn->setEnabled(false); m_connectBtn->setEnabled(false);
m_availableDevList->setEnabled(false); m_availableDevList->setEnabled(false);
} }
/** /**
@ -268,13 +260,13 @@ void ConnectionManager::suspendPolling()
*/ */
void ConnectionManager::resumePolling() void ConnectionManager::resumePolling()
{ {
foreach (IConnection *cnx, m_connectionsList) foreach (IConnection *cnx, m_connectionsList)
{ {
cnx->resumePolling(); cnx->resumePolling();
} }
m_connectBtn->setEnabled(true); m_connectBtn->setEnabled(true);
m_availableDevList->setEnabled(true); m_availableDevList->setEnabled(true);
} }
/** /**
@ -291,9 +283,9 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
// disconnect them. // disconnect them.
for (QLinkedList<devListItem>::iterator iter = m_devList.begin(); iter != m_devList.end(); ) for (QLinkedList<devListItem>::iterator iter = m_devList.begin(); iter != m_devList.end(); )
{ {
if (iter->connection == connection) if (iter->connection == connection)
{ {
// See if device exists in the updated availability list // See if device exists in the updated availability list
bool found = false; bool found = false;
foreach (IConnection::device dev, availableDev) foreach (IConnection::device dev, availableDev)
@ -303,17 +295,16 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
if (!found) { if (!found) {
if (m_connectionDevice.connection && m_connectionDevice.connection == connection && m_connectionDevice.device == iter->device) if (m_connectionDevice.connection && m_connectionDevice.connection == connection && m_connectionDevice.device == iter->device)
{ // we are currently using the one we are about to erase { // we are currently using the one we are about to erase
//onConnectionClosed(m_connectionDevice.connection);
disconnectDevice(); disconnectDevice();
} }
iter = m_devList.erase(iter); iter = m_devList.erase(iter);
} else } else
++iter; ++iter;
} }
else else
++iter; ++iter;
} }
// Go through the list of available devices. If they are not present in the device list // Go through the list of available devices. If they are not present in the device list
// add them. // add them.
@ -344,9 +335,6 @@ void ConnectionManager::registerDevice(IConnection *conn, IConnection::device de
devListItem d; devListItem d;
d.connection = conn; d.connection = conn;
d.device = device; d.device = device;
d.devName = conn->shortName() + ": " + device.name;
d.Name = device.name;
d.displayName = conn->shortName() + " : " + device.displayName;
m_devList.append(d); m_devList.append(d);
} }
@ -369,12 +357,24 @@ void ConnectionManager::devChanged(IConnection *connection)
//remove registered devices of this IConnection from the list //remove registered devices of this IConnection from the list
updateConnectionList(connection); updateConnectionList(connection);
updateConnectionDropdown();
//disable connection button if the liNameif (m_availableDevList->count() > 0)
if (m_availableDevList->count() > 0)
m_connectBtn->setEnabled(true);
else
m_connectBtn->setEnabled(false);
}
void ConnectionManager::updateConnectionDropdown()
{
//add all the list again to the combobox //add all the list again to the combobox
foreach (devListItem d, m_devList) foreach (devListItem d, m_devList)
{ {
m_availableDevList->addItem(d.displayName); m_availableDevList->addItem(d.getConName());
m_availableDevList->setItemData(m_availableDevList->count()-1,(const QString)d.devName,Qt::ToolTipRole); m_availableDevList->setItemData(m_availableDevList->count()-1, d.getConName(), Qt::ToolTipRole);
if(!m_ioDev && d.displayName.startsWith("USB")) if(!m_ioDev && d.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);
@ -389,19 +389,10 @@ void ConnectionManager::devChanged(IConnection *connection)
{ {
for(int x=0;x<m_availableDevList->count();++x) for(int x=0;x<m_availableDevList->count();++x)
{ {
if(m_connectionDevice.devName==m_availableDevList->itemData(x,Qt::ToolTipRole).toString()) if(m_connectionDevice.getConName()==m_availableDevList->itemData(x,Qt::ToolTipRole).toString())
m_availableDevList->setCurrentIndex(x); m_availableDevList->setCurrentIndex(x);
} }
} }
//disable connection button if the liNameif (m_availableDevList->count() > 0)
if (m_availableDevList->count() > 0)
m_connectBtn->setEnabled(true);
else
m_connectBtn->setEnabled(false);
}
} }
void Core::ConnectionManager::connectionsCallBack() void Core::ConnectionManager::connectionsCallBack()
@ -412,4 +403,6 @@ void Core::ConnectionManager::connectionsCallBack()
} }
connectionBackup.clear(); connectionBackup.clear();
disconnect(ExtensionSystem::PluginManager::instance(),SIGNAL(pluginsLoadEnded()),this,SLOT(connectionsCallBack())); disconnect(ExtensionSystem::PluginManager::instance(),SIGNAL(pluginsLoadEnded()),this,SLOT(connectionsCallBack()));
}
} //namespace Core } //namespace Core

View File

@ -56,13 +56,17 @@ namespace Internal {
} // namespace Internal } // namespace Internal
struct devListItem class devListItem
{ {
public:
IConnection *connection; IConnection *connection;
IConnection::device device; IConnection::device device;
QString devName;
QString Name; QString getConName() {
QString displayName; if (connection == NULL)
return "";
return connection->shortName() + ": " + device.displayName;
}
}; };
@ -85,6 +89,7 @@ public:
protected: protected:
void updateConnectionList(IConnection *connection); void updateConnectionList(IConnection *connection);
void registerDevice(IConnection *conn, IConnection::device device); void registerDevice(IConnection *conn, IConnection::device device);
void updateConnectionDropdown();
devListItem findDevice(const QString &devName); devListItem findDevice(const QString &devName);
signals: signals:

View File

@ -249,8 +249,8 @@ void UploaderGadgetWidget::goToBootloader(UAVObject* callerObj, bool success)
// The board is now reset: we have to disconnect telemetry // The board is now reset: we have to disconnect telemetry
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager(); Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
QString dli = cm->getCurrentDevice().Name; QString dli = cm->getCurrentDevice().getConName();
QString dlj = cm->getCurrentDevice().devName; QString dlj = cm->getCurrentDevice().getConName();
cm->disconnectDevice(); cm->disconnectDevice();
QTimer::singleShot(200, &m_eventloop, SLOT(quit())); QTimer::singleShot(200, &m_eventloop, SLOT(quit()));
m_eventloop.exec(); m_eventloop.exec();