/
pi_coder
/
ByteMachine
Обзор
Документация
Войти
/
pi_coder
/
ByteMachine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/nodes/forms/form_com_reader_node.cpp
408 строк
13 KB
pimenov-and
Восстановление функционала старой версии
28 июл 2026, 20:04
28 июл 2026, 20:04
98412d0
Код
Авторство
О чём код?
//////////////////////////////////////////////////////////////// // ByteMachine // Виджет настройки для узла ComReaderNode //////////////////////////////////////////////////////////////// #include "form_com_reader_node.h" #include "../show_node_state.h" #include "../com_port_manager.h" #include "global_settings.h" #include "icons.h" //============================================================== // Конструктор с параметрами //============================================================== FormComReaderNode::FormComReaderNode(ComReaderNode *node, QWidget *parent) : QWidget{parent} { Q_ASSERT(node != nullptr); ui_.setupUi(this); initWidgets(); setNode(node); setConnections(); correctColorScheme(); #ifdef Q_OS_WASM const QFont font{"Segoe UI", 10}; setFont(font); ui_.labelName_->setFont(font); ui_.lineEditName_->setFont(font); ui_.labelComment_->setFont(font); ui_.plainTextEditComment_->setFont(font); #endif // Q_OS_WASM } //============================================================== // Функция вызывается при изменении свойств узла //============================================================== void FormComReaderNode::slotChangedNodeProp(PropValue value) { // Имя if (value.name == "name") { const QString name = node_->name(); ui_.lineEditName_->setText(name); ui_.lineEditName_->setToolTip(name); } // Имя порта else if (value.name == "portName") { const QString portName = node_->portName(); ui_.comboBoxPortName_->setCurrentText(portName); const bool isPortNameModified = node_->isPortNameModified(); ui_.pushBtnResetPortName_->setEnabled(isPortNameModified); } // Частота else if (value.name == "baudRate") { const ComPortBaudRates baud = node_->baudRate(); const int baudIndex = baudRateToIndex(baud); Q_ASSERT(baudIndex != -1); ui_.comboBoxBaudRate_->setCurrentIndex(baudIndex); const bool isBaudRateModified = node_->isBaudRateModified(); ui_.pushBtnResetBoudRate_->setEnabled(isBaudRateModified); } // Режим разбора пакетов else if (value.name == "packetMode") { } // Комментарий else if (value.name == "comment") { const QString comment = node_->comment(); ui_.lineEditComment_->setText(comment); ui_.lineEditComment_->setToolTip(comment); const bool isCommentChanged = node_->isCommentModified(); ui_.pushBtnResetComment_->setEnabled(isCommentChanged); } } //============================================================== // Функция вызывается при изменении состояния узла //============================================================== void FormComReaderNode::slotChangedNodeState(const NodeStateInfo &stateInfo) { showNodeState(stateInfo, ui_.plainTextEditState_); } //============================================================== // Функция вызывается при изменении состояния порта (открылся/закрылся) //============================================================== void FormComReaderNode::slotChangedPortState(bool isOpen) { if (isOpen) { ui_.pushBtnOpenClose_->setText(tr("Close")); } else { ui_.pushBtnOpenClose_->setText(tr("Open")); } } //============================================================== // Функция вызывается при завершении редактирования имени //============================================================== void FormComReaderNode::slotEditingFinishedName() { const QString name = ui_.lineEditName_->text(); node_->setName(name); } //============================================================== // Изменение имени порта //============================================================== void FormComReaderNode::slotChangedPortName(int index) { if (index == -1) { return; } const QString portName = ui_.comboBoxPortName_->itemText(index); node_->setPortName(portName); } //============================================================== // Сброс имени порта //============================================================== void FormComReaderNode::slotResetPortName() { node_->resetPortName(); ui_.comboBoxPortName_->setFocus(); } //============================================================== // Изменение частоты порта //============================================================== void FormComReaderNode::slotChangedBaudRate(int index) { if (index == -1) { return; } const ComPortBaudRates baud = indexToBaudRate(index); node_->setBaudRate(baud); } //============================================================== // Сброс частоты порта //============================================================== void FormComReaderNode::slotResetBaudRate() { node_->resetBaudRate(); ui_.comboBoxBaudRate_->setFocus(); } //============================================================== // Функция вызывается для открытия/закрытия порта //============================================================== void FormComReaderNode::slotOpenClose() { if (ui_.pushBtnOpenClose_->text() == tr("Open")) { if (node_->openPort()) { ui_.pushBtnOpenClose_->setText(tr("Close")); } } else { node_->closePort(); ui_.pushBtnOpenClose_->setText(tr("Open")); } } //============================================================== // Функция вызывается при завершении редактирования комментария //============================================================== void FormComReaderNode::slotEditingFinishedComment() { const QString comment = ui_.lineEditComment_->text(); node_->setComment(comment); } //============================================================== // Сброс комментария //============================================================== void FormComReaderNode::slotResetComment() { node_->resetComment(); ui_.lineEditComment_->setFocus(); } //============================================================== // Функция вызывается при изменении состояния формы //============================================================== void FormComReaderNode::changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) { ui_.retranslateUi(this); } else if (event->type() == QEvent::PaletteChange) { correctColorScheme(); } QWidget::changeEvent(event); } //============================================================== // Задание соединений //============================================================== void FormComReaderNode::setConnections() { connect(node_, &ComReaderNode::sigChangedProp, this, &FormComReaderNode::slotChangedNodeProp); connect(node_, &ComReaderNode::sigChangedState, this, &FormComReaderNode::slotChangedNodeState); connect(node_, &ComReaderNode::sigChangedPortState, this, &FormComReaderNode::slotChangedPortState); connect(ui_.lineEditName_, &QLineEdit::editingFinished, this, &FormComReaderNode::slotEditingFinishedName); connect(ui_.comboBoxPortName_, qOverload<int>(&QComboBox::currentIndexChanged), this, &FormComReaderNode::slotChangedPortName); connect(ui_.pushBtnResetPortName_, &QPushButton::clicked, this, &FormComReaderNode::slotResetPortName); connect(ui_.comboBoxBaudRate_, qOverload<int>(&QComboBox::currentIndexChanged), this, &FormComReaderNode::slotChangedBaudRate); connect(ui_.pushBtnResetBoudRate_, &QPushButton::clicked, this, &FormComReaderNode::slotResetBaudRate); connect(ui_.pushBtnOpenClose_, &QPushButton::clicked, this, &FormComReaderNode::slotOpenClose); connect(ui_.lineEditComment_, &QLineEdit::editingFinished, this, &FormComReaderNode::slotEditingFinishedComment); connect(ui_.pushBtnResetComment_, &QPushButton::clicked, this, &FormComReaderNode::slotResetComment); } //============================================================== // Задание узла //============================================================== void FormComReaderNode::setNode(ComReaderNode *node) { Q_ASSERT(node != nullptr); node_ = node; // Задание имени const QString name = node_->name(); ui_.lineEditName_->setText(name); ui_.lineEditName_->setToolTip(name); // Задание имени порта const QString portName = node_->portName(); ui_.comboBoxPortName_->setCurrentText(portName); const bool isPortNameModified = node_->isPortNameModified(); ui_.pushBtnResetPortName_->setEnabled(isPortNameModified); // Задание частоты порта const ComPortBaudRates baud = node_->baudRate(); const int baudIndex = baudRateToIndex(baud); Q_ASSERT(baudIndex != -1); ui_.comboBoxBaudRate_->setCurrentIndex(baudIndex); const bool isBaudRateModified = node_->isBaudRateModified(); ui_.pushBtnResetBoudRate_->setEnabled(isBaudRateModified); // Получение состояния порта const bool isOpenPort = node_->isOpenPort(); ui_.pushBtnOpenClose_->setText(isOpenPort ? tr("Close") : tr("Open")); // Задание комментария const QString comment = node_->comment(); ui_.lineEditComment_->setText(comment); ui_.lineEditComment_->setToolTip(comment); const bool isCommentModified = node_->isCommentModified(); ui_.pushBtnResetComment_->setEnabled(isCommentModified); // Задание состояния узла showNodeState(node_->stateInfo(), ui_.plainTextEditState_); } //============================================================== // Инициализация виджетов //============================================================== void FormComReaderNode::initWidgets() { QComboBox *const comboBoxPortName = ui_.comboBoxPortName_; comboBoxPortName->clear(); comboBoxPortName->addItems(comPortManager()->portNames()); } //============================================================== // Корректировка цветовой схемы //============================================================== void FormComReaderNode::correctColorScheme() { const ColorThemes theme = globalSettings()->colorTheme(); const QPixmap resetIcon = Icons::resetValue(theme); ui_.pushBtnResetBoudRate_->setIcon(resetIcon); ui_.pushBtnResetPortName_->setIcon(resetIcon); ui_.pushBtnResetComment_->setIcon(resetIcon); } //============================================================== // Конвертация частоты порта в индекс //============================================================== /* static */ int FormComReaderNode::baudRateToIndex(ComPortBaudRates baud) { switch (baud) { case ComPortBaudRates::Baud_1200: { return 0; } case ComPortBaudRates::Baud_2400: { return 1; } case ComPortBaudRates::Baud_4800: { return 2; } case ComPortBaudRates::Baud_9600: { return 3; } case ComPortBaudRates::Baud_19200: { return 4; } case ComPortBaudRates::Baud_38400: { return 5; } case ComPortBaudRates::Baud_57600: { return 6; } case ComPortBaudRates::Baud_115200: { return 7; } default: { return -1; } } } //============================================================== // Конвертация индекса в частоту порта //============================================================== /* static */ ComPortBaudRates FormComReaderNode::indexToBaudRate(int index) { switch (index) { case 0: { return ComPortBaudRates::Baud_1200; } case 1: { return ComPortBaudRates::Baud_2400; } case 2: { return ComPortBaudRates::Baud_4800; } case 3: { return ComPortBaudRates::Baud_9600; } case 4: { return ComPortBaudRates::Baud_19200; } case 5: { return ComPortBaudRates::Baud_38400; } case 6: { return ComPortBaudRates::Baud_57600; } case 7: { return ComPortBaudRates::Baud_115200; } default: { return ComPortBaudRates::Unknown; } } }