|
- #include "mainwindow.h"
- #include "hmidocument.h"
- #include "plcdocument.h"
- #include <QMenuBar>
- #include <QAction>
- #include <QToolButton>
- #include <QMimeData>
- #include <QDrag>
- #include <QTabBar>
- #include <QEvent>
- #include <QMouseEvent>
- #include <QDragEnterEvent>
- #include <QDragMoveEvent>
- #include <QDropEvent>
- #include <QDebug>
- #include <QVBoxLayout>
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QTextEdit>
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- setWindowTitle("综合平台编程器");
- setGeometry(500, 200, 1000, 700);
-
- // 初始化标签页
- m_tabWidget = new QTabWidget(this);
- m_tabWidget->setTabsClosable(true);
- setCentralWidget(m_tabWidget);
- connect(m_tabWidget, &QTabWidget::currentChanged, this, &MainWindow::onTabChanged);
- connect(m_tabWidget, &QTabWidget::tabCloseRequested, this, &MainWindow::onCloseTab);
- createMenus();// 创建菜单和工具栏
- createToolbars();
- }
-
- MainWindow::~MainWindow()
- {
- }
-
- // 创建菜单栏
- void MainWindow::createMenus()
- {
- // 设置菜单栏字体大小
- QFont menuFont = menuBar()->font();
- menuFont.setPointSize(15); // 菜单栏文字大小
- menuBar()->setFont(menuFont);
-
- // 创建文件菜单
- QMenu *fileMenu = menuBar()->addMenu("文件");
- QMenu *editMenu = menuBar()->addMenu("操作");
-
- // 设置菜单项字体大小(适用于所有子菜单)
- QFont itemFont = fileMenu->font();
- itemFont.setPointSize(12); // 菜单项文字大小
- fileMenu->setFont(itemFont);
- editMenu->setFont(itemFont);
-
- // 新建HMI动作
- QAction *newHmiAction = new QAction("新建HMI(&H)", this);
- newHmiAction->setShortcut(tr("Ctrl+H"));
- newHmiAction->setFont(itemFont); // 设置动作文字大小
- connect(newHmiAction, &QAction::triggered, this, &MainWindow::onNewHMI);
- fileMenu->addAction(newHmiAction);
-
- // 新建PLC动作
- QAction *newPlcAction = new QAction("新建PLC(&P)", this);
- newPlcAction->setShortcut(tr("Ctrl+P"));
- newPlcAction->setFont(itemFont);
- connect(newPlcAction, &QAction::triggered, this, &MainWindow::onNewPLC);
- fileMenu->addAction(newPlcAction);
-
- // 打开动作
- m_openAction = new QAction("打开(&O)", this);
- m_openAction->setShortcut(QKeySequence::Open);
- m_openAction->setStatusTip("打开现有文档");
- m_openAction->setFont(itemFont);
- connect(m_openAction, &QAction::triggered, this, &MainWindow::onOpen);
- fileMenu->addAction(m_openAction);
-
- // 保存动作
- m_saveAction = new QAction("保存(&S)", this);
- m_saveAction->setShortcut(QKeySequence::Save);
- m_saveAction->setStatusTip("保存当前文档");
- m_saveAction->setFont(itemFont);
- connect(m_saveAction, &QAction::triggered, this, &MainWindow::onSave);
- fileMenu->addAction(m_saveAction);
-
- // 另存为动作
- m_saveAsAction = new QAction("另存为(&A)", this);
- m_saveAsAction->setShortcut(QKeySequence::SaveAs);
- m_saveAsAction->setStatusTip("将文档另存为");
- m_saveAsAction->setFont(itemFont);
- connect(m_saveAsAction, &QAction::triggered, this, &MainWindow::onSaveAs);
- fileMenu->addAction(m_saveAsAction);
-
- // 操作菜单 - 添加复制、粘贴、删除功能
- QAction *copyAction = new QAction("复制(&C)", this);
- copyAction->setShortcut(QKeySequence::Copy);
- copyAction->setStatusTip("复制选中的项目");
- copyAction->setFont(itemFont);
- connect(copyAction, &QAction::triggered, this, [this]() {
- if (auto hmiDoc = dynamic_cast<HMIDocument*>(m_tabWidget->currentWidget())) {
- hmiDoc->copySelectedItems();
- }
- });
-
- QAction *pasteAction = new QAction("粘贴(&V)", this);
- pasteAction->setShortcut(QKeySequence::Paste);
- pasteAction->setStatusTip("粘贴复制的项目");
- pasteAction->setFont(itemFont);
- connect(pasteAction, &QAction::triggered, this, [this]() {
- if (auto hmiDoc = dynamic_cast<HMIDocument*>(m_tabWidget->currentWidget())) {
- hmiDoc->pasteItems();
- }
- });
-
- QAction *deleteAction = new QAction("删除(&D)", this);
- deleteAction->setShortcut(QKeySequence::Delete);
- deleteAction->setStatusTip("删除选中的项目");
- deleteAction->setFont(itemFont);
- connect(deleteAction, &QAction::triggered, this, [this]() {
- if (auto hmiDoc = dynamic_cast<HMIDocument*>(m_tabWidget->currentWidget())) {
- hmiDoc->deleteSelectedItems();
- }
- });
-
- // 添加到操作菜单
- editMenu->addAction(copyAction);
- editMenu->addAction(pasteAction);
- editMenu->addAction(deleteAction);
- }
-
- // 创建左侧工具栏
- void MainWindow::createToolbars()
- {
- m_leftToolBar = new QToolBar("绘图工具栏", this);
- addToolBar(Qt::LeftToolBarArea, m_leftToolBar);
- m_leftToolBar->setAllowedAreas(Qt::LeftToolBarArea); // 仅允许在左侧
- m_leftToolBar->setFixedWidth(230);
- }
-
- // 更新工具栏(根据当前文档类型)
- void MainWindow::updateToolBar(BaseDocument *doc)
- {
- m_leftToolBar->clear();// 清空现有工具
- if (!doc) return;
- if (doc->type() == BaseDocument::HMI)// HMI文档显示绘图工具
- {
- HMIDocument *hmiDoc = dynamic_cast<HMIDocument*>(doc);
- if (!hmiDoc) return;
- // 画指示灯按钮(支持拖拽)
- QToolButton *ellipseBtn = new QToolButton;
- ellipseBtn->setText("指示灯");
- ellipseBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- ellipseBtn->setIcon(QIcon("../two/untitled/images/灯泡.png"));//可替换为实际图标
- ellipseBtn->installEventFilter(this);//为按钮安装事件过滤器处理拖拽
- ellipseBtn->setProperty("toolType", "指示灯");
- m_leftToolBar->addWidget(ellipseBtn);
- ellipseBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 15px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- // 画按钮按钮(支持拖拽)
- QToolButton *rectBtn = new QToolButton;
- rectBtn->setText("按钮");
- rectBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- rectBtn->setIcon(QIcon("../two/untitled/images/按钮.png"));//可替换为实际图标
- rectBtn->installEventFilter(this);
- rectBtn->setProperty("toolType", "按钮");
- m_leftToolBar->addWidget(rectBtn);
- rectBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
- }
- // PLC按钮工具
- else if (doc->type() == BaseDocument::PLC)
- {
- // 常开触点按钮
- QToolButton *normallyOpenBtn = new QToolButton;
- normallyOpenBtn->setText("常开");
- normallyOpenBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- normallyOpenBtn->setIcon(QIcon("../two/untitled/images/T-常开触点-01.png")); // 替换为实际图标
- normallyOpenBtn->installEventFilter(this);
- normallyOpenBtn->setProperty("toolType", "常开");
- m_leftToolBar->addWidget(normallyOpenBtn);
- normallyOpenBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- // 常闭触点按钮
- QToolButton *normallyClosedBtn = new QToolButton;
- normallyClosedBtn->setText("常闭");
- normallyClosedBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- normallyClosedBtn->setIcon(QIcon("../two/untitled/images/T-常闭触点-01-01.png")); // 替换为实际图标
- normallyClosedBtn->installEventFilter(this);
- normallyClosedBtn->setProperty("toolType", "常闭");
- m_leftToolBar->addWidget(normallyClosedBtn);
- normallyClosedBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- // 大于按钮
- QToolButton *greaterThanBtn = new QToolButton;
- greaterThanBtn->setText("大于");
- greaterThanBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- greaterThanBtn->setIcon(QIcon("../two/untitled/images/大于号.png")); // 替换为实际图标
- greaterThanBtn->installEventFilter(this);
- greaterThanBtn->setProperty("toolType", "大于");
- m_leftToolBar->addWidget(greaterThanBtn);
- greaterThanBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- // 大于等于按钮
- QToolButton *greaterThanEqualBtn = new QToolButton;
- greaterThanEqualBtn->setText("大于等于");
- greaterThanEqualBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- greaterThanEqualBtn->setIcon(QIcon("../two/untitled/images/大于等于.png")); // 替换为实际图标
- greaterThanEqualBtn->installEventFilter(this);
- greaterThanEqualBtn->setProperty("toolType", "大于等于");
- m_leftToolBar->addWidget(greaterThanEqualBtn);
- greaterThanEqualBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 2px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- // 小于按钮
- QToolButton *lessThanBtn = new QToolButton;
- lessThanBtn->setText("小于");
- lessThanBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- lessThanBtn->setIcon(QIcon("../two/untitled/images/小于号.png")); // 替换为实际图标
- lessThanBtn->installEventFilter(this);
- lessThanBtn->setProperty("toolType", "小于");
- m_leftToolBar->addWidget(lessThanBtn);
- lessThanBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- // 小于等于按钮
- QToolButton *lessThanEqualBtn = new QToolButton;
- lessThanEqualBtn->setText("小于等于");
- lessThanEqualBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- lessThanEqualBtn->setIcon(QIcon("../two/untitled/images/小于等于.png")); // 替换为实际图标
- lessThanEqualBtn->installEventFilter(this);
- lessThanEqualBtn->setProperty("toolType", "小于等于");
- m_leftToolBar->addWidget(lessThanEqualBtn);
- lessThanEqualBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 2px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
- // 等于按钮
- QToolButton *equalBtn = new QToolButton;
- equalBtn->setText("等于");
- equalBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- equalBtn->setIcon(QIcon("../two/untitled/images/等于.png")); // 替换为实际图标
- equalBtn->installEventFilter(this);
- equalBtn->setProperty("toolType", "等于");
- m_leftToolBar->addWidget(equalBtn);
- equalBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
- // 线圈按钮
- QToolButton *coilBtn = new QToolButton;
- coilBtn->setText("线圈");
- coilBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- coilBtn->setIcon(QIcon("../two/untitled/images/添加线圈.png")); // 替换为实际图标
- coilBtn->installEventFilter(this);
- coilBtn->setProperty("toolType", "线圈");
- m_leftToolBar->addWidget(coilBtn);
- coilBtn->setStyleSheet(R"(
- QToolButton {
- margin: 2 auto;
- background-color: #f0f0f0;
- border-radius: 10px;
- border: 1px solid #ccc;
- padding: 10px 28px;
- font-size: 25px;
- font-weight: bold;
- color: #333;
- }
- QToolButton:hover {
- background-color: #e0e0e0;
- }
- )");
-
- normallyOpenBtn->installEventFilter(this);
- normallyClosedBtn->installEventFilter(this);
- greaterThanBtn->installEventFilter(this);
- greaterThanEqualBtn->installEventFilter(this);
- lessThanBtn->installEventFilter(this);
- lessThanEqualBtn->installEventFilter(this);
- equalBtn->installEventFilter(this);
- coilBtn->installEventFilter(this);
- }
- }
-
- // 事件过滤器处理拖拽
- bool MainWindow::eventFilter(QObject *obj, QEvent *event)
- {
- QToolButton *toolBtn = qobject_cast<QToolButton*>(obj);
- if (toolBtn && event->type() == QEvent::MouseButtonPress)
- {
- QMouseEvent *me = static_cast<QMouseEvent*>(event);
- if (me->button() == Qt::LeftButton)
- {
- QString toolType = toolBtn->property("toolType").toString();
- if (!toolType.isEmpty())
- {
- BaseDocument* currentDoc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
- if (currentDoc && currentDoc->type() == BaseDocument::PLC)//PLC文档处理
- {
- PLCDocument* plcDoc = static_cast<PLCDocument*>(currentDoc);
- // 设置当前工具
- plcDoc->setCurrentTool(toolType);
- // 创建拖拽对象
- QMimeData *mime = new QMimeData;
- mime->setText(toolType);
- QDrag *drag = new QDrag(toolBtn);
- drag->setMimeData(mime);
- drag->exec(Qt::CopyAction);
- return true;
- }
- // HMI文档处理(原有代码)
- if (currentDoc && currentDoc->type() == BaseDocument::HMI)
- {
- HMIDocument* hmiDoc = static_cast<HMIDocument*>(currentDoc);
- if (toolType == "指示灯")
- {
- hmiDoc->startDrawingEllipse();//点击生成标志
- }
- else if (toolType == "按钮")
- {
- hmiDoc->startDrawingRectangle();//点击生成标志
- }
- // 新增拖拽逻辑(与PLC一致)
- QMimeData *mime = new QMimeData;
- mime->setText(toolType);//传递工具类型("指示灯"或"按钮")
- QDrag *drag = new QDrag(toolBtn);
- drag->setMimeData(mime);
- drag->exec(Qt::CopyAction);//启动拖拽
- return true;
- }
- }
- }
- }
- return QMainWindow::eventFilter(obj, event);
- }
-
- // 新建HMI文档
- void MainWindow::onNewHMI()
- {
- m_hmiCount++;
- HMIDocument *doc = new HMIDocument;
- doc->setTitle("HMI文档 " + QString::number(m_hmiCount));
- m_tabWidget->addTab(doc, doc->title());
- m_tabWidget->setCurrentWidget(doc);
- updateToolBar(doc); // 更新工具栏为HMI工具
- }
-
- // 新建PLC文档
- void MainWindow::onNewPLC()
- {
- m_plcCount++;
- PLCDocument *doc = new PLCDocument;
- m_tabWidget->addTab(doc, QString("PLC文档 %1").arg(m_plcCount));
- m_tabWidget->setCurrentWidget(doc);
- updateToolBar(doc); // 更新工具栏为PLC工具
- }
- // 标签页切换时更新工具栏
- void MainWindow::onTabChanged(int idx)
- {
- if (idx < 0)
- {
- updateToolBar(nullptr);
- return;
- }
- BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->widget(idx));
- updateToolBar(doc);
- }
- // 保存文档
- void MainWindow::onSave()
- {
- BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
- if (!doc) return;
-
- if (doc->filePath().isEmpty()) {
- saveDocumentAs(doc);
- } else {
- saveDocument(doc);
- }
- }
- // 另存为文档
- void MainWindow::onSaveAs()
- {
- BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->currentWidget());
- if (doc) {
- saveDocumentAs(doc);
- }
- }
- // 打开文档
- void MainWindow::onOpen()
- {
- QString filePath = QFileDialog::getOpenFileName(
- this,
- "打开文档",
- "",
- "HMI文档 (*.hmi);;PLC文档 (*.plc)"
- );
-
- if (filePath.isEmpty()) return;
-
- QFileInfo fileInfo(filePath);
- BaseDocument *doc = nullptr;
-
- if (fileInfo.suffix().toLower() == "hmi") {
- doc = new HMIDocument;
- } else if (fileInfo.suffix().toLower() == "plc") {
- doc = new PLCDocument;
- } else {
- QMessageBox::warning(this, "打开文档", "不支持的文件格式");
- return;
- }
-
- if (doc->loadFromFile(filePath)) {
- m_tabWidget->addTab(doc, doc->title());
- m_tabWidget->setCurrentWidget(doc);
- updateToolBar(doc);
- } else {
- QMessageBox::critical(this, "打开文档", "无法加载文档");
- delete doc;
- }
- }
- // 关闭标签页
- void MainWindow::onCloseTab(int index)
- {
- BaseDocument *doc = dynamic_cast<BaseDocument*>(m_tabWidget->widget(index));
- if (!doc) return;
-
- if (doc->isModified())
- {
- QMessageBox::StandardButton reply = QMessageBox::question(
- this,
- "保存文档",
- QString("文档 '%1' 已修改,是否保存更改?").arg(doc->title()),
- QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel
- );
-
- if (reply == QMessageBox::Save) {
- saveDocument(doc);
- } else if (reply == QMessageBox::Cancel) {
- return;
- }
- }
-
- m_tabWidget->removeTab(index);
- delete doc;
- }
- // 保存文档
- void MainWindow::saveDocument(BaseDocument *doc)
- {
- // 如果文件路径为空(新文件),执行“另存为”
- if (doc->filePath().isEmpty())
- {
- // 直接调用另存为函数
- saveDocumentAs(doc);
- }
- else
- {
- // 如果已有文件路径,直接保存
- if (doc->saveToFile(doc->filePath()))
- {
- doc->setModified(false); // 清除修改状态
- QMessageBox::information(this, "保存文档", "文档保存成功");
- }
- else
- {
- QMessageBox::critical(this, "保存文档", "无法保存文档");
- }
- }
- }
- // 另存为文档
- void MainWindow::saveDocumentAs(BaseDocument *doc)
- {
- QString filePath = QFileDialog::getSaveFileName(
- this,
- "保存为",
- doc->filePath().isEmpty() ? doc->title() : doc->filePath(),
- doc->type() == BaseDocument::HMI ?"HMI文档 (*.hmi)" :"PLC文档 (*.plc)"
- );
-
- if (filePath.isEmpty()) return;
- if (doc->saveToFile(filePath))
- {
- doc->setModified(false); // 清除修改状态
- QMessageBox::information(this, "保存文档", "文档保存成功");
- }
- else
- {
- QMessageBox::critical(this, "保存文档", "无法保存文档");
- }
- }
|