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.

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