|
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include "item.h"
-
- #include <QListWidgetItem>
- #include <QMimeData>
- #include <QDrag>
- #include <QPainter>
- #include <QMouseEvent>
- #include <QDropEvent>
- #include <QGraphicsView>
- #include <QMessageBox>
- #include <QAction>
- #include <QFileDialog>
- #include "creatitem.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- registerManager = new RegisterManager(this);
- plc_ = new PLC(registerManager, ui->widget);
- hmi_ = new HMI(ui->widget);
- modbusManager = new ModbusManager(registerManager, this);
- plc_->show();
- hmi_->hide();
- connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange);
- connect(ui->action_hmi,&QAction::triggered,this,&MainWindow::hmiChange);
-
- connect(ui->action_new, &QAction::triggered, this, &MainWindow::newProject);
- connect(ui->action_save, &QAction::triggered, this, &MainWindow::saveProject);
- connect(ui->action_open, &QAction::triggered, this, &MainWindow::openProject);
- connect(ui->action_connect, &QAction::triggered, this, &MainWindow::connection);
- connect(ui->action_disconnect, &QAction::triggered, this, &MainWindow::disconnection);
-
- connect(modbusManager, &ModbusManager::connectionStatusChanged,
- plc_, &PLC::updateConnectionStatus);
-
-
- plc_->applyProjectToScene(plcProject_); // 初始空
- hmi_->applyProjectToScene(hmiProject_);
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::plcChange()
- {
- plc_->show();
- hmi_->hide();
- currentIsPLC_ = true;
- setWindowTitle((plcFilePath_.isEmpty() ? "未命名项目" : QFileInfo(plcFilePath_).fileName()) + " - PLC编辑器");
- }
-
- void MainWindow::hmiChange()
- {
- hmi_->show();
- plc_->hide();
- currentIsPLC_ = false;
- setWindowTitle((hmiFilePath_.isEmpty() ? "未命名项目" : QFileInfo(hmiFilePath_).fileName()) + " - HMI编辑器");
- }
-
- void MainWindow::newProject()
- {
- if (currentIsPLC_) {
- plcProject_.clear();
- plcFilePath_.clear();
- plc_->clearScene();
- setWindowTitle("未命名项目 - PLC编辑器");
- } else {
- hmiProject_.clear();
- hmiFilePath_.clear();
- hmi_->clearScene();
- setWindowTitle("未命名项目 - HMI编辑器");
- }
- }
-
- void MainWindow::openProject()
- {
- if (currentIsPLC_)
- {
- QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.plcproj)");
- if (filePath.isEmpty()) return;
- if (plcProject_.loadFromFile(filePath))
- {
- plcFilePath_ = filePath;
- plc_->applyProjectToScene(plcProject_);
- setWindowTitle(QFileInfo(plcFilePath_).fileName() + " - PLC编辑器");
- }
- else
- {
- QMessageBox::critical(this, "错误", "无法打开PLC项目文件");
- }
- }
- else
- {
- QString filePath = QFileDialog::getOpenFileName(this, "打开项目", "", "项目文件 (*.hmiproj)");
- if (filePath.isEmpty()) return;
- if (hmiProject_.loadFromFile(filePath))
- {
- hmiFilePath_ = filePath;
- hmi_->applyProjectToScene(hmiProject_);
- setWindowTitle(QFileInfo(hmiFilePath_).fileName() + " - HMI编辑器");
- }
- else
- {
- QMessageBox::critical(this, "错误", "无法打开HMI项目文件");
- }
- }
- }
-
- void MainWindow::saveProject()
- {
- if (currentIsPLC_) {
- if (plcFilePath_.isEmpty()) {
- QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "PLC项目文件 (*.plcproj)");
- if (filePath.isEmpty()) return;
- if (!filePath.endsWith(".plcproj", Qt::CaseInsensitive))
- filePath += ".plcproj";
- plcFilePath_ = filePath;
- }
- plc_->extractSceneToProject(plcProject_);
- if (!plcProject_.saveToFile(plcFilePath_)) {
- QMessageBox::critical(this, "错误", "保存项目失败");
- }
- } else {
- if (hmiFilePath_.isEmpty()) {
- QString filePath = QFileDialog::getSaveFileName(this, "保存项目", "", "HMI项目文件 (*.hmiproj)");
- if (filePath.isEmpty()) return;
- if (!filePath.endsWith(".hmiproj", Qt::CaseInsensitive))
- filePath += ".hmiproj";
- hmiFilePath_ = filePath;
- }
- hmi_->extractSceneToProject(hmiProject_);
- if (!hmiProject_.saveToFile(hmiFilePath_)) {
- QMessageBox::critical(this, "错误", "保存项目失败");
- }
- }
- }
-
- void MainWindow::connection()
- {
- modbusManager->connectToDevice("COM1",QSerialPort::BaudRate(9600),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1));
- modbusManager->startSimulation(2000);
- }
-
- void MainWindow::disconnection()
- {
- modbusManager->disconnectDevice();
- modbusManager->stopSimulation();
- }
|