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.

178 line
5.3 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. registerManager = new RegisterManager(this);
  21. plc_ = new PLC(registerManager, ui->widget);
  22. hmi_ = new HMI(ui->widget);
  23. modbusManager = new ModbusManager(registerManager, this);
  24. plc_->show();
  25. hmi_->hide();
  26. connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange);
  27. connect(ui->action_hmi,&QAction::triggered,this,&MainWindow::hmiChange);
  28. connect(ui->action_new, &QAction::triggered, this, &MainWindow::newProject);
  29. connect(ui->action_save, &QAction::triggered, this, &MainWindow::saveProject);
  30. connect(ui->action_open, &QAction::triggered, this, &MainWindow::openProject);
  31. connect(ui->action_connect, &QAction::triggered, this, &MainWindow::connection);
  32. connect(ui->action_disconnect, &QAction::triggered, this, &MainWindow::disconnection);
  33. connect(modbusManager, &ModbusManager::connectionStatusChanged,
  34. this, &MainWindow::updateConnectionStatus);
  35. plc_->applyProjectToScene(plcProject_); // 初始空
  36. hmi_->applyProjectToScene(hmiProject_);
  37. }
  38. MainWindow::~MainWindow()
  39. {
  40. delete ui;
  41. }
  42. void MainWindow::plcChange()
  43. {
  44. plc_->show();
  45. hmi_->hide();
  46. currentIsPLC_ = true;
  47. setWindowTitle((plcFilePath_.isEmpty() ? "未命名项目" : QFileInfo(plcFilePath_).fileName()) + " - PLC编辑器");
  48. }
  49. void MainWindow::hmiChange()
  50. {
  51. hmi_->show();
  52. plc_->hide();
  53. currentIsPLC_ = false;
  54. setWindowTitle((hmiFilePath_.isEmpty() ? "未命名项目" : QFileInfo(hmiFilePath_).fileName()) + " - HMI编辑器");
  55. }
  56. void MainWindow::newProject()
  57. {
  58. if (currentIsPLC_) {
  59. plcProject_.clear();
  60. plcFilePath_.clear();
  61. plc_->clearScene();
  62. setWindowTitle("未命名项目 - PLC编辑器");
  63. } else {
  64. hmi_->newPage();
  65. hmiProject_.clear(); // 清除项目数据
  66. hmiFilePath_.clear();
  67. // 更新窗口标题
  68. setWindowTitle("未命名项目 - HMI编辑器");
  69. }
  70. }
  71. void MainWindow::openProject()
  72. {
  73. if (currentIsPLC_)
  74. {
  75. QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.plcproj)");
  76. if (filePath.isEmpty()) return;
  77. if (plcProject_.loadFromFile(filePath))
  78. {
  79. plcFilePath_ = filePath;
  80. plc_->applyProjectToScene(plcProject_);
  81. setWindowTitle(QFileInfo(plcFilePath_).fileName() + " - PLC编辑器");
  82. }
  83. else
  84. {
  85. QMessageBox::critical(this, "错误", "无法打开PLC项目文件");
  86. }
  87. }
  88. else
  89. {
  90. QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.hmiproj)");
  91. if (filePath.isEmpty()) return;
  92. Project newProject;
  93. if (newProject.loadFromFile(filePath)) {
  94. // 创建新页面
  95. hmi_->newPage();
  96. // 应用项目到新页面
  97. int newPageIndex = hmi_->currentPageIndex();
  98. hmi_->applyProjectToScene(newProject, newPageIndex);
  99. // 保存文件路径
  100. hmiFilePath_ = filePath;
  101. setWindowTitle(QFileInfo(hmiFilePath_).fileName() + " - HMI编辑器");
  102. } else {
  103. QMessageBox::critical(this, "错误", "无法打开HMI项目文件");
  104. }
  105. }
  106. }
  107. void MainWindow::saveProject()
  108. {
  109. if (currentIsPLC_) {
  110. if (plcFilePath_.isEmpty()) {
  111. QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "PLC项目文件 (*.plcproj)");
  112. if (filePath.isEmpty()) return;
  113. if (!filePath.endsWith(".plcproj", Qt::CaseInsensitive))
  114. filePath += ".plcproj";
  115. plcFilePath_ = filePath;
  116. }
  117. plc_->extractSceneToProject(plcProject_);
  118. if (!plcProject_.saveToFile(plcFilePath_)) {
  119. QMessageBox::critical(this, "错误", "保存项目失败");
  120. }
  121. } else {
  122. if (hmiFilePath_.isEmpty()) {
  123. QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "HMI项目文件 (*.hmiproj)");
  124. if (filePath.isEmpty()) return;
  125. if (!filePath.endsWith(".hmiproj", Qt::CaseInsensitive))
  126. filePath += ".hmiproj";
  127. hmiFilePath_ = filePath;
  128. }
  129. // 从当前页面提取项目
  130. hmi_->extractSceneToProject(hmiProject_, hmi_->currentPageIndex());
  131. if (!hmiProject_.saveToFile(hmiFilePath_)) {
  132. QMessageBox::critical(this, "错误", "保存项目失败");
  133. }
  134. }
  135. }
  136. void MainWindow::connection()
  137. {
  138. modbusManager->connectToDevice("COM1",QSerialPort::BaudRate(9600),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1));
  139. modbusManager->startSimulation(2000);
  140. }
  141. void MainWindow::disconnection()
  142. {
  143. modbusManager->disconnectDevice();
  144. modbusManager->stopSimulation();
  145. }
  146. void MainWindow::updateConnectionStatus(bool connection)
  147. {
  148. if (connection)
  149. {
  150. ui->textEdit->append("连接");
  151. }
  152. else
  153. {
  154. ui->textEdit->append("断开");
  155. }
  156. }