7#include "portselector.h"
9#include "indipropertyswitch.h"
10#include "ksnotification.h"
11#include "ekos_debug.h"
13#include "indi/driverinfo.h"
14#include "indi/clientmanager.h"
17#include <QDialogButtonBox>
23const QStringList Device::BAUD_RATES = {
"9600",
"19200",
"38400",
"57600",
"115200",
"230400" };
24const QString Device::ACTIVE_STYLESHEET =
"background-color: #004400;";
30 uint8_t row) : m_Name(device->getDeviceName()), m_Device(device), m_Grid(grid), m_Row(row)
37 connect(m_Device.get(), &ISD::GenericDevice::propertyUpdated,
this, &Device::updateProperty);
48 INDI::Property modeProperty = m_Device->getBaseDevice().getSwitch(
"CONNECTION_MODE");
49 if (!modeProperty.isValid())
54 m_LED->setState(KLed::On);
66 m_SerialB->setToolTip(
i18n(
"Select <b>Serial</b> if your device is connected via Serial to USB adapter."));
67 m_Grid->addWidget(m_SerialB, m_Row, 2);
70 INDI::PropertyView<ISwitch> connectionMode = *(m_Device->getBaseDevice().getSwitch(
"CONNECTION_MODE"));
71 IUResetSwitch(&connectionMode);
72 connectionMode.at(0)->setState(ISS_ON);
73 connectionMode.at(1)->setState(ISS_OFF);
74 m_Device->sendNewProperty(&connectionMode);
80 m_NetworkB->setToolTip(
i18n(
"Select <b>Network</b> if your device is connected via Ethernet or WiFi."));
81 m_Grid->addWidget(m_NetworkB, m_Row, 3);
84 INDI::PropertyView<ISwitch> connectionMode = *(m_Device->getBaseDevice().getSwitch(
"CONNECTION_MODE"));
85 IUResetSwitch(&connectionMode);
86 connectionMode.at(0)->setState(ISS_OFF);
87 connectionMode.at(1)->setState(ISS_ON);
88 m_Device->sendNewProperty(&connectionMode);
91 INDI::PropertyView<ISwitch> connectionMode = *(modeProperty.getSwitch());
92 if (connectionMode.findWidgetByName(
"CONNECTION_SERIAL"))
94 if (m_Device->getBaseDevice().getProperty(
"SYSTEM_PORTS").isValid())
99 m_Ports->setToolTip(
i18n(
"Select Serial port"));
101 INDI::PropertyView<ISwitch> systemPorts = *(m_Device->getBaseDevice().getSwitch(
"SYSTEM_PORTS"));
102 for (
auto &oneSwitch : systemPorts)
103 m_Ports->addItem(oneSwitch.getLabel());
108 INDI::Property systemPorts = m_Device->getBaseDevice().getProperty(
"SYSTEM_PORTS");
109 if (systemPorts.isValid())
111 INDI::PropertyView<ISwitch> systemPortsSwitch = *(systemPorts.getSwitch());
112 IUResetSwitch(&systemPortsSwitch);
113 systemPortsSwitch.at(index)->setState(ISS_ON);
114 m_Device->sendNewProperty(&systemPortsSwitch);
120 INDI::PropertyView<IText> port = *(m_Device->getBaseDevice().getText(
"DEVICE_PORT"));
121 port.at(0)->setText(m_Ports->lineEdit()->text().toLatin1().constData());
122 m_Device->sendNewProperty(&port);
126 if (m_Device->getBaseDevice()->getProperty(
"DEVICE_BAUD_RATE").isValid())
130 m_BaudRates->setToolTip(
i18n(
"Select Baud rate"));
134 INDI::PropertyView<ISwitch> systemBauds = *(m_Device->getBaseDevice().getSwitch(
"DEVICE_BAUD_RATE"));
135 IUResetSwitch(&systemBauds);
136 systemBauds.at(index)->setState(ISS_ON);
137 m_Device->sendNewProperty(&systemBauds);
142 m_SerialB->setEnabled(
false);
144 if (connectionMode.findWidgetByName(
"CONNECTION_TCP"))
151 INDI::PropertyView<IText> server = *(m_Device->getBaseDevice().getText(
"DEVICE_ADDRESS"));
152 server.at(0)->setText(m_HostName->text().toLatin1().constData());
153 m_Device->sendNewProperty(&server);
160 auto server = m_Device->getProperty(
"DEVICE_ADDRESS").getText();
161 server->at(1)->setText(m_HostPort->text().toLatin1().constData());
162 m_Device->sendNewProperty(server);
168 auto connectionType = m_Device->getProperty(
"CONNECTION_TYPE").getSwitch();
169 connectionType->reset();
170 connectionType->at(0)->setState(ISS_ON);
171 m_Device->sendNewProperty(connectionType);
177 auto connectionType = m_Device->getProperty(
"CONNECTION_TYPE").getSwitch();
178 connectionType->reset();
179 connectionType->at(1)->setState(ISS_ON);
180 m_Device->sendNewProperty(connectionType);
184 m_NetworkB->setEnabled(
false);
188 m_ConnectB->setToolTip(
i18n(
"Connect"));
190 m_Grid->addWidget(m_ConnectB, m_Row, 8);
195 m_DisconnectB->setToolTip(
i18n(
"Disconnect"));
197 m_Grid->addWidget(m_DisconnectB, m_Row, 9);
200 setConnectionMode(
static_cast<ConnectionMode
>(connectionMode.findOnSwitchIndex()));
208void Device::syncGUI()
211 if (m_LED ==
nullptr)
217 if (m_LED ==
nullptr)
221 m_LED->setColor(ColorCode.value(m_Device->getState(
"CONNECTION")));
223 auto connectionMode = m_Device->getProperty(
"CONNECTION_MODE").getSwitch();
225 if (connectionMode->findOnSwitchIndex() == CONNECTION_SERIAL)
227 m_ActiveConnectionMode = CONNECTION_SERIAL;
228 m_SerialB->setStyleSheet(ACTIVE_STYLESHEET);
229 m_NetworkB->setStyleSheet(
QString());
233 auto port = m_Device->getProperty(
"DEVICE_PORT").getText();
234 m_Ports->setEditText(port->at(0)->getText());
239 auto systemBauds = m_Device->getProperty(
"DEVICE_BAUD_RATE").getSwitch();
240 m_BaudRates->setCurrentIndex(systemBauds->findOnSwitchIndex());
245 m_ActiveConnectionMode = CONNECTION_NETWORK;
246 m_SerialB->setStyleSheet(
QString());
247 m_NetworkB->setStyleSheet(ACTIVE_STYLESHEET);
249 if (m_Device->getProperty(
"DEVICE_ADDRESS").isValid())
251 auto server = m_Device->getProperty(
"DEVICE_ADDRESS").getText();
252 m_HostName->setText(server->at(0)->getText());
253 m_HostPort->setText(server->at(1)->getText());
256 if (m_Device->getProperty(
"CONNECTION_TYPE").isValid())
258 auto connectionType = m_Device->getBaseDevice().getSwitch(
"CONNECTION_TYPE").getSwitch();
259 m_HostProtocolTCP->setStyleSheet(connectionType->findOnSwitchIndex() == 0 ? ACTIVE_STYLESHEET :
QString());
260 m_HostProtocolUDP->setStyleSheet(connectionType->findOnSwitchIndex() == 1 ? ACTIVE_STYLESHEET :
QString());
268uint8_t Device::systemPortCount()
const
271 return m_Ports->count();
279void Device::setConnectionMode(ConnectionMode mode)
281 m_ActiveConnectionMode = mode;
283 if (mode == CONNECTION_SERIAL)
287 m_Grid->removeWidget(m_HostName);
288 m_HostName->setParent(
nullptr);
289 m_Grid->removeWidget(m_HostPort);
290 m_HostPort->setParent(
nullptr);
291 m_Grid->removeWidget(m_HostProtocolTCP);
292 m_HostProtocolTCP->setParent(
nullptr);
293 m_Grid->removeWidget(m_HostProtocolUDP);
294 m_HostProtocolUDP->setParent(
nullptr);
297 m_Grid->addWidget(m_Ports, m_Row, 4, 1, 2);
298 m_Grid->addWidget(m_BaudRates, m_Row, 6, 1, 2);
299 m_Grid->setColumnStretch(4, 2);
301 else if (mode == CONNECTION_NETWORK)
305 m_Grid->removeWidget(m_Ports);
306 m_Ports->setParent(
nullptr);
307 m_Grid->removeWidget(m_BaudRates);
308 m_BaudRates->setParent(
nullptr);
311 m_Grid->addWidget(m_HostName, m_Row, 4);
312 m_Grid->addWidget(m_HostPort, m_Row, 5);
313 m_Grid->addWidget(m_HostProtocolTCP, m_Row, 6);
314 m_Grid->addWidget(m_HostProtocolUDP, m_Row, 7);
315 m_Grid->setColumnStretch(4, 2);
322void Device::updateProperty(INDI::Property prop)
324 if (prop.isNameMatch(
"CONNECTION_MODE"))
326 auto svp = prop.getSwitch();
327 setConnectionMode(
static_cast<ConnectionMode
>(svp->findOnSwitchIndex()));
330 else if (prop.isNameMatch(
"CONNECTION_TYPE") || prop.isNameMatch(
"SYSTEM_PORTS") ||
331 prop.isNameMatch(
"DEVICE_BAUD_RATE") || prop.isNameMatch(
"DEVICE_PORT"))
333 else if (prop.isNameMatch(
"CONNECTION") && m_ConnectB)
335 if (m_Device->isConnected())
337 m_ConnectB->setStyleSheet(ACTIVE_STYLESHEET);
338 m_DisconnectB->setStyleSheet(
QString());
342 m_ConnectB->setStyleSheet(
QString());
343 m_DisconnectB->setStyleSheet(ACTIVE_STYLESHEET);
351void Device::setConnected()
359void Device::setDisconnected()
385 setWindowTitle(
i18nc(
"@title:window",
"Port Selector"));
396 auto pos = std::find_if(m_Devices.begin(), m_Devices.end(), [device](std::unique_ptr<Device> &oneDevice)
398 return oneDevice->name() == device->getDeviceName();
402 if (pos == m_Devices.end())
404 std::unique_ptr<Device> dev(
new Device(device, m_Layout, m_Devices.size()));
405 m_Devices.push_back(std::move(dev));
418void Dialog::removeDevice(
const QString &name)
420 m_Devices.erase(std::remove_if(m_Devices.begin(), m_Devices.end(), [name](
const auto & oneDevice)
422 return oneDevice->name() == name;
423 }), m_Devices.end());
431bool Dialog::shouldShow()
const
433 if (m_Devices.empty())
436 uint32_t systemPortCount = 0;
437 uint32_t serialDevices = 0;
438 uint32_t networkDevices = 0;
440 for (
const auto &oneDevice : m_Devices)
442 if (oneDevice->systemPortCount() > 0)
443 systemPortCount = oneDevice->systemPortCount();
445 if (oneDevice->activeConnectionMode() == CONNECTION_SERIAL)
447 else if (oneDevice->activeConnectionMode() == CONNECTION_NETWORK)
453 if (networkDevices == 0 && serialDevices <= 1 && systemPortCount <= 1)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void addLayout(QLayout *layout, int stretch)
void addStretch(int stretch)
void activated(int index)
void addItems(const QStringList &texts)
void setEditable(bool editable)
QIcon fromTheme(const QString &name)
void setText(const QString &)
void setPlaceholderText(const QString &)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)