152 lines
4.3 KiB
C++
152 lines
4.3 KiB
C++
#include "parameterdevice.h"
|
||
#include "ui_parameterdevice.h"
|
||
|
||
void sortParameterBoxesByID(QVector<ParameterBox*>& parameterBoxes) {
|
||
std::sort(parameterBoxes.begin(), parameterBoxes.end(),
|
||
[](ParameterBox* a, ParameterBox* b) {
|
||
return a->getID() < b->getID();
|
||
});
|
||
}
|
||
|
||
ParameterDevice::ParameterDevice(QWidget *parent) :
|
||
QWidget(parent),
|
||
ui(new Ui::ParameterDevice)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
updateTimer = new QTimer(this);
|
||
updateTimer->setSingleShot(true);
|
||
connect(updateTimer, &QTimer::timeout, this, &ParameterDevice::sortScrollArea);
|
||
}
|
||
|
||
ParameterDevice::~ParameterDevice()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void ParameterDevice::on_selectiveRadio_clicked()
|
||
{
|
||
ui->selectiveBox->setEnabled(true);
|
||
mode = selectiveRequest;
|
||
}
|
||
|
||
void ParameterDevice::on_fullRadio_clicked()
|
||
{
|
||
ui->selectiveBox->setEnabled(false);
|
||
mode = fullRequest;
|
||
}
|
||
|
||
void ParameterDevice::on_checkButton_clicked()
|
||
{
|
||
for (ParameterBox* box : parameterBoxes) {
|
||
if (box) {
|
||
box->deleteLater();
|
||
}
|
||
}
|
||
parameterBoxes.clear();
|
||
quint16 strAdr;
|
||
int cnt;
|
||
strAdr = ui->adrSpin->value();
|
||
switch (mode) {
|
||
case fullRequest:
|
||
strAdr = 0x80;
|
||
cnt = 128;
|
||
break;
|
||
case selectiveRequest:
|
||
cnt = ui->countSpin->value();
|
||
break;
|
||
}
|
||
QProgressDialog progressDialog("Обработка...", "Отмена", 0, cnt, this);
|
||
progressDialog.setWindowModality(Qt::WindowModal);
|
||
progressDialog.setMinimumDuration(0); // показывать сразу
|
||
QEventLoop loop;
|
||
connect(this, &ParameterDevice::transmitEnd, &loop, &QEventLoop::quit);
|
||
for (int i = 0; i < cnt; i ++) {
|
||
// Обновляем прогресс
|
||
progressDialog.setValue(i);
|
||
if (progressDialog.wasCanceled()) {
|
||
break; // пользователь отменил
|
||
}
|
||
QByteArray data = QByteArray::fromHex("0E04");
|
||
data.append(strAdr+i);
|
||
QModbusRequest request(QModbusRequest::EncapsulatedInterfaceTransport, data);
|
||
emit read(request, strAdr+i);
|
||
loop.exec();
|
||
if(errorAtTransmit)
|
||
{
|
||
errorAtTransmit = false;
|
||
break;
|
||
}
|
||
}
|
||
progressDialog.setValue(cnt);
|
||
sortParameterBoxesByID(parameterBoxes);
|
||
sortScrollArea();
|
||
}
|
||
|
||
void ParameterDevice::setError(QString error)
|
||
{
|
||
|
||
QMessageBox::information(nullptr, "Получен ответ", error);
|
||
errorAtTransmit = true;
|
||
emit transmitEnd();
|
||
}
|
||
|
||
void ParameterDevice::on_adrSpin_valueChanged(int arg1)
|
||
{
|
||
ui->countSpin->setRange(1, 256-arg1);
|
||
}
|
||
|
||
void ParameterDevice::setAnswer(QString reply, quint16 objectID)
|
||
{
|
||
ParameterBox *newbox = new ParameterBox(nullptr, ParameterBox::Info, objectID);
|
||
newbox->setData(reply);
|
||
switch (newbox->getType()) {
|
||
case ParameterBox::Coil:
|
||
connect(newbox, &ParameterBox::writeParameter, this, [this](int adr, quint16 value){
|
||
emit writeSingleCoil(adr, (bool)value);
|
||
});
|
||
break;
|
||
case ParameterBox::HR:
|
||
connect(newbox, &ParameterBox::writeParameter, this, [this](int adr, quint16 value){
|
||
emit writeSingleRegister(adr, value);
|
||
});
|
||
break;
|
||
}
|
||
parameterBoxes.append(newbox);
|
||
emit transmitEnd();
|
||
|
||
// // Остановить, если уже запущен
|
||
// if (updateTimer->isActive()) {
|
||
// updateTimer->stop();
|
||
// }
|
||
// // Запустить с задержкой 6 секунд
|
||
// updateTimer->start(6000);
|
||
}
|
||
|
||
void ParameterDevice::sortScrollArea()
|
||
{
|
||
//sortParameterBoxesByID(parameterBoxes);
|
||
//ui->parameterBoxHubContents->layout()->deleteLater();
|
||
QLayout* pblayout = ui->parameterBoxHubContents->layout();
|
||
if (!pblayout) {
|
||
// Создайте новый лейаут, если его нет
|
||
pblayout = new QVBoxLayout(ui->parameterBoxHubContents);
|
||
ui->parameterBoxHubContents->setLayout(pblayout);
|
||
}
|
||
QLayoutItem *item;
|
||
while ((item = pblayout->takeAt(0)) != nullptr){
|
||
//delete item;
|
||
}
|
||
|
||
//ui->parameterBoxHubContents->setLayout(pblayout);
|
||
|
||
for (int i = 0; i < parameterBoxes.count(); i++) {
|
||
pblayout->addWidget(parameterBoxes.at(i));
|
||
}
|
||
|
||
ui->parameterBoxHubContents->update();
|
||
ui->parameterBoxHubContents->adjustSize();
|
||
ui->scrollArea->updateGeometry();
|
||
ui->scrollArea->viewport()->update();
|
||
}
|