Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

172 rindas
6.6 KiB

  1. #include "mainwindow.h"
  2. #include "modules/HMI/hmieditorwidget.h"
  3. #include "modules/PLC/plceditorwidget.h"
  4. #include <QFileDialog>
  5. #include <QMessageBox>
  6. #include <QInputDialog>
  7. #include <QTabBar>
  8. #include <QDebug>
  9. MainWindow::MainWindow(QWidget *parent)
  10. : QMainWindow(parent)
  11. {
  12. setWindowTitle("综合平台编程器");
  13. setFixedSize(1000, 740);
  14. tabWidget = new QTabWidget(this);
  15. setCentralWidget(tabWidget);
  16. tabWidget->setTabsClosable(true);
  17. connect(tabWidget, &QTabWidget::tabCloseRequested, this, &MainWindow::onTabCloseRequested);
  18. tabWidget->setStyleSheet("QTabWidget { margin: 10px; }");
  19. fileManager = new FileManager(this);
  20. createMenus();
  21. }
  22. MainWindow::~MainWindow() {}
  23. void MainWindow::createMenus() {
  24. fileMenu = menuBar()->addMenu("文件(&F)");
  25. newAction = new QAction("新建(&N)", this);
  26. newAction->setShortcut(QKeySequence("Ctrl+N"));
  27. connect(newAction, &QAction::triggered, this, &MainWindow::onNewFile);
  28. fileMenu->addAction(newAction);
  29. openAction = new QAction("打开(&O)", this);
  30. openAction->setShortcut(QKeySequence("Ctrl+O"));
  31. connect(openAction, &QAction::triggered, this, &MainWindow::onOpenFile);
  32. fileMenu->addAction(openAction);
  33. saveAction = new QAction("保存(&S)", this);
  34. saveAction->setShortcut(QKeySequence("Ctrl+S"));
  35. connect(saveAction, &QAction::triggered, this, &MainWindow::onSaveFile);
  36. fileMenu->addAction(saveAction);
  37. closeAction = new QAction("关闭(&C)", this);
  38. closeAction->setShortcut(QKeySequence("Ctrl+W"));
  39. connect(closeAction, &QAction::triggered, this, &MainWindow::onCloseFile);
  40. fileMenu->addAction(closeAction);
  41. simulateMenu = menuBar()->addMenu("仿真(&M)");
  42. simulateAction = new QAction("启动仿真(&R)", this);
  43. simulateAction->setShortcut(QKeySequence("F5"));
  44. connect(simulateAction, &QAction::triggered, this, &MainWindow::onSimulate);
  45. simulateMenu->addAction(simulateAction);
  46. }
  47. void MainWindow::onTabCloseRequested(int index)
  48. {
  49. if (index < 0 || index >= tabWidget->count()) return;
  50. QWidget* widget = tabWidget->widget(index);
  51. if (widget) {
  52. tabWidget->removeTab(index);
  53. delete widget;
  54. }
  55. }
  56. void MainWindow::onCloseFile()
  57. {
  58. int currentIndex = tabWidget->currentIndex();
  59. if (currentIndex >= 0) {
  60. onTabCloseRequested(currentIndex);
  61. }
  62. }
  63. void MainWindow::onNewFile() {
  64. QStringList types = {"HMI文件", "PLC文件"};
  65. bool ok;
  66. QString type = QInputDialog::getItem(this, "选择文件类型", "请选择要创建的文件类型:", types, 0, false, &ok);
  67. if (ok && !type.isEmpty()) {
  68. if (type == "HMI文件") {
  69. HmiEditorWidget* newHmi = new HmiEditorWidget(this);
  70. tabWidget->addTab(newHmi, "HMI编辑器");
  71. tabWidget->setCurrentWidget(newHmi);
  72. qDebug() << "MainWindow: 创建新HMI编辑器,ID:" << newHmi;
  73. } else if (type == "PLC文件") {
  74. PlcEditorWidget* newPlc = new PlcEditorWidget(this);
  75. tabWidget->addTab(newPlc, "PLC编辑器");
  76. tabWidget->setCurrentWidget(newPlc);
  77. qDebug() << "MainWindow: 创建新PLC编辑器,ID:" << newPlc;
  78. }
  79. }
  80. }
  81. void MainWindow::onOpenFile() {
  82. QString filePath = QFileDialog::getOpenFileName(this, "打开文件", "", "HMI文件 (*.hmi);;PLC文件 (*.plc);;所有文件 (*)");
  83. if (!filePath.isEmpty()) {
  84. QFileInfo info(filePath);
  85. if (info.suffix() == "hmi") {
  86. HmiEditorWidget* hmi = new HmiEditorWidget(this);
  87. if (fileManager->loadHmiFile(hmi, filePath)) {
  88. tabWidget->addTab(hmi, info.fileName());
  89. tabWidget->setCurrentWidget(hmi);
  90. QMessageBox::information(this, "加载成功", "HMI文件已加载:" + filePath);
  91. qDebug() << "MainWindow: 打开HMI文件,编辑器ID:" << hmi << "路径:" << filePath;
  92. } else {
  93. delete hmi;
  94. QMessageBox::warning(this, "加载失败", "无法加载HMI文件:" + filePath);
  95. }
  96. } else if (info.suffix() == "plc") {
  97. PlcEditorWidget* plc = new PlcEditorWidget(this);
  98. if (fileManager->loadPlcFile(plc, filePath)) {
  99. tabWidget->addTab(plc, info.fileName());
  100. tabWidget->setCurrentWidget(plc);
  101. QMessageBox::information(this, "加载成功", "PLC文件已加载:" + filePath);
  102. qDebug() << "MainWindow: 打开PLC文件,编辑器ID:" << plc << "路径:" << filePath;
  103. } else {
  104. delete plc;
  105. QMessageBox::warning(this, "加载失败", "无法加载PLC文件:" + filePath);
  106. }
  107. }
  108. }
  109. }
  110. void MainWindow::onSaveFile()
  111. {
  112. QWidget* currentWidget = tabWidget->currentWidget();
  113. if (!currentWidget) {
  114. QMessageBox::warning(this, "保存失败", "没有打开的文件");
  115. return;
  116. }
  117. QString filter;
  118. bool isHmi = qobject_cast<HmiEditorWidget*>(currentWidget) != nullptr;
  119. bool isPlc = qobject_cast<PlcEditorWidget*>(currentWidget) != nullptr;
  120. if (isHmi) {
  121. filter = "HMI文件 (*.hmi)";
  122. } else if (isPlc) {
  123. filter = "PLC文件 (*.plc)";
  124. } else {
  125. QMessageBox::warning(this, "保存失败", "无法识别的文件类型");
  126. return;
  127. }
  128. QString filePath = QFileDialog::getSaveFileName(this, "保存文件", "", filter);
  129. if (!filePath.isEmpty()) {
  130. bool success = false;
  131. if (isHmi) {
  132. HmiEditorWidget* hmi = qobject_cast<HmiEditorWidget*>(currentWidget);
  133. success = fileManager->saveHmiFile(hmi, filePath);
  134. qDebug() << "MainWindow: 保存HMI文件,编辑器ID:" << hmi << "路径:" << filePath << "结果:" << success;
  135. } else if (isPlc) {
  136. PlcEditorWidget* plc = qobject_cast<PlcEditorWidget*>(currentWidget);
  137. success = fileManager->savePlcFile(plc, filePath);
  138. qDebug() << "MainWindow: 保存PLC文件,编辑器ID:" << plc << "路径:" << filePath << "结果:" << success;
  139. }
  140. if (success) {
  141. QMessageBox::information(this, "保存成功", "文件已保存到:" + filePath);
  142. int currentIndex = tabWidget->currentIndex();
  143. if (currentIndex >= 0) {
  144. tabWidget->setTabText(currentIndex, QFileInfo(filePath).fileName());
  145. }
  146. } else {
  147. QMessageBox::warning(this, "保存失败", "无法保存文件:" + filePath);
  148. }
  149. }
  150. }
  151. void MainWindow::onSimulate() {
  152. QMessageBox::information(this, "仿真", "启动仿真功能\n(实际实现需要根据业务逻辑开发)");
  153. }