综合平台编辑器
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.

112 lines
3.0 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. }
  24. MainWindow::~MainWindow()
  25. {
  26. delete ui_;
  27. }
  28. void MainWindow::initMainWindow()
  29. {
  30. setWindowTitle("综合平台编辑器");
  31. setWindowIcon(QIcon(":/resource/image/editor.png"));
  32. // 运行默认打开HMI页面
  33. ui_->tabWidget->setCurrentIndex(1);
  34. }
  35. // 实现槽函数
  36. void MainWindow::appendLog(const QString& message)
  37. {
  38. ui_->textEdit->append(message);
  39. }
  40. // 新建菜单动作槽函数
  41. void MainWindow::onNewFile()
  42. {
  43. if (!hmi_)
  44. {
  45. return;
  46. }
  47. if (hmi_->isModified())
  48. {
  49. auto ret = QMessageBox::warning(this, "提示",
  50. "当前文件有未保存的更改,是否保存?",
  51. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  52. if (ret == QMessageBox::Save)
  53. {
  54. QString fileName = QFileDialog::getSaveFileName(this, "保存HMI文件", "", "HMI文件 (*.json)");
  55. if (!fileName.isEmpty())
  56. {
  57. if (!hmi_->saveToFile(fileName))
  58. {
  59. QMessageBox::warning(this, "保存失败", "文件保存失败!");
  60. return; // 不新建,保存失败
  61. }
  62. }
  63. else
  64. {
  65. return; // 取消保存,取消新建
  66. }
  67. }
  68. else if (ret == QMessageBox::Cancel)
  69. {
  70. return; // 取消新建
  71. }
  72. // 如果是Discard,直接继续新建,丢弃当前修改
  73. }
  74. // 清空编辑区,新建页面
  75. hmi_->resetPages();
  76. }
  77. void MainWindow::onOpenFile()
  78. {
  79. QString fileName = QFileDialog::getOpenFileName(this, "打开HMI文件", "", "HMI文件 (*.hmi)");
  80. if (!fileName.isEmpty())
  81. {
  82. if (!hmi_->openFromFile(fileName))
  83. {
  84. QMessageBox::warning(this, "打开失败", "文件打开失败或格式不正确!");
  85. }
  86. }
  87. }
  88. void MainWindow::onSaveFile()
  89. {
  90. QString fileName = QFileDialog::getSaveFileName(this, "保存HMI文件", "", "HMI文件 (*.hmi)");
  91. if (!fileName.isEmpty())
  92. {
  93. if (!hmi_->saveToFile(fileName))
  94. {
  95. QMessageBox::warning(this, "保存失败", "文件保存失败!");
  96. }
  97. }
  98. }