1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-17 02:52:12 +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) :
QWidget(mainWindow),
m_availableDevList(0),
m_availableDevList(0),
m_connectBtn(0),
m_ioDev(NULL),
m_mainWindow(mainWindow)
m_ioDev(NULL),
m_mainWindow(mainWindow)
{
QHBoxLayout *layout = new QHBoxLayout;
layout->setSpacing(5);
@ -69,7 +69,7 @@ ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, QTabWidge
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()
@ -82,8 +82,8 @@ void ConnectionManager::init()
{
//register to the plugin manager so we can receive
//new connection object from plugins
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(objectAdded(QObject*)), this, SLOT(objectAdded(QObject*)));
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject*)), this, SLOT(aboutToRemoveObject(QObject*)));
}
/**
@ -95,7 +95,7 @@ bool ConnectionManager::connectDevice()
if (!connection_device.connection)
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)
return false;
@ -105,15 +105,6 @@ bool ConnectionManager::connectDevice()
if (!io_dev->isOpen()) {
qDebug() << "Error: io_dev->isOpen() returned FALSE .. could not open connection to " << connection_device.devName
<< ": " << 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;
}
@ -150,8 +141,9 @@ bool ConnectionManager::disconnectDevice()
emit deviceAboutToDisconnect();
try {
if (m_connectionDevice.connection)
m_connectionDevice.connection->closeDevice(m_connectionDevice.devName);
if (m_connectionDevice.connection) {
m_connectionDevice.connection->closeDevice(m_connectionDevice.getConName());
}
} catch (...) { // handle exception
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
IConnection *connection = Aggregation::query<IConnection>(obj);
if (!connection) return;
if (!connection) return;
//qDebug() << "Connection object registered:" << connection->connectionName();
//qDebug() << connection->availableDevices();
@ -184,24 +176,24 @@ void ConnectionManager::objectAdded(QObject *obj)
// to do things
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)
{
//Check if a plugin added a connection object to the pool
IConnection *connection = Aggregation::query<IConnection>(obj);
if (!connection) return;
if (!connection) return;
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();
m_connectionDevice.connection = NULL;
m_ioDev = NULL;
}
m_connectionDevice.connection = NULL;
m_ioDev = NULL;
}
if (m_connectionsList.contains(connection))
m_connectionsList.removeAt(m_connectionsList.indexOf(connection));
if (m_connectionsList.contains(connection))
m_connectionsList.removeAt(m_connectionsList.indexOf(connection));
}
@ -233,9 +225,9 @@ void ConnectionManager::onConnectPressed()
*/
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;
}
@ -253,13 +245,13 @@ devListItem ConnectionManager::findDevice(const QString &devName)
*/
void ConnectionManager::suspendPolling()
{
foreach (IConnection *cnx, m_connectionsList)
{
foreach (IConnection *cnx, m_connectionsList)
{
cnx->suspendPolling();
}
m_connectBtn->setEnabled(false);
m_availableDevList->setEnabled(false);
m_connectBtn->setEnabled(false);
m_availableDevList->setEnabled(false);
}
/**
@ -268,13 +260,13 @@ void ConnectionManager::suspendPolling()
*/
void ConnectionManager::resumePolling()
{
foreach (IConnection *cnx, m_connectionsList)
{
foreach (IConnection *cnx, m_connectionsList)
{
cnx->resumePolling();
}
m_connectBtn->setEnabled(true);
m_availableDevList->setEnabled(true);
m_connectBtn->setEnabled(true);
m_availableDevList->setEnabled(true);
}
/**
@ -291,9 +283,9 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
// disconnect them.
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
bool found = false;
foreach (IConnection::device dev, availableDev)
@ -303,17 +295,16 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
if (!found) {
if (m_connectionDevice.connection && m_connectionDevice.connection == connection && m_connectionDevice.device == iter->device)
{ // we are currently using the one we are about to erase
//onConnectionClosed(m_connectionDevice.connection);
disconnectDevice();
}
iter = m_devList.erase(iter);
} else
++iter;
}
else
++iter;
}
}
else
++iter;
}
// Go through the list of available devices. If they are not present in the device list
// add them.
@ -344,9 +335,6 @@ void ConnectionManager::registerDevice(IConnection *conn, IConnection::device de
devListItem d;
d.connection = conn;
d.device = device;
d.devName = conn->shortName() + ": " + device.name;
d.Name = device.name;
d.displayName = conn->shortName() + " : " + device.displayName;
m_devList.append(d);
}
@ -369,12 +357,24 @@ void ConnectionManager::devChanged(IConnection *connection)
//remove registered devices of this IConnection from the list
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
foreach (devListItem d, m_devList)
{
m_availableDevList->addItem(d.displayName);
m_availableDevList->setItemData(m_availableDevList->count()-1,(const QString)d.devName,Qt::ToolTipRole);
if(!m_ioDev && d.displayName.startsWith("USB"))
m_availableDevList->addItem(d.getConName());
m_availableDevList->setItemData(m_availableDevList->count()-1, d.getConName(), Qt::ToolTipRole);
if(!m_ioDev && d.getConName().startsWith("USB"))
{
if(m_mainWindow->generalSettings()->autoConnect() || m_mainWindow->generalSettings()->autoSelect())
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)
{
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);
}
}
//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()
@ -412,4 +403,6 @@ void Core::ConnectionManager::connectionsCallBack()
}
connectionBackup.clear();
disconnect(ExtensionSystem::PluginManager::instance(),SIGNAL(pluginsLoadEnded()),this,SLOT(connectionsCallBack()));
}
} //namespace Core

View File

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

View File

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