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.

136 lines
4.1 KiB

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "item.h"
  4. #include <QListWidgetItem>
  5. #include <QMimeData>
  6. #include <QDrag>
  7. #include <QPainter>
  8. #include <QMouseEvent>
  9. #include <QDropEvent>
  10. #include <QGraphicsView>
  11. #include <QMessageBox>
  12. #include <QAction>
  13. #include <QFileDialog>
  14. #include "creatitem.h"
  15. MainWindow::MainWindow(QWidget *parent)
  16. : QMainWindow(parent)
  17. , ui(new Ui::MainWindow)
  18. {
  19. ui->setupUi(this);
  20. plc_ = new PLC(ui->widget);
  21. hmi_ = new HMI(ui->widget);
  22. plc_->show();
  23. hmi_->hide();
  24. connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange);
  25. connect(ui->action_hmi,&QAction::triggered,this,&MainWindow::hmiChange);
  26. connect(ui->action_new, &QAction::triggered, this, &MainWindow::newProject);
  27. connect(ui->action_save, &QAction::triggered, this, &MainWindow::saveProject);
  28. connect(ui->action_open, &QAction::triggered, this, &MainWindow::openProject);
  29. plc_->applyProjectToScene(plcProject_); // 初始空
  30. hmi_->applyProjectToScene(hmiProject_);
  31. }
  32. MainWindow::~MainWindow()
  33. {
  34. delete ui;
  35. }
  36. void MainWindow::plcChange()
  37. {
  38. plc_->show();
  39. hmi_->hide();
  40. currentIsPLC_ = true;
  41. setWindowTitle((plcFilePath_.isEmpty() ? "未命名项目" : QFileInfo(plcFilePath_).fileName()) + " - PLC编辑器");
  42. }
  43. void MainWindow::hmiChange()
  44. {
  45. hmi_->show();
  46. plc_->hide();
  47. currentIsPLC_ = false;
  48. setWindowTitle((hmiFilePath_.isEmpty() ? "未命名项目" : QFileInfo(hmiFilePath_).fileName()) + " - HMI编辑器");
  49. }
  50. void MainWindow::newProject()
  51. {
  52. if (currentIsPLC_) {
  53. plcProject_.clear();
  54. plcFilePath_.clear();
  55. plc_->clearScene();
  56. setWindowTitle("未命名项目 - PLC编辑器");
  57. } else {
  58. hmiProject_.clear();
  59. hmiFilePath_.clear();
  60. hmi_->clearScene();
  61. setWindowTitle("未命名项目 - HMI编辑器");
  62. }
  63. }
  64. void MainWindow::openProject()
  65. {
  66. if (currentIsPLC_)
  67. {
  68. QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.plcproj)");
  69. if (filePath.isEmpty()) return;
  70. if (plcProject_.loadFromFile(filePath))
  71. {
  72. plcFilePath_ = filePath;
  73. plc_->applyProjectToScene(plcProject_);
  74. setWindowTitle(QFileInfo(plcFilePath_).fileName() + " - PLC编辑器");
  75. }
  76. else
  77. {
  78. QMessageBox::critical(this, "错误", "无法打开PLC项目文件");
  79. }
  80. }
  81. else
  82. {
  83. QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.hmiproj)");
  84. if (filePath.isEmpty()) return;
  85. if (hmiProject_.loadFromFile(filePath))
  86. {
  87. hmiFilePath_ = filePath;
  88. hmi_->applyProjectToScene(hmiProject_);
  89. setWindowTitle(QFileInfo(hmiFilePath_).fileName() + " - HMI编辑器");
  90. }
  91. else
  92. {
  93. QMessageBox::critical(this, "错误", "无法打开HMI项目文件");
  94. }
  95. }
  96. }
  97. void MainWindow::saveProject()
  98. {
  99. if (currentIsPLC_) {
  100. if (plcFilePath_.isEmpty()) {
  101. QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "PLC项目文件 (*.plcproj)");
  102. if (filePath.isEmpty()) return;
  103. if (!filePath.endsWith(".plcproj", Qt::CaseInsensitive))
  104. filePath += ".plcproj";
  105. plcFilePath_ = filePath;
  106. }
  107. plc_->extractSceneToProject(plcProject_);
  108. if (!plcProject_.saveToFile(plcFilePath_)) {
  109. QMessageBox::critical(this, "错误", "保存项目失败");
  110. }
  111. } else {
  112. if (hmiFilePath_.isEmpty()) {
  113. QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "HMI项目文件 (*.hmiproj)");
  114. if (filePath.isEmpty()) return;
  115. if (!filePath.endsWith(".hmiproj", Qt::CaseInsensitive))
  116. filePath += ".hmiproj";
  117. hmiFilePath_ = filePath;
  118. }
  119. hmi_->extractSceneToProject(hmiProject_);
  120. if (!hmiProject_.saveToFile(hmiFilePath_)) {
  121. QMessageBox::critical(this, "错误", "保存项目失败");
  122. }
  123. }
  124. }