123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- #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);
- }
- /// <summary>
- /// 菜单 File - New
- /// </summary>
- 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";
- }
- }
- }
- }
- /// <summary>
- /// 菜单 File - Open
- /// </summary>
- 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);
- }
- /// <summary>
- /// 菜单 File - Save
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 菜单 File - Save As
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// task - Run
- /// </summary>
- void WindowMain::onTaskRun()
- {
- // 启动所有Task的执行
- g_pTaskManager->executeAllTasks(false);
- }
- /// <summary>
- /// task - Runonce
- /// </summary>
- void WindowMain::onTaskRunonce()
- {
- // 启动所有Task的执行一次
- g_pTaskManager->executeAllTasks(true);
- }
- /// <summary>
- /// task - Stop
- /// </summary>
- void WindowMain::onTaskStop()
- {
- // 停止所有任务
- g_pTaskManager->stopAllTask(false);
- }
- /// <summary>
- /// 系统
- /// </summary>
- void WindowMain::onPreferences()
- {
- DialogPreferences dlg;
- int res = dlg.exec();
- if (res != QDialog::Accepted)
- {
- return;
- }
- }
- void WindowMain::onAppInfo()
- {
- }
|