综合平台编辑器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 line
3.5 KiB

  1. #include "mainwindow.h"
  2. #include <QToolBar>
  3. #include <QToolButton>
  4. #include <QVBoxLayout>
  5. #include <QButtonGroup>
  6. #include <QFileDialog>
  7. #include <QMessageBox>
  8. #include <QCloseEvent>
  9. MainWindow::MainWindow(QWidget *parent)
  10. : QMainWindow(parent)
  11. , ui_(new Ui::MainWindow)
  12. {
  13. ui_->setupUi(this);
  14. hmi_ = new HMIModule(ui_, this);
  15. hmi_->init();
  16. initMainWindow();
  17. // 连接 HMIModule 的信号到 MainWindow 的槽
  18. connect(hmi_, &HMIModule::logMessageGenerated, this, &MainWindow::appendLog);
  19. // 新建/打开/保存文件
  20. connect(ui_->actionNew, &QAction::triggered, this, &MainWindow::onNewFile);
  21. connect(ui_->actionOpen, &QAction::triggered, this, &MainWindow::onOpenFile);
  22. connect(ui_->actionSave, &QAction::triggered, this, &MainWindow::onSaveFile);
  23. connect(ui_->actionStart, &QAction::triggered, this, &MainWindow::startSimulation);
  24. connect(ui_->actionStop, &QAction::triggered, this, &MainWindow::stopSimulation);
  25. }
  26. MainWindow::~MainWindow()
  27. {
  28. delete ui_;
  29. }
  30. void MainWindow::initMainWindow()
  31. {
  32. setWindowTitle("综合平台编辑器");
  33. setWindowIcon(QIcon(":/resource/image/editor.png"));
  34. // 运行默认打开HMI页面
  35. ui_->tabWidget->setCurrentIndex(1);
  36. }
  37. // 实现槽函数
  38. void MainWindow::appendLog(const QString& message)
  39. {
  40. ui_->textEdit->append(message);
  41. }
  42. // 新建菜单动作槽函数
  43. void MainWindow::onNewFile()
  44. {
  45. if (!hmi_)
  46. {
  47. return;
  48. }
  49. if (hmi_->isModified())
  50. {
  51. auto ret = QMessageBox::warning(this, "提示",
  52. "当前文件有未保存的更改,是否保存?",
  53. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  54. if (ret == QMessageBox::Save)
  55. {
  56. QString fileName = QFileDialog::getSaveFileName(this, "保存HMI文件", "", "HMI文件 (*.json)");
  57. if (!fileName.isEmpty())
  58. {
  59. if (!hmi_->saveToFile(fileName))
  60. {
  61. QMessageBox::warning(this, "保存失败", "文件保存失败!");
  62. return; // 不新建,保存失败
  63. }
  64. }
  65. else
  66. {
  67. return; // 取消保存,取消新建
  68. }
  69. }
  70. else if (ret == QMessageBox::Cancel)
  71. {
  72. return; // 取消新建
  73. }
  74. // 如果是Discard,直接继续新建,丢弃当前修改
  75. }
  76. // 清空编辑区,新建页面
  77. hmi_->resetPages();
  78. }
  79. void MainWindow::onOpenFile()
  80. {
  81. QString fileName = QFileDialog::getOpenFileName(this, "打开HMI文件", "", "HMI文件 (*.hmi)");
  82. if (!fileName.isEmpty())
  83. {
  84. if (!hmi_->openFromFile(fileName))
  85. {
  86. QMessageBox::warning(this, "打开失败", "文件打开失败或格式不正确!");
  87. }
  88. }
  89. }
  90. void MainWindow::onSaveFile()
  91. {
  92. QString fileName = QFileDialog::getSaveFileName(this, "保存HMI文件", "", "HMI文件 (*.hmi)");
  93. if (!fileName.isEmpty())
  94. {
  95. if (!hmi_->saveToFile(fileName))
  96. {
  97. QMessageBox::warning(this, "保存失败", "文件保存失败!");
  98. }
  99. }
  100. }
  101. void MainWindow::startSimulation()
  102. {
  103. if (hmi_ && hmi_->getModbusManager()) {
  104. // 自己的串口参数,如 COM2 / 9600
  105. hmi_->getModbusManager()->startSimulation("COM2", 9600, QSerialPort::NoParity, QSerialPort::Data8, QSerialPort::OneStop);
  106. }
  107. }
  108. void MainWindow::stopSimulation()
  109. {
  110. if (hmi_ && hmi_->getModbusManager()) {
  111. hmi_->getModbusManager()->stopSimulation();
  112. }
  113. }