refactor
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
#include <QModbusDataUnit>
|
#include <QModbusDataUnit>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
class ColoredSquare : public QWidget {
|
class ColoredSquare final : public QWidget {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ColoredSquare(QWidget *parent = nullptr);
|
explicit ColoredSquare(QWidget *parent = nullptr);
|
||||||
@@ -25,12 +25,12 @@ private:
|
|||||||
QColor m_color;
|
QColor m_color;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Mainwindows : public QWidget {
|
class MainWindows final : public QWidget {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Mainwindows(QWidget *parent = nullptr);
|
explicit MainWindows(QWidget *parent = nullptr);
|
||||||
~Mainwindows() override;
|
~MainWindows() override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onStateChanged(QModbusDevice::State state);
|
void onStateChanged(QModbusDevice::State state);
|
||||||
@@ -41,17 +41,16 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
void createUIElements();
|
void createUIElements();
|
||||||
void initModbusConnection();
|
void initModbusConnection();
|
||||||
void writeRegister(int index);
|
|
||||||
void readRegisters();
|
void readRegisters();
|
||||||
void loadConfiguration();
|
void loadConfiguration();
|
||||||
|
|
||||||
QTableWidget* m_table;
|
QTableWidget* m_table{};
|
||||||
QString m_ipAddress;
|
QString m_ipAddress;
|
||||||
int m_port;
|
int m_port{};
|
||||||
QVector<int> m_buttonRegisters;
|
QVector<int> m_buttonRegisters;
|
||||||
QVector<int> m_indicatorRegisters;
|
QVector<int> m_indicatorRegisters;
|
||||||
int m_buttonCount;
|
int m_buttonCount{};
|
||||||
int m_indicatorCount;
|
int m_indicatorCount{};
|
||||||
QVector<QPushButton*> m_buttons;
|
QVector<QPushButton*> m_buttons;
|
||||||
QVector<ColoredSquare*> m_squares;
|
QVector<ColoredSquare*> m_squares;
|
||||||
QVector<int> m_colorIndices;
|
QVector<int> m_colorIndices;
|
||||||
@@ -61,13 +60,13 @@ private:
|
|||||||
QVector<QString> m_indicatorLabels;
|
QVector<QString> m_indicatorLabels;
|
||||||
|
|
||||||
QModbusTcpClient* m_modbusClient;
|
QModbusTcpClient* m_modbusClient;
|
||||||
QPushButton* m_connectButton;
|
QPushButton* m_connectButton{};
|
||||||
bool m_connected;
|
bool m_connected;
|
||||||
int m_responseTimeout;
|
int m_responseTimeout{};
|
||||||
int m_connectTimeout;
|
int m_connectTimeout{};
|
||||||
int m_pollInterval;
|
int m_pollInterval{};
|
||||||
QTimer* m_pollTimer;
|
QTimer* m_pollTimer{};
|
||||||
QLabel* m_statusLabel;
|
QLabel* m_statusLabel{};
|
||||||
int m_requestCounter;
|
int m_requestCounter;
|
||||||
int m_responseCounter;
|
int m_responseCounter;
|
||||||
void updateStatusBar();
|
void updateStatusBar();
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
qDebug() << QApplication::style();
|
qDebug() << QApplication::style();
|
||||||
|
|
||||||
auto *window = new Mainwindows();
|
auto *window = new MainWindows();
|
||||||
window->show();
|
window->show();
|
||||||
//window->init();
|
//window->init();
|
||||||
QApplication::exec();
|
QApplication::exec();
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ void ColoredSquare::paintEvent(QPaintEvent *) {
|
|||||||
painter.fillRect(rect(), m_color);
|
painter.fillRect(rect(), m_color);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mainwindows::Mainwindows(QWidget *parent)
|
MainWindows::MainWindows(QWidget *parent)
|
||||||
: QWidget(parent), m_modbusClient(nullptr), m_connected(false),
|
: QWidget(parent), m_modbusClient(nullptr), m_connected(false),
|
||||||
m_requestCounter(0), m_responseCounter(0) {
|
m_requestCounter(0), m_responseCounter(0) {
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ Mainwindows::Mainwindows(QWidget *parent)
|
|||||||
initModbusConnection();
|
initModbusConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::loadConfiguration() {
|
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);
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ void Mainwindows::loadConfiguration() {
|
|||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::initModbusConnection() {
|
void MainWindows::initModbusConnection() {
|
||||||
m_modbusClient = new QModbusTcpClient(this);
|
m_modbusClient = new QModbusTcpClient(this);
|
||||||
|
|
||||||
// Set timeouts
|
// Set timeouts
|
||||||
@@ -126,15 +126,15 @@ void Mainwindows::initModbusConnection() {
|
|||||||
m_modbusClient->setNumberOfRetries(1);
|
m_modbusClient->setNumberOfRetries(1);
|
||||||
|
|
||||||
connect(m_modbusClient, &QModbusClient::stateChanged,
|
connect(m_modbusClient, &QModbusClient::stateChanged,
|
||||||
this, &Mainwindows::onStateChanged);
|
this, &MainWindows::onStateChanged);
|
||||||
|
|
||||||
// Initialize poll timer
|
// Initialize poll timer
|
||||||
m_pollTimer = new QTimer(this);
|
m_pollTimer = new QTimer(this);
|
||||||
m_pollTimer->setInterval(m_pollInterval);
|
m_pollTimer->setInterval(m_pollInterval);
|
||||||
connect(m_pollTimer, &QTimer::timeout, this, &Mainwindows::onPollTimer);
|
connect(m_pollTimer, &QTimer::timeout, this, &MainWindows::onPollTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::onStateChanged(QModbusDevice::State state) {
|
void MainWindows::onStateChanged(QModbusDevice::State state) {
|
||||||
if (state == QModbusDevice::ConnectedState) {
|
if (state == QModbusDevice::ConnectedState) {
|
||||||
m_connected = true;
|
m_connected = true;
|
||||||
m_connectButton->setText("Disconnect");
|
m_connectButton->setText("Disconnect");
|
||||||
@@ -151,7 +151,7 @@ void Mainwindows::onStateChanged(QModbusDevice::State state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::onConnectButtonClicked() {
|
void MainWindows::onConnectButtonClicked() {
|
||||||
if (!m_connected) {
|
if (!m_connected) {
|
||||||
if (!m_modbusClient->connectDevice()) {
|
if (!m_modbusClient->connectDevice()) {
|
||||||
QMessageBox::critical(this, tr("Connection Error"),
|
QMessageBox::critical(this, tr("Connection Error"),
|
||||||
@@ -170,13 +170,13 @@ void Mainwindows::onConnectButtonClicked() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::onPollTimer() {
|
void MainWindows::onPollTimer() {
|
||||||
if (m_connected) {
|
if (m_connected) {
|
||||||
readRegisters();
|
readRegisters();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::readRegisters() {
|
void MainWindows::readRegisters() {
|
||||||
if (!m_connected) return;
|
if (!m_connected) return;
|
||||||
|
|
||||||
// Увеличиваем счетчик запросов
|
// Увеличиваем счетчик запросов
|
||||||
@@ -189,7 +189,7 @@ void Mainwindows::readRegisters() {
|
|||||||
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, m_indicatorRegisters[i], 1);
|
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, m_indicatorRegisters[i], 1);
|
||||||
if (auto *reply = m_modbusClient->sendReadRequest(readUnit, 1)) {
|
if (auto *reply = m_modbusClient->sendReadRequest(readUnit, 1)) {
|
||||||
if (!reply->isFinished()) {
|
if (!reply->isFinished()) {
|
||||||
connect(reply, &QModbusReply::finished, this, &Mainwindows::onReadReady);
|
connect(reply, &QModbusReply::finished, this, &MainWindows::onReadReady);
|
||||||
} else {
|
} else {
|
||||||
delete reply;
|
delete reply;
|
||||||
}
|
}
|
||||||
@@ -203,7 +203,7 @@ void Mainwindows::readRegisters() {
|
|||||||
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, m_buttonRegisters[i], 1);
|
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, m_buttonRegisters[i], 1);
|
||||||
if (auto *reply = m_modbusClient->sendReadRequest(readUnit, 1)) {
|
if (auto *reply = m_modbusClient->sendReadRequest(readUnit, 1)) {
|
||||||
if (!reply->isFinished()) {
|
if (!reply->isFinished()) {
|
||||||
connect(reply, &QModbusReply::finished, this, &Mainwindows::onReadReady);
|
connect(reply, &QModbusReply::finished, this, &MainWindows::onReadReady);
|
||||||
} else {
|
} else {
|
||||||
delete reply;
|
delete reply;
|
||||||
}
|
}
|
||||||
@@ -212,7 +212,7 @@ void Mainwindows::readRegisters() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::onReadReady() {
|
void MainWindows::onReadReady() {
|
||||||
auto reply = qobject_cast<QModbusReply *>(sender());
|
auto reply = qobject_cast<QModbusReply *>(sender());
|
||||||
if (!reply) return;
|
if (!reply) return;
|
||||||
|
|
||||||
@@ -260,7 +260,7 @@ void Mainwindows::onReadReady() {
|
|||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::createUIElements() {
|
void MainWindows::createUIElements() {
|
||||||
auto* mainLayout = new QVBoxLayout(this);
|
auto* mainLayout = new QVBoxLayout(this);
|
||||||
|
|
||||||
// Добавляем статусную строку
|
// Добавляем статусную строку
|
||||||
@@ -270,7 +270,7 @@ void Mainwindows::createUIElements() {
|
|||||||
|
|
||||||
// Add connect button at the top
|
// Add connect button at the top
|
||||||
m_connectButton = new QPushButton("Connect", this);
|
m_connectButton = new QPushButton("Connect", this);
|
||||||
connect(m_connectButton, &QPushButton::clicked, this, &Mainwindows::onConnectButtonClicked);
|
connect(m_connectButton, &QPushButton::clicked, this, &MainWindows::onConnectButtonClicked);
|
||||||
mainLayout->addWidget(m_connectButton);
|
mainLayout->addWidget(m_connectButton);
|
||||||
|
|
||||||
// Create table with maximum of buttons or indicators
|
// Create table with maximum of buttons or indicators
|
||||||
@@ -340,7 +340,7 @@ void Mainwindows::createUIElements() {
|
|||||||
m_table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
|
m_table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
|
||||||
m_table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
|
m_table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
|
||||||
m_table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed);
|
m_table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed);
|
||||||
m_table->setColumnWidth(0, 150); // Label column
|
m_table->setColumnWidth(0, 180); // Label column
|
||||||
m_table->setColumnWidth(1, 100); // Button column
|
m_table->setColumnWidth(1, 100); // Button column
|
||||||
m_table->setColumnWidth(2, 50); // Square column
|
m_table->setColumnWidth(2, 50); // Square column
|
||||||
m_table->setColumnWidth(3, 100); // Status text column
|
m_table->setColumnWidth(3, 100); // Status text column
|
||||||
@@ -357,7 +357,7 @@ void Mainwindows::createUIElements() {
|
|||||||
setFixedSize(totalWidth, totalHeight);
|
setFixedSize(totalWidth, totalHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::updateStatusBar() {
|
void MainWindows::updateStatusBar() {
|
||||||
QString status = QString("IP: %1 Port: %2 | Requests: %3 Responses: %4")
|
QString status = QString("IP: %1 Port: %2 | Requests: %3 Responses: %4")
|
||||||
.arg(m_ipAddress)
|
.arg(m_ipAddress)
|
||||||
.arg(m_port)
|
.arg(m_port)
|
||||||
@@ -366,7 +366,7 @@ void Mainwindows::updateStatusBar() {
|
|||||||
m_statusLabel->setText(status);
|
m_statusLabel->setText(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mainwindows::~Mainwindows() {
|
MainWindows::~MainWindows() {
|
||||||
if (m_pollTimer) {
|
if (m_pollTimer) {
|
||||||
m_pollTimer->stop();
|
m_pollTimer->stop();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user