added label
This commit is contained in:
@@ -57,6 +57,8 @@ private:
|
|||||||
QVector<int> m_colorIndices;
|
QVector<int> m_colorIndices;
|
||||||
QVector<int> m_buttonValues; // Add this new member to store button states
|
QVector<int> m_buttonValues; // Add this new member to store button states
|
||||||
QVector<int> m_buttonBits; // Add this new member for button bits
|
QVector<int> m_buttonBits; // Add this new member for button bits
|
||||||
|
QVector<QString> m_buttonLabels;
|
||||||
|
QVector<QString> m_indicatorLabels;
|
||||||
|
|
||||||
QModbusTcpClient* m_modbusClient;
|
QModbusTcpClient* m_modbusClient;
|
||||||
QPushButton* m_connectButton;
|
QPushButton* m_connectButton;
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ void ColoredSquare::paintEvent(QPaintEvent *) {
|
|||||||
Mainwindows::Mainwindows(QWidget *parent)
|
Mainwindows::Mainwindows(QWidget *parent)
|
||||||
: QWidget(parent), m_modbusClient(nullptr), m_connected(false) {
|
: QWidget(parent), m_modbusClient(nullptr), m_connected(false) {
|
||||||
|
|
||||||
|
// Устанавливаем кодировку для всего приложения
|
||||||
|
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
|
||||||
|
|
||||||
|
// Устанавливаем шрифт с поддержкой кириллицы
|
||||||
|
QFont font("Arial", 10); // или можно использовать "MS Shell Dlg 2"
|
||||||
|
this->setFont(font);
|
||||||
|
|
||||||
loadConfiguration();
|
loadConfiguration();
|
||||||
createUIElements();
|
createUIElements();
|
||||||
initModbusConnection();
|
initModbusConnection();
|
||||||
@@ -38,6 +45,9 @@ void Mainwindows::loadConfiguration() {
|
|||||||
const QString filePath = QCoreApplication::applicationDirPath() + "/config.ini";
|
const QString filePath = QCoreApplication::applicationDirPath() + "/config.ini";
|
||||||
QSettings settings(filePath, QSettings::IniFormat);
|
QSettings settings(filePath, QSettings::IniFormat);
|
||||||
|
|
||||||
|
// Устанавливаем кодировку для чтения файла конфигурации
|
||||||
|
settings.setIniCodec("UTF-8");
|
||||||
|
|
||||||
settings.beginGroup("Connection");
|
settings.beginGroup("Connection");
|
||||||
m_ipAddress = settings.value("ip", "127.0.0.1").toString();
|
m_ipAddress = settings.value("ip", "127.0.0.1").toString();
|
||||||
m_port = settings.value("port", 502).toInt();
|
m_port = settings.value("port", 502).toInt();
|
||||||
@@ -82,6 +92,30 @@ void Mainwindows::loadConfiguration() {
|
|||||||
}
|
}
|
||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
|
|
||||||
|
// Load button labels
|
||||||
|
settings.beginGroup("Registers");
|
||||||
|
for(int i = 1; i <= m_buttonCount; ++i) {
|
||||||
|
QString labelKey = QString("button_label%1").arg(i);
|
||||||
|
QString label = settings.value(labelKey).toString();
|
||||||
|
if(label.isEmpty()) {
|
||||||
|
label = QString("Button %1").arg(i);
|
||||||
|
}
|
||||||
|
qDebug() << "Loading button label:" << labelKey << "=" << label;
|
||||||
|
m_buttonLabels.append(label);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load indicator labels
|
||||||
|
for(int i = 1; i <= m_indicatorCount; ++i) {
|
||||||
|
QString labelKey = QString("indicator_label%1").arg(i);
|
||||||
|
QString label = settings.value(labelKey).toString();
|
||||||
|
if(label.isEmpty()) {
|
||||||
|
label = QString("Indicator %1").arg(i);
|
||||||
|
}
|
||||||
|
qDebug() << "Loading indicator label:" << labelKey << "=" << label;
|
||||||
|
m_indicatorLabels.append(label);
|
||||||
|
}
|
||||||
|
settings.endGroup();
|
||||||
|
|
||||||
qDebug() << "Loaded" << m_buttonCount << "buttons and" << m_indicatorCount << "indicators";
|
qDebug() << "Loaded" << m_buttonCount << "buttons and" << m_indicatorCount << "indicators";
|
||||||
qDebug() << "Button registers:" << m_buttonRegisters;
|
qDebug() << "Button registers:" << m_buttonRegisters;
|
||||||
qDebug() << "Indicator registers:" << m_indicatorRegisters;
|
qDebug() << "Indicator registers:" << m_indicatorRegisters;
|
||||||
@@ -217,7 +251,7 @@ void Mainwindows::createUIElements() {
|
|||||||
|
|
||||||
// Create table with maximum of buttons or indicators
|
// Create table with maximum of buttons or indicators
|
||||||
int rowCount = qMax(m_buttonCount, m_indicatorCount);
|
int rowCount = qMax(m_buttonCount, m_indicatorCount);
|
||||||
m_table = new QTableWidget(rowCount, 2, this);
|
m_table = new QTableWidget(rowCount, 3, this); // Changed from 2 to 3 columns
|
||||||
|
|
||||||
// Setup table properties
|
// Setup table properties
|
||||||
m_table->verticalHeader()->setVisible(false);
|
m_table->verticalHeader()->setVisible(false);
|
||||||
@@ -226,13 +260,27 @@ void Mainwindows::createUIElements() {
|
|||||||
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
m_table->setSelectionMode(QAbstractItemView::NoSelection);
|
m_table->setSelectionMode(QAbstractItemView::NoSelection);
|
||||||
|
|
||||||
|
// Устанавливаем шрифт для таблицы
|
||||||
|
QFont tableFont("Arial", 10);
|
||||||
|
m_table->setFont(tableFont);
|
||||||
|
|
||||||
// Create UI elements
|
// Create UI elements
|
||||||
for(int i = 0; i < rowCount; ++i) {
|
for(int i = 0; i < rowCount; ++i) {
|
||||||
|
// Add label in first column
|
||||||
|
QString label;
|
||||||
|
if (i < m_buttonCount) {
|
||||||
|
label = m_buttonLabels[i];
|
||||||
|
} else if (i < m_indicatorCount) {
|
||||||
|
label = m_indicatorLabels[i];
|
||||||
|
}
|
||||||
|
m_table->setItem(i, 0, new QTableWidgetItem(label));
|
||||||
|
|
||||||
if (i < m_buttonCount) {
|
if (i < m_buttonCount) {
|
||||||
auto* button = new QPushButton(QString("Register %1 Bit %2").arg(m_buttonRegisters[i]).arg(m_buttonBits[i]), this);
|
auto* button = new QPushButton(QString("Register %1 Bit %2").arg(m_buttonRegisters[i]).arg(m_buttonBits[i]), this);
|
||||||
|
button->setFont(tableFont); // Устанавливаем шрифт для кнопок
|
||||||
m_buttons.append(button);
|
m_buttons.append(button);
|
||||||
m_buttonValues.append(0);
|
m_buttonValues.append(0);
|
||||||
m_table->setCellWidget(i, 0, button);
|
m_table->setCellWidget(i, 1, button); // Changed from column 0 to 1
|
||||||
connect(button, &QPushButton::clicked, [this, i]() {
|
connect(button, &QPushButton::clicked, [this, i]() {
|
||||||
QModbusDataUnit writeUnit(QModbusDataUnit::HoldingRegisters, m_buttonRegisters[i], 1);
|
QModbusDataUnit writeUnit(QModbusDataUnit::HoldingRegisters, m_buttonRegisters[i], 1);
|
||||||
int currentValue = m_buttonValues[i];
|
int currentValue = m_buttonValues[i];
|
||||||
@@ -253,18 +301,25 @@ void Mainwindows::createUIElements() {
|
|||||||
square->setFixedSize(m_table->rowHeight(i), m_table->rowHeight(i));
|
square->setFixedSize(m_table->rowHeight(i), m_table->rowHeight(i));
|
||||||
m_squares.append(square);
|
m_squares.append(square);
|
||||||
m_colorIndices.append(0);
|
m_colorIndices.append(0);
|
||||||
m_table->setCellWidget(i, 1, square);
|
m_table->setCellWidget(i, 2, square); // Changed from column 1 to 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust column sizes
|
// Adjust column sizes
|
||||||
m_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
|
m_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
|
||||||
m_table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
|
m_table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
|
||||||
m_table->setColumnWidth(0, 100);
|
m_table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
|
||||||
m_table->setColumnWidth(1, 100);
|
m_table->setColumnWidth(0, 150); // Label column
|
||||||
|
m_table->setColumnWidth(1, 100); // Button column
|
||||||
|
m_table->setColumnWidth(2, 50); // Square column
|
||||||
|
|
||||||
mainLayout->addWidget(m_table);
|
mainLayout->addWidget(m_table);
|
||||||
mainLayout->setContentsMargins(0, 0, 0, 0);
|
mainLayout->setContentsMargins(0, 0, 0, 0);
|
||||||
|
|
||||||
|
// Устанавливаем фиксированный размер окна на основе размера таблицы
|
||||||
|
int totalWidth = m_table->horizontalHeader()->length() + 2; // +2 для рамки
|
||||||
|
int totalHeight = m_table->verticalHeader()->length() + m_connectButton->height() + 2;
|
||||||
|
setFixedSize(totalWidth, totalHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mainwindows::~Mainwindows() {
|
Mainwindows::~Mainwindows() {
|
||||||
|
|||||||
19
config.ini
19
config.ini
@@ -6,15 +6,28 @@ connectTimeout=2000
|
|||||||
timeBetweenPolls=100
|
timeBetweenPolls=100
|
||||||
|
|
||||||
[Registers]
|
[Registers]
|
||||||
buttonCount=3
|
buttonCount=5
|
||||||
indicatorCount=4
|
indicatorCount=4
|
||||||
button_reg1=3
|
button_reg1=3
|
||||||
button_bit1=0
|
button_bit1=0
|
||||||
button_reg2=4
|
button_label1=valve 1
|
||||||
|
button_reg2=3
|
||||||
button_bit2=1
|
button_bit2=1
|
||||||
button_reg3=5
|
button_label2=Клапан 2
|
||||||
|
button_reg3=3
|
||||||
button_bit3=2
|
button_bit3=2
|
||||||
|
button_label3=Клапан 3
|
||||||
|
button_reg4=3
|
||||||
|
button_bit4=3
|
||||||
|
button_label4=Клапан 4
|
||||||
|
button_reg5=5
|
||||||
|
button_bit5=3
|
||||||
|
button_label5=Насос
|
||||||
indicator_reg1=13
|
indicator_reg1=13
|
||||||
|
indicator_label1=Статус клапана 1
|
||||||
indicator_reg2=14
|
indicator_reg2=14
|
||||||
|
indicator_label2=Статус клапана 2
|
||||||
indicator_reg3=15
|
indicator_reg3=15
|
||||||
|
indicator_label3=Статус клапана 3
|
||||||
indicator_reg4=16
|
indicator_reg4=16
|
||||||
|
indicator_label4=Статус насоса
|
||||||
|
|||||||
Reference in New Issue
Block a user