|
- #include "mainwindow.h"
- #include <QToolBar>
- #include <QToolButton>
- #include <QVBoxLayout>
- #include <QButtonGroup>
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QCloseEvent>
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui_(new Ui::MainWindow)
- {
- ui_->setupUi(this);
- hmi_ = new HMIModule(ui_, this);
- hmi_->init();
-
- initMainWindow();
- // 连接 HMIModule 的信号到 MainWindow 的槽
- connect(hmi_, &HMIModule::logMessageGenerated, this, &MainWindow::appendLog);
-
- // 新建/打开/保存文件
- connect(ui_->actionNew, &QAction::triggered, this, &MainWindow::onNewFile);
- connect(ui_->actionOpen, &QAction::triggered, this, &MainWindow::onOpenFile);
- connect(ui_->actionSave, &QAction::triggered, this, &MainWindow::onSaveFile);
- }
-
- MainWindow::~MainWindow()
- {
- delete ui_;
- }
-
- void MainWindow::initMainWindow()
- {
- setWindowTitle("综合平台编辑器");
- setWindowIcon(QIcon(":/resource/image/editor.png"));
- // 运行默认打开HMI页面
- ui_->tabWidget->setCurrentIndex(1);
- }
-
- // 实现槽函数
- void MainWindow::appendLog(const QString& message)
- {
- ui_->textEdit->append(message);
- }
-
- // 新建菜单动作槽函数
- void MainWindow::onNewFile()
- {
- if (!hmi_)
- {
- return;
- }
-
- if (hmi_->isModified())
- {
- auto ret = QMessageBox::warning(this, "提示",
- "当前文件有未保存的更改,是否保存?",
- QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
-
- if (ret == QMessageBox::Save)
- {
- QString fileName = QFileDialog::getSaveFileName(this, "保存HMI文件", "", "HMI文件 (*.json)");
- if (!fileName.isEmpty())
- {
- if (!hmi_->saveToFile(fileName))
- {
- QMessageBox::warning(this, "保存失败", "文件保存失败!");
- return; // 不新建,保存失败
- }
- }
- else
- {
- return; // 取消保存,取消新建
- }
- }
- else if (ret == QMessageBox::Cancel)
- {
- return; // 取消新建
- }
- // 如果是Discard,直接继续新建,丢弃当前修改
- }
- // 清空编辑区,新建页面
- hmi_->resetPages();
- }
-
- void MainWindow::onOpenFile()
- {
- QString fileName = QFileDialog::getOpenFileName(this, "打开HMI文件", "", "HMI文件 (*.hmi)");
- if (!fileName.isEmpty())
- {
- if (!hmi_->openFromFile(fileName))
- {
- QMessageBox::warning(this, "打开失败", "文件打开失败或格式不正确!");
- }
- }
- }
-
- void MainWindow::onSaveFile()
- {
-
- QString fileName = QFileDialog::getSaveFileName(this, "保存HMI文件", "", "HMI文件 (*.hmi)");
- if (!fileName.isEmpty())
- {
- if (!hmi_->saveToFile(fileName))
- {
- QMessageBox::warning(this, "保存失败", "文件保存失败!");
- }
- }
- }
|