您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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