TSI_Config/keyphase.cpp

193 lines
10 KiB
C++
Raw Normal View History

2024-12-17 18:53:41 +08:00
#include "keyphase.h"
#include "ui_keyphase.h"
2025-03-23 14:03:48 +08:00
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
2024-12-17 18:53:41 +08:00
2025-04-02 14:28:53 +08:00
#include "data_config.h"
#include "config_mgr.h"
#include "keyphase_data.h"
2025-03-27 10:16:01 +08:00
KeyPhase::KeyPhase(int slot_no_, QWidget *parent)
2024-12-17 18:53:41 +08:00
: QDialog(parent)
2025-03-27 10:16:01 +08:00
, ui(new Ui::KeyPhase) {
2024-12-17 18:53:41 +08:00
ui->setupUi(this);
2025-03-11 16:42:00 +08:00
ui->widget_body->setProperty("flag", "body");
2025-03-27 10:16:01 +08:00
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2025-03-23 14:03:48 +08:00
slot_no = slot_no_;
QString slot = QString("%1").arg(slot_no);
ui->label_slot->setText(slot);
2025-04-02 14:28:53 +08:00
// QString filePath_keyphase = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\keyphase.json").arg(slot_no);
// readJsonFile(filePath_keyphase);
2025-03-23 14:03:48 +08:00
Init();
2025-03-28 10:17:18 +08:00
connect(ui->radioButton_manual_threshold_1, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_1_clicked);
connect(ui->radioButton_manual_threshold_2, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_2_clicked);
connect(ui->radioButton_manual_threshold_3, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_3_clicked);
connect(ui->radioButton_manual_threshold_4, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_4_clicked);
2024-12-17 18:53:41 +08:00
}
2025-03-27 10:16:01 +08:00
KeyPhase::~KeyPhase() {
2024-12-17 18:53:41 +08:00
delete ui;
}
2025-03-27 10:16:01 +08:00
void KeyPhase::on_pushButton_cancel_clicked() {
this->close();
}
2025-03-28 10:17:18 +08:00
void KeyPhase::on_manual_threshold_1_clicked(bool checked) {
if (checked) {
ui->doubleSpinBox_threshold_1->setEnabled(true);
ui->doubleSpinBox_hysteresis_1->setEnabled(true);
return;
}
ui->doubleSpinBox_threshold_1->setEnabled(false);
ui->doubleSpinBox_hysteresis_1->setEnabled(false);
}
void KeyPhase::on_manual_threshold_2_clicked(bool checked) {
if (checked) {
ui->doubleSpinBox_threshold_2->setEnabled(true);
ui->doubleSpinBox_hysteresis_2->setEnabled(true);
return;
}
ui->doubleSpinBox_threshold_2->setEnabled(false);
ui->doubleSpinBox_hysteresis_2->setEnabled(false);
}
void KeyPhase::on_manual_threshold_3_clicked(bool checked) {
if (checked) {
ui->doubleSpinBox_threshold_3->setEnabled(true);
ui->doubleSpinBox_hysteresis_3->setEnabled(true);
return;
}
ui->doubleSpinBox_threshold_3->setEnabled(false);
ui->doubleSpinBox_hysteresis_3->setEnabled(false);
}
void KeyPhase::on_manual_threshold_4_clicked(bool checked) {
if (checked) {
ui->doubleSpinBox_threshold_4->setEnabled(true);
ui->doubleSpinBox_hysteresis_4->setEnabled(true);
return;
}
ui->doubleSpinBox_threshold_4->setEnabled(false);
ui->doubleSpinBox_hysteresis_4->setEnabled(false);
}
2025-04-02 14:28:53 +08:00
void KeyPhase::UpdateData(std::shared_ptr<KeyphaseData> &keyphase_data) {
keyphase_data->card_type_ = kCardKeyphase;
keyphase_data->slot_ = slot_no;
keyphase_data->version_ = 1;
for (int i = 0; i < CHANNEL_COUNT; i++) {
if (i + 1 == 1) {
keyphase_data->variables_[i].active = ui->checkBox_enable_1->isChecked();
keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_1->value();
keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_1->value();
keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_1->isChecked();
keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_1->value();
keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_1->value();
keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_1->value();
} else if (i + 1 == 2) {
keyphase_data->variables_[i].active = ui->checkBox_enable_2->isChecked();
keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_2->value();
keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_2->value();
keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_2->isChecked();
keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_2->value();
keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_2->value();
keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_2->value();
} else if (i + 1 == 3) {
keyphase_data->variables_[i].active = ui->checkBox_enable_3->isChecked();
keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_3->value();
keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_3->value();
keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_3->isChecked();
keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_3->value();
keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_3->value();
keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_3->value();
} else if (i + 1 == 4) {
keyphase_data->variables_[i].active = ui->checkBox_enable_4->isChecked();
keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_4->value();
keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_4->value();
keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_4->isChecked();
keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_4->value();
keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_4->value();
keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_4->value();
}
2025-03-23 14:03:48 +08:00
}
}
2025-04-02 14:28:53 +08:00
2025-03-27 10:16:01 +08:00
void KeyPhase::Init() {
2025-04-02 14:28:53 +08:00
std::shared_ptr<CardBase> base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no);
if (base_ptr == nullptr) {
// do nothing or use template to init it.
std::shared_ptr<KeyphaseData> keyphase_data = std::make_shared<KeyphaseData>();
ConfigMgr::Instance()->AddCard(keyphase_data);
UpdateData(keyphase_data);
return;
}
std::shared_ptr<KeyphaseData> keyphase_data = std::dynamic_pointer_cast<KeyphaseData>(base_ptr);
2025-04-02 09:59:55 +08:00
for (int i = 0; i < CHANNEL_COUNT; i++) {
2025-04-02 14:28:53 +08:00
if (i + 1 == 1) {
ui->checkBox_enable_1->setChecked(keyphase_data->variables_[i].active);
ui->doubleSpinBox_high_1->setValue(keyphase_data->variables_[i].normal_voltage_high);
ui->doubleSpinBox_low_1->setValue(keyphase_data->variables_[i].normal_voltage_low);
if (keyphase_data->variables_[i].automatic_threshold) {
2025-03-23 14:03:48 +08:00
ui->radioButton_automatic_threshold_1->setChecked(true);
2025-03-27 10:16:01 +08:00
} else {
2025-03-23 14:03:48 +08:00
ui->radioButton_manual_threshold_1->setChecked(true);
2025-03-27 10:16:01 +08:00
}
2025-04-02 14:28:53 +08:00
ui->doubleSpinBox_threshold_1->setValue(keyphase_data->variables_[i].threshold);
ui->doubleSpinBox_hysteresis_1->setValue(keyphase_data->variables_[i].hysteresis);
ui->spinBox_events_per_revolution_1->setValue(keyphase_data->variables_[i].events_per_revolution);
} else if (i + 1 == 2) {
ui->checkBox_enable_2->setChecked(keyphase_data->variables_[i].active);
ui->doubleSpinBox_high_2->setValue(keyphase_data->variables_[i].normal_voltage_high);
ui->doubleSpinBox_low_2->setValue(keyphase_data->variables_[i].normal_voltage_low);
if (keyphase_data->variables_[i].automatic_threshold) {
2025-03-23 14:03:48 +08:00
ui->radioButton_automatic_threshold_2->setChecked(true);
2025-03-27 10:16:01 +08:00
} else {
2025-03-23 14:03:48 +08:00
ui->radioButton_manual_threshold_2->setChecked(true);
2025-03-27 10:16:01 +08:00
}
2025-04-02 14:28:53 +08:00
ui->doubleSpinBox_threshold_2->setValue(keyphase_data->variables_[i].threshold);
ui->doubleSpinBox_hysteresis_2->setValue(keyphase_data->variables_[i].hysteresis);
ui->spinBox_events_per_revolution_2->setValue(keyphase_data->variables_[i].events_per_revolution);
} else if (i + 1 == 3) {
ui->checkBox_enable_3->setChecked(keyphase_data->variables_[i].active);
ui->doubleSpinBox_high_3->setValue(keyphase_data->variables_[i].normal_voltage_high);
ui->doubleSpinBox_low_3->setValue(keyphase_data->variables_[i].normal_voltage_low);
if (keyphase_data->variables_[i].automatic_threshold) {
2025-03-23 14:03:48 +08:00
ui->radioButton_automatic_threshold_3->setChecked(true);
2025-03-27 10:16:01 +08:00
} else {
2025-03-23 14:03:48 +08:00
ui->radioButton_manual_threshold_3->setChecked(true);
2025-03-27 10:16:01 +08:00
}
2025-04-02 14:28:53 +08:00
ui->doubleSpinBox_threshold_3->setValue(keyphase_data->variables_[i].threshold);
ui->doubleSpinBox_hysteresis_3->setValue(keyphase_data->variables_[i].hysteresis);
ui->spinBox_events_per_revolution_3->setValue(keyphase_data->variables_[i].events_per_revolution);
} else if (i + 1 == 4) {
ui->checkBox_enable_4->setChecked(keyphase_data->variables_[i].active);
ui->doubleSpinBox_high_4->setValue(keyphase_data->variables_[i].normal_voltage_high);
ui->doubleSpinBox_low_4->setValue(keyphase_data->variables_[i].normal_voltage_low);
if (keyphase_data->variables_[i].automatic_threshold) {
2025-03-23 14:03:48 +08:00
ui->radioButton_automatic_threshold_4->setChecked(true);
2025-03-27 10:16:01 +08:00
} else {
2025-03-23 14:03:48 +08:00
ui->radioButton_manual_threshold_4->setChecked(true);
2025-03-27 10:16:01 +08:00
}
2025-04-02 14:28:53 +08:00
ui->doubleSpinBox_threshold_4->setValue(keyphase_data->variables_[i].threshold);
ui->doubleSpinBox_hysteresis_4->setValue(keyphase_data->variables_[i].hysteresis);
ui->spinBox_events_per_revolution_4->setValue(keyphase_data->variables_[i].events_per_revolution);
2025-03-23 14:03:48 +08:00
}
}
}
2025-03-27 10:16:01 +08:00
void KeyPhase::on_pushButton_confirm_clicked() {
2025-04-02 14:28:53 +08:00
std::shared_ptr<CardBase> base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no);
if (base_ptr == nullptr) {
qCritical() << " should not be here";
2025-03-23 14:03:48 +08:00
return;
}
2025-04-02 14:28:53 +08:00
std::shared_ptr<KeyphaseData> keyphase_data = std::dynamic_pointer_cast<KeyphaseData>(base_ptr);
UpdateData(keyphase_data);
this->close();
2025-02-18 10:55:51 +08:00
}