WindowAppResourceManagerTree.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. #include "WindowAppResourceManagerTree.h"
  2. #include "DialogNewPou.h"
  3. #include "DialogNewGVL.h"
  4. #include "DialogNewTask.h"
  5. #include "DialogNewUI.h"
  6. #include "WindowAppPouFrame.h"
  7. #include "PouManager.h"
  8. #include "TaskManager.h"
  9. #include "WindowAppMdiFrame.h"
  10. #include "WindowAppMdiSubContainer.h"
  11. #include "WindowAppTaskView.h"
  12. #include "WindowAppTaskMonitorView.h"
  13. #include "WindowAppUiFrame.h"
  14. #include "WindowApp.h"
  15. #include "UiManager.h"
  16. #include "Document.h"
  17. // 各个节点的名称
  18. #define NODE_NAME_GVL "全局变量组"
  19. #define NODE_NAME_PROGRAM "程序管理器"
  20. #define NODE_NAME_TASKMANAGER "任务管理器"
  21. #define NODE_NAME_UIDESIGN "页面设计器"
  22. #define NODE_NAME_USER "用户管理器"
  23. #define NODE_NAME_HARDWARE "外部硬件管理器"
  24. WindowAppResourceManagerTree::WindowAppResourceManagerTree(QTreeWidget* parent)
  25. : QTreeWidget(parent)
  26. {
  27. // 响应自定义右键菜单
  28. this->setContextMenuPolicy(Qt::CustomContextMenu);
  29. // 创建界面布局
  30. createLayouts();
  31. // 创建各个节点的右键菜单
  32. createMenus();
  33. VPGlobal::Register(this);
  34. m_pMdiFrame = nullptr;
  35. }
  36. WindowAppResourceManagerTree::~WindowAppResourceManagerTree()
  37. {
  38. }
  39. /// <summary>
  40. /// 创建界面布局
  41. /// </summary>
  42. void WindowAppResourceManagerTree::createLayouts()
  43. {
  44. // 1列
  45. this->setColumnCount(1);
  46. // 隐藏表头
  47. this->setHeaderHidden(true);
  48. // this->setHeaderLabel(("TaskBox"));
  49. // 显示虚线
  50. this->setStyle(QStyleFactory::create("windows"));
  51. this->setStyleSheet("QTreeView::branch:has-children:!has-siblings:closed,\
  52. QTreeView::branch:closed:has-children:has-siblings{border-image: none; image: url(:/image/btn_down_black.png);}\
  53. QTreeView::branch:open:has-children:!has-siblings,\
  54. QTreeView::branch:open:has-children:has-siblings{border-image: none; image: url(:/image/btn_up_black.png);}");
  55. // 各个根节点的名称
  56. m_appNodeNames.push_back((NODE_NAME_GVL));
  57. m_appNodeNames.push_back((NODE_NAME_PROGRAM));
  58. m_appNodeNames.push_back((NODE_NAME_TASKMANAGER));
  59. m_appNodeNames.push_back((NODE_NAME_UIDESIGN));
  60. m_appNodeNames.push_back((NODE_NAME_USER));
  61. QFont fontFollowNode;
  62. fontFollowNode.setPointSize(8);
  63. fontFollowNode.setBold(true);
  64. fontFollowNode.setUnderline(true);
  65. fontFollowNode.setWeight(75);
  66. fontFollowNode.setStrikeOut(false);
  67. QFont fontChildNode;
  68. fontChildNode.setPointSize(8);
  69. fontChildNode.setBold(true);
  70. // 创建各个根节点
  71. // QList<QTreeWidgetItem*> items;
  72. this->setFocusPolicy(Qt::NoFocus);
  73. //屏蔽节点双击事件
  74. this->setExpandsOnDoubleClick(false);
  75. // 所有节点自动展开
  76. this->setItemsExpandable(false);
  77. m_appNodeIcons.append(QIcon(":/image/GVL.png"));
  78. m_appNodeIcons.append(QIcon(":/image/Program.png"));
  79. m_appNodeIcons.append(QIcon(":/image/Task.png"));
  80. m_appNodeIcons.append(QIcon(":/image/UI.png"));
  81. m_appNodeIcons.append(QIcon(":/image/Authority.png"));
  82. m_appNodeIcons.append(QIcon(":/image/Authority.png"));
  83. m_appNodeIcons.append(QIcon(":/image/User.png"));
  84. // 创建App根节点
  85. topAppItem = new QTreeWidgetItem(this, QStringList("应用程序管理器"));
  86. topAppItem->setIcon(0, QIcon(":/image/Option.png"));
  87. topAppItem->setFont(0, fontFollowNode);
  88. // 创建App各子节点
  89. for (int i = 0; i < (short)SUBVIEW_TYPE::SUBVIEW_COUNT; i++)
  90. {
  91. QTreeWidgetItem* newItem = new QTreeWidgetItem(topAppItem, QStringList(m_appNodeNames[i]));
  92. newItem->setIcon(0, m_appNodeIcons[i]);
  93. newItem->setFont(0, fontChildNode);
  94. m_rootNodes.append(newItem);
  95. }
  96. // 创建App下的任务管理和调试窗口
  97. topDebugItem = new QTreeWidgetItem(this, QStringList("调试信息管理器"));
  98. topDebugItem->setIcon(0, QIcon(":/image/Hardware.png"));
  99. topDebugItem->setFont(0, fontFollowNode);
  100. // 默认的 App - Debug - TaskMonitor 节点
  101. QTreeWidgetItem* TaskMonitorItem = new QTreeWidgetItem(topDebugItem, QStringList(GROUP_NAME_TASKMONITOR));
  102. TaskMonitorItem->setIcon(0, QIcon(":/image/Com.png"));
  103. TaskMonitorItem->setFont(0, fontChildNode);
  104. // 默认的 App - Log - LogView 节点
  105. QTreeWidgetItem* LogViewItem = new QTreeWidgetItem(topDebugItem, QStringList(GROUP_NAME_LOGVIEW));
  106. LogViewItem->setIcon(0, QIcon(":/image/Cam.png"));
  107. LogViewItem->setFont(0, fontChildNode);
  108. // 创建硬件组,未来调整为子节点同步与硬件组态的工具(每个添加一个硬件到硬件组态,同步添加一个子节点)
  109. topHardwareItem = new QTreeWidgetItem(this, QStringList("外部硬件管理器"));
  110. topHardwareItem->setIcon(0, QIcon(":/image/Hardware.png"));
  111. topHardwareItem->setFont(0, fontFollowNode);
  112. // 默认的 Hardware - HDW Config节点
  113. QTreeWidgetItem* ConfigItem = new QTreeWidgetItem(topHardwareItem, QStringList(GROUP_NAME_HARDWARE));
  114. ConfigItem->setIcon(0, QIcon(":/image/HDWConf.png"));
  115. ConfigItem->setFont(0, fontChildNode);
  116. // 设置根节点风格
  117. this->setRootIsDecorated(true);
  118. // 设置所有的节点皆可以展开
  119. this->setItemsExpandable(true);
  120. // 默认展开所有节点
  121. this->expandAll();
  122. }
  123. /// <summary>
  124. /// 创建各个节点的右键菜单
  125. /// </summary>
  126. void WindowAppResourceManagerTree::createMenus()
  127. {
  128. // 根节点的菜单
  129. m_menuGVLRoot = new QMenu(this);
  130. m_menuPouRoot = new QMenu(this);
  131. m_menuTaskRoot = new QMenu(this);
  132. m_menuUiRoot = new QMenu(this);
  133. // 子节点的菜单
  134. m_menuGVLChild = new QMenu(this);
  135. m_menuPouChild = new QMenu(this);
  136. m_menuTaskChild = new QMenu(this);
  137. m_menuUiChild = new QMenu(this);
  138. m_menuScriptChild = new QMenu(this);
  139. // GVL Action
  140. m_actionGVLNew = new QAction(("New Gvl"), this);
  141. m_actionGVLEdit = new QAction(("Edit Gvl"), this);
  142. m_actionGVLRename = new QAction(("Rename Gvl"), this);
  143. m_actionGVLDelete = new QAction(("Delete Gvl"), this);
  144. // Pou Action
  145. m_actionPouNew = new QAction(("New Pou"), this);
  146. m_actionPouRename = new QAction(("Rename Pou"), this);
  147. m_actionPouChangeMode = new QAction(("Change to Block Mode"), this);
  148. m_actionPouDelete = new QAction(("Delete Pou"), this);
  149. m_actionPouExport = new QAction(("Export Pou"), this);
  150. m_actionPouImport = new QAction(("Import Pou"), this);
  151. // Task Action
  152. m_actionTaskNew = new QAction(("New Task"), this);
  153. m_actionTaskAddPou = new QAction(("Add Pou"), this);
  154. //m_actionTaskRename = new QAction(("Rename Task"), this);
  155. m_actionTaskDelete = new QAction(("Delete Task"), this);
  156. // UI Action
  157. m_actionUiNew = new QAction(("New UI"), this);
  158. m_actionUiMainPage = new QAction("Set main page", this);
  159. m_actionUiDelete = new QAction(("Delete"), this);
  160. // 添加Action
  161. m_menuGVLRoot->addAction(m_actionGVLNew);
  162. m_menuGVLChild->addAction(m_actionGVLEdit);
  163. m_menuGVLChild->addAction(m_actionGVLRename);
  164. m_menuGVLChild->addSeparator();
  165. m_menuGVLChild->addAction(m_actionGVLDelete);
  166. m_menuPouRoot->addAction(m_actionPouNew);
  167. m_menuPouRoot->addAction(m_actionPouImport);
  168. m_menuPouChild->addAction(m_actionPouExport);
  169. m_menuPouChild->addSeparator();
  170. m_menuPouChild->addAction(m_actionPouRename);
  171. m_menuPouChild->addAction(m_actionPouDelete);
  172. m_menuPouChild->addSeparator();
  173. m_menuPouChild->addAction(m_actionPouChangeMode);
  174. m_menuTaskRoot->addAction(m_actionTaskNew);
  175. m_menuTaskChild->addAction(m_actionTaskAddPou);
  176. //m_menuTaskChild->addAction(m_actionTaskRename);
  177. m_menuTaskChild->addSeparator();
  178. m_menuTaskChild->addAction(m_actionTaskDelete);
  179. m_menuUiRoot->addAction(m_actionUiNew);
  180. m_menuUiChild->addAction(m_actionUiDelete);
  181. m_menuUiChild->addAction(m_actionUiMainPage);
  182. // 右键菜单
  183. connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(onTreeContextMenu(const QPoint&)));
  184. // 双击事件
  185. connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(onTreeItemDblClicked(QTreeWidgetItem*, int)));
  186. // 菜单响应函数
  187. connect(m_menuGVLRoot, SIGNAL(triggered(QAction*)), this, SLOT(onMenuGVL(QAction*)));
  188. connect(m_menuGVLChild, SIGNAL(triggered(QAction*)), this, SLOT(onMenuGVL(QAction*)));
  189. connect(m_menuPouRoot, SIGNAL(triggered(QAction*)), this, SLOT(onMenuPou(QAction*)));
  190. connect(m_menuPouChild, SIGNAL(triggered(QAction*)), this, SLOT(onMenuPou(QAction*)));
  191. connect(m_menuTaskRoot, SIGNAL(triggered(QAction*)), this, SLOT(onMenuTask(QAction*)));
  192. connect(m_menuTaskChild, SIGNAL(triggered(QAction*)), this, SLOT(onMenuTask(QAction*)));
  193. connect(m_menuUiRoot, SIGNAL(triggered(QAction*)), this, SLOT(onMenuUI(QAction*)));
  194. connect(m_menuUiChild, SIGNAL(triggered(QAction*)), this, SLOT(onMenuUI(QAction*)));
  195. }
  196. /// <summary>
  197. /// 查找子节点
  198. /// </summary>
  199. /// <param name="strItemName"></param>
  200. QTreeWidgetItem* WindowAppResourceManagerTree::findChildItem(const QString& strItemName)
  201. {
  202. QTreeWidgetItemIterator it(this);
  203. while (*it)
  204. {
  205. QString strText = (*it)->text(0);
  206. if (strText == strItemName)
  207. {
  208. return *it;
  209. }
  210. ++it;
  211. }
  212. return nullptr;
  213. }
  214. /// <summary>
  215. /// 删除所有子节点
  216. /// </summary>
  217. void WindowAppResourceManagerTree::removeAllSubNodes()
  218. {
  219. // 先收起所有子节点
  220. // this->collapseAll();
  221. // 遍历删除所有根节点下的子节点
  222. for(QTreeWidgetItem * rootNode : m_rootNodes)
  223. {
  224. int itemChildrenCounts = rootNode->childCount();
  225. while (itemChildrenCounts--)
  226. {
  227. QTreeWidgetItem* child = rootNode->child(itemChildrenCounts);
  228. //// 不删除 Task Monitor的节点
  229. //if (child->text(0) == TASKMONITOR_GROUP_NAME)
  230. //{
  231. // continue;
  232. //}
  233. rootNode->removeChild(child);
  234. delete child;
  235. child = nullptr;
  236. }
  237. }
  238. // 设置当前项为空
  239. this->setCurrentItem(nullptr);
  240. // 最后打开所有节点
  241. // this->expandAll();
  242. }
  243. /// <summary>
  244. /// 2022-2-22 创建默认的几个视图
  245. /// </summary>
  246. void WindowAppResourceManagerTree::initDefaultViews()
  247. {
  248. // 创建默认的Task视图
  249. this->addDefaultTaskMonitorNode(GROUP_NAME_TASKMONITOR);
  250. // 创建默认的Log视图
  251. this->addDefaultLogViewNode(GROUP_NAME_LOGVIEW);
  252. // 创建默认的Hdw视图(TODO,此处应该加入加载硬件组态的逻辑)
  253. this->addDefaultHWConfigViewNode(GROUP_NAME_HARDWARE);
  254. }
  255. /// <summary>
  256. /// 2022-2-22 创建默认的几个节点
  257. /// </summary>
  258. void WindowAppResourceManagerTree::initDefaultNodes()
  259. {
  260. // 创建默认的测试视图(For Test)
  261. this->addUiNode("UI_1", false);
  262. // 创建默认的Pou视图
  263. this->addPouNode("POU_1", false);
  264. // 创建默认的GVL视图
  265. this->addGvlNode("GVL_1", GVL_MODE::GVL_STANDARD, nullptr, false);
  266. // 创建默认的Task视图
  267. this->initDefaultTaskNodes("Task_1");
  268. }
  269. /// <summary>
  270. /// 创建初始 Task 节点
  271. /// </summary>
  272. /// <param name="strTaskName"></param>
  273. void WindowAppResourceManagerTree::initDefaultTaskNodes(QString strTaskName)
  274. {
  275. WindowAppTaskView* pTaskView = (WindowAppTaskView*)this->addTaskNode(strTaskName, false);
  276. // 将用户输入的Task参数绑定到视图中
  277. TASK_OPTION option;
  278. option.strName = strTaskName;
  279. pTaskView->addNewTask(option);
  280. QVector<POU*> pPous;
  281. // const QMap<QString, POU*>& allPous = g_pPouManager->getAllPous();
  282. // // 遍历所有的Pou信息
  283. // QMapIterator<QString, POU*> it(allPous);
  284. // while (it.hasNext())
  285. // {
  286. // QString strPouGroupName = it.next().key();
  287. // // 需要跳过Hardware以及功能块类型的Pou
  288. // if (strPouGroupName == GROUP_NAME_HARDWARE)
  289. // {
  290. // continue;
  291. // }
  292. // POU* pPou = it.value();
  293. // pPous.push_back(pPou);
  294. // }
  295. QString strPouName = "POU_1";
  296. POU* pPou = g_pPouManager->getPouByName(strPouName);
  297. if (pPou != nullptr)
  298. {
  299. pPous.push_back(pPou);
  300. }
  301. pTaskView->addTaskPous(pPous);
  302. addPouNodeToTask(strPouName, strTaskName);
  303. }
  304. /// <summary>
  305. /// 添加一个Pou节点
  306. /// </summary>
  307. /// <param name="strName"></param>
  308. QWidget* WindowAppResourceManagerTree::addPouNode(QString strName, bool bActive)
  309. {
  310. // 2021/6/25增加,首先查找这个节点是否存在,如果存在则不重复添加了,直接返回对应指针
  311. QWidget* pSubView = m_pMdiFrame->findSubView(strName);
  312. if (pSubView == nullptr)
  313. {
  314. // 树形结构中添加一个节点(如果此节点不存在的话)
  315. if (!this->findChildItem(strName))
  316. {
  317. addChildItem(SUBVIEW_TYPE::SUBVIEW_POU, strName);
  318. }
  319. // 然后添加对应的子窗口视图
  320. return m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_POU, bActive);
  321. }
  322. else
  323. {
  324. return pSubView;
  325. }
  326. }
  327. /// <summary>
  328. /// 添加一个GVL节点
  329. /// </summary>
  330. /// <param name="strName"></param>
  331. QWidget* WindowAppResourceManagerTree::addGvlNode(QString strName, GVL_MODE gvlType, DllTool* pHdwTool, bool bActive)
  332. {
  333. // 2021/6/25增加,首先查找这个节点是否存在,如果存在则不重复添加了,直接返回对应指针
  334. QWidget* pSubView = m_pMdiFrame->findSubView(strName);
  335. if (pSubView == nullptr)
  336. {
  337. // 树形结构中添加一个节点
  338. addChildItem(SUBVIEW_TYPE::SUBVIEW_GVL, strName);
  339. // 然后添加对应的子窗口视图,并返回新视图指针
  340. return m_pMdiFrame->addGvlSubView(strName, gvlType, pHdwTool, bActive);
  341. }
  342. else
  343. {
  344. return pSubView;
  345. }
  346. }
  347. /// <summary>
  348. /// 添加一个Task节点
  349. /// </summary>
  350. /// <param name="strName"></param>
  351. /// <returns></returns>
  352. QWidget* WindowAppResourceManagerTree::addTaskNode(QString strName, bool bActive)
  353. {
  354. QWidget* pSubView = m_pMdiFrame->findSubView(strName);
  355. if (pSubView == nullptr)
  356. {
  357. // 树形结构中添加一个节点
  358. addChildItem(SUBVIEW_TYPE::SUBVIEW_TASK, strName);
  359. // 然后添加对应的子窗口视图,并返回新视图指针
  360. return m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_TASK, bActive);
  361. }
  362. else
  363. {
  364. return pSubView;
  365. }
  366. }
  367. /// <summary>
  368. /// 添加一个UI节点
  369. /// </summary>
  370. /// <param name="strName"></param>
  371. /// <param name="bActive"></param>
  372. /// <param name="bFromDoc">是否从反序列化中恢复</param>
  373. /// <returns></returns>
  374. QWidget* WindowAppResourceManagerTree::addUiNode(QString strName, bool bActive, bool bFromDoc)
  375. {
  376. QWidget* pSubView = m_pMdiFrame->findSubView(strName);
  377. if (pSubView == nullptr)
  378. {
  379. bool bMainPage = false;
  380. // 2022-10-16 增加,如果是从反序列化中来,则有Main后缀的页面会被设定为主页
  381. if (bFromDoc)
  382. {
  383. if (strName.indexOf(UI_MAINPAGE_POSTFIX) >= 0)
  384. {
  385. bMainPage = true;
  386. }
  387. }
  388. // 如果不是从反序列化中来,则根据页面数量设定主页
  389. else
  390. {
  391. // 如果只有一个页面,那这个页面就是主页
  392. if (g_pUiManager->getAllUiNames().size() <= 0)
  393. {
  394. strName += UI_MAINPAGE_POSTFIX;
  395. bMainPage = true;
  396. }
  397. }
  398. // 树形结构中添加一个节点
  399. addChildItem(SUBVIEW_TYPE::SUBVIEW_UI, strName);
  400. // 然后添加对应的子窗口视图,并返回新视图指针
  401. // 2022-9-21,如果需要通过激活的页面来到树形结构里进行反查的话,那么带(main)的主页是会搜索不到的
  402. // 这里干脆就不做区分了,直接在Mdi窗口汇总把(main)加上去
  403. // 这样 strShowTitle 和 strRealTitle 其实就失去意义了
  404. QWidget* pNewSubView = m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_UI, bActive);
  405. // 如果当前是UI的主页,则通知uimanger设定新的主页
  406. if (bMainPage)
  407. {
  408. g_pUiManager->setMainPage(strName);
  409. this->m_strMainTitle = strName;
  410. }
  411. return pNewSubView;
  412. }
  413. else
  414. {
  415. return pSubView;
  416. }
  417. }
  418. ///// <summary>
  419. ///// 添加一个UI节点
  420. ///// </summary>
  421. ///// <param name="strName"></param>
  422. ///// <returns></returns>
  423. //QWidget* WindowAppResourceManagerTree::addUiNode(QString strName, bool bActive)
  424. //{
  425. // QWidget* pSubView = m_pMdiFrame->findSubView(strName);
  426. // if (pSubView == nullptr)
  427. // {
  428. // // 如果只有一个页面,那这个页面就是主页
  429. // QString strShowName = strName;
  430. // bool bNewMainPage = false;
  431. // if (g_pUiManager->getAllUiNames().size() <= 0)
  432. // {
  433. // strShowName += UI_MAINPAGE_POSTFIX;
  434. //
  435. // // 并且记录下mainpage名字的对应关系
  436. // m_uiMainTitle.strShowTitle = strShowName;
  437. // m_uiMainTitle.strRealTitle = strName;
  438. //
  439. // bNewMainPage = true;
  440. // }
  441. //
  442. // // 树形结构中添加一个节点
  443. // addChildItem(SUBVIEW_TYPE::SUBVIEW_UI, strShowName);
  444. //
  445. // // 然后添加对应的子窗口视图,并返回新视图指针
  446. // // 2022-9-21,如果需要通过激活的页面来到树形结构里进行反查的话,那么带(main)的主页是会搜索不到的
  447. // // 这里干脆就不做区分了,直接在Mdi窗口汇总把(main)加上去
  448. // // 这样 strShowTitle 和 strRealTitle 其实就失去意义了
  449. // // QWidget* pNewSubView = m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_UI, bActive);
  450. // QWidget* pNewSubView = m_pMdiFrame->addSubView(strShowName, SUBVIEW_TYPE::SUBVIEW_UI, bActive);
  451. //
  452. // // 如果仅有一个页面,则通知uimanger设定新的主页
  453. // if (bNewMainPage)
  454. // {
  455. // g_pUiManager->setMainPage(strShowName);
  456. // }
  457. //
  458. // return pNewSubView;
  459. // }
  460. // else
  461. // {
  462. // return pSubView;
  463. // }
  464. //}
  465. /// <summary>
  466. /// 添加子节点
  467. /// </summary>
  468. /// <param name="nodeIndex"></param>
  469. /// <param name="itemName"></param>
  470. void WindowAppResourceManagerTree::addChildItem(SUBVIEW_TYPE nodeIndex, const QString& strItemName)
  471. {
  472. // 根据类型在对应的位置新建节点
  473. QTreeWidgetItem* newItem = new QTreeWidgetItem(m_rootNodes[(short)nodeIndex], QStringList(strItemName));
  474. // 设置图标
  475. newItem->setIcon(0, m_appNodeIcons[(short)nodeIndex]);
  476. // 设置字体
  477. QFont font;
  478. font.setPointSize(8);
  479. newItem->setFont(0, font);
  480. // 将节点添加到界面中
  481. m_rootNodes[(short)nodeIndex]->addChild(newItem);
  482. }
  483. /// <summary>
  484. /// 弹出右键菜单
  485. /// </summary>
  486. /// <param name="pt"></param>
  487. void WindowAppResourceManagerTree::onTreeContextMenu(const QPoint& pt)
  488. {
  489. // qDebug() << "WindowAppTreeTask::onTreeContextMenu";
  490. // 获取鼠标当前位置的Item
  491. QTreeWidgetItem* curItem = this->itemAt(pt);
  492. if (curItem == nullptr)
  493. {
  494. qDebug() << "WindowAppTreeTask::onTreeContextMenu - nullptr";
  495. return;
  496. }
  497. // GVL 根节点
  498. if (curItem == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_GVL])
  499. {
  500. m_menuGVLRoot->exec(QCursor::pos());
  501. return;
  502. }
  503. // GVL子节点
  504. if (curItem->parent() == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_GVL])
  505. {
  506. m_menuGVLChild->exec(QCursor::pos());
  507. return;
  508. }
  509. // Pou 根节点
  510. if (curItem == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_POU])
  511. {
  512. m_menuPouRoot->exec(QCursor::pos());
  513. return;
  514. }
  515. // Pou 子节点
  516. if (curItem->parent() == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_POU])
  517. {
  518. m_menuPouChild->exec(QCursor::pos());
  519. return;
  520. }
  521. // Task Manager 根节点
  522. if (curItem == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_TASK])
  523. {
  524. m_menuTaskRoot->exec(QCursor::pos());
  525. return;
  526. }
  527. // Task Manager子节点
  528. if (curItem->parent() == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_TASK])
  529. {
  530. m_menuTaskChild->exec(QCursor::pos());
  531. return;
  532. }
  533. // Task Manager孙节点
  534. if (curItem->parent()->parent() == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_TASK])
  535. {
  536. m_menuPouChild->exec(QCursor::pos());
  537. return;
  538. }
  539. // Design 根节点
  540. if (curItem == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_UI])
  541. {
  542. m_menuUiRoot->exec(QCursor::pos());
  543. return;
  544. }
  545. // Design 子节点
  546. if (curItem->parent() == m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_UI])
  547. {
  548. m_menuUiChild->exec(QCursor::pos());
  549. return;
  550. }
  551. }
  552. /// <summary>
  553. /// 节点双击,切换对应子页面
  554. /// </summary>
  555. void WindowAppResourceManagerTree::onTreeItemDblClicked(QTreeWidgetItem* clickedItem, int column)
  556. {
  557. Q_UNUSED(column);
  558. // qDebug() << "WindowAppTreeTask::onTreeItemDblClicked";
  559. // 如果双击的是父节点,则不做任何操作
  560. if (clickedItem->parent() == nullptr)
  561. {
  562. return;
  563. }
  564. // 如果是子节点,则切换到对应页面
  565. if ( clickedItem->parent()!=topAppItem
  566. //&& clickedItem->parent()!=topHardwareItem
  567. )
  568. {
  569. // qDebug() << "WindowAppTreeTask::onTreeItemDblClicked child";
  570. // 2021-9-28,如果点击的是UI页面,此处需要加一个名字转换,因为UI的主页后面有main字样
  571. QString strItemName = clickedItem->text(0);
  572. // 2022-9-22 统一了节点和子页面名称,此处不做转换了
  573. //if (strItemName == m_uiMainTitle.strShowTitle)
  574. //{
  575. // strItemName = m_uiMainTitle.strRealTitle;
  576. //}
  577. m_pMdiFrame->activeSubView(strItemName);
  578. // 根据当前双击节点的父节点,通知WindowApp切换对应的工具箱
  579. this->switchAppToolbox(clickedItem->parent());
  580. }
  581. }
  582. /// <summary>
  583. /// 2022-9-20,根据用户选择的子窗体,激活App中对应的工具箱Tab(仅Pou和UI)
  584. /// </summary>
  585. /// <param name="currentParentItem"></param>
  586. void WindowAppResourceManagerTree::switchAppToolbox(QTreeWidgetItem* currentParentItem)
  587. {
  588. // 如果当前双击的是Pou节点,则通知WindowApp切换到对应的视觉工具箱
  589. if (currentParentItem->text(0) == NODE_NAME_PROGRAM)
  590. {
  591. m_pWindowApp->activeToolBoxView(APPTOOLBOX_VIEW_VISION);
  592. }
  593. // 如果当前双击的是Ui节点,则通过WindowApp切换到对应的UI工具箱
  594. else if(currentParentItem->text(0) == NODE_NAME_UIDESIGN)
  595. {
  596. m_pWindowApp->activeToolBoxView(APPTOOLBOX_VIEW_UI);
  597. }
  598. // 如果当前双击的是硬件组态节点,则通知WindowApp切换到对应的视觉工具箱
  599. else if (currentParentItem->text(0) == NODE_NAME_HARDWARE)
  600. {
  601. m_pWindowApp->activeToolBoxView(APPTOOLBOX_VIEW_VISION, WindowAppTreeTool::TREETOOL_MODE::MODE_HARDWARE);
  602. }
  603. // 否则通知WindowApp隐藏工具箱
  604. else
  605. {
  606. m_pWindowApp->activeToolBoxView(APPTOOLBOX_VIEW_NULL);
  607. }
  608. }
  609. /// <summary>
  610. /// 2022-9-21 根据用户激活的子窗体的名字,激活App中对应的工具箱Tab(仅Pou和UI)
  611. /// </summary>
  612. /// <param name="strSubviewName"></param>
  613. void WindowAppResourceManagerTree::switchAppToolbox(const QString& strSubviewName)
  614. {
  615. // 首先根据名字找到对应的节点
  616. QTreeWidgetItem* pItem = this->findChildItem(strSubviewName);
  617. // 如果是子节点
  618. if (pItem->parent() != topAppItem)
  619. {
  620. // 根据当前双击节点的父节点,通知WindowApp切换对应的工具箱
  621. this->switchAppToolbox(pItem->parent());
  622. }
  623. }
  624. /// <summary>
  625. /// GVL的节点菜单
  626. /// </summary>
  627. /// <param name="action"></param>
  628. void WindowAppResourceManagerTree::onMenuGVL(QAction* action)
  629. {
  630. QTreeWidgetItem* currentItem = this->currentItem();
  631. if (currentItem == nullptr)
  632. {
  633. return;
  634. }
  635. // 获取当前选中的节点名称
  636. QString strName = currentItem->text(0);
  637. if (action == m_actionGVLNew)
  638. {
  639. doMenuGvlNew();
  640. }
  641. else if (action == m_actionGVLDelete)
  642. {
  643. doMenuGvlDelete(strName);
  644. }
  645. // qDebug() << "WindowAppTreeTask::onMenuGVL";
  646. }
  647. /// <summary>
  648. /// Pou相关菜单
  649. /// </summary>
  650. /// <param name="action"></param>
  651. void WindowAppResourceManagerTree::onMenuPou(QAction* action)
  652. {
  653. QTreeWidgetItem* currentItem = this->currentItem();
  654. if (currentItem == nullptr)
  655. {
  656. return;
  657. }
  658. // 获取当前选中的节点名称
  659. QString strName = currentItem->text(0);
  660. if (action == m_actionPouNew)
  661. {
  662. doMenuPouNew();
  663. }
  664. else if (action == m_actionPouImport)
  665. {
  666. doMenuPouImport();
  667. }
  668. else if (action == m_actionPouExport)
  669. {
  670. doMenuPouExport(strName);
  671. }
  672. else if (action == m_actionPouRename)
  673. {
  674. doMenuPouRename(strName);
  675. }
  676. else if (action == m_actionPouDelete)
  677. {
  678. doMenuPouDelete(strName);
  679. }
  680. else if (action == m_actionPouChangeMode)
  681. {
  682. doMenuPouChangeMode(strName);
  683. }
  684. // qDebug() << "WindowAppTreeTask::onMenuPou";
  685. }
  686. /// <summary>
  687. /// Task Config相关菜单
  688. /// </summary>
  689. /// <param name="action"></param>
  690. void WindowAppResourceManagerTree::onMenuTask(QAction* action)
  691. {
  692. // qDebug() << "WindowAppTreeTask::onMenuTaskManager";
  693. QTreeWidgetItem* currentItem = this->currentItem();
  694. if (currentItem == nullptr)
  695. {
  696. return;
  697. }
  698. // 获取当前选中的节点名称
  699. QString strName = currentItem->text(0);
  700. if (action == m_actionTaskNew)
  701. {
  702. doMenuTaskNew();
  703. }
  704. else if (action == m_actionTaskAddPou)
  705. {
  706. doMenuTaskAddPou(strName);
  707. }
  708. else if (action == m_actionTaskDelete)
  709. {
  710. doMenuTaskDelete(strName);
  711. }
  712. }
  713. /// <summary>
  714. /// UI 菜单
  715. /// </summary>
  716. /// <param name="action"></param>
  717. void WindowAppResourceManagerTree::onMenuUI(QAction* action)
  718. {
  719. // qDebug() << "WindowAppTreeTask::onMenuUI";
  720. QTreeWidgetItem* currentItem = this->currentItem();
  721. if (currentItem == nullptr)
  722. {
  723. return;
  724. }
  725. // 获取当前选中的节点名称
  726. QString strName = currentItem->text(0);
  727. if (action == m_actionUiNew)
  728. {
  729. doMenuUiNew();
  730. }
  731. else if (action == m_actionUiMainPage)
  732. {
  733. doMenuUiMainPage(strName);
  734. }
  735. else if (action == m_actionUiDelete)
  736. {
  737. doMenuUiDelete(strName);
  738. }
  739. }
  740. /// <summary>
  741. /// Gvl - New
  742. /// </summary>
  743. void WindowAppResourceManagerTree::doMenuGvlNew()
  744. {
  745. // 生成默认名字
  746. int nGvlSize = g_pGvlManager->getAllGvls()->size();
  747. QString strGvlName("Gvl_" + QString::number(nGvlSize));
  748. DialogNewGVL dlgNewGvl;
  749. dlgNewGvl.setWindowModality(Qt::WindowModal);
  750. dlgNewGvl.setDefaultName(strGvlName);
  751. int res = dlgNewGvl.exec();
  752. if (res != QDialog::Accepted)
  753. {
  754. return;
  755. }
  756. // 名称和类型
  757. QString gvlName = dlgNewGvl.m_gvlName;
  758. GVL_MODE gvlType = dlgNewGvl.m_gvlType;
  759. switch (gvlType)
  760. {
  761. case GVL_MODE::GVL_BASIC:
  762. gvlName = gvlName + GVL_BASIC_POSTFIX;
  763. break;
  764. case GVL_MODE::GVL_STANDARD:
  765. gvlName = gvlName + GVL_STANDARD_POSTFIX;
  766. break;
  767. case GVL_MODE::GVL_SYSTEM:
  768. break;
  769. case GVL_MODE::GVL_DB:
  770. {
  771. gvlName = gvlName + GVL_DB_POSTFIX;
  772. }
  773. break;
  774. default:
  775. break;
  776. }
  777. // 检查节点是否有重复的名称
  778. if (this->findChildItem(gvlName) != nullptr)
  779. {
  780. Utility::VPCriticalMessageBox(gvlName + " is duplicated!");
  781. return;
  782. }
  783. // 2022-3-2,如果是DB模式,还需要额外绑定硬件和DB数据表
  784. if (gvlType == GVL_MODE::GVL_DB)
  785. {
  786. // 添加对应的节点和视图
  787. this->addGvlNode(gvlName, gvlType, dlgNewGvl.m_pSelHdwTool->pDllPtr, true);
  788. g_pGvlManager->bindHdwToolAndDB(dlgNewGvl.m_pSelHdwTool, gvlName);
  789. }
  790. else
  791. {
  792. // 添加对应的节点和视图
  793. this->addGvlNode(gvlName, gvlType, nullptr, true);
  794. }
  795. }
  796. /// <summary>
  797. /// Gvl - Delete
  798. /// </summary>
  799. void WindowAppResourceManagerTree::doMenuGvlDelete(const QString& strName)
  800. {
  801. // 当前选中Pou的名字
  802. QString strDelGvlName = strName;
  803. // 查找父节点
  804. QTreeWidgetItem* pParentItem = this->currentItem()->parent();
  805. if (pParentItem)
  806. {
  807. TOOL* pTool = g_pGvlManager->getHdwToolByDbName(strDelGvlName);
  808. g_pGvlManager->unbindHdwToolAndDB(pTool , strDelGvlName);
  809. // 删除指定的gvl
  810. g_pGvlManager->removeGvl(strDelGvlName);
  811. // 因为在pouReset()中会删除页面,所以此处应该可以注释掉了
  812. if (pParentItem == m_rootNodes[(int)SUBVIEW_TYPE::SUBVIEW_GVL])
  813. {
  814. m_pMdiFrame->deleteSubView(strDelGvlName);
  815. }
  816. // 最后删除此节点
  817. QTreeWidgetItem* currentItem = this->currentItem();
  818. pParentItem->removeChild(currentItem);
  819. }
  820. }
  821. /// <summary>
  822. /// Program - New
  823. /// </summary>
  824. void WindowAppResourceManagerTree::doMenuPouNew()
  825. {
  826. // 生成默认名字
  827. int nPouSize = g_pPouManager->getAllPous().size();
  828. QString strPouName( "Pou_" + QString::number(nPouSize));
  829. DialogNewPou dlgNewPou;
  830. dlgNewPou.setWindowModality(Qt::WindowModal);
  831. dlgNewPou.setDefaultName(strPouName);
  832. int res = dlgNewPou.exec();
  833. if (res != QDialog::Accepted)
  834. {
  835. return;
  836. }
  837. QString pouName = dlgNewPou.m_strPouName + POU_POSTFIX;
  838. //Pou_TYPE pouType = dlgNewPou.pouType;
  839. // 检查节点是否有重复的名称
  840. if (this->findChildItem(pouName) != nullptr)
  841. {
  842. Utility::VPCriticalMessageBox(pouName + " is duplicated!");
  843. return;
  844. }
  845. // 添加对应的节点和视图
  846. this->addPouNode(pouName, true);
  847. // 2022-9-20,新增了一个Pou节点的时候,也通知WindowApp切换到Pou工具箱
  848. m_pWindowApp->activeToolBoxView(APPTOOLBOX_VIEW_VISION);
  849. }
  850. /// <summary>
  851. /// Program - Import
  852. /// </summary>
  853. void WindowAppResourceManagerTree::doMenuPouImport()
  854. {
  855. // 获取加载的全路径
  856. QString strFullPath = QFileDialog::getOpenFileName(this,
  857. tr("Open POU"), "/home/POU", tr("POU Files (*.pou)"));
  858. if (strFullPath.isEmpty())
  859. {
  860. return;
  861. }
  862. // 执行导入
  863. VPGlobal::m_pDoc->loadSinglePou(strFullPath);
  864. }
  865. /// <summary>
  866. /// Program - Export
  867. /// </summary>
  868. void WindowAppResourceManagerTree::doMenuPouExport(const QString& strName)
  869. {
  870. // 根据当前节点生成默认文件名
  871. QString strExportPouName = strName;
  872. QString strDefaultFileName = strExportPouName + "_Export.pou";
  873. // 提示选择存储位置
  874. QString m_strSavePath = QFileDialog::getSaveFileName(this, tr("save POU"), strDefaultFileName, "POU (*.pou)");
  875. if (m_strSavePath.isEmpty())
  876. {
  877. return;
  878. }
  879. // 开始导出
  880. VPGlobal::m_pDoc->saveSinglePou(m_strSavePath, strExportPouName);
  881. }
  882. // Program - Rename
  883. void WindowAppResourceManagerTree::doMenuPouRename(const QString& strName)
  884. {
  885. qDebug() << "WindowAppResourceManager::DoMenuPouRename";
  886. Q_UNUSED(strName);
  887. }
  888. /// <summary>
  889. /// Program - Delete
  890. /// </summary>
  891. void WindowAppResourceManagerTree::doMenuPouDelete(const QString& strName)
  892. {
  893. // qDebug() << "WindowAppResourceManager::DoMenuPouDelete";
  894. // 当前选中Pou的名字
  895. QString strDelPouName = strName;
  896. // 2021-8-17,首先需要检查待删除的Pou是否被Task选中
  897. POU* pDelPou = g_pPouManager->getPouByName(strDelPouName);
  898. if (pDelPou->isSelByTask())
  899. {
  900. Utility::VPCriticalMessageBox("Can't delete this pou : this pou is running in task[" + pDelPou->m_pParentTask->strName + "]");
  901. return;
  902. }
  903. // 查找父节点
  904. QTreeWidgetItem* pParentItem = this->currentItem()->parent();
  905. // 因为在pouReset()中会删除页面,所以此处应该可以注释掉了
  906. //if (pParentItem == m_rootNodes[(int)SUBVIEW_TYPE::SUBVIEW_POU])
  907. //{
  908. // m_pMdiFrame->deleteSubView(strDelPouName);
  909. //}
  910. // 最后删除此节点
  911. QTreeWidgetItem* currentItem = this->currentItem();
  912. pParentItem->removeChild(currentItem);
  913. // 2021-8-23增加,清除相关Pou数据结构
  914. VPGlobal::m_pDoc->pouReset(strDelPouName);
  915. ////如果没有父节点就直接删除
  916. //if (currentItem->parent() == NULL)
  917. //{
  918. // delete ui->tv_Source->takeTopLevelItem(ui->tv_Source->currentIndex().row());
  919. //}
  920. //else
  921. //{
  922. // //如果有父节点就要用父节点的takeChild删除节点
  923. // delete currentItem->parent()->takeChild(ui->tv_Source->currentIndex().row());
  924. //}
  925. }
  926. /// <summary>
  927. /// Program - Change Mode
  928. /// </summary>
  929. void WindowAppResourceManagerTree::doMenuPouChangeMode(const QString& strName)
  930. {
  931. qDebug() << "WindowAppTreeTask::DoMenuPouChangeMode";
  932. Q_UNUSED(strName);
  933. }
  934. /// <summary>
  935. /// Task - New
  936. /// </summary>
  937. void WindowAppResourceManagerTree::doMenuTaskNew()
  938. {
  939. // 生成默认名字
  940. int nTaskSize = g_pTaskManager->getAllTasks().size() + 1;
  941. QString strTaskName("Task_" + QString::number(nTaskSize));
  942. DialogNewTask dlgNewTask;
  943. dlgNewTask.setWindowModality(Qt::WindowModal);
  944. dlgNewTask.setDefaultName(strTaskName);
  945. int res = dlgNewTask.exec();
  946. if (res != QDialog::Accepted)
  947. {
  948. return;
  949. }
  950. QString taskName = dlgNewTask.m_TaskOption.strName;
  951. // 检查节点是否有重复的名称
  952. if (this->findChildItem(taskName) != nullptr)
  953. {
  954. Utility::VPCriticalMessageBox(taskName + " is duplicated!");
  955. return;
  956. }
  957. // 添加对应的节点和视图
  958. WindowAppTaskView* pTaskView = (WindowAppTaskView*)this->addTaskNode(taskName);
  959. // 将用户输入的Task参数绑定到视图中
  960. pTaskView->addNewTask(dlgNewTask.m_TaskOption);
  961. }
  962. /// <summary>
  963. /// Task - Add Pou
  964. /// </summary>
  965. void WindowAppResourceManagerTree::doMenuTaskAddPou(const QString& strTaskName)
  966. {
  967. Q_UNUSED(strTaskName);
  968. // 生成默认名字
  969. int nPouSize = g_pPouManager->getAllPous().size();
  970. QString strPouName("Pou_" + QString::number(nPouSize));
  971. DialogNewPou dlgNewPou;
  972. dlgNewPou.setWindowModality(Qt::WindowModal);
  973. dlgNewPou.setDefaultName(strPouName);
  974. int res = dlgNewPou.exec();
  975. if (res != QDialog::Accepted)
  976. {
  977. return;
  978. }
  979. QString pouName = dlgNewPou.m_strPouName + POU_POSTFIX;
  980. //Pou_TYPE pouType = dlgNewPou.pouType;
  981. // 检查节点是否有重复的名称
  982. if (this->findChildItem(pouName) != nullptr)
  983. {
  984. Utility::VPCriticalMessageBox(pouName + " is duplicated!");
  985. return;
  986. }
  987. // 添加对应的节点和视图
  988. this->addPouNode(pouName, true);
  989. // 添加 task 节点后的子节点
  990. this->addPouNodeToTask(pouName, strTaskName);
  991. }
  992. /// <summary>
  993. /// Task - Del
  994. /// </summary>
  995. void WindowAppResourceManagerTree::doMenuTaskDelete(const QString& strName)
  996. {
  997. // 获取要删除的Task名字
  998. QString strDelTaskName = strName;
  999. // 不允许删除 Task Monitor节点
  1000. if (strDelTaskName == GROUP_NAME_TASKMONITOR)
  1001. {
  1002. Utility::VPCriticalMessageBox("Can not delete TaskMonitor node!");
  1003. return;
  1004. }
  1005. // 数据结构中删除对应Task(此处需要先删数据再删视图,否则Task指针就随着视图删除也一起释放掉了)
  1006. g_pTaskManager->delTask(strDelTaskName);
  1007. // 查找父节点
  1008. QTreeWidgetItem* pParentItem = this->currentItem()->parent();
  1009. // Mdi删除对应的Task窗口
  1010. if (pParentItem == m_rootNodes[(int)SUBVIEW_TYPE::SUBVIEW_TASK])
  1011. {
  1012. m_pMdiFrame->deleteSubView(strName);
  1013. }
  1014. // 最后删除此节点
  1015. QTreeWidgetItem* currentItem = this->currentItem();
  1016. pParentItem->removeChild(currentItem);
  1017. // Task Monitor View中删除对应Task
  1018. VPGlobal::getTaskMonitorView()->delTask(strDelTaskName);
  1019. }
  1020. /// <summary>
  1021. /// UI - New
  1022. /// </summary>
  1023. void WindowAppResourceManagerTree::doMenuUiNew()
  1024. {
  1025. // 生成默认名字
  1026. int nUiSize = g_pUiManager->getAllUiNames().size() + 1;
  1027. QString strUiName("Ui_" + QString::number(nUiSize));
  1028. DialogNewUI dlgNewUI;
  1029. dlgNewUI.setWindowModality(Qt::WindowModal);
  1030. dlgNewUI.setDefaultName(strUiName);
  1031. int res = dlgNewUI.exec();
  1032. if (res != QDialog::Accepted)
  1033. {
  1034. return;
  1035. }
  1036. QString uiName = dlgNewUI.m_strUiName;
  1037. // 检查节点是否有重复的名称
  1038. // 并且不能与主页的实际名称重复才行(主页在树节点中会加main后缀)
  1039. //if (this->findChildItem(uiName) != nullptr
  1040. // && uiName!= m_uiMainTitle.strRealTitle
  1041. // )
  1042. if( g_pUiManager->getPageByName(uiName)!=nullptr )
  1043. {
  1044. Utility::VPCriticalMessageBox(uiName + " is duplicated!");
  1045. return;
  1046. }
  1047. // 添加对应的节点和视图
  1048. this->addUiNode(uiName, true);
  1049. // 2022-9-20,新增了一个UI节点的时候,也通知WindowApp切换到UI工具箱
  1050. m_pWindowApp->activeToolBoxView(APPTOOLBOX_VIEW_UI);
  1051. }
  1052. /// <summary>
  1053. /// UI - setMainPage
  1054. /// </summary>
  1055. void WindowAppResourceManagerTree::doMenuUiMainPage(const QString& strName)
  1056. {
  1057. // 通过UIManager设定主页
  1058. bool bRet = g_pUiManager->setMainPage(strName);
  1059. if (!bRet)
  1060. {
  1061. return;
  1062. }
  1063. // 如果设定成功,则首先去掉当前主页的后缀
  1064. QString strCurMain = m_strMainTitle;
  1065. if (!strCurMain.isEmpty())
  1066. {
  1067. // 枚举UI下所有节点,找到主页面节点
  1068. QTreeWidgetItemIterator it(m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_UI]);
  1069. while (*it)
  1070. {
  1071. if ((*it)->text(0) == strCurMain)
  1072. {
  1073. strCurMain.remove(UI_MAINPAGE_POSTFIX);
  1074. (*it)->setText(0, strCurMain);
  1075. break;
  1076. }
  1077. it++;
  1078. }
  1079. }
  1080. // 然后设定树形结构中新主页的名字
  1081. QString strNewMain = strName + UI_MAINPAGE_POSTFIX;
  1082. QTreeWidgetItemIterator it(m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_UI]);
  1083. while (*it)
  1084. {
  1085. if ((*it)->text(0) == strName)
  1086. {
  1087. (*it)->setText(0, strNewMain);
  1088. break;
  1089. }
  1090. it++;
  1091. }
  1092. // 最后保存新的主页信息
  1093. this->m_strMainTitle = strNewMain;
  1094. }
  1095. ///// <summary>
  1096. ///// UI - setMainPage
  1097. ///// </summary>
  1098. //void WindowAppResourceManagerTree::doMenuUiMainPage(const QString& strName)
  1099. //{
  1100. // // 通过UIManager设定主页
  1101. // bool bRet = g_pUiManager->setMainPage(strName);
  1102. //
  1103. // if (!bRet)
  1104. // {
  1105. // return;
  1106. // }
  1107. //
  1108. // // 如果设定成功,则首先去掉当前主页的后缀
  1109. // QString strCurMain = m_uiMainTitle.strShowTitle;
  1110. // if (!strCurMain.isEmpty())
  1111. // {
  1112. // // 枚举UI下所有节点,找到主页面节点
  1113. // QTreeWidgetItemIterator it(m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_UI]);
  1114. // while (*it)
  1115. // {
  1116. // if ((*it)->text(0) == strCurMain)
  1117. // {
  1118. // strCurMain.remove(UI_MAINPAGE_POSTFIX);
  1119. //
  1120. // (*it)->setText(0, strCurMain);
  1121. //
  1122. // break;
  1123. // }
  1124. //
  1125. // it++;
  1126. // }
  1127. // }
  1128. //
  1129. // // 然后设定树形结构中新主页的名字
  1130. // QString strNewMain = strName + UI_MAINPAGE_POSTFIX;
  1131. // QTreeWidgetItemIterator it(m_rootNodes[(short)SUBVIEW_TYPE::SUBVIEW_UI]);
  1132. // while (*it)
  1133. // {
  1134. // if ((*it)->text(0) == strName)
  1135. // {
  1136. // (*it)->setText(0, strNewMain);
  1137. //
  1138. // break;
  1139. // }
  1140. //
  1141. // it++;
  1142. // }
  1143. //
  1144. // // 最后保存新的主页信息
  1145. // m_uiMainTitle.strRealTitle = strName;
  1146. // m_uiMainTitle.strShowTitle = strNewMain;
  1147. //}
  1148. /// <summary>
  1149. /// UI - Del
  1150. /// </summary>
  1151. void WindowAppResourceManagerTree::doMenuUiDelete(const QString& strName)
  1152. {
  1153. //// 转换一下实际名称
  1154. //QString strRealName = strName;
  1155. //if (strName == m_uiMainTitle.strShowTitle)
  1156. //{
  1157. // strRealName = m_uiMainTitle.strRealTitle;
  1158. //}
  1159. // 通过UIManager删除页面
  1160. bool bRet = g_pUiManager->deleteUi(strName);
  1161. // 如果删除成功,则继续执行后续操作
  1162. if (!bRet)
  1163. {
  1164. return;
  1165. }
  1166. // 查找父节点
  1167. QTreeWidgetItem* pParentItem = this->currentItem()->parent();
  1168. // Mdi删除对应的Task窗口
  1169. if (pParentItem == m_rootNodes[(int)SUBVIEW_TYPE::SUBVIEW_UI])
  1170. {
  1171. m_pMdiFrame->deleteSubView(strName);
  1172. }
  1173. // 最后删除此节点
  1174. QTreeWidgetItem* currentItem = this->currentItem();
  1175. pParentItem->removeChild(currentItem);
  1176. }
  1177. /// <summary>
  1178. /// 重置(删除所有创建的节点和子视图)
  1179. /// </summary>
  1180. void WindowAppResourceManagerTree::reset()
  1181. {
  1182. // 删除所有子视图
  1183. m_pMdiFrame->deleteAllSubViews();
  1184. // 移除所有子节点
  1185. this->removeAllSubNodes();
  1186. // 2022-2-222 重新添加几个默认视图(不包括硬件组态)
  1187. // 创建默认的Task视图
  1188. this->addDefaultTaskMonitorNode(GROUP_NAME_TASKMONITOR);
  1189. // 创建默认的Log视图
  1190. this->addDefaultLogViewNode(GROUP_NAME_LOGVIEW);
  1191. // this->initDefaultViews();
  1192. }
  1193. /// <summary>
  1194. /// 添加默认的总任务管理视图节点
  1195. /// </summary>
  1196. QWidget* WindowAppResourceManagerTree::addDefaultTaskMonitorNode(QString strName)
  1197. {
  1198. // 树形结构中添加一个节点
  1199. //addChildItem(SUBVIEW_TYPE::SUBVIEW_TASK, strName);
  1200. // 然后添加对应的子窗口视图
  1201. return m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_TASKMONITOR, false);
  1202. }
  1203. /// <summary>
  1204. /// 添加默认的Debug视图节点
  1205. /// </summary>
  1206. QWidget* WindowAppResourceManagerTree::addDefaultLogViewNode(QString strName)
  1207. {
  1208. return m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_LOG, false);
  1209. }
  1210. /// <summary>
  1211. /// 添加默认的Hardware组态视图节点
  1212. /// </summary>
  1213. /// <param name="strName"></param>
  1214. /// <returns></returns>
  1215. QWidget* WindowAppResourceManagerTree::addDefaultHWConfigViewNode(QString strName)
  1216. {
  1217. //// 树形结构中添加一个节点
  1218. //addChildItem(SUBVIEW_TYPE::SUBVIEW_HDW, strName);
  1219. return m_pMdiFrame->addSubView(strName, SUBVIEW_TYPE::SUBVIEW_HDW, false);
  1220. }
  1221. /// <summary>
  1222. /// 添加一个Pou节点到Task节点
  1223. /// </summary>
  1224. /// <param name="strPouName"></param>
  1225. /// <param name="strTaskName"></param>
  1226. void WindowAppResourceManagerTree::addPouNodeToTask(QString strPouName, QString strTaskName)
  1227. {
  1228. // 查找 Task 节点
  1229. QTreeWidgetItem* itmTask = findChildItem(strTaskName);
  1230. if (itmTask != nullptr)
  1231. {
  1232. QTreeWidgetItem* newItem = new QTreeWidgetItem(itmTask, QStringList(strPouName));
  1233. // 设置图标
  1234. newItem->setIcon(0, m_appNodeIcons[(short)1]);
  1235. // 设置字体
  1236. QFont font;
  1237. font.setPointSize(8);
  1238. newItem->setFont(0, font);
  1239. // 将节点添加到界面中
  1240. itmTask->addChild(newItem);
  1241. WindowAppTaskView* pTaskView = (WindowAppTaskView*)m_pMdiFrame->findSubView(strTaskName);
  1242. if (pTaskView != nullptr)
  1243. {
  1244. QVector<POU*> pPous;
  1245. POU* pPou = g_pPouManager->getPouByName(strPouName);
  1246. if (pPou != nullptr)
  1247. {
  1248. pPous.push_back(pPou);
  1249. }
  1250. pTaskView->addTaskPous(pPous);
  1251. }
  1252. }
  1253. }