|
@@ -21,8 +21,6 @@ MainWindow::MainWindow(QWidget *parent) |
|
|
, modbusManager(new ModbusManager(registerManager, this)) |
|
|
, modbusManager(new ModbusManager(registerManager, this)) |
|
|
{ |
|
|
{ |
|
|
ui->setupUi(this); |
|
|
ui->setupUi(this); |
|
|
// registerManager = new RegisterManager(this); |
|
|
|
|
|
// modbusManager = new ModbusManager(registerManager, this); |
|
|
|
|
|
// 初始化PLC标签页 |
|
|
// 初始化PLC标签页 |
|
|
ui->plc_tab_widget->setTabsClosable(true); |
|
|
ui->plc_tab_widget->setTabsClosable(true); |
|
|
ui->plc_tab_widget->setMovable(true); |
|
|
ui->plc_tab_widget->setMovable(true); |
|
@@ -42,8 +40,8 @@ MainWindow::MainWindow(QWidget *parent) |
|
|
|
|
|
|
|
|
createComponents(); |
|
|
createComponents(); |
|
|
|
|
|
|
|
|
connect(ui->plc_tab_widget, &QTabWidget::tabCloseRequested,this,&MainWindow::onTabCloseRequested); |
|
|
|
|
|
connect(ui->hmi_tab_widget, &QTabWidget::tabCloseRequested,this,&MainWindow::onTabCloseRequested); |
|
|
|
|
|
|
|
|
connect(ui->plc_tab_widget, &QTabWidget::tabCloseRequested, this, &MainWindow::onTabCloseRequested); |
|
|
|
|
|
connect(ui->hmi_tab_widget, &QTabWidget::tabCloseRequested, this, &MainWindow::onTabCloseRequested); |
|
|
connect(ui->listWidget,&QListWidget::currentTextChanged,this,&MainWindow::onListwidgetCurrenttextchanged); |
|
|
connect(ui->listWidget,&QListWidget::currentTextChanged,this,&MainWindow::onListwidgetCurrenttextchanged); |
|
|
|
|
|
|
|
|
connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange); |
|
|
connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange); |
|
@@ -64,7 +62,7 @@ MainWindow::MainWindow(QWidget *parent) |
|
|
// this, &PLC::handleModbusError); |
|
|
// this, &PLC::handleModbusError); |
|
|
|
|
|
|
|
|
ui->stackedWidget->setCurrentIndex(0); |
|
|
ui->stackedWidget->setCurrentIndex(0); |
|
|
setWindowTitle("未命名项目 - PLC编辑器"); |
|
|
|
|
|
|
|
|
setWindowTitle("PLC编辑器"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
MainWindow::~MainWindow() |
|
|
MainWindow::~MainWindow() |
|
@@ -90,6 +88,7 @@ void MainWindow::newPage(bool isPlc) |
|
|
if (isPlc) { |
|
|
if (isPlc) { |
|
|
// 添加到PLC标签页 |
|
|
// 添加到PLC标签页 |
|
|
int newIndex = ui->plc_tab_widget->addTab(newView, QString("页面 %1").arg(plcScenes.size() + 1)); |
|
|
int newIndex = ui->plc_tab_widget->addTab(newView, QString("页面 %1").arg(plcScenes.size() + 1)); |
|
|
|
|
|
plcPageTitles.append(""); // 初始化为空字符串 |
|
|
ui->plc_tab_widget->setCurrentIndex(newIndex); |
|
|
ui->plc_tab_widget->setCurrentIndex(newIndex); |
|
|
|
|
|
|
|
|
// 保存场景和视图 |
|
|
// 保存场景和视图 |
|
@@ -102,6 +101,7 @@ void MainWindow::newPage(bool isPlc) |
|
|
} else { |
|
|
} else { |
|
|
// 添加到HMI标签页 |
|
|
// 添加到HMI标签页 |
|
|
int newIndex = ui->hmi_tab_widget->addTab(newView, QString("页面 %1").arg(hmiScenes.size() + 1)); |
|
|
int newIndex = ui->hmi_tab_widget->addTab(newView, QString("页面 %1").arg(hmiScenes.size() + 1)); |
|
|
|
|
|
hmiPageTitles.append(""); // 初始化为空字符串 |
|
|
ui->hmi_tab_widget->setCurrentIndex(newIndex); |
|
|
ui->hmi_tab_widget->setCurrentIndex(newIndex); |
|
|
|
|
|
|
|
|
// 保存场景和视图 |
|
|
// 保存场景和视图 |
|
@@ -411,7 +411,7 @@ void MainWindow::plcChange() |
|
|
currentIsPLC_ = true; |
|
|
currentIsPLC_ = true; |
|
|
createComponents(); |
|
|
createComponents(); |
|
|
ui->stackedWidget->setCurrentIndex(0); // 显示PLC页面 |
|
|
ui->stackedWidget->setCurrentIndex(0); // 显示PLC页面 |
|
|
// setWindowTitle((plcFilePath_.isEmpty() ? "未命名项目" : QFileInfo(plcFilePath_).fileName()) + " - PLC编辑器"); |
|
|
|
|
|
|
|
|
setWindowTitle("PLC编辑器"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -420,18 +420,30 @@ void MainWindow::hmiChange() |
|
|
currentIsPLC_ = false; |
|
|
currentIsPLC_ = false; |
|
|
createComponents(); |
|
|
createComponents(); |
|
|
ui->stackedWidget->setCurrentIndex(1); // 显示HMI页面 |
|
|
ui->stackedWidget->setCurrentIndex(1); // 显示HMI页面 |
|
|
// setWindowTitle((hmiFilePath_.isEmpty() ? "未命名项目" : QFileInfo(hmiFilePath_).fileName()) + " - HMI编辑器"); |
|
|
|
|
|
|
|
|
setWindowTitle("HMI编辑器"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void MainWindow::newProject() |
|
|
void MainWindow::newProject() |
|
|
{ |
|
|
{ |
|
|
|
|
|
// 检查当前tabwidget是否只有一个页面且页面内容为空 |
|
|
|
|
|
if (currentIsPLC_ && plcScenes.size() == 1 && plcScenes[0]->items().isEmpty()) { |
|
|
|
|
|
// 复用第一个页面 |
|
|
|
|
|
clearScene(); |
|
|
|
|
|
setWindowTitle("PLC编辑器"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if (!currentIsPLC_ && hmiScenes.size() == 1 && hmiScenes[0]->items().isEmpty()) { |
|
|
|
|
|
clearScene(); |
|
|
|
|
|
setWindowTitle("HMI编辑器"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
newPage(currentIsPLC_); |
|
|
newPage(currentIsPLC_); |
|
|
|
|
|
|
|
|
// 更新窗口标题 |
|
|
// 更新窗口标题 |
|
|
if (currentIsPLC_) { |
|
|
if (currentIsPLC_) { |
|
|
setWindowTitle("未命名项目 - PLC编辑器"); |
|
|
|
|
|
|
|
|
setWindowTitle("PLC编辑器"); |
|
|
} else { |
|
|
} else { |
|
|
setWindowTitle("未命名项目 - HMI编辑器"); |
|
|
|
|
|
|
|
|
setWindowTitle("HMI编辑器"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -452,16 +464,31 @@ void MainWindow::openProject() |
|
|
|
|
|
|
|
|
Project project; |
|
|
Project project; |
|
|
if (project.loadFromFile(filePath)) { |
|
|
if (project.loadFromFile(filePath)) { |
|
|
// 在当前模块新建一个页面 |
|
|
|
|
|
newPage(currentIsPLC_); |
|
|
|
|
|
int newPageIndex = currentPageIndex(); |
|
|
|
|
|
|
|
|
// // 在当前模块新建一个页面 |
|
|
|
|
|
// newPage(currentIsPLC_); |
|
|
|
|
|
// int newPageIndex = currentPageIndex(); |
|
|
|
|
|
int newPageIndex = -1; |
|
|
|
|
|
if (currentIsPLC_ && plcScenes.size() == 1 && plcScenes[0]->items().isEmpty()) { |
|
|
|
|
|
newPageIndex = 0; |
|
|
|
|
|
} else if (!currentIsPLC_ && hmiScenes.size() == 1 && hmiScenes[0]->items().isEmpty()) { |
|
|
|
|
|
newPageIndex = 0; |
|
|
|
|
|
} else { |
|
|
|
|
|
newPage(currentIsPLC_); |
|
|
|
|
|
newPageIndex = currentPageIndex(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 将项目应用到新页面 |
|
|
// 将项目应用到新页面 |
|
|
applyProjectToScene(project, newPageIndex); |
|
|
applyProjectToScene(project, newPageIndex); |
|
|
|
|
|
|
|
|
// 更新窗口标题 |
|
|
|
|
|
setWindowTitle(QFileInfo(filePath).fileName() + |
|
|
|
|
|
(currentIsPLC_ ? " - PLC编辑器" : " - HMI编辑器")); |
|
|
|
|
|
|
|
|
// 设置标签页标题为文件名 |
|
|
|
|
|
QString fileName = QFileInfo(filePath).fileName(); |
|
|
|
|
|
if (currentIsPLC_) { |
|
|
|
|
|
plcPageTitles[newPageIndex] = fileName; |
|
|
|
|
|
ui->plc_tab_widget->setTabText(newPageIndex, fileName); |
|
|
|
|
|
} else { |
|
|
|
|
|
hmiPageTitles[newPageIndex] = fileName; |
|
|
|
|
|
ui->hmi_tab_widget->setTabText(newPageIndex, fileName); |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
QMessageBox::critical(this, "错误", "无法打开项目文件"); |
|
|
QMessageBox::critical(this, "错误", "无法打开项目文件"); |
|
|
} |
|
|
} |
|
@@ -495,9 +522,17 @@ void MainWindow::saveProject() |
|
|
extractSceneToProject(project, currentPageIndex()); |
|
|
extractSceneToProject(project, currentPageIndex()); |
|
|
|
|
|
|
|
|
if (project.saveToFile(filePath)) { |
|
|
if (project.saveToFile(filePath)) { |
|
|
// 更新窗口标题 |
|
|
|
|
|
setWindowTitle(QFileInfo(filePath).fileName() + |
|
|
|
|
|
(currentIsPLC_ ? " - PLC编辑器" : " - HMI编辑器")); |
|
|
|
|
|
|
|
|
// 设置标签页标题为文件名 |
|
|
|
|
|
QString fileName = QFileInfo(filePath).fileName(); |
|
|
|
|
|
int currentIndex = currentPageIndex(); |
|
|
|
|
|
|
|
|
|
|
|
if (currentIsPLC_) { |
|
|
|
|
|
plcPageTitles[currentIndex] = fileName; |
|
|
|
|
|
ui->plc_tab_widget->setTabText(currentIndex, fileName); |
|
|
|
|
|
} else { |
|
|
|
|
|
hmiPageTitles[currentIndex] = fileName; |
|
|
|
|
|
ui->hmi_tab_widget->setTabText(currentIndex, fileName); |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
QMessageBox::critical(this, "错误", "保存项目失败"); |
|
|
QMessageBox::critical(this, "错误", "保存项目失败"); |
|
|
} |
|
|
} |
|
@@ -534,22 +569,19 @@ void MainWindow::onListwidgetCurrenttextchanged(const QString ¤tText) |
|
|
|
|
|
|
|
|
void MainWindow::onTabCloseRequested(int index) |
|
|
void MainWindow::onTabCloseRequested(int index) |
|
|
{ |
|
|
{ |
|
|
if (currentIsPLC_) { |
|
|
|
|
|
if (plcScenes.size() > 1) { |
|
|
|
|
|
delete plcScenes.takeAt(index); |
|
|
|
|
|
delete plcViews.takeAt(index); |
|
|
|
|
|
ui->plc_tab_widget->removeTab(index); |
|
|
|
|
|
} else { |
|
|
|
|
|
QMessageBox::information(this, "提示", "至少保留一个PLC页面"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (plcScenes.size() == 1) { |
|
|
|
|
|
QMessageBox::information(this, "提示", "至少保留一个页面"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if (currentIsPLC_) |
|
|
|
|
|
{ |
|
|
|
|
|
plcPageTitles.removeAt(index); |
|
|
|
|
|
delete plcScenes.takeAt(index); |
|
|
|
|
|
delete plcViews.takeAt(index); |
|
|
} else { |
|
|
} else { |
|
|
if (hmiScenes.size() > 1) { |
|
|
|
|
|
delete hmiScenes.takeAt(index); |
|
|
|
|
|
delete hmiViews.takeAt(index); |
|
|
|
|
|
ui->hmi_tab_widget->removeTab(index); |
|
|
|
|
|
} else { |
|
|
|
|
|
QMessageBox::information(this, "提示", "至少保留一个HMI页面"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
hmiPageTitles.removeAt(index); |
|
|
|
|
|
delete hmiScenes.takeAt(index); |
|
|
|
|
|
delete hmiViews.takeAt(index); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|