WindowAppMdiFrame.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include "WindowAppMdiFrame.h"
  2. #include "WindowAppMdiSubContainer.h"
  3. #include "WindowAppGvlView.h"
  4. #include "WindowAppPouFrame.h"
  5. #include "WindowAppTaskView.h"
  6. #include "WindowAppTaskMonitorView.h"
  7. #include "WindowAppLogView.h"
  8. #include "WindowAppUiFrame.h"
  9. #include "WindowAppResourceManagerTree.h"
  10. #include "Pou.h"
  11. WindowAppMdiFrame::WindowAppMdiFrame()
  12. {
  13. // 初始化UI
  14. this->initUI();
  15. VPGlobal::Register(this);
  16. }
  17. WindowAppMdiFrame::~WindowAppMdiFrame()
  18. {
  19. }
  20. /// <summary>
  21. /// 初始化UI
  22. /// </summary>
  23. void WindowAppMdiFrame::initUI()
  24. {
  25. // 设置自身的风格
  26. // Tab方式
  27. this->setViewMode(QMdiArea::TabbedView);
  28. // 是否可以关闭子窗口
  29. this->setTabsClosable(true);
  30. // 采用梯形标题栏
  31. //this->setTabShape(QTabWidget::Triangular);
  32. // 设置标题栏的方向是在正上方
  33. this->setTabPosition(QTabWidget::North);
  34. // Tab是否可以拖动调整顺序
  35. this->setTabsMovable(true);
  36. }
  37. /// <summary>
  38. /// 根据类型添加对应类型的窗体
  39. /// </summary>
  40. /// <param name="strTitle"></param>
  41. /// <param name="viewType"></param>
  42. QWidget* WindowAppMdiFrame::addSubView(QString strTitle, SUBVIEW_TYPE viewType, bool bActive)
  43. {
  44. // 创建子视图的容器( SubWindow )
  45. WindowAppMdiSubContainer* subViewContainer = new WindowAppMdiSubContainer(strTitle);
  46. // 根据不同的类型创建对应视图
  47. QWidget* newView = nullptr;
  48. // QToolBar* viewToolBar = nullptr;
  49. if (viewType == SUBVIEW_TYPE::SUBVIEW_POU)
  50. {
  51. // 创建Pou视图
  52. newView = new WindowAppPouFrame(strTitle, subViewContainer);
  53. //// 2022-8-29 增加,注册PouFrame指针便于后续统一管理
  54. //g_pFrameManager->registerNewPouFrame(strTitle, (WindowAppPouFrame*)newView);
  55. }
  56. // 2022-2-16 Gvl视图有更多的参数,另外独立出来了
  57. //else if (viewType == SUBVIEW_TYPE::SUBVIEW_GVL)
  58. //{
  59. // // 创建Gvl视图
  60. // newView = new WindowAppGvlView(strTitle, subViewContainer);
  61. //}
  62. else if (viewType == SUBVIEW_TYPE::SUBVIEW_TASKMONITOR)
  63. {
  64. // 创建Task Monitor视图
  65. newView = new WindowAppTaskMonitorView(strTitle, subViewContainer);
  66. // 在VPGlobal中注册 Task Monitor
  67. VPGlobal::Register((WindowAppTaskMonitorView*)newView);
  68. }
  69. else if (viewType == SUBVIEW_TYPE::SUBVIEW_TASK)
  70. {
  71. // 创建Task视图
  72. newView = new WindowAppTaskView(strTitle, subViewContainer);
  73. }
  74. else if (viewType == SUBVIEW_TYPE::SUBVIEW_LOG)
  75. {
  76. // 创建Log视图
  77. newView = new WindowAppLogView(strTitle, subViewContainer);
  78. }
  79. else if (viewType == SUBVIEW_TYPE::SUBVIEW_HDW)
  80. {
  81. // 创建Hardware视图
  82. newView = new WindowAppPouFrame(strTitle, subViewContainer);
  83. }
  84. else if (viewType == SUBVIEW_TYPE::SUBVIEW_UI)
  85. {
  86. // 创建 UI 视图
  87. newView = new WindowAppUiFrame(strTitle, subViewContainer);
  88. //// 2022-8-29 增加,注册UiFrame指针便于后续统一管理
  89. //g_pFrameManager->registerNewUiFrame(strTitle, (WindowAppUiFrame*)newView);
  90. }
  91. else
  92. {
  93. // 报告一个错误
  94. Utility::VPCriticalMessageBox("Invalid WindowAppMdiSubContainer type:" + (short)viewType);
  95. return nullptr;
  96. }
  97. // 保存视图和子窗体容器信息
  98. m_containerToViews.insert(subViewContainer, newView);
  99. m_nameToContainers.insert(strTitle, subViewContainer);
  100. if (bActive)
  101. {
  102. // 激活此子窗口
  103. activeSubView(strTitle);
  104. // 设置子窗口的初始化大小
  105. AjustSubWindowGeometry(newView, subViewContainer);
  106. }
  107. return newView;
  108. }
  109. /// <summary>
  110. /// 2022-2-16 添加Gvl子窗体(由于GVL窗体参数增加,额外独立出来了)
  111. /// </summary>
  112. /// <param name="strTitle"></param>
  113. /// <param name="gvlType"></param>
  114. /// <param name="bActive"></param>
  115. /// <returns></returns>
  116. QWidget* WindowAppMdiFrame::addGvlSubView(QString strTitle, GVL_MODE gvlType, DllTool* pHdwTool, bool bActive /*= false*/)
  117. {
  118. // 创建子视图的容器( SubWindow )
  119. WindowAppMdiSubContainer* subViewContainer = new WindowAppMdiSubContainer(strTitle);
  120. // 创建视图
  121. QWidget* newView = new WindowAppGvlView(strTitle, gvlType, pHdwTool, subViewContainer);
  122. // 保存视图和子窗体容器信息
  123. m_containerToViews.insert(subViewContainer, newView);
  124. m_nameToContainers.insert(strTitle, subViewContainer);
  125. if (bActive)
  126. {
  127. // 激活此子窗口
  128. activeSubView(strTitle);
  129. // 设置子窗口的初始化大小
  130. AjustSubWindowGeometry(newView, subViewContainer);
  131. }
  132. return newView;
  133. }
  134. /// <summary>
  135. /// 激活对应的子窗体(按名称)
  136. /// </summary>
  137. /// <param name="strTitle"></param>
  138. void WindowAppMdiFrame::activeSubView(QString strTitle)
  139. {
  140. WindowAppMdiSubContainer* pSub = m_liveContainers[strTitle];
  141. if ( pSub==nullptr )
  142. {
  143. pSub = qobject_cast<WindowAppMdiSubContainer*>(this->addSubWindow(m_nameToContainers[strTitle]));
  144. if (pSub == nullptr)
  145. {
  146. return;
  147. }
  148. m_liveContainers.insert(strTitle, pSub);
  149. }
  150. // 激活窗体
  151. this->setActiveSubWindow(pSub);
  152. // 显示窗体
  153. pSub->showNormal();
  154. // 绑定关闭信号
  155. connect(
  156. pSub,
  157. &WindowAppMdiSubContainer::subWindowClosed,
  158. this,
  159. &WindowAppMdiFrame::onSubWindowClosed
  160. );
  161. // 绑定激活信号
  162. connect(
  163. pSub,
  164. &WindowAppMdiSubContainer::subWindowActivated,
  165. this,
  166. &WindowAppMdiFrame::onSubWindowActivated
  167. );
  168. // 调整内部的View尺寸
  169. QWidget* pSubView = this->findSubView(strTitle);
  170. AjustSubWindowGeometry(pSubView, pSub);
  171. vDebug() << pSub->m_strTitle;
  172. }
  173. /// <summary>
  174. /// 删除指定的子窗体(按名称)
  175. /// </summary>
  176. /// <param name="strTitle"></param>
  177. void WindowAppMdiFrame::deleteSubView(QString strTitle)
  178. {
  179. qDebug() << "[MDI] Delete sub view - " << strTitle;
  180. WindowAppMdiSubContainer* pContainer = m_nameToContainers[strTitle];
  181. if (pContainer == nullptr)
  182. {
  183. return;
  184. }
  185. // 从数据结构中清除此窗体
  186. m_containerToViews.remove(pContainer);
  187. m_nameToContainers.remove(strTitle);
  188. m_liveContainers.remove(strTitle);
  189. //g_pFrameManager->unregisterPouFrame(strTitle);
  190. //g_pFrameManager->unregisterUiFrame(strTitle);
  191. //// 关闭此窗体
  192. //if (this->isActiveWindow())
  193. //{
  194. // this->removeSubWindow(pContainer);
  195. //}
  196. // 删除窗体
  197. delete pContainer;
  198. pContainer = nullptr;
  199. // 如果还有剩余的子窗体的话,则激活
  200. if (m_liveContainers.size() > 0)
  201. {
  202. // 随便激活一个剩下的
  203. this->activateNextSubWindow();
  204. }
  205. }
  206. /// <summary>
  207. /// 删除所有子窗体
  208. /// </summary>
  209. void WindowAppMdiFrame::deleteAllSubViews()
  210. {
  211. QHashIterator<QString, WindowAppMdiSubContainer*> iter(m_nameToContainers);
  212. // 循环关闭所有窗体
  213. while (iter.hasNext())
  214. {
  215. QString strTitle = iter.next().key();
  216. if (strTitle != GROUP_NAME_HARDWARE
  217. && strTitle != GROUP_NAME_TASKMONITOR
  218. && strTitle != GROUP_NAME_LOGVIEW
  219. )
  220. {
  221. this->deleteSubView(strTitle);
  222. }
  223. }
  224. //m_containerToViews.clear();
  225. //m_nameToContainers.clear();
  226. //m_liveContainers.clear();
  227. }
  228. /// <summary>
  229. /// 根据名字查找对应的子窗体
  230. /// </summary>
  231. /// <param name="strNewName"></param>
  232. /// <returns></returns>
  233. QWidget* WindowAppMdiFrame::findSubView(QString strNewName)
  234. {
  235. QHashIterator<QString, WindowAppMdiSubContainer*> iter(m_nameToContainers);
  236. while (iter.hasNext())
  237. {
  238. if (iter.next().key() == strNewName)
  239. {
  240. return m_containerToViews[iter.value()];
  241. }
  242. }
  243. return nullptr;
  244. }
  245. /// <summary>
  246. /// 当子窗口关闭时触发
  247. /// </summary>
  248. /// <param name=""></param>
  249. void WindowAppMdiFrame::onSubWindowClosed(WindowAppMdiSubContainer* pSub)
  250. {
  251. // qDebug() << "WindowAppMdiFrame::onSubWindowClosed";
  252. this->removeSubWindow(m_liveContainers[pSub->m_strTitle]);
  253. m_liveContainers.remove(pSub->m_strTitle);
  254. }
  255. /// <summary>
  256. /// 2022-9-21,当子窗口激活时触发
  257. /// </summary>
  258. /// <param name=""></param>
  259. void WindowAppMdiFrame::onSubWindowActivated(WindowAppMdiSubContainer* pSub)
  260. {
  261. // 调用ResourceManager来进行对应的Toolbox切换
  262. g_pResourceManager->switchAppToolbox(pSub->m_strTitle);
  263. }
  264. //void WindowAppMdiFrame::setActiveSubWindow(QMdiSubWindow* window)
  265. //{
  266. // vDebug() << "setActiveSubWindow";
  267. //}
  268. /// <summary>
  269. /// 调整子窗体大小
  270. /// </summary>
  271. /// <param name="pView"></param>
  272. /// <param name="pContainer"></param>
  273. void WindowAppMdiFrame::AjustSubWindowGeometry(QWidget* pView, WindowAppMdiSubContainer* pContainer)
  274. {
  275. if (pView == nullptr)
  276. {
  277. return;
  278. }
  279. pView->resize(this->frameGeometry().size());
  280. pContainer->resize(this->frameGeometry().size());
  281. QApplication::sendEvent(pView , new QEvent(QResizeEvent::Resize));
  282. // qDebug() << "Resize " << pView << " + " << pContainer;
  283. }
  284. /// <summary>
  285. /// 自动缩放
  286. /// </summary>
  287. /// <param name="resizeEvent"></param>
  288. void WindowAppMdiFrame::resizeEvent(QResizeEvent* resizeEvent)
  289. {
  290. Q_UNUSED(resizeEvent);
  291. // qDebug() << "WindowAppMdiFrame::resizeEvent";
  292. // 调整所有窗口大小
  293. QHashIterator<WindowAppMdiSubContainer* ,QWidget*> iter(m_containerToViews);
  294. while (iter.hasNext())
  295. {
  296. this->AjustSubWindowGeometry(iter.value(), iter.next().key());
  297. }
  298. }