Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

662 wiersze
23 KiB

  1. #include "mainwindow.h"
  2. #include "hmidocument.h"
  3. #include "plcdocument.h"
  4. #include <QMenuBar>
  5. #include <QAction>
  6. #include <QToolButton>
  7. #include <QMimeData>
  8. #include <QDrag>
  9. #include <QTabBar>
  10. #include <QEvent>
  11. #include <QMouseEvent>
  12. #include <QDragEnterEvent>
  13. #include <QDragMoveEvent>
  14. #include <QDropEvent>
  15. #include <QDebug>
  16. #include <QVBoxLayout>
  17. #include <QFileDialog>
  18. #include <QMessageBox>
  19. #include <QTextEdit>
  20. #include<QDockWidget>
  21. MainWindow::MainWindow(QWidget *parent)
  22. : QMainWindow(parent)
  23. {
  24. setWindowTitle("综合平台编程器");
  25. setGeometry(500, 200, 1000, 700);
  26. // 初始化标签页
  27. m_tabWidget = new QTabWidget(this);
  28. m_tabWidget->setTabsClosable(true);
  29. setCentralWidget(m_tabWidget);
  30. connect(m_tabWidget, &QTabWidget::currentChanged, this, &MainWindow::onTabChanged);
  31. connect(m_tabWidget, &QTabWidget::tabCloseRequested, this, &MainWindow::onCloseTab);
  32. createMenus();// 创建菜单和工具栏
  33. createToolbars();
  34. createLogPanel(); // 创建日志面板
  35. }
  36. MainWindow::~MainWindow()
  37. {
  38. }
  39. void MainWindow::createLogPanel()
  40. {
  41. // 创建可停靠窗口
  42. m_logDock = new QDockWidget("消息输出", this);
  43. m_logDock->setAllowedAreas(Qt::BottomDockWidgetArea);
  44. m_logDock->setFeatures(QDockWidget::DockWidgetClosable |
  45. QDockWidget::DockWidgetMovable |
  46. QDockWidget::DockWidgetFloatable); // 添加垂直标题栏
  47. // 设置高度范围而非固定高度
  48. m_logDock->setMinimumHeight(150);
  49. m_logDock->setMaximumHeight(400);
  50. // 创建主内容部件
  51. QWidget* logWidget = new QWidget;
  52. QVBoxLayout* logLayout = new QVBoxLayout;
  53. logLayout->setContentsMargins(2, 2, 2, 2);
  54. // 创建文本编辑框用于显示日志
  55. m_logEdit = new QTextEdit;
  56. m_logEdit->setReadOnly(true);
  57. m_logEdit->setFont(QFont("Consolas", 10));
  58. m_logEdit->setStyleSheet("background-color: #f8f8f8; border: 1px solid #d9d9d9;");
  59. m_logEdit->setMinimumHeight(100);
  60. // 创建控制按钮
  61. QHBoxLayout* btnLayout = new QHBoxLayout;
  62. m_clearLogBtn = new QPushButton("清空日志");
  63. btnLayout->addWidget(m_clearLogBtn);
  64. btnLayout->addStretch();
  65. // 组合布局
  66. logLayout->addLayout(btnLayout);
  67. logLayout->addWidget(m_logEdit);
  68. logWidget->setLayout(logLayout);
  69. m_logDock->setWidget(logWidget);
  70. addDockWidget(Qt::BottomDockWidgetArea, m_logDock);
  71. // 初始显示状态
  72. m_logDock->show();
  73. // 连接信号
  74. connect(m_clearLogBtn, &QPushButton::clicked, this, &MainWindow::onClearLogButtonClicked);
  75. }
  76. // 槽函数实现
  77. void MainWindow::onClearLogButtonClicked()
  78. {
  79. m_logEdit->clear();
  80. m_logEdit->append("日志已清空");
  81. }
  82. // 创建菜单栏
  83. void MainWindow::createMenus()
  84. {
  85. // 设置菜单栏字体大小
  86. QFont menuFont = menuBar()->font();
  87. menuFont.setPointSize(15); // 菜单栏文字大小
  88. menuBar()->setFont(menuFont);
  89. // 创建文件菜单
  90. QMenu *fileMenu = menuBar()->addMenu("文件");
  91. QMenu *editMenu = menuBar()->addMenu("操作");
  92. // 设置菜单项字体大小(适用于所有子菜单)
  93. QFont itemFont = fileMenu->font();
  94. itemFont.setPointSize(12); // 菜单项文字大小
  95. fileMenu->setFont(itemFont);
  96. editMenu->setFont(itemFont);
  97. // 新建HMI动作
  98. QAction *newHmiAction = new QAction("新建HMI(&H)", this);
  99. newHmiAction->setShortcut(tr("Ctrl+H"));
  100. newHmiAction->setFont(itemFont); // 设置动作文字大小
  101. connect(newHmiAction, &QAction::triggered, this, &MainWindow::onNewHMI);
  102. fileMenu->addAction(newHmiAction);
  103. // 新建PLC动作
  104. QAction *newPlcAction = new QAction("新建PLC(&P)", this);
  105. newPlcAction->setShortcut(tr("Ctrl+P"));
  106. newPlcAction->setFont(itemFont);
  107. connect(newPlcAction, &QAction::triggered, this, &MainWindow::onNewPLC);
  108. fileMenu->addAction(newPlcAction);
  109. // 打开动作
  110. m_openAction = new QAction("打开(&O)", this);
  111. m_openAction->setShortcut(QKeySequence::Open);
  112. m_openAction->setStatusTip("打开现有文档");
  113. m_openAction->setFont(itemFont);
  114. connect(m_openAction, &QAction::triggered, this, &MainWindow::onOpen);
  115. fileMenu->addAction(m_openAction);
  116. // 保存动作
  117. m_saveAction = new QAction("保存(&S)", this);
  118. m_saveAction->setShortcut(QKeySequence::Save);
  119. m_saveAction->setStatusTip("保存当前文档");
  120. m_saveAction->setFont(itemFont);
  121. connect(m_saveAction, &QAction::triggered, this, &MainWindow::onSave);
  122. fileMenu->addAction(m_saveAction);
  123. // 另存为动作
  124. m_saveAsAction = new QAction("另存为(&A)", this);
  125. m_saveAsAction->setShortcut(QKeySequence::SaveAs);
  126. m_saveAsAction->setStatusTip("将文档另存为");
  127. m_saveAsAction->setFont(itemFont);
  128. connect(m_saveAsAction, &QAction::triggered, this, &MainWindow::onSaveAs);
  129. fileMenu->addAction(m_saveAsAction);
  130. // 操作菜单 - 添加复制、粘贴、删除功能
  131. QAction *copyAction = new QAction("复制(&C)", this);
  132. copyAction->setShortcut(QKeySequence::Copy);
  133. copyAction->setStatusTip("复制选中的项目");
  134. copyAction->setFont(itemFont);
  135. connect(copyAction, &QAction::triggered, this, [this]() {
  136. if (auto hmiDoc = dynamic_cast<HMIDocument*>(m_tabWidget->currentWidget())) {
  137. hmiDoc->copySelectedItems();
  138. }
  139. });
  140. QAction *pasteAction = new QAction("粘贴(&V)", this);
  141. pasteAction->setShortcut(QKeySequence::Paste);
  142. pasteAction->setStatusTip("粘贴复制的项目");
  143. pasteAction->setFont(itemFont);
  144. connect(pasteAction, &QAction::triggered, this, [this]() {
  145. if (auto hmiDoc = dynamic_cast<HMIDocument*>(m_tabWidget->currentWidget())) {
  146. hmiDoc->pasteItems();
  147. }
  148. });
  149. QAction *deleteAction = new QAction("删除(&D)", this);
  150. deleteAction->setShortcut(QKeySequence::Delete);
  151. deleteAction->setStatusTip("删除选中的项目");
  152. deleteAction->setFont(itemFont);
  153. connect(deleteAction, &QAction::triggered, this, [this]() {
  154. if (auto hmiDoc = dynamic_cast<HMIDocument*>(m_tabWidget->currentWidget())) {
  155. hmiDoc->deleteSelectedItems();
  156. }
  157. });
  158. // 添加到操作菜单
  159. editMenu->addAction(copyAction);
  160. editMenu->addAction(pasteAction);
  161. editMenu->addAction(deleteAction);
  162. }
  163. // 创建左侧工具栏
  164. void MainWindow::createToolbars()
  165. {
  166. m_leftToolBar = new QToolBar("绘图工具栏", this);
  167. addToolBar(Qt::LeftToolBarArea, m_leftToolBar);
  168. m_leftToolBar->setAllowedAreas(Qt::LeftToolBarArea); // 仅允许在左侧
  169. m_leftToolBar->setFixedWidth(230);
  170. }
  171. // 更新工具栏(根据当前文档类型)
  172. void MainWindow::updateToolBar(BaseDocument *doc)
  173. {
  174. m_leftToolBar->clear();// 清空现有工具
  175. if (!doc) return;
  176. if (doc->type() == BaseDocument::HMI)// HMI文档显示绘图工具
  177. {
  178. HMIDocument *hmiDoc = dynamic_cast<HMIDocument*>(doc);
  179. if (!hmiDoc) return;
  180. // 画指示灯按钮(支持拖拽)
  181. QToolButton *ellipseBtn = new QToolButton;
  182. ellipseBtn->setText("指示灯");
  183. ellipseBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  184. ellipseBtn->setIcon(QIcon("../two/untitled/images/灯泡.png"));//可替换为实际图标
  185. ellipseBtn->installEventFilter(this);//为按钮安装事件过滤器处理拖拽
  186. ellipseBtn->setProperty("toolType", "指示灯");
  187. m_leftToolBar->addWidget(ellipseBtn);
  188. ellipseBtn->setStyleSheet(R"(
  189. QToolButton {
  190. margin: 2 auto;
  191. background-color: #f0f0f0;
  192. border-radius: 10px;
  193. border: 1px solid #ccc;
  194. padding: 10px 15px;
  195. font-size: 25px;
  196. font-weight: bold;
  197. color: #333;
  198. }
  199. QToolButton:hover {
  200. background-color: #e0e0e0;
  201. }
  202. )");
  203. // 画按钮按钮(支持拖拽)
  204. QToolButton *rectBtn = new QToolButton;
  205. rectBtn->setText("按钮");
  206. rectBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  207. rectBtn->setIcon(QIcon("../two/untitled/images/按钮.png"));//可替换为实际图标
  208. rectBtn->installEventFilter(this);
  209. rectBtn->setProperty("toolType", "按钮");
  210. m_leftToolBar->addWidget(rectBtn);
  211. rectBtn->setStyleSheet(R"(
  212. QToolButton {
  213. margin: 2 auto;
  214. background-color: #f0f0f0;
  215. border-radius: 10px;
  216. border: 1px solid #ccc;
  217. padding: 10px 28px;
  218. font-size: 25px;
  219. font-weight: bold;
  220. color: #333;
  221. }
  222. QToolButton:hover {
  223. background-color: #e0e0e0;
  224. }
  225. )");
  226. }
  227. // PLC按钮工具
  228. else if (doc->type() == BaseDocument::PLC)
  229. {
  230. // 常开触点按钮
  231. QToolButton *normallyOpenBtn = new QToolButton;
  232. normallyOpenBtn->setText("常开");
  233. normallyOpenBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  234. normallyOpenBtn->setIcon(QIcon("../two/untitled/images/T-常开触点-01.png")); // 替换为实际图标
  235. normallyOpenBtn->installEventFilter(this);
  236. normallyOpenBtn->setProperty("toolType", "常开");
  237. m_leftToolBar->addWidget(normallyOpenBtn);
  238. normallyOpenBtn->setStyleSheet(R"(
  239. QToolButton {
  240. margin: 2 auto;
  241. background-color: #f0f0f0;
  242. border-radius: 10px;
  243. border: 1px solid #ccc;
  244. padding: 10px 28px;
  245. font-size: 25px;
  246. font-weight: bold;
  247. color: #333;
  248. }
  249. QToolButton:hover {
  250. background-color: #e0e0e0;
  251. }
  252. )");
  253. // 常闭触点按钮
  254. QToolButton *normallyClosedBtn = new QToolButton;
  255. normallyClosedBtn->setText("常闭");
  256. normallyClosedBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  257. normallyClosedBtn->setIcon(QIcon("../two/untitled/images/T-常闭触点-01-01.png")); // 替换为实际图标
  258. normallyClosedBtn->installEventFilter(this);
  259. normallyClosedBtn->setProperty("toolType", "常闭");
  260. m_leftToolBar->addWidget(normallyClosedBtn);
  261. normallyClosedBtn->setStyleSheet(R"(
  262. QToolButton {
  263. margin: 2 auto;
  264. background-color: #f0f0f0;
  265. border-radius: 10px;
  266. border: 1px solid #ccc;
  267. padding: 10px 28px;
  268. font-size: 25px;
  269. font-weight: bold;
  270. color: #333;
  271. }
  272. QToolButton:hover {
  273. background-color: #e0e0e0;
  274. }
  275. )");
  276. // 大于按钮
  277. QToolButton *greaterThanBtn = new QToolButton;
  278. greaterThanBtn->setText("大于");
  279. greaterThanBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  280. greaterThanBtn->setIcon(QIcon("../two/untitled/images/大于号.png")); // 替换为实际图标
  281. greaterThanBtn->installEventFilter(this);
  282. greaterThanBtn->setProperty("toolType", "大于");
  283. m_leftToolBar->addWidget(greaterThanBtn);
  284. greaterThanBtn->setStyleSheet(R"(
  285. QToolButton {
  286. margin: 2 auto;
  287. background-color: #f0f0f0;
  288. border-radius: 10px;
  289. border: 1px solid #ccc;
  290. padding: 10px 28px;
  291. font-size: 25px;
  292. font-weight: bold;
  293. color: #333;
  294. }
  295. QToolButton:hover {
  296. background-color: #e0e0e0;
  297. }
  298. )");
  299. // 大于等于按钮
  300. QToolButton *greaterThanEqualBtn = new QToolButton;
  301. greaterThanEqualBtn->setText("大于等于");
  302. greaterThanEqualBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  303. greaterThanEqualBtn->setIcon(QIcon("../two/untitled/images/大于等于.png")); // 替换为实际图标
  304. greaterThanEqualBtn->installEventFilter(this);
  305. greaterThanEqualBtn->setProperty("toolType", "大于等于");
  306. m_leftToolBar->addWidget(greaterThanEqualBtn);
  307. greaterThanEqualBtn->setStyleSheet(R"(
  308. QToolButton {
  309. margin: 2 auto;
  310. background-color: #f0f0f0;
  311. border-radius: 10px;
  312. border: 1px solid #ccc;
  313. padding: 10px 2px;
  314. font-size: 25px;
  315. font-weight: bold;
  316. color: #333;
  317. }
  318. QToolButton:hover {
  319. background-color: #e0e0e0;
  320. }
  321. )");
  322. // 小于按钮
  323. QToolButton *lessThanBtn = new QToolButton;
  324. lessThanBtn->setText("小于");
  325. lessThanBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  326. lessThanBtn->setIcon(QIcon("../two/untitled/images/小于号.png")); // 替换为实际图标
  327. lessThanBtn->installEventFilter(this);
  328. lessThanBtn->setProperty("toolType", "小于");
  329. m_leftToolBar->addWidget(lessThanBtn);
  330. lessThanBtn->setStyleSheet(R"(
  331. QToolButton {
  332. margin: 2 auto;
  333. background-color: #f0f0f0;
  334. border-radius: 10px;
  335. border: 1px solid #ccc;
  336. padding: 10px 28px;
  337. font-size: 25px;
  338. font-weight: bold;
  339. color: #333;
  340. }
  341. QToolButton:hover {
  342. background-color: #e0e0e0;
  343. }
  344. )");
  345. // 小于等于按钮
  346. QToolButton *lessThanEqualBtn = new QToolButton;
  347. lessThanEqualBtn->setText("小于等于");
  348. lessThanEqualBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  349. lessThanEqualBtn->setIcon(QIcon("../two/untitled/images/小于等于.png")); // 替换为实际图标
  350. lessThanEqualBtn->installEventFilter(this);
  351. lessThanEqualBtn->setProperty("toolType", "小于等于");
  352. m_leftToolBar->addWidget(lessThanEqualBtn);
  353. lessThanEqualBtn->setStyleSheet(R"(
  354. QToolButton {
  355. margin: 2 auto;
  356. background-color: #f0f0f0;
  357. border-radius: 10px;
  358. border: 1px solid #ccc;
  359. padding: 10px 2px;
  360. font-size: 25px;
  361. font-weight: bold;
  362. color: #333;
  363. }
  364. QToolButton:hover {
  365. background-color: #e0e0e0;
  366. }
  367. )");
  368. // 等于按钮
  369. QToolButton *equalBtn = new QToolButton;
  370. equalBtn->setText("等于");
  371. equalBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  372. equalBtn->setIcon(QIcon("../two/untitled/images/等于.png")); // 替换为实际图标
  373. equalBtn->installEventFilter(this);
  374. equalBtn->setProperty("toolType", "等于");
  375. m_leftToolBar->addWidget(equalBtn);
  376. equalBtn->setStyleSheet(R"(
  377. QToolButton {
  378. margin: 2 auto;
  379. background-color: #f0f0f0;
  380. border-radius: 10px;
  381. border: 1px solid #ccc;
  382. padding: 10px 28px;
  383. font-size: 25px;
  384. font-weight: bold;
  385. color: #333;
  386. }
  387. QToolButton:hover {
  388. background-color: #e0e0e0;
  389. }
  390. )");
  391. // 线圈按钮
  392. QToolButton *coilBtn = new QToolButton;
  393. coilBtn->setText("线圈");
  394. coilBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  395. coilBtn->setIcon(QIcon("../two/untitled/images/添加线圈.png")); // 替换为实际图标
  396. coilBtn->installEventFilter(this);
  397. coilBtn->setProperty("toolType", "线圈");
  398. m_leftToolBar->addWidget(coilBtn);
  399. coilBtn->setStyleSheet(R"(
  400. QToolButton {
  401. margin: 2 auto;
  402. background-color: #f0f0f0;
  403. border-radius: 10px;
  404. border: 1px solid #ccc;
  405. padding: 10px 28px;
  406. font-size: 25px;
  407. font-weight: bold;
  408. color: #333;
  409. }
  410. QToolButton:hover {
  411. background-color: #e0e0e0;
  412. }
  413. )");
  414. normallyOpenBtn->installEventFilter(this);
  415. normallyClosedBtn->installEventFilter(this);
  416. greaterThanBtn->installEventFilter(this);
  417. greaterThanEqualBtn->installEventFilter(this);
  418. lessThanBtn->installEventFilter(this);
  419. lessThanEqualBtn->installEventFilter(this);
  420. equalBtn->installEventFilter(this);
  421. coilBtn->installEventFilter(this);
  422. }
  423. }
  424. // 事件过滤器处理拖拽
  425. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
  426. {
  427. QToolButton *toolBtn = qobject_cast<QToolButton*>(obj);
  428. if (toolBtn && event->type() == QEvent::MouseButtonPress)
  429. {
  430. QMouseEvent *me = static_cast<QMouseEvent*>(event);
  431. if (me->button() == Qt::LeftButton)
  432. {
  433. QString toolType = toolBtn->property("toolType").toString();
  434. if (!toolType.isEmpty())
  435. {
  436. BaseDocument* currentDoc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
  437. if (currentDoc && currentDoc->type() == BaseDocument::PLC)//PLC文档处理
  438. {
  439. PLCDocument* plcDoc = static_cast<PLCDocument*>(currentDoc);
  440. // 设置当前工具
  441. plcDoc->setCurrentTool(toolType);
  442. // 创建拖拽对象
  443. QMimeData *mime = new QMimeData;
  444. mime->setText(toolType);
  445. QDrag *drag = new QDrag(toolBtn);
  446. drag->setMimeData(mime);
  447. drag->exec(Qt::CopyAction);
  448. return true;
  449. }
  450. // HMI文档处理(原有代码)
  451. if (currentDoc && currentDoc->type() == BaseDocument::HMI)
  452. {
  453. HMIDocument* hmiDoc = static_cast<HMIDocument*>(currentDoc);
  454. if (toolType == "指示灯")
  455. {
  456. hmiDoc->startDrawingEllipse();//点击生成标志
  457. }
  458. else if (toolType == "按钮")
  459. {
  460. hmiDoc->startDrawingRectangle();//点击生成标志
  461. }
  462. // 新增拖拽逻辑(与PLC一致)
  463. QMimeData *mime = new QMimeData;
  464. mime->setText(toolType);//传递工具类型("指示灯"或"按钮")
  465. QDrag *drag = new QDrag(toolBtn);
  466. drag->setMimeData(mime);
  467. drag->exec(Qt::CopyAction);//启动拖拽
  468. return true;
  469. }
  470. }
  471. }
  472. }
  473. return QMainWindow::eventFilter(obj, event);
  474. }
  475. // 新建HMI文档
  476. void MainWindow::onNewHMI()
  477. {
  478. m_hmiCount++;
  479. HMIDocument *doc = new HMIDocument;
  480. doc->setTitle("HMI文档 " + QString::number(m_hmiCount));
  481. m_tabWidget->addTab(doc, doc->title());
  482. m_tabWidget->setCurrentWidget(doc);
  483. updateToolBar(doc); // 更新工具栏为HMI工具
  484. }
  485. // 新建PLC文档
  486. void MainWindow::onNewPLC()
  487. {
  488. m_plcCount++;
  489. PLCDocument *doc = new PLCDocument;
  490. m_tabWidget->addTab(doc, QString("PLC文档 %1").arg(m_plcCount));
  491. m_tabWidget->setCurrentWidget(doc);
  492. updateToolBar(doc); // 更新工具栏为PLC工具
  493. }
  494. // 标签页切换时更新工具栏
  495. void MainWindow::onTabChanged(int idx)
  496. {
  497. if (idx < 0)
  498. {
  499. updateToolBar(nullptr);
  500. return;
  501. }
  502. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->widget(idx));
  503. updateToolBar(doc);
  504. }
  505. // 保存文档
  506. void MainWindow::onSave()
  507. {
  508. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
  509. if (!doc) return;
  510. if (doc->filePath().isEmpty()) {
  511. saveDocumentAs(doc);
  512. } else {
  513. saveDocument(doc);
  514. }
  515. }
  516. // 另存为文档
  517. void MainWindow::onSaveAs()
  518. {
  519. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
  520. if (doc) {
  521. saveDocumentAs(doc);
  522. }
  523. }
  524. // 打开文档
  525. void MainWindow::onOpen()
  526. {
  527. QString filePath = QFileDialog::getOpenFileName(
  528. this,
  529. "打开文档",
  530. "",
  531. "HMI文档 (*.hmi);;PLC文档 (*.plc)"
  532. );
  533. if (filePath.isEmpty()) return;
  534. QFileInfo fileInfo(filePath);
  535. BaseDocument *doc = nullptr;
  536. if (fileInfo.suffix().toLower() == "hmi") {
  537. doc = new HMIDocument;
  538. } else if (fileInfo.suffix().toLower() == "plc") {
  539. doc = new PLCDocument;
  540. } else {
  541. QMessageBox::warning(this, "打开文档", "不支持的文件格式");
  542. return;
  543. }
  544. if (doc->loadFromFile(filePath)) {
  545. m_tabWidget->addTab(doc, doc->title());
  546. m_tabWidget->setCurrentWidget(doc);
  547. updateToolBar(doc);
  548. } else {
  549. QMessageBox::critical(this, "打开文档", "无法加载文档");
  550. delete doc;
  551. }
  552. }
  553. // 关闭标签页
  554. void MainWindow::onCloseTab(int index)
  555. {
  556. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->widget(index));
  557. if (!doc) return;
  558. if (doc->isModified())
  559. {
  560. QMessageBox::StandardButton reply = QMessageBox::question(
  561. this,
  562. "保存文档",
  563. QString("文档 '%1' 已修改,是否保存更改?").arg(doc->title()),
  564. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel
  565. );
  566. if (reply == QMessageBox::Save) {
  567. saveDocument(doc);
  568. } else if (reply == QMessageBox::Cancel) {
  569. return;
  570. }
  571. }
  572. m_tabWidget->removeTab(index);
  573. delete doc;
  574. }
  575. // 保存文档
  576. void MainWindow::saveDocument(BaseDocument *doc)
  577. {
  578. // 如果文件路径为空(新文件),执行“另存为”
  579. if (doc->filePath().isEmpty())
  580. {
  581. // 直接调用另存为函数
  582. saveDocumentAs(doc);
  583. }
  584. else
  585. {
  586. // 如果已有文件路径,直接保存
  587. if (doc->saveToFile(doc->filePath()))
  588. {
  589. doc->setModified(false); // 清除修改状态
  590. QMessageBox::information(this, "保存文档", "文档保存成功");
  591. }
  592. else
  593. {
  594. QMessageBox::critical(this, "保存文档", "无法保存文档");
  595. }
  596. }
  597. }
  598. // 另存为文档
  599. void MainWindow::saveDocumentAs(BaseDocument *doc)
  600. {
  601. QString filePath = QFileDialog::getSaveFileName(
  602. this,
  603. "保存为",
  604. doc->filePath().isEmpty() ? doc->title() : doc->filePath(),
  605. doc->type() == BaseDocument::HMI ?"HMI文档 (*.hmi)" :"PLC文档 (*.plc)"
  606. );
  607. if (filePath.isEmpty()) return;
  608. if (doc->saveToFile(filePath))
  609. {
  610. doc->setModified(false); // 清除修改状态
  611. QMessageBox::information(this, "保存文档", "文档保存成功");
  612. }
  613. else
  614. {
  615. QMessageBox::critical(this, "保存文档", "无法保存文档");
  616. }
  617. }