|
- #include "mainwindow.h"
- #include "modules/HMI/hmieditorwidget.h"
- #include "modules/PLC/plceditorwidget.h"
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QInputDialog>
- #include <QTabBar>
- #include <QDebug>
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- setWindowTitle("综合平台编程器");
- setFixedSize(1000, 740);
- tabWidget = new QTabWidget(this);
- setCentralWidget(tabWidget);
- tabWidget->setTabsClosable(true);
- connect(tabWidget, &QTabWidget::tabCloseRequested, this, &MainWindow::onTabCloseRequested);
- tabWidget->setStyleSheet("QTabWidget { margin: 10px; }");
-
- fileManager = new FileManager(this);
- createMenus();
- }
-
- MainWindow::~MainWindow() {}
-
- void MainWindow::createMenus() {
- fileMenu = menuBar()->addMenu("文件(&F)");
- newAction = new QAction("新建(&N)", this);
- newAction->setShortcut(QKeySequence("Ctrl+N"));
- connect(newAction, &QAction::triggered, this, &MainWindow::onNewFile);
- fileMenu->addAction(newAction);
-
- openAction = new QAction("打开(&O)", this);
- openAction->setShortcut(QKeySequence("Ctrl+O"));
- connect(openAction, &QAction::triggered, this, &MainWindow::onOpenFile);
- fileMenu->addAction(openAction);
-
- saveAction = new QAction("保存(&S)", this);
- saveAction->setShortcut(QKeySequence("Ctrl+S"));
- connect(saveAction, &QAction::triggered, this, &MainWindow::onSaveFile);
- fileMenu->addAction(saveAction);
-
- closeAction = new QAction("关闭(&C)", this);
- closeAction->setShortcut(QKeySequence("Ctrl+W"));
- connect(closeAction, &QAction::triggered, this, &MainWindow::onCloseFile);
- fileMenu->addAction(closeAction);
-
- simulateMenu = menuBar()->addMenu("仿真(&M)");
- simulateAction = new QAction("启动仿真(&R)", this);
- simulateAction->setShortcut(QKeySequence("F5"));
- connect(simulateAction, &QAction::triggered, this, &MainWindow::onSimulate);
- simulateMenu->addAction(simulateAction);
- }
-
- void MainWindow::onTabCloseRequested(int index)
- {
- if (index < 0 || index >= tabWidget->count()) return;
- QWidget* widget = tabWidget->widget(index);
- if (widget) {
- tabWidget->removeTab(index);
- delete widget;
- }
- }
-
- void MainWindow::onCloseFile()
- {
- int currentIndex = tabWidget->currentIndex();
- if (currentIndex >= 0) {
- onTabCloseRequested(currentIndex);
- }
- }
-
- void MainWindow::onNewFile() {
- QStringList types = {"HMI文件", "PLC文件"};
- bool ok;
- QString type = QInputDialog::getItem(this, "选择文件类型", "请选择要创建的文件类型:", types, 0, false, &ok);
- if (ok && !type.isEmpty()) {
- if (type == "HMI文件") {
- HmiEditorWidget* newHmi = new HmiEditorWidget(this);
- tabWidget->addTab(newHmi, "HMI编辑器");
- tabWidget->setCurrentWidget(newHmi);
- qDebug() << "MainWindow: 创建新HMI编辑器,ID:" << newHmi;
- } else if (type == "PLC文件") {
- PlcEditorWidget* newPlc = new PlcEditorWidget(this);
- tabWidget->addTab(newPlc, "PLC编辑器");
- tabWidget->setCurrentWidget(newPlc);
- qDebug() << "MainWindow: 创建新PLC编辑器,ID:" << newPlc;
- }
- }
- }
-
- void MainWindow::onOpenFile() {
- QString filePath = QFileDialog::getOpenFileName(this, "打开文件", "", "HMI文件 (*.hmi);;PLC文件 (*.plc);;所有文件 (*)");
- if (!filePath.isEmpty()) {
- QFileInfo info(filePath);
- if (info.suffix() == "hmi") {
- HmiEditorWidget* hmi = new HmiEditorWidget(this);
- if (fileManager->loadHmiFile(hmi, filePath)) {
- tabWidget->addTab(hmi, info.fileName());
- tabWidget->setCurrentWidget(hmi);
- QMessageBox::information(this, "加载成功", "HMI文件已加载:" + filePath);
- qDebug() << "MainWindow: 打开HMI文件,编辑器ID:" << hmi << "路径:" << filePath;
- } else {
- delete hmi;
- QMessageBox::warning(this, "加载失败", "无法加载HMI文件:" + filePath);
- }
- } else if (info.suffix() == "plc") {
- PlcEditorWidget* plc = new PlcEditorWidget(this);
- if (fileManager->loadPlcFile(plc, filePath)) {
- tabWidget->addTab(plc, info.fileName());
- tabWidget->setCurrentWidget(plc);
- QMessageBox::information(this, "加载成功", "PLC文件已加载:" + filePath);
- qDebug() << "MainWindow: 打开PLC文件,编辑器ID:" << plc << "路径:" << filePath;
- } else {
- delete plc;
- QMessageBox::warning(this, "加载失败", "无法加载PLC文件:" + filePath);
- }
- }
- }
- }
-
- void MainWindow::onSaveFile()
- {
- QWidget* currentWidget = tabWidget->currentWidget();
- if (!currentWidget) {
- QMessageBox::warning(this, "保存失败", "没有打开的文件");
- return;
- }
-
- QString filter;
- bool isHmi = qobject_cast<HmiEditorWidget*>(currentWidget) != nullptr;
- bool isPlc = qobject_cast<PlcEditorWidget*>(currentWidget) != nullptr;
-
- if (isHmi) {
- filter = "HMI文件 (*.hmi)";
- } else if (isPlc) {
- filter = "PLC文件 (*.plc)";
- } else {
- QMessageBox::warning(this, "保存失败", "无法识别的文件类型");
- return;
- }
-
- QString filePath = QFileDialog::getSaveFileName(this, "保存文件", "", filter);
- if (!filePath.isEmpty()) {
- bool success = false;
- if (isHmi) {
- HmiEditorWidget* hmi = qobject_cast<HmiEditorWidget*>(currentWidget);
- success = fileManager->saveHmiFile(hmi, filePath);
- qDebug() << "MainWindow: 保存HMI文件,编辑器ID:" << hmi << "路径:" << filePath << "结果:" << success;
- } else if (isPlc) {
- PlcEditorWidget* plc = qobject_cast<PlcEditorWidget*>(currentWidget);
- success = fileManager->savePlcFile(plc, filePath);
- qDebug() << "MainWindow: 保存PLC文件,编辑器ID:" << plc << "路径:" << filePath << "结果:" << success;
- }
-
- if (success) {
- QMessageBox::information(this, "保存成功", "文件已保存到:" + filePath);
- int currentIndex = tabWidget->currentIndex();
- if (currentIndex >= 0) {
- tabWidget->setTabText(currentIndex, QFileInfo(filePath).fileName());
- }
- } else {
- QMessageBox::warning(this, "保存失败", "无法保存文件:" + filePath);
- }
- }
- }
-
- void MainWindow::onSimulate() {
- QMessageBox::information(this, "仿真", "启动仿真功能\n(实际实现需要根据业务逻辑开发)");
- }
|