25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

695 lines
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. QMenu *simulationMenu = menuBar()->addMenu("仿真");
  183. simulationMenu->setFont(itemFont);
  184. // 运行动作 (暂时留空)
  185. QAction *runAction = new QAction("运行", this);
  186. runAction->setFont(itemFont);
  187. runAction->setEnabled(false);
  188. simulationMenu->addAction(runAction);
  189. }
  190. // 创建左侧工具栏
  191. void MainWindow::createToolbars()
  192. {
  193. m_leftToolBar = new QToolBar("绘图工具栏", this);
  194. addToolBar(Qt::LeftToolBarArea, m_leftToolBar);
  195. m_leftToolBar->setAllowedAreas(Qt::LeftToolBarArea); // 仅允许在左侧
  196. m_leftToolBar->setFixedWidth(230);
  197. }
  198. // 更新工具栏(根据当前文档类型)
  199. void MainWindow::updateToolBar(BaseDocument *doc)
  200. {
  201. m_leftToolBar->clear();// 清空现有工具
  202. if (!doc) return;
  203. if (doc->type() == BaseDocument::HMI)// HMI文档显示绘图工具
  204. {
  205. HMIDocument *hmiDoc = dynamic_cast<HMIDocument*>(doc);
  206. if (!hmiDoc) return;
  207. // 画指示灯按钮(支持拖拽)
  208. QToolButton *ellipseBtn = new QToolButton;
  209. ellipseBtn->setText("指示灯");
  210. ellipseBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  211. ellipseBtn->setIcon(QIcon("../two/untitled/images/灯泡.png"));//可替换为实际图标
  212. ellipseBtn->installEventFilter(this);//为按钮安装事件过滤器处理拖拽
  213. ellipseBtn->setProperty("toolType", "指示灯");
  214. m_leftToolBar->addWidget(ellipseBtn);
  215. ellipseBtn->setStyleSheet(R"(
  216. QToolButton {
  217. margin: 2 auto;
  218. background-color: #f0f0f0;
  219. border-radius: 10px;
  220. border: 1px solid #ccc;
  221. padding: 10px 15px;
  222. font-size: 25px;
  223. font-weight: bold;
  224. color: #333;
  225. }
  226. QToolButton:hover {
  227. background-color: #e0e0e0;
  228. }
  229. )");
  230. // 画按钮按钮(支持拖拽)
  231. QToolButton *rectBtn = new QToolButton;
  232. rectBtn->setText("按钮");
  233. rectBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  234. rectBtn->setIcon(QIcon("../two/untitled/images/按钮.png"));//可替换为实际图标
  235. rectBtn->installEventFilter(this);
  236. rectBtn->setProperty("toolType", "按钮");
  237. m_leftToolBar->addWidget(rectBtn);
  238. rectBtn->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. // PLC按钮工具
  255. else if (doc->type() == BaseDocument::PLC)
  256. {
  257. // 常开触点按钮
  258. QToolButton *normallyOpenBtn = new QToolButton;
  259. normallyOpenBtn->setText("常开");
  260. normallyOpenBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  261. normallyOpenBtn->setIcon(QIcon("../two/untitled/images/T-常开触点-01.png")); // 替换为实际图标
  262. normallyOpenBtn->installEventFilter(this);
  263. normallyOpenBtn->setProperty("toolType", "常开");
  264. m_leftToolBar->addWidget(normallyOpenBtn);
  265. normallyOpenBtn->setStyleSheet(R"(
  266. QToolButton {
  267. margin: 2 auto;
  268. background-color: #f0f0f0;
  269. border-radius: 10px;
  270. border: 1px solid #ccc;
  271. padding: 10px 28px;
  272. font-size: 25px;
  273. font-weight: bold;
  274. color: #333;
  275. }
  276. QToolButton:hover {
  277. background-color: #e0e0e0;
  278. }
  279. )");
  280. // 常闭触点按钮
  281. QToolButton *normallyClosedBtn = new QToolButton;
  282. normallyClosedBtn->setText("常闭");
  283. normallyClosedBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  284. normallyClosedBtn->setIcon(QIcon("../two/untitled/images/T-常闭触点-01-01.png")); // 替换为实际图标
  285. normallyClosedBtn->installEventFilter(this);
  286. normallyClosedBtn->setProperty("toolType", "常闭");
  287. m_leftToolBar->addWidget(normallyClosedBtn);
  288. normallyClosedBtn->setStyleSheet(R"(
  289. QToolButton {
  290. margin: 2 auto;
  291. background-color: #f0f0f0;
  292. border-radius: 10px;
  293. border: 1px solid #ccc;
  294. padding: 10px 28px;
  295. font-size: 25px;
  296. font-weight: bold;
  297. color: #333;
  298. }
  299. QToolButton:hover {
  300. background-color: #e0e0e0;
  301. }
  302. )");
  303. // 大于按钮
  304. QToolButton *greaterThanBtn = new QToolButton;
  305. greaterThanBtn->setText("大于");
  306. greaterThanBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  307. greaterThanBtn->setIcon(QIcon("../two/untitled/images/大于号.png")); // 替换为实际图标
  308. greaterThanBtn->installEventFilter(this);
  309. greaterThanBtn->setProperty("toolType", "大于");
  310. m_leftToolBar->addWidget(greaterThanBtn);
  311. greaterThanBtn->setStyleSheet(R"(
  312. QToolButton {
  313. margin: 2 auto;
  314. background-color: #f0f0f0;
  315. border-radius: 10px;
  316. border: 1px solid #ccc;
  317. padding: 10px 28px;
  318. font-size: 25px;
  319. font-weight: bold;
  320. color: #333;
  321. }
  322. QToolButton:hover {
  323. background-color: #e0e0e0;
  324. }
  325. )");
  326. // 大于等于按钮
  327. QToolButton *greaterThanEqualBtn = new QToolButton;
  328. greaterThanEqualBtn->setText("大于等于");
  329. greaterThanEqualBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  330. greaterThanEqualBtn->setIcon(QIcon("../two/untitled/images/大于等于.png")); // 替换为实际图标
  331. greaterThanEqualBtn->installEventFilter(this);
  332. greaterThanEqualBtn->setProperty("toolType", "大于等于");
  333. m_leftToolBar->addWidget(greaterThanEqualBtn);
  334. greaterThanEqualBtn->setStyleSheet(R"(
  335. QToolButton {
  336. margin: 2 auto;
  337. background-color: #f0f0f0;
  338. border-radius: 10px;
  339. border: 1px solid #ccc;
  340. padding: 10px 2px;
  341. font-size: 25px;
  342. font-weight: bold;
  343. color: #333;
  344. }
  345. QToolButton:hover {
  346. background-color: #e0e0e0;
  347. }
  348. )");
  349. // 小于按钮
  350. QToolButton *lessThanBtn = new QToolButton;
  351. lessThanBtn->setText("小于");
  352. lessThanBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  353. lessThanBtn->setIcon(QIcon("../two/untitled/images/小于号.png")); // 替换为实际图标
  354. lessThanBtn->installEventFilter(this);
  355. lessThanBtn->setProperty("toolType", "小于");
  356. m_leftToolBar->addWidget(lessThanBtn);
  357. lessThanBtn->setStyleSheet(R"(
  358. QToolButton {
  359. margin: 2 auto;
  360. background-color: #f0f0f0;
  361. border-radius: 10px;
  362. border: 1px solid #ccc;
  363. padding: 10px 28px;
  364. font-size: 25px;
  365. font-weight: bold;
  366. color: #333;
  367. }
  368. QToolButton:hover {
  369. background-color: #e0e0e0;
  370. }
  371. )");
  372. // 小于等于按钮
  373. QToolButton *lessThanEqualBtn = new QToolButton;
  374. lessThanEqualBtn->setText("小于等于");
  375. lessThanEqualBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  376. lessThanEqualBtn->setIcon(QIcon("../two/untitled/images/小于等于.png")); // 替换为实际图标
  377. lessThanEqualBtn->installEventFilter(this);
  378. lessThanEqualBtn->setProperty("toolType", "小于等于");
  379. m_leftToolBar->addWidget(lessThanEqualBtn);
  380. lessThanEqualBtn->setStyleSheet(R"(
  381. QToolButton {
  382. margin: 2 auto;
  383. background-color: #f0f0f0;
  384. border-radius: 10px;
  385. border: 1px solid #ccc;
  386. padding: 10px 2px;
  387. font-size: 25px;
  388. font-weight: bold;
  389. color: #333;
  390. }
  391. QToolButton:hover {
  392. background-color: #e0e0e0;
  393. }
  394. )");
  395. // 等于按钮
  396. QToolButton *equalBtn = new QToolButton;
  397. equalBtn->setText("等于");
  398. equalBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  399. equalBtn->setIcon(QIcon("../two/untitled/images/等于.png")); // 替换为实际图标
  400. equalBtn->installEventFilter(this);
  401. equalBtn->setProperty("toolType", "等于");
  402. m_leftToolBar->addWidget(equalBtn);
  403. equalBtn->setStyleSheet(R"(
  404. QToolButton {
  405. margin: 2 auto;
  406. background-color: #f0f0f0;
  407. border-radius: 10px;
  408. border: 1px solid #ccc;
  409. padding: 10px 28px;
  410. font-size: 25px;
  411. font-weight: bold;
  412. color: #333;
  413. }
  414. QToolButton:hover {
  415. background-color: #e0e0e0;
  416. }
  417. )");
  418. // 线圈按钮
  419. QToolButton *coilBtn = new QToolButton;
  420. coilBtn->setText("线圈");
  421. coilBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  422. coilBtn->setIcon(QIcon("../two/untitled/images/添加线圈.png")); // 替换为实际图标
  423. coilBtn->installEventFilter(this);
  424. coilBtn->setProperty("toolType", "线圈");
  425. m_leftToolBar->addWidget(coilBtn);
  426. coilBtn->setStyleSheet(R"(
  427. QToolButton {
  428. margin: 2 auto;
  429. background-color: #f0f0f0;
  430. border-radius: 10px;
  431. border: 1px solid #ccc;
  432. padding: 10px 28px;
  433. font-size: 25px;
  434. font-weight: bold;
  435. color: #333;
  436. }
  437. QToolButton:hover {
  438. background-color: #e0e0e0;
  439. }
  440. )");
  441. normallyOpenBtn->installEventFilter(this);
  442. normallyClosedBtn->installEventFilter(this);
  443. greaterThanBtn->installEventFilter(this);
  444. greaterThanEqualBtn->installEventFilter(this);
  445. lessThanBtn->installEventFilter(this);
  446. lessThanEqualBtn->installEventFilter(this);
  447. equalBtn->installEventFilter(this);
  448. coilBtn->installEventFilter(this);
  449. }
  450. }
  451. // 事件过滤器处理拖拽
  452. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
  453. {
  454. QToolButton *toolBtn = qobject_cast<QToolButton*>(obj);
  455. if (toolBtn && event->type() == QEvent::MouseButtonPress)
  456. {
  457. QMouseEvent *me = static_cast<QMouseEvent*>(event);
  458. if (me->button() == Qt::LeftButton)
  459. {
  460. QString toolType = toolBtn->property("toolType").toString();
  461. if (!toolType.isEmpty())
  462. {
  463. BaseDocument* currentDoc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
  464. if (currentDoc && currentDoc->type() == BaseDocument::PLC)//PLC文档处理
  465. {
  466. PLCDocument* plcDoc = static_cast<PLCDocument*>(currentDoc);
  467. // 设置当前工具
  468. plcDoc->setCurrentTool(toolType);
  469. // 创建拖拽对象
  470. QMimeData *mime = new QMimeData;
  471. mime->setText(toolType);
  472. QDrag *drag = new QDrag(toolBtn);
  473. drag->setMimeData(mime);
  474. drag->exec(Qt::CopyAction);
  475. return true;
  476. }
  477. // HMI文档处理
  478. if (currentDoc && currentDoc->type() == BaseDocument::HMI)
  479. {
  480. HMIDocument* hmiDoc = static_cast<HMIDocument*>(currentDoc);
  481. if (toolType == "指示灯")
  482. {
  483. hmiDoc->startDrawingEllipse();//点击生成标志
  484. }
  485. else if (toolType == "按钮")
  486. {
  487. hmiDoc->startDrawingRectangle();//点击生成标志
  488. }
  489. // 新增拖拽逻辑
  490. QMimeData *mime = new QMimeData;
  491. mime->setText(toolType);//传递工具类型("指示灯"或"按钮")
  492. QDrag *drag = new QDrag(toolBtn);
  493. drag->setMimeData(mime);
  494. drag->exec(Qt::CopyAction);//启动拖拽
  495. return true;
  496. }
  497. }
  498. }
  499. }
  500. return QMainWindow::eventFilter(obj, event);
  501. }
  502. // 新建HMI文档
  503. void MainWindow::onNewHMI()
  504. {
  505. m_hmiCount++;
  506. HMIDocument *doc = new HMIDocument;
  507. doc->setTitle("HMI文档 " + QString::number(m_hmiCount));
  508. m_tabWidget->addTab(doc, doc->title());
  509. m_tabWidget->setCurrentWidget(doc);
  510. updateToolBar(doc); // 更新工具栏为HMI工具
  511. }
  512. // 新建PLC文档
  513. void MainWindow::onNewPLC()
  514. {
  515. m_plcCount++;
  516. PLCDocument *doc = new PLCDocument;
  517. m_tabWidget->addTab(doc, QString("PLC文档 %1").arg(m_plcCount));
  518. m_tabWidget->setCurrentWidget(doc);
  519. updateToolBar(doc); // 更新工具栏为PLC工具
  520. }
  521. // 标签页切换时更新工具栏
  522. void MainWindow::onTabChanged(int idx)
  523. {
  524. if (idx < 0)
  525. {
  526. updateToolBar(nullptr);
  527. return;
  528. }
  529. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->widget(idx));
  530. updateToolBar(doc);
  531. }
  532. // 保存文档
  533. void MainWindow::onSave()
  534. {
  535. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
  536. if (!doc) return;
  537. if (doc->filePath().isEmpty()) {
  538. saveDocumentAs(doc);
  539. } else {
  540. saveDocument(doc);
  541. }
  542. }
  543. // 另存为文档
  544. void MainWindow::onSaveAs()
  545. {
  546. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
  547. if (doc) {
  548. saveDocumentAs(doc);
  549. }
  550. }
  551. // 打开文档
  552. void MainWindow::onOpen()
  553. {
  554. QString filePath = QFileDialog::getOpenFileName(
  555. this,
  556. "打开文档",
  557. "",
  558. "HMI文档 (*.hmi);;PLC文档 (*.plc)"
  559. );
  560. if (filePath.isEmpty()) return;
  561. QFileInfo fileInfo(filePath);
  562. BaseDocument *doc = nullptr;
  563. if (fileInfo.suffix().toLower() == "hmi") {
  564. doc = new HMIDocument;
  565. } else if (fileInfo.suffix().toLower() == "plc") {
  566. doc = new PLCDocument;
  567. } else {
  568. QMessageBox::warning(this, "打开文档", "不支持的文件格式");
  569. return;
  570. }
  571. if (doc->loadFromFile(filePath)) {
  572. m_tabWidget->addTab(doc, doc->title());
  573. m_tabWidget->setCurrentWidget(doc);
  574. updateToolBar(doc);
  575. } else {
  576. QMessageBox::critical(this, "打开文档", "无法加载文档");
  577. delete doc;
  578. }
  579. }
  580. // 关闭标签页
  581. void MainWindow::onCloseTab(int index)
  582. {
  583. BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->widget(index));
  584. if (!doc) return;
  585. if (doc->isModified())
  586. {
  587. QMessageBox::StandardButton reply = QMessageBox::question(
  588. this,
  589. "保存文档",
  590. QString("文档 '%1' 已修改,是否保存更改?").arg(doc->title()),
  591. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel
  592. );
  593. if (reply == QMessageBox::Save) {
  594. saveDocument(doc);
  595. } else if (reply == QMessageBox::Cancel) {
  596. return;
  597. }
  598. }
  599. m_tabWidget->removeTab(index);
  600. delete doc;
  601. }
  602. // 保存文档
  603. void MainWindow::saveDocument(BaseDocument *doc)
  604. {
  605. // 如果文件路径为空(新文件),执行“另存为”
  606. if (doc->filePath().isEmpty())
  607. {
  608. // 直接调用另存为函数
  609. saveDocumentAs(doc);
  610. }
  611. else
  612. {
  613. // 如果已有文件路径,直接保存
  614. if (doc->saveToFile(doc->filePath()))
  615. {
  616. doc->setModified(false); // 清除修改状态
  617. QMessageBox::information(this, "保存文档", "文档保存成功");
  618. }
  619. else
  620. {
  621. QMessageBox::critical(this, "保存文档", "无法保存文档");
  622. }
  623. }
  624. }
  625. // 另存为文档
  626. void MainWindow::saveDocumentAs(BaseDocument *doc)
  627. {
  628. QString filePath = QFileDialog::getSaveFileName(
  629. this,
  630. "保存为",
  631. doc->filePath().isEmpty() ? doc->title() : doc->filePath(),
  632. doc->type() == BaseDocument::HMI ?"HMI文档 (*.hmi)" :"PLC文档 (*.plc)"
  633. );
  634. if (filePath.isEmpty()) return;
  635. if (doc->saveToFile(filePath))
  636. {
  637. doc->setModified(false); // 清除修改状态
  638. QMessageBox::information(this, "保存文档", "文档保存成功");
  639. }
  640. else
  641. {
  642. QMessageBox::critical(this, "保存文档", "无法保存文档");
  643. }
  644. }