#include "WindowMain.h"
#include "ui_WindowMain.h"
#include "Preferences.h"
#include "WindowRuntime.h"
#include "WindowApp.h"
#include "Document.h"
#include "DialogUserMsg.h"
#include "TaskManager.h"
#include "DialogPreferences.h"
#include "DialogNewProject.h"
WindowMain::WindowMain(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::WindowMain)
{
ui->setupUi(this);
// 设置主窗体标题
this->setWindowTitle(thePrefs.m_strProductName );
this->setStyleSheet("QToolButton:hover{background-color:rgb(60, 60, 60);}QToolButton{padding-right:10px;height:30;font:9pt'微软雅黑';}QToolBar::separator{background-color:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 rgb(37, 37, 37),stop:0.33 rgb(37, 33, 37),stop:0.331 rgb(60, 60, 60),stop:0.66 rgb(60, 60, 60),stop:0.661 rgb(37, 37, 37),stop:1 rgb(37, 37, 37));width:2px;height:5px;}QToolBar{font:9pt'微软雅黑';background-color:rgb(37, 37, 37);border:no;}");
//this->setStyleSheet("QTableWidget{padding-left:10px;border:0px;background-color:rgb(37, 37, 37);font:9pt'微软雅黑';}QMainWindow{background-color: rgb(59, 62, 65);font:9pt'微软雅黑';}QWidget#listtool{border:0px;background-color:rgb(37, 37, 37);font:9pt'微软雅黑';}QTreeWidget{font:9pt'微软雅黑';border:0px;background-color:rgb(37, 37, 37);}QListWidget{font:9pt'微软雅黑';border:0px;background-color:rgb(37, 37, 37);}QDockWidget{font:9pt'微软雅黑';color:white;background-color:rgb(37, 37, 37);}QTabBar::tab{width:120px;height:50px;color:white;border-top: 3px;}QTabBar::tab:selected{width:120px;height:50px;color:white;border-top: 3px solid rgb(65, 177, 225);}QTabBar::tab:hover{width:120px;height:50px;color:white;border-top: 3px solid rgb(65, 177, 225);}QTreeView::item::icon{padding-left:10px;}");
thePrefs.Init();
// 创建菜单Action
createMainActions();
// 创建状态栏
createStatusbar();
// 创建界面布局
createLayouts();
g_pDialogUserMsg = new DialogUserMsg(this);
// 向VPGlobal注册
VPGlobal::Register(this);
m_Doc.loadHdw(DOC_HARDWARE_FULLPATH);
}
WindowMain::~WindowMain()
{
thePrefs.Uninit();
m_Doc.saveHdw(DOC_HARDWARE_FULLPATH);
delete ui;
}
//////////////////////////////////////////
// 创建Action
void WindowMain::createMainActions()
{
connect(ui->action_new, &QAction::triggered, this, &WindowMain::onFileNew);
connect(ui->action_open, &QAction::triggered, this, &WindowMain::onFileOpen);
connect(ui->action_save, &QAction::triggered, this, &WindowMain::onFileSave);
// 此处缺一个saveAs的action
// connect(ui->action_save, &QAction::triggered, this, &WindowMain::onFileSaveAs);
//connect(fileExitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->action_runtime, &QAction::triggered, this, &WindowMain::onViewRunTime);
connect(ui->action_app, &QAction::triggered, this, &WindowMain::onViewApp);
connect(ui->action_run, &QAction::triggered, this, &WindowMain::onTaskRun);
connect(ui->action_runonce, &QAction::triggered, this, &WindowMain::onTaskRunonce);
connect(ui->action_stop, &QAction::triggered, this, &WindowMain::onTaskStop);
// Help
helpAboutAction = new QAction(("About"), this);
}
//////////////////////////////////////////////
// 创建状态栏
void WindowMain::createStatusbar()
{
mainStatusBar = ui->statusbar;
mainStatusBar->setStyleSheet("background-color:rgb(9, 101, 90);border:0px;");
statusLabel = new StatusBar;
statusLabel->setLineFixedWidth(true);
statusLabel->start();
mainStatusBar->addWidget(statusLabel);
}
///////////////////////////////////////////////
// 创建界面布局
void WindowMain::createLayouts()
{
// 界面总体垂直布局
layoutSubMain = new QVBoxLayout();
// 各个子窗体
runtimeWindow = new WindowRuntime();
appWindow = new WindowApp();
// 中部窗体切换布局
stackWindows = new QStackedWidget();
stackWindows->addWidget(runtimeWindow);
stackWindows->addWidget(appWindow);
// 默认显示APPe界面
stackWindows->setCurrentIndex(MAIN_VIEW_RUNTIME);
ui->centralwidget->layout()->setMargin(0);
ui->centralwidget->layout()->setSpacing(0);
ui->centralwidget->layout()->addWidget(stackWindows);
}
//////////////////////////////////////////////
// 点击按钮切换窗体
void WindowMain::onViewRunTime()
{
stackWindows->setCurrentIndex(MAIN_VIEW_RUNTIME);
}
//////////////////////////////////////////////
// 点击按钮切换窗体
void WindowMain::onViewApp()
{
stackWindows->setCurrentIndex(MAIN_VIEW_APP);
}
///
/// 菜单 File - New
///
void WindowMain::onFileNew()
{
qDebug() << "WindowMain::onFileNew";
DialogNewProject dlgNewProject;
dlgNewProject.setWindowModality(Qt::WindowModal);
dlgNewProject.m_strProjectName = thePrefs.m_strProjectName;
dlgNewProject.m_strProjectPath = thePrefs.m_strProjectPath;
dlgNewProject.m_strProjectInfo = thePrefs.m_strProjectInfo;
int res = dlgNewProject.exec();
if (res == QDialog::Accepted)
{
thePrefs.m_strProjectName = dlgNewProject.m_strProjectName;
thePrefs.m_strProjectPath = dlgNewProject.m_strProjectPath;
thePrefs.m_strProjectInfo = dlgNewProject.m_strProjectInfo;
// TODO: 1:判断用户是否选择了空项目,如果选择了空项目,即全部数据清空
{
m_Doc.systemReset();
}
// TODO: 2:如果用户未选择空项目,即加载 Default.vpp 文件
if (! dlgNewProject.m_bIsEmptyProject)
{
QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
// Default.vpp 用来预设一些常用配置(如预设一个Task,预设一个POU,预设几个全局变量变量,预设一个UI,并配置好常用的参数)
// Default.vpp 不可直接编辑。防止搞坏预设参数。
QString strPath = runPath + "/Default" + DOC_POSTFIX;
QFileInfo FileInfo(strPath);
if (FileInfo.exists() == true)
{
m_Doc.Load(strPath);
}
else
{
QString strFilePath = FileInfo.absoluteFilePath();
qDebug() << "Open File: " << strFilePath << " Error";
}
}
}
}
///
/// 菜单 File - Open
///
void WindowMain::onFileOpen()
{
// 2021-8-12增加,此处需要先激活一下3号窗体
// 否则由于窗体没有获取到初始化大小而导致界面尺寸不正确
//onViewApp();
qDebug() << "WindowMain::onFileOpen";
QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
QString strFilePath = QFileDialog::getOpenFileName
(
this,
("Open"),
runPath + "//..//Project",
("VisionPlus Files (*.vpp)")
);
// 打开对应文件
if (!strFilePath.isEmpty())
{
m_Doc.Load(strFilePath);
QFileInfo FileInfo(strFilePath);
QString strProjectName = FileInfo.baseName();
QString strProjectPath = FileInfo.path();
thePrefs.m_strProjectName = strProjectName;
thePrefs.m_strProjectPath = strProjectPath;
}
//m_Doc.Load(QString(runPath + "/test") + DOC_POSTFIX);
}
///
/// 菜单 File - Save
///
void WindowMain::onFileSave()
{
//// 如果没有选择路径,则按照另存为的方式保存
//if (!m_Doc.isSelPath())
//{
// onFileSaveAs();
//
// return;
//}
// 如果工程名为缺省,即代表当前工程未保存,需要找个地方保存文件
if (thePrefs.m_strProjectName == "Default")
{
onFileSaveAs();
return;
}
else
{
QString strFilePath = thePrefs.m_strProjectPath + "//" + thePrefs.m_strProjectName;
// 否则直接保存
m_Doc.Save( strFilePath + DOC_POSTFIX);
// 2022-2-24增加,同时保存硬件组态
m_Doc.saveHdw(DOC_HARDWARE_FULLPATH);
// m_Doc.Save(QString("./test") + DOC_POSTFIX);
QFileInfo FileInfo(strFilePath);
QString strProjectName = FileInfo.baseName();
QString strProjectPath = FileInfo.path();
thePrefs.m_strProjectName = strProjectName;
thePrefs.m_strProjectPath = strProjectPath;
}
}
///
/// 菜单 File - Save As
///
void WindowMain::onFileSaveAs()
{
//qDebug() << "WindowMain::onFileSave";
QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
QString strFileName = "Project_" + QDateTime::currentDateTime().toString("[yyyy-MM-dd hh-mm]");
QString strPath = runPath + "//..//Project//" + strFileName;
QString strFilePath = QFileDialog::getSaveFileName
(
this,
"Save As",
strPath,
("VisionPlus Files (*.vpp)")
);
// 如果选定了文件名
if (!strFilePath.isNull())
{
// m_Doc.setSavePath(strFilePath);
m_Doc.Save(strFilePath);
// 2022-2-24增加,同时保存硬件组态
m_Doc.saveHdw(DOC_HARDWARE_FULLPATH);
QFileInfo FileInfo(strFilePath);
QString strProjectName = FileInfo.baseName();
QString strProjectPath = FileInfo.path();
thePrefs.m_strProjectName = strProjectName;
thePrefs.m_strProjectPath = strProjectPath;
}
}
///
/// task - Run
///
void WindowMain::onTaskRun()
{
// 启动所有Task的执行
g_pTaskManager->executeAllTasks(false);
}
///
/// task - Runonce
///
void WindowMain::onTaskRunonce()
{
// 启动所有Task的执行一次
g_pTaskManager->executeAllTasks(true);
}
///
/// task - Stop
///
void WindowMain::onTaskStop()
{
// 停止所有任务
g_pTaskManager->stopAllTask(false);
}
///
/// 系统
///
void WindowMain::onPreferences()
{
DialogPreferences dlg;
int res = dlg.exec();
if (res != QDialog::Accepted)
{
return;
}
}
void WindowMain::onAppInfo()
{
}