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.

155 lines
4.8 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. plc_, &PLC::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. hmiProject_.clear();
  65. hmiFilePath_.clear();
  66. hmi_->clearScene();
  67. setWindowTitle("未命名项目 - HMI编辑器");
  68. }
  69. }
  70. void MainWindow::openProject()
  71. {
  72. if (currentIsPLC_)
  73. {
  74. QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.plcproj)");
  75. if (filePath.isEmpty()) return;
  76. if (plcProject_.loadFromFile(filePath))
  77. {
  78. plcFilePath_ = filePath;
  79. plc_->applyProjectToScene(plcProject_);
  80. setWindowTitle(QFileInfo(plcFilePath_).fileName() + " - PLC编辑器");
  81. }
  82. else
  83. {
  84. QMessageBox::critical(this, "错误", "无法打开PLC项目文件");
  85. }
  86. }
  87. else
  88. {
  89. QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.hmiproj)");
  90. if (filePath.isEmpty()) return;
  91. if (hmiProject_.loadFromFile(filePath))
  92. {
  93. hmiFilePath_ = filePath;
  94. hmi_->applyProjectToScene(hmiProject_);
  95. setWindowTitle(QFileInfo(hmiFilePath_).fileName() + " - HMI编辑器");
  96. }
  97. else
  98. {
  99. QMessageBox::critical(this, "错误", "无法打开HMI项目文件");
  100. }
  101. }
  102. }
  103. void MainWindow::saveProject()
  104. {
  105. if (currentIsPLC_) {
  106. if (plcFilePath_.isEmpty()) {
  107. QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "PLC项目文件 (*.plcproj)");
  108. if (filePath.isEmpty()) return;
  109. if (!filePath.endsWith(".plcproj", Qt::CaseInsensitive))
  110. filePath += ".plcproj";
  111. plcFilePath_ = filePath;
  112. }
  113. plc_->extractSceneToProject(plcProject_);
  114. if (!plcProject_.saveToFile(plcFilePath_)) {
  115. QMessageBox::critical(this, "错误", "保存项目失败");
  116. }
  117. } else {
  118. if (hmiFilePath_.isEmpty()) {
  119. QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "HMI项目文件 (*.hmiproj)");
  120. if (filePath.isEmpty()) return;
  121. if (!filePath.endsWith(".hmiproj", Qt::CaseInsensitive))
  122. filePath += ".hmiproj";
  123. hmiFilePath_ = filePath;
  124. }
  125. hmi_->extractSceneToProject(hmiProject_);
  126. if (!hmiProject_.saveToFile(hmiFilePath_)) {
  127. QMessageBox::critical(this, "错误", "保存项目失败");
  128. }
  129. }
  130. }
  131. void MainWindow::connection()
  132. {
  133. modbusManager->connectToDevice("COM1",QSerialPort::BaudRate(9600),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1));
  134. modbusManager->startSimulation(2000);
  135. }
  136. void MainWindow::disconnection()
  137. {
  138. modbusManager->disconnectDevice();
  139. modbusManager->stopSimulation();
  140. }