#include "mainwindow.h" #include #include #include #include #include #include #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setWindowTitle("综合平台编程器"); setGeometry(100, 100, 1000, 700); // 初始化标签页 m_tabWidget = new QTabWidget(this); m_tabWidget->setTabsClosable(true); setCentralWidget(m_tabWidget); connect(m_tabWidget, &QTabWidget::currentChanged, this, &MainWindow::onTabChanged); // 创建菜单和工具栏 createMenus(); createToolbars(); } MainWindow::~MainWindow() { // 标签页和工具栏由Qt自动销毁 } // 创建菜单栏 // 在createMenus()函数中添加以下代码,用于创建操作菜单及其功能 void MainWindow::createMenus() { // 创建文件菜单(保持原有代码) QMenu *fileMenu = menuBar()->addMenu("文件"); QMenu *editMenu = menuBar()->addMenu("操作"); QMenu *simulationMenu = menuBar()->addMenu("仿真"); // 新建HMI动作(保持原有代码) QAction *newHmiAction = new QAction("新建HMI(&H)", this); newHmiAction->setShortcut(tr("Ctrl+H")); newHmiAction->setStatusTip("创建新的HMI文档"); connect(newHmiAction, &QAction::triggered, this, &MainWindow::onNewHMI); fileMenu->addAction(newHmiAction); // 新建PLC动作(保持原有代码) QAction *newPlcAction = new QAction("新建PLC(&P)", this); newPlcAction->setShortcut(tr("Ctrl+P")); newPlcAction->setStatusTip("创建新的PLC文档"); connect(newPlcAction, &QAction::triggered, this, &MainWindow::onNewPLC); fileMenu->addAction(newPlcAction); // 操作菜单 - 添加复制、粘贴、删除功能 QAction *copyAction = new QAction("复制(&C)", this); copyAction->setShortcut(QKeySequence::Copy); // 标准复制快捷键 Ctrl+C copyAction->setStatusTip("复制选中的项目"); connect(copyAction, &QAction::triggered, this, [this]() { // 获取当前活动的HMI文档 if (auto hmiDoc = dynamic_cast(m_tabWidget->currentWidget())) { hmiDoc->copySelectedItems(); } }); QAction *pasteAction = new QAction("粘贴(&V)", this); pasteAction->setShortcut(QKeySequence::Paste); // 标准粘贴快捷键 Ctrl+V pasteAction->setStatusTip("粘贴复制的项目"); connect(pasteAction, &QAction::triggered, this, [this]() { if (auto hmiDoc = dynamic_cast(m_tabWidget->currentWidget())) { hmiDoc->pasteItems(); } }); QAction *deleteAction = new QAction("删除(&D)", this); deleteAction->setShortcut(QKeySequence::Delete); // 删除键 deleteAction->setStatusTip("删除选中的项目"); connect(deleteAction, &QAction::triggered, this, [this]() { if (auto hmiDoc = dynamic_cast(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); // 仅允许在左侧 } // 更新工具栏(根据当前文档类型) void MainWindow::updateToolBar(BaseDocument *doc) { // 清空现有工具 m_leftToolBar->clear(); if (!doc) return; // HMI文档显示绘图工具 if (doc->type() == BaseDocument::HMI) { HMIDocument *hmiDoc = dynamic_cast(doc); if (!hmiDoc) return; // 画指示灯按钮(支持拖拽) QToolButton *ellipseBtn = new QToolButton; ellipseBtn->setText("指示灯"); ellipseBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); ellipseBtn->setIcon(QIcon("../two/untitled/images/灯泡.png")); // 可替换为实际图标 ellipseBtn->setMinimumSize(120, 120); connect(ellipseBtn, &QToolButton::clicked, [=]() { hmiDoc->setDrawEllipse(true); hmiDoc->setDrawRectangle(false); }); // 为按钮安装事件过滤器处理拖拽 ellipseBtn->installEventFilter(this); ellipseBtn->setProperty("toolType", "指示灯"); m_leftToolBar->addWidget(ellipseBtn); // 画按钮按钮(支持拖拽) QToolButton *rectBtn = new QToolButton; rectBtn->setText("按钮"); rectBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); rectBtn->setIcon(QIcon("../two/untitled/images/按钮.png")); // 可替换为实际图标 rectBtn->setMinimumSize(120, 120); connect(rectBtn, &QToolButton::clicked, [=]() { hmiDoc->setDrawRectangle(true); hmiDoc->setDrawEllipse(false); }); // 为按钮安装事件过滤器处理拖拽 rectBtn->installEventFilter(this); rectBtn->setProperty("toolType", "按钮"); m_leftToolBar->addWidget(rectBtn); } // PLC文档可添加自己的工具 else if (doc->type() == BaseDocument::PLC) { m_leftToolBar->addAction("常开触点"); m_leftToolBar->addAction("常闭触点"); } } // 事件过滤器处理拖拽 bool MainWindow::eventFilter(QObject *obj, QEvent *event) { // 检查是否是工具栏按钮的鼠标按下事件 QToolButton *toolBtn = qobject_cast(obj); if (toolBtn && event->type() == QEvent::MouseButtonPress) { QMouseEvent *me = static_cast(event); if (me->button() == Qt::LeftButton) { // 获取工具类型 QString toolType = toolBtn->property("toolType").toString(); if (!toolType.isEmpty()) { // 创建拖拽 QMimeData *mime = new QMimeData; mime->setText(toolType); QDrag *drag = new QDrag(obj); 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(QString("HMI文档 %1").arg(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(m_tabWidget->widget(idx)); updateToolBar(doc); }