TaskWindow.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // workspace2.h : interface of the CWorkSpaceBar2 class
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. #pragma once
  5. #include "TaskTreeCtrl.h"
  6. #include "ToolManager.h"
  7. #include "CustomToolTipCtrl.h"
  8. #include "NodeWatchDlg.h"
  9. #include "HWindow.h"
  10. #include "FunctionBlock.h"
  11. #include "TaskCtrl.h"
  12. typedef unordered_map<TOOL_INTERFACE*, CNodeWatchDlg* > WATCH;
  13. class CTaskWindow : public CBCGPStatic
  14. {
  15. DECLARE_DYNAMIC(CTaskWindow)
  16. public:
  17. CTaskWindow();
  18. // Attributes
  19. protected:
  20. // Operations
  21. public:
  22. // Overrides
  23. // Implementation
  24. public:
  25. virtual ~CTaskWindow();
  26. virtual BOOL PreTranslateMessage(MSG* pMsg);
  27. // 序列化
  28. BOOL DoSerialize(CArchive& ar);
  29. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  30. // Generated message map functions
  31. protected:
  32. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  33. afx_msg void OnSize(UINT nType, int cx, int cy);
  34. // afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
  35. afx_msg void OnPaint();
  36. // 当窗体激活时触发,变更当前工作窗体
  37. afx_msg void OnSetFocus(CWnd* pOldWnd);
  38. // public:
  39. // virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
  40. #ifdef _DEBUG
  41. virtual void AssertValid() const;
  42. #ifndef _WIN32_WCE
  43. virtual void Dump(CDumpContext& dc) const;
  44. #endif
  45. #endif
  46. protected:
  47. DECLARE_MESSAGE_MAP()
  48. public:
  49. CBCGPToolBarImages& GetCaptionIcons()
  50. {
  51. return m_CaptionIcons;
  52. }
  53. protected:
  54. CBCGPToolBarImages m_CaptionIcons;
  55. // 加载.bmp格式图片
  56. CBitmap m_Bitmap[3];
  57. int m_nMarginLeft;
  58. int m_nMarginTop;
  59. int m_nMarginBottom;
  60. int m_nMarginRight;
  61. afx_msg void OnUpdate(CCmdUI* pCmdUI);
  62. afx_msg void OnBnClickedUp();
  63. afx_msg void OnBnClickedDown();
  64. afx_msg void OnBnClickedFront();
  65. afx_msg void OnBnClickedBottom();
  66. afx_msg void OnBnClickedLeftAlign();
  67. afx_msg void OnBnClickedRightAlign();
  68. afx_msg void OnBnClickedTopAlign();
  69. afx_msg void OnBnClickedBottomAlign();
  70. afx_msg void OnBnClickedZoomIn();
  71. afx_msg void OnBnClickedZoomOut();
  72. afx_msg void OnBnClickedZoomZero();
  73. afx_msg void OnBnClickedSettings();
  74. afx_msg void OnBnClickedRunOnce();
  75. static void WINAPI SysRunOce(LPVOID pParam);
  76. public:
  77. vector<TOOL> m_SelTools; // 当前任务栏中选择的工具
  78. public:
  79. // 从工具箱中添加新的工具到任务栏
  80. void AddNewTools(CString strToolName, HTREEITEM hPrevItem = NULL);
  81. // 从工具箱中添加新的工具到任务栏(用于反序列化使用)
  82. BOOL AddNewTools(const TOOL& newTool);
  83. // 从工具箱中添加新的工具到任务栏
  84. BOOL AddNewTools2(const TOOL& newTool, CString* p_strShowName);
  85. // 根据用户拖动的内容添加接口连线
  86. BOOL AddLink(const HTREEITEM& m_hItemDragS, const HTREEITEM& m_hItemDragD);
  87. // 根据用户拖动的内容添加接口连线 // 连接的起点 // 连接的终点
  88. BOOL AddLink(const TOOL_INTERFACE& source, const TOOL_INTERFACE& dest, bool bIsLoad = false);
  89. // 根据用户拖动的内容添加接口连线
  90. BOOL AddTestLink(const HTREEITEM& m_hItemDragS, const HTREEITEM& m_hItemDragD);
  91. // 添加工具到任务栏的时候智能链接
  92. void SmartLink();
  93. // 通过输入的目标端口执行智能链接动作
  94. void ToSmartLink(const TOOL_INTERFACE& DestInf);
  95. //// 判断一个节点是否是输入节点
  96. //BOOL IsInputNode(const HTREEITEM& handle);
  97. //// 判断一个节点是否是输出节点
  98. //BOOL IsOutputNode(const HTREEITEM& handle);
  99. //// 获取接口的输入输出方式
  100. //INF_WAY GetInterfaceByHandle(const HTREEITEM& handle);
  101. // 绘制链接
  102. void DrawLink(CDC* pDC);
  103. // 绘制预览链接
  104. void DrawPreviewLink(CDC* pDC);
  105. // 更新节点数据结构
  106. void UpdateTools(bool bIsLoad = false );
  107. // 更新现有工具
  108. void UpdateExistTool(TOOL& tool, TOOL& tool_Info);
  109. // 更新新工具
  110. void UpdateNewTool(const CString& strToolShowName,TOOL tool_Info, bool bIsLoad = false);
  111. // 根据句柄转换成对应的TOOL
  112. TOOL* GetToolByHandle(const HTREEITEM& handle);
  113. // 根据显示的名称转换乘对应的TOOL
  114. TOOL* GetToolByShowName(const CString strName);
  115. // 根据句柄转换成对应的Interface
  116. TOOL_INTERFACE* GetInterfaceByHandle(const HTREEITEM& handle);
  117. // 根据 名字 转换成对应的Interface
  118. TOOL_INTERFACE* GetInterfaceByName(const CString strParentName, const CString strName);
  119. //// 更新节点存储数据结构
  120. //void UpdateTools(const HTREEITEM& m_hItemDragS, const HTREEITEM& m_hItemDragD);
  121. //// 更新所有节点的句柄
  122. //void UpdateHandles();
  123. //// 更新所有节点的连接信息
  124. //void UpdateLink();
  125. // 重置整个TaskWindow的相关数据
  126. void Reset();
  127. // CTaskTreeCtrl m_wndTree; // 树形控件
  128. CTaskCtrl m_editor;
  129. LINK_MAP m_TsetLinks;
  130. protected:
  131. // 更新所有节点的连接信息
  132. void UpdateLink();
  133. // 具体的进行连线绘制
  134. //void DrawLinkLine(CDC* pDC, const CRect& rcStart , const CRect& rcEnd);
  135. //BOOL UpdateLinkInfo(TOOL_INTERFACE& info);
  136. // 建立连接
  137. bool MakeLink(const TOOL_INTERFACE& source, const TOOL_INTERFACE& dest, bool bShow = true);
  138. // 建立Test连接
  139. bool MakeTestLink(const TOOL_INTERFACE& source, const TOOL_INTERFACE& dest);
  140. // 判断是否是同一个接口
  141. bool IsSameInterface(const TOOL_INTERFACE& source, const TOOL_INTERFACE& dest);
  142. // 计算连接线的长度
  143. int CalcLinkLineLength(const TOOL_INTERFACE& source, const TOOL_INTERFACE& dest);
  144. // 通过工具显示的名字查询工具真正的名字
  145. CString GetToolRealname(const CString& strShowName);
  146. // 计算连线的绘制坐标
  147. LINK_POINTS CalcLinkPoints(const LINK& lnk);
  148. // 计算连线的触发区域
  149. LINK_RECTS CalcLinkRects(const LINK_POINTS& lps);
  150. // 获取某个Interface相连的接口
  151. TOOL_INTERFACE* GetHigherLinkInterface(const TOOL_INTERFACE& inf);
  152. // 根据Interface获取Tool
  153. // TOOL* GetToolByInterface(TOOL_INTERFACE& inf);
  154. //// 为节点的参数拷贝值
  155. //void AssignParamValue(PARAM& dest, const PARAM& source);
  156. //// 为节点的参数赋值(模板)
  157. //template<typename T>
  158. //void AssignParamValue(PARAM& dest, const T& value);
  159. // 从dll中获取工具指针
  160. BOOL GetToolPtrByDll( TOOL& tool );
  161. // 保存工具信息到文件中
  162. BOOL SaveToFile(CArchive& ar);
  163. // 从文件中加载工具信息
  164. BOOL LoadFromFile(CArchive& ar);
  165. void OnBnClickedButtonCopy();
  166. void OnBnClickedButtonPaste();
  167. void SortProtToolID(const TOOL* pSourceTool, const TOOL* pDestTool);
  168. void ShowNodeWatch(/*const*/ TOOL_INTERFACE* pInf);
  169. void ValueLink(/*const */TOOL_INTERFACE* pLinkInf,/* const*/ TOOL_INTERFACE& inf);
  170. public:
  171. // 调用dll中的Input
  172. template<typename T>
  173. void CallInputByDll(T& t, TOOL_INTERFACE& inf);
  174. public:
  175. template<typename T>
  176. void CallInputByDllAddress(T& t, TOOL_INTERFACE& inf);
  177. // 根据Interface获取Tool
  178. TOOL* GetToolByInterface(TOOL_INTERFACE& inf);
  179. // 根据Interface获取Interface
  180. TOOL_INTERFACE* GetInterfaceByInterface(TOOL_INTERFACE& inf);
  181. // 根据Tool获取Tool
  182. TOOL* GetToolByTool(TOOL& tool);
  183. public:
  184. // 调用dll中的Output
  185. template<typename T>
  186. T& CallOutputByDll(TOOL_INTERFACE& inf);
  187. // 执行Dll的Input操作(根据不同类型,不同的处理)
  188. bool DoDllInput(/*TOOL& tool,*/ TOOL_INTERFACE& inf);
  189. bool DoDllProt(TOOL& tool, TOOL_INTERFACE& inf);
  190. //// 执行Dll的Output操作(根据不同类型,不同的处理)
  191. //BOOL DoDllOutput(TOOL_INTERFACE& inf);
  192. // 执行Dll的Execute操作(根据不同类型,不同的处理)
  193. int DoDllExecute(TOOL& tool);
  194. // 专门用来获取Dll中绑定控件的接口
  195. CWnd* DoDllControlOutputWnd(TOOL_INTERFACE& inf);
  196. QHalconWindow*& DoDllControlOutputImage(TOOL_INTERFACE& inf);
  197. // 执行任务 2018.10.15
  198. void RunProcess();
  199. void ExProt(/*const*/ TOOL& tool);
  200. // 当 Item单击时候
  201. void OnItemLButtonDown(const HTREEITEM& handle);
  202. // 当Item双击时 根节点
  203. void OnItemDblclk(const HTREEITEM& handle);
  204. // 当Item双击时 子节点
  205. void OnItemNodeDblclk(const HTREEITEM& handle);
  206. // EnableItem
  207. bool OnItemEnable(const HTREEITEM& handle);
  208. // 删除Item
  209. void OnItemDelete(const HTREEITEM& handle);
  210. // 判断有无链接
  211. bool IsLink(const HTREEITEM& handle);
  212. // 判断是否受保护
  213. bool IsProtected(const HTREEITEM& handle);
  214. // 编辑工具名称
  215. void OnItemToolRename(const HTREEITEM& handle);
  216. // 编辑工具名称
  217. void ToolRename(TOOL* pTool, const CString strNewName);
  218. // 当Item执行时
  219. void OnItemExecute(const HTREEITEM& handle);
  220. // Execute All
  221. void OnItemExecuteAll();
  222. // OnExecuteT ool
  223. void OnExecuteTool(CString strToolName);
  224. void OnUpdateToolState(CString strToolName, int nState);
  225. // 右键菜单Link to...
  226. void OnItemLink(const HTREEITEM& source, UINT nLinkDestIndex);
  227. //// 右键菜单Link from...
  228. //void OnItemLinkFrom(HTREEITEM hSelItem);
  229. // 右键菜单UnLink
  230. void OnItemUnLink(HTREEITEM hSelItem);
  231. // 根据右键菜单获取可以link的接口,并且返回本接口是输入还是输出
  232. INF_DIRECTION GetPopLinkNodes(vector<TOOL_INTERFACE>&, HTREEITEM hSelItem);
  233. // 动态隐藏接口
  234. void OnItemHideInterface(const HTREEITEM& handle);
  235. // 添加一个新的输入接口到所选的工具中,返回插入的索引值
  236. int AddDynamicInterface(TOOL& activeTool, const DLL_INF& newInf, const HTREEITEM& handle);
  237. // 添加一个新的接口到界面中
  238. void AddNewInterfaceToTree(const HTREEITEM& handle, TOOL_INTERFACE& newInf, const int& nPos);
  239. //获取端口的UI值
  240. INTERFACE_DISP_TYPE UpdateUIValue(CString& str, HImage* hv_Image, HObject* hv_Obj, TOOL_INTERFACE* pInf);
  241. // 为Tool添加执行结果的图标
  242. void DrawToolResult(NMLVCUSTOMDRAW* pLVCD);
  243. // 为Tool添加执行结果的 Colour
  244. void DrawToolColour(NMLVCUSTOMDRAW* pLVCD);
  245. // 重置所有工具执行状态
  246. void ResetToolExecCode();
  247. // 重绘Tool的返回值状态
  248. void RedrawToolExecCode(const TOOL& tool);
  249. //
  250. static void CALLBACK ToolStateCallback(CString strToolName, int nState, long long userParam);// Log 回调
  251. //
  252. static void CALLBACK ToolLogCallback(CString strToolName, MSG_WAY nType, CString strMassage, long long userParam);// Log 回调
  253. // //
  254. static void CALLBACK ToolOnExecuteCallback(CString strToolName, long long userParam);// Log 回调
  255. //
  256. static void CALLBACK ToolBusyCallback(CString strToolName, int nState, CString strMassage, long long userParam);// ToolBusy 回调
  257. protected:
  258. // 绘制使用的画笔
  259. CPen m_penLink; // 绘制连接
  260. CPen m_penLinkBold; // 绘制连接(粗线)
  261. CPen m_penLinkTest; // 绘制连接(虚线)
  262. map<CString, CString> m_RenameList; // 工具重命名的字典表
  263. vector<TOOL_INTERFACE> m_PopLinkNodes; // 右键菜单弹出的Link to列表
  264. CCustomToolTipCtrl* m_ToolTip;
  265. HTREEITEM m_Item;
  266. CBCGPToolBar m_wndToolBar;
  267. public:
  268. //主任务:即该任务线程不论任何时候,都一直循环运行。点击了主页上的运行按钮后,一直循环运行
  269. //事件任务: 该任务线程不能主动运行。(不参与循环运行),只在某个事件发生时候执行,并且只执行一次,事件由工具产生
  270. TASK_MODE m_TaskMode; // 工具的运行模式。主任务 或事件任务
  271. LINK_MAP m_Links; // 接口之间的连接信息(key为输入接口,唯一)
  272. HANDLE m_hExecuteEven; // 管理当前任务的执行
  273. // HANDLE m_hBusyEven; // 当前任务执行完成的事件
  274. HANDLE m_hDoneEven; // 当前任务执行完成的事件
  275. int m_RunningTime; // 当前任务的执行时间
  276. double m_dRunningCount; // 执行计数
  277. CWinThread* m_hThread; // 线程句柄
  278. bool m_bExitThread; // 用于退出线程
  279. int m_nIndex; // 当前任务窗体的索引号
  280. int m_nSleepTime; // 任务执行间隔时间
  281. CString m_strTaskName; // 任务窗体的名称
  282. protected:
  283. public:
  284. void WatchClear(); //
  285. WATCH mWatch; //
  286. };
  287. // 调用dll中的Input
  288. template<typename T>
  289. void CTaskWindow::CallInputByDllAddress(T& t, TOOL_INTERFACE& inf)
  290. {
  291. // 由于该模板函数没有在该类中实例化,所以编译器无法知道模板的定义,将该模板函数的定义放在头文件中是个不错的选择
  292. TOOL* pTool = GetToolByInterface(inf);
  293. if (pTool != NULL)
  294. {
  295. ASSERT(pTool != NULL);
  296. CDllTool* pPtr = pTool->pDllPtr;
  297. ASSERT(pPtr != NULL);
  298. pPtr->SetValue<T>(inf.nIndex - 1, t, INF_BY_ADDRESS);
  299. }
  300. else
  301. {
  302. CString strMsg;
  303. strMsg.Format(_T("%s.%s"), inf.strParentName, inf.strName);
  304. USES_CONVERSION;
  305. char * s_str = T2A(strMsg);
  306. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  307. }
  308. }
  309. /////////////////////////////////////////////////////////////////////////////