WindowMain.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #include "WindowMain.h"
  2. #include "ui_WindowMain.h"
  3. #include "Preferences.h"
  4. #include "WindowRuntime.h"
  5. #include "WindowApp.h"
  6. #include "Document.h"
  7. #include "DialogUserMsg.h"
  8. #include "TaskManager.h"
  9. #include "DialogPreferences.h"
  10. #include "DialogNewProject.h"
  11. WindowMain::WindowMain(QWidget *parent)
  12. : QMainWindow(parent)
  13. , ui(new Ui::WindowMain)
  14. {
  15. ui->setupUi(this);
  16. // 设置主窗体标题
  17. this->setWindowTitle(thePrefs.m_strProductName );
  18. 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;}");
  19. //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;}");
  20. thePrefs.Init();
  21. // 创建菜单Action
  22. createMainActions();
  23. // 创建状态栏
  24. createStatusbar();
  25. // 创建界面布局
  26. createLayouts();
  27. g_pDialogUserMsg = new DialogUserMsg(this);
  28. // 向VPGlobal注册
  29. VPGlobal::Register(this);
  30. m_Doc.loadHdw(DOC_HARDWARE_FULLPATH);
  31. }
  32. WindowMain::~WindowMain()
  33. {
  34. thePrefs.Uninit();
  35. m_Doc.saveHdw(DOC_HARDWARE_FULLPATH);
  36. delete ui;
  37. }
  38. //////////////////////////////////////////
  39. // 创建Action
  40. void WindowMain::createMainActions()
  41. {
  42. connect(ui->action_new, &QAction::triggered, this, &WindowMain::onFileNew);
  43. connect(ui->action_open, &QAction::triggered, this, &WindowMain::onFileOpen);
  44. connect(ui->action_save, &QAction::triggered, this, &WindowMain::onFileSave);
  45. // 此处缺一个saveAs的action
  46. // connect(ui->action_save, &QAction::triggered, this, &WindowMain::onFileSaveAs);
  47. //connect(fileExitAction, SIGNAL(triggered()), this, SLOT(close()));
  48. connect(ui->action_runtime, &QAction::triggered, this, &WindowMain::onViewRunTime);
  49. connect(ui->action_app, &QAction::triggered, this, &WindowMain::onViewApp);
  50. connect(ui->action_run, &QAction::triggered, this, &WindowMain::onTaskRun);
  51. connect(ui->action_runonce, &QAction::triggered, this, &WindowMain::onTaskRunonce);
  52. connect(ui->action_stop, &QAction::triggered, this, &WindowMain::onTaskStop);
  53. // Help
  54. helpAboutAction = new QAction(("About"), this);
  55. }
  56. //////////////////////////////////////////////
  57. // 创建状态栏
  58. void WindowMain::createStatusbar()
  59. {
  60. mainStatusBar = ui->statusbar;
  61. mainStatusBar->setStyleSheet("background-color:rgb(9, 101, 90);border:0px;");
  62. statusLabel = new StatusBar;
  63. statusLabel->setLineFixedWidth(true);
  64. statusLabel->start();
  65. mainStatusBar->addWidget(statusLabel);
  66. }
  67. ///////////////////////////////////////////////
  68. // 创建界面布局
  69. void WindowMain::createLayouts()
  70. {
  71. // 界面总体垂直布局
  72. layoutSubMain = new QVBoxLayout();
  73. // 各个子窗体
  74. runtimeWindow = new WindowRuntime();
  75. appWindow = new WindowApp();
  76. // 中部窗体切换布局
  77. stackWindows = new QStackedWidget();
  78. stackWindows->addWidget(runtimeWindow);
  79. stackWindows->addWidget(appWindow);
  80. // 默认显示APPe界面
  81. stackWindows->setCurrentIndex(MAIN_VIEW_RUNTIME);
  82. ui->centralwidget->layout()->setMargin(0);
  83. ui->centralwidget->layout()->setSpacing(0);
  84. ui->centralwidget->layout()->addWidget(stackWindows);
  85. }
  86. //////////////////////////////////////////////
  87. // 点击按钮切换窗体
  88. void WindowMain::onViewRunTime()
  89. {
  90. stackWindows->setCurrentIndex(MAIN_VIEW_RUNTIME);
  91. }
  92. //////////////////////////////////////////////
  93. // 点击按钮切换窗体
  94. void WindowMain::onViewApp()
  95. {
  96. stackWindows->setCurrentIndex(MAIN_VIEW_APP);
  97. }
  98. /// <summary>
  99. /// 菜单 File - New
  100. /// </summary>
  101. void WindowMain::onFileNew()
  102. {
  103. qDebug() << "WindowMain::onFileNew";
  104. DialogNewProject dlgNewProject;
  105. dlgNewProject.setWindowModality(Qt::WindowModal);
  106. dlgNewProject.m_strProjectName = thePrefs.m_strProjectName;
  107. dlgNewProject.m_strProjectPath = thePrefs.m_strProjectPath;
  108. dlgNewProject.m_strProjectInfo = thePrefs.m_strProjectInfo;
  109. int res = dlgNewProject.exec();
  110. if (res == QDialog::Accepted)
  111. {
  112. thePrefs.m_strProjectName = dlgNewProject.m_strProjectName;
  113. thePrefs.m_strProjectPath = dlgNewProject.m_strProjectPath;
  114. thePrefs.m_strProjectInfo = dlgNewProject.m_strProjectInfo;
  115. // TODO: 1:判断用户是否选择了空项目,如果选择了空项目,即全部数据清空
  116. {
  117. m_Doc.systemReset();
  118. }
  119. // TODO: 2:如果用户未选择空项目,即加载 Default.vpp 文件
  120. if (! dlgNewProject.m_bIsEmptyProject)
  121. {
  122. QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
  123. // Default.vpp 用来预设一些常用配置(如预设一个Task,预设一个POU,预设几个全局变量变量,预设一个UI,并配置好常用的参数)
  124. // Default.vpp 不可直接编辑。防止搞坏预设参数。
  125. QString strPath = runPath + "/Default" + DOC_POSTFIX;
  126. QFileInfo FileInfo(strPath);
  127. if (FileInfo.exists() == true)
  128. {
  129. m_Doc.Load(strPath);
  130. }
  131. else
  132. {
  133. QString strFilePath = FileInfo.absoluteFilePath();
  134. qDebug() << "Open File: " << strFilePath << " Error";
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// 菜单 File - Open
  141. /// </summary>
  142. void WindowMain::onFileOpen()
  143. {
  144. // 2021-8-12增加,此处需要先激活一下3号窗体
  145. // 否则由于窗体没有获取到初始化大小而导致界面尺寸不正确
  146. //onViewApp();
  147. qDebug() << "WindowMain::onFileOpen";
  148. QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
  149. QString strFilePath = QFileDialog::getOpenFileName
  150. (
  151. this,
  152. ("Open"),
  153. runPath + "//..//Project",
  154. ("VisionPlus Files (*.vpp)")
  155. );
  156. // 打开对应文件
  157. if (!strFilePath.isEmpty())
  158. {
  159. m_Doc.Load(strFilePath);
  160. QFileInfo FileInfo(strFilePath);
  161. QString strProjectName = FileInfo.baseName();
  162. QString strProjectPath = FileInfo.path();
  163. thePrefs.m_strProjectName = strProjectName;
  164. thePrefs.m_strProjectPath = strProjectPath;
  165. }
  166. //m_Doc.Load(QString(runPath + "/test") + DOC_POSTFIX);
  167. }
  168. /// <summary>
  169. /// 菜单 File - Save
  170. /// </summary>
  171. void WindowMain::onFileSave()
  172. {
  173. //// 如果没有选择路径,则按照另存为的方式保存
  174. //if (!m_Doc.isSelPath())
  175. //{
  176. // onFileSaveAs();
  177. //
  178. // return;
  179. //}
  180. // 如果工程名为缺省,即代表当前工程未保存,需要找个地方保存文件
  181. if (thePrefs.m_strProjectName == "Default")
  182. {
  183. onFileSaveAs();
  184. return;
  185. }
  186. else
  187. {
  188. QString strFilePath = thePrefs.m_strProjectPath + "//" + thePrefs.m_strProjectName;
  189. // 否则直接保存
  190. m_Doc.Save( strFilePath + DOC_POSTFIX);
  191. // 2022-2-24增加,同时保存硬件组态
  192. m_Doc.saveHdw(DOC_HARDWARE_FULLPATH);
  193. // m_Doc.Save(QString("./test") + DOC_POSTFIX);
  194. QFileInfo FileInfo(strFilePath);
  195. QString strProjectName = FileInfo.baseName();
  196. QString strProjectPath = FileInfo.path();
  197. thePrefs.m_strProjectName = strProjectName;
  198. thePrefs.m_strProjectPath = strProjectPath;
  199. }
  200. }
  201. /// <summary>
  202. /// 菜单 File - Save As
  203. /// </summary>
  204. void WindowMain::onFileSaveAs()
  205. {
  206. //qDebug() << "WindowMain::onFileSave";
  207. QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
  208. QString strFileName = "Project_" + QDateTime::currentDateTime().toString("[yyyy-MM-dd hh-mm]");
  209. QString strPath = runPath + "//..//Project//" + strFileName;
  210. QString strFilePath = QFileDialog::getSaveFileName
  211. (
  212. this,
  213. "Save As",
  214. strPath,
  215. ("VisionPlus Files (*.vpp)")
  216. );
  217. // 如果选定了文件名
  218. if (!strFilePath.isNull())
  219. {
  220. // m_Doc.setSavePath(strFilePath);
  221. m_Doc.Save(strFilePath);
  222. // 2022-2-24增加,同时保存硬件组态
  223. m_Doc.saveHdw(DOC_HARDWARE_FULLPATH);
  224. QFileInfo FileInfo(strFilePath);
  225. QString strProjectName = FileInfo.baseName();
  226. QString strProjectPath = FileInfo.path();
  227. thePrefs.m_strProjectName = strProjectName;
  228. thePrefs.m_strProjectPath = strProjectPath;
  229. }
  230. }
  231. /// <summary>
  232. /// task - Run
  233. /// </summary>
  234. void WindowMain::onTaskRun()
  235. {
  236. // 启动所有Task的执行
  237. g_pTaskManager->executeAllTasks(false);
  238. }
  239. /// <summary>
  240. /// task - Runonce
  241. /// </summary>
  242. void WindowMain::onTaskRunonce()
  243. {
  244. // 启动所有Task的执行一次
  245. g_pTaskManager->executeAllTasks(true);
  246. }
  247. /// <summary>
  248. /// task - Stop
  249. /// </summary>
  250. void WindowMain::onTaskStop()
  251. {
  252. // 停止所有任务
  253. g_pTaskManager->stopAllTask(false);
  254. }
  255. /// <summary>
  256. /// 系统
  257. /// </summary>
  258. void WindowMain::onPreferences()
  259. {
  260. DialogPreferences dlg;
  261. int res = dlg.exec();
  262. if (res != QDialog::Accepted)
  263. {
  264. return;
  265. }
  266. }
  267. void WindowMain::onAppInfo()
  268. {
  269. }