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

GCS ConnectionManager: Use some cleaner code coventions

This commit is contained in:
James Cotton 2012-09-08 15:45:08 -05:00
parent 19eaa77a15
commit 8e8cbd0b75
2 changed files with 19 additions and 20 deletions

View File

@ -91,7 +91,7 @@ void ConnectionManager::init()
*/ */
bool ConnectionManager::connectDevice() bool ConnectionManager::connectDevice()
{ {
devListItem connection_device = findDevice(m_availableDevList->itemData(m_availableDevList->currentIndex(),Qt::ToolTipRole).toString()); DevListItem connection_device = findDevice(m_availableDevList->itemData(m_availableDevList->currentIndex(),Qt::ToolTipRole).toString());
if (!connection_device.connection) if (!connection_device.connection)
return false; return false;
@ -217,9 +217,9 @@ void ConnectionManager::onConnectPressed()
/** /**
* 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(const QString &devName)
{ {
foreach (devListItem d, m_devList) foreach (DevListItem d, m_devList)
{ {
if (d.getConName() == devName) if (d.getConName() == devName)
return d; return d;
@ -227,7 +227,7 @@ devListItem ConnectionManager::findDevice(const QString &devName)
qDebug() << "findDevice: cannot find " << devName << " in device list"; qDebug() << "findDevice: cannot find " << devName << " in device list";
devListItem d; DevListItem d;
d.connection = NULL; d.connection = NULL;
return d; return d;
} }
@ -276,21 +276,16 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
// available device list then remove them. If they are connected, then // available device list then remove them. If they are connected, then
// 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 = availableDev.contains(iter->device);
foreach (IConnection::device dev, availableDev)
if (iter->device == dev) // TODO: Need some way of indicating a IOConnection::device matches a devListItem
found = true;
if (!found) { if (!found) {
// we are currently using the one we are about to erase
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
disconnectDevice(); disconnectDevice();
}
iter = m_devList.erase(iter); iter = m_devList.erase(iter);
} else } else
@ -307,7 +302,7 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
foreach (IConnection::device dev, availableDev) foreach (IConnection::device dev, availableDev)
{ {
bool found = false; bool found = false;
foreach (devListItem devList, m_devList) foreach (DevListItem devList, m_devList)
if (devList.device == dev) if (devList.device == dev)
found = true; found = true;
@ -326,7 +321,7 @@ void ConnectionManager::updateConnectionList(IConnection *connection)
*/ */
void ConnectionManager::registerDevice(IConnection *conn, IConnection::device device) void ConnectionManager::registerDevice(IConnection *conn, IConnection::device device)
{ {
devListItem d; DevListItem d;
d.connection = conn; d.connection = conn;
d.device = device; d.device = device;
m_devList.append(d); m_devList.append(d);
@ -364,7 +359,7 @@ 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) foreach (DevListItem d, m_devList)
{ {
m_availableDevList->addItem(d.getConName()); m_availableDevList->addItem(d.getConName());
m_availableDevList->setItemData(m_availableDevList->count()-1, d.getConName(), Qt::ToolTipRole); m_availableDevList->setItemData(m_availableDevList->count()-1, d.getConName(), Qt::ToolTipRole);

View File

@ -56,7 +56,7 @@ namespace Internal {
} // namespace Internal } // namespace Internal
class devListItem class DevListItem
{ {
public: public:
IConnection *connection; IConnection *connection;
@ -67,6 +67,10 @@ public:
return ""; return "";
return connection->shortName() + ": " + device.displayName; return connection->shortName() + ": " + device.displayName;
} }
bool operator==(const DevListItem &rhs) {
return connection == rhs.connection && device == rhs.device;
}
}; };
@ -81,7 +85,7 @@ public:
void init(); void init();
QIODevice* getCurrentConnection() { return m_ioDev; } QIODevice* getCurrentConnection() { return m_ioDev; }
devListItem getCurrentDevice() { return m_connectionDevice;} DevListItem getCurrentDevice() { return m_connectionDevice;}
bool disconnectDevice(); bool disconnectDevice();
void suspendPolling(); void suspendPolling();
void resumePolling(); void resumePolling();
@ -90,7 +94,7 @@ 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(); void updateConnectionDropdown();
devListItem findDevice(const QString &devName); DevListItem findDevice(const QString &devName);
signals: signals:
void deviceConnected(QIODevice *dev); void deviceConnected(QIODevice *dev);
@ -108,11 +112,11 @@ private slots:
protected: protected:
QComboBox *m_availableDevList; QComboBox *m_availableDevList;
QPushButton *m_connectBtn; QPushButton *m_connectBtn;
QLinkedList<devListItem> m_devList; QLinkedList<DevListItem> m_devList;
QList<IConnection*> m_connectionsList; QList<IConnection*> m_connectionsList;
//currently connected connection plugin //currently connected connection plugin
devListItem m_connectionDevice; DevListItem m_connectionDevice;
//currently connected QIODevice //currently connected QIODevice
QIODevice *m_ioDev; QIODevice *m_ioDev;