#include "channelwidget.h" #include #include ChannelWidget::ChannelWidget(uint16_t deviceSerial, uint16_t channelNumber, struct eismultiplexer* multiplexer, QWidget *parent) : QWidget(parent), deviceSerial(deviceSerial), channelNumber(channelNumber), multiplexer(multiplexer) { // Create layout QHBoxLayout* layout = new QHBoxLayout(this); // Create label with device serial and channel number label = new QLabel(QString("Device %1, Channel %2").arg(deviceSerial).arg(channelNumber), this); layout->addWidget(label); // Create checkbox checkbox = new QCheckBox(this); layout->addWidget(checkbox); // Connect checkbox signal connect(checkbox, &QCheckBox::toggled, this, &ChannelWidget::onChannelToggled); // Set layout setLayout(layout); } ChannelWidget::~ChannelWidget() { // Nothing to clean up } uint16_t ChannelWidget::getDeviceSerial() const { return deviceSerial; } uint16_t ChannelWidget::getChannelNumber() const { return channelNumber; } bool ChannelWidget::isChecked() const { return checkbox->isChecked(); } void ChannelWidget::onChannelToggled(bool checked) { if (checked) { // Emit signal before actually turning on the channel emit channelAboutToBeTurnedOn(deviceSerial, channelNumber); } channel_t channelFlag = static_cast(1 << channelNumber); if (checked) { if (eismultiplexer_connect_channel(multiplexer.get(), channelFlag) < 0) { QMessageBox::warning(this, tr("Connection Failed"), tr("Failed to connect channel %1 on device %2").arg(channelNumber).arg(deviceSerial)); qWarning() << "Failed to connect channel" << channelNumber << "on device" << deviceSerial; checkbox->blockSignals(true); checkbox->setChecked(false); setEnabled(false); // Gray out the widget } } else { if (eismultiplexer_disconnect_channel(multiplexer.get(), channelFlag) < 0) { QMessageBox::warning(this, tr("Disconnection Failed"), tr("Failed to disconnect channel %1 on device %2").arg(channelNumber).arg(deviceSerial)); qWarning() << "Failed to disconnect channel" << channelNumber << "on device" << deviceSerial; checkbox->blockSignals(true); checkbox->setChecked(true); setEnabled(false); // Gray out the widget } } }