dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
controllerpage.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 #include "controllerpage.h"
31 #include "ui_controllerpage.h"
32 #include "setupwizard.h"
33 
36 
38  : AbstractWizardPage(wizard, parent)
39  , ui(new Ui::ControllerPage)
40 {
41  ui->setupUi(this);
42 
43  m_connectionManager = getWizard()->getConnectionManager();
44  Q_ASSERT(m_connectionManager);
45  connect(m_connectionManager, &Core::ConnectionManager::availableDevicesChanged, this,
46  &ControllerPage::devicesChanged);
47 
48  ExtensionSystem::PluginManager *pluginManager = ExtensionSystem::PluginManager::instance();
49  Q_ASSERT(pluginManager);
50  m_telemetryManager = pluginManager->getObject<TelemetryManager>();
51  Q_ASSERT(m_telemetryManager);
52  connect(m_telemetryManager, &TelemetryManager::connected, this,
53  &ControllerPage::connectionStatusChanged);
54  /* Consider us "connected" when telemetry manager says so, but
55  * disconnected the instant connection manager knows the conn is down.
56  */
57  connect(m_connectionManager, &Core::ConnectionManager::deviceDisconnected, this,
58  &ControllerPage::connectionStatusChanged);
59 
60  connect(ui->connectButton, &QAbstractButton::clicked, this, &ControllerPage::connectDisconnect);
61 
62  setupDeviceList();
63 }
64 
66 {
67  delete ui;
68 }
69 
71 {
72  if (anyControllerConnected()) {
74  setControllerType(type);
75  } else {
76  setControllerType(NULL);
77  }
78  emit completeChanged();
79 }
80 
82 {
84 
85  if (type == NULL)
86  return false;
87 
88  /*
89  * Only allow you to continue with setup wizard if you have a USB-type
90  * connection... or if the type doesn't support USB
91  * (e.g. simulation/posix)
92  */
93  return !type->isUSBSupported()
94  || m_connectionManager->getCurrentDevice().getConName().startsWith("USB:",
95  Qt::CaseInsensitive);
96 }
97 
99 {
101  return true;
102 }
103 
104 bool ControllerPage::anyControllerConnected()
105 {
106  return m_telemetryManager->isConnected();
107 }
108 
109 void ControllerPage::setupDeviceList()
110 {
111  devicesChanged(m_connectionManager->getAvailableDevices());
112  connectionStatusChanged();
113 }
114 
115 void ControllerPage::setControllerType(Core::IBoardType *board)
116 {
117  if (board == NULL)
118  ui->boardTypeLabel->setText("Unknown");
119  else
120  ui->boardTypeLabel->setText(board->shortName());
121 }
122 
123 void ControllerPage::devicesChanged(QLinkedList<Core::DevListItem> devices)
124 {
125  // Get the selected item before the update if any
126  QString currSelectedDeviceName = ui->deviceCombo->currentIndex() != -1
127  ? ui->deviceCombo->itemData(ui->deviceCombo->currentIndex(), Qt::ToolTipRole).toString()
128  : "";
129 
130  // Clear the box
131  ui->deviceCombo->clear();
132 
133  int indexOfSelectedItem = -1;
134 
135  /* Sadly comparison of device doesn't seem to work... */
136  QString refName = m_connectionManager->getCurrentDevice().getConName();
137 
138  // Loop and fill the combo with items from connectionmanager
139  foreach (Core::DevListItem deviceItem, devices) {
140  QString deviceName = deviceItem.getConName();
141  if ((deviceName != refName) &&
142  (!deviceName.startsWith("USB:", Qt::CaseInsensitive))) {
143  continue;
144  }
145  ui->deviceCombo->addItem(deviceName);
146  ui->deviceCombo->setItemData(ui->deviceCombo->count() - 1, deviceName, Qt::ToolTipRole);
147  if (currSelectedDeviceName != "" && currSelectedDeviceName == deviceName) {
148  indexOfSelectedItem = ui->deviceCombo->count() - 1;
149  }
150  }
151 
152  // Re select the item that was selected before if any
153  if (indexOfSelectedItem != -1) {
154  ui->deviceCombo->setCurrentIndex(indexOfSelectedItem);
155  }
156 }
157 
158 void ControllerPage::connectionStatusChanged()
159 {
160  if (m_connectionManager->isConnected()) {
161  ui->deviceCombo->setEnabled(false);
162  ui->connectButton->setText(tr("Disconnect"));
163  QString connectedDeviceName = m_connectionManager->getCurrentDevice().getConName();
164  for (int i = 0; i < ui->deviceCombo->count(); ++i) {
165  if (connectedDeviceName == ui->deviceCombo->itemData(i, Qt::ToolTipRole).toString()) {
166  ui->deviceCombo->setCurrentIndex(i);
167  break;
168  }
169  }
170 
171  setControllerType(getControllerType());
172  qDebug() << "Connection status changed: Connected, controller type: "
173  << getControllerType();
174  } else {
175  ui->deviceCombo->setEnabled(true);
176  ui->connectButton->setText(tr("Connect"));
177  setControllerType(NULL);
178  qDebug() << "Connection status changed: Disconnected";
179  }
180  emit completeChanged();
181 }
182 
183 void ControllerPage::connectDisconnect()
184 {
185  if (m_connectionManager->isConnected()) {
186  m_connectionManager->disconnectDevice();
187  } else {
188  m_connectionManager->connectDevice(m_connectionManager->findDevice(
189  ui->deviceCombo->itemData(ui->deviceCombo->currentIndex(), Qt::ToolTipRole)
190  .toString()));
191  }
192  emit completeChanged();
193 }
The SetupWizard class is the main interface to the setup wizard. It provides selects the sequence of ...
Definition: setupwizard.h:47
SetupWizard * getWizard() const
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
DevListItem findDevice(const QString &devName)
for i
Definition: OPPlots.m:140
ControllerPage(SetupWizard *wizard, QWidget *parent=nullptr)
bool isComplete() const
QLinkedList< DevListItem > getAvailableDevices()
Core::ConnectionManager * getConnectionManager()
Definition: setupwizard.h:108
void availableDevicesChanged(const QLinkedList< Core::DevListItem > devices)
virtual bool isUSBSupported()
Definition: iboardtype.h:233
void setControllerType(Core::IBoardType *type)
Definition: setupwizard.h:55
virtual QString shortName()=0
bool connectDevice(DevListItem device)
bool isConnected() const
Core::IBoardType * getControllerType() const
ControllerPage::getControllerType get the interface for the connected board.