ModuleManageDialog.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. using Resources;
  2. using SmartCoalApplication.Core;
  3. using SmartCoalApplication.Core.CustomControl;
  4. using SmartCoalApplication.Resources;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace SmartCoalApplication.Rule
  16. {
  17. /// <summary>
  18. /// 设置->模块管理
  19. /// </summary>
  20. internal class ModuleManageDialog : PdnBaseForm
  21. {
  22. private AppWorkspace appWorkspace;
  23. private ToolStripItemCollection collectionLeft;
  24. private TreeNode animeLeft;
  25. private string txtPath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\ModuleConfig.txt";
  26. private string[] menuIdArr;
  27. public ModuleManageDialog(AppWorkspace appWorkspace)
  28. {
  29. this.appWorkspace = appWorkspace;
  30. this.ShowInTaskbar = false;
  31. InitializeComponent();
  32. InitializeLanguageText();
  33. this.Icon = PdnInfo.AppIcon;
  34. this.treeView1.CheckBoxes = true;
  35. InitVisibleMenuId();
  36. InitLeftTreeViewData();
  37. this.Text = PdnResources.GetString("Menu.Setting.ModuleSetting.Text");
  38. }
  39. private void InitializeLanguageText()
  40. {
  41. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  42. this.button2.Text = PdnResources.GetString("ConfirmLanguageDialog.CancelTB.ActionText");
  43. this.button1.Text = PdnResources.GetString("Menu.application.text");
  44. this.groupBox2.Text = PdnResources.GetString("Menu.Set.Focusparams.Availablemodules.text");
  45. this.label1.Text = PdnResources.GetString("Menu.Set.Modulemanagement.Restartthesngs.text");
  46. }
  47. /// <summary>
  48. /// 获取txt文件中已保存的菜单可用id
  49. /// </summary>
  50. private void InitVisibleMenuId()
  51. {
  52. if (System.IO.File.Exists(txtPath))
  53. {
  54. string str = System.IO.File.ReadAllText(txtPath);
  55. if (str.IndexOf(',') != -1)
  56. {
  57. menuIdArr = str.Split(',');
  58. }
  59. else
  60. {
  61. if (!string.IsNullOrEmpty(str))
  62. {
  63. menuIdArr = new string[] { str };
  64. }
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 初始化左侧treeview数据
  70. /// </summary>
  71. private void InitLeftTreeViewData()
  72. {
  73. this.collectionLeft = this.appWorkspace.ToolBar.MainMenu.Items;
  74. this.animeLeft = new TreeNode(PdnResources.GetString("Menu.menu.Text"));
  75. this.RecursiveDataLeft(collectionLeft, animeLeft);
  76. this.animeLeft.Expand();
  77. this.treeView1.Nodes.Add(animeLeft);
  78. }
  79. /// <summary>
  80. /// 左侧递归进行数据组织
  81. /// </summary>
  82. private void RecursiveDataLeft(ToolStripItemCollection collection, TreeNode anime)
  83. {
  84. for (int i = 0; i < collection.Count; i++)
  85. {
  86. TreeNode node = new TreeNode(collection[i].Text);
  87. if (collection[i].GetType() != typeof(ToolStripSeparator))
  88. {
  89. PdnMenuItem item = (PdnMenuItem)collection[i];
  90. if (!item.CanShowInSenseShield)
  91. continue;
  92. node.Tag = item.MenuId;
  93. if (menuIdArr != null && menuIdArr.Count() > 0)
  94. {
  95. if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
  96. {
  97. node.Checked = true;
  98. }
  99. else
  100. {
  101. node.Checked = false;
  102. //兄弟节点只要有一个没选,其父节点也不选//#20756
  103. TreeNode nodeParent = anime;// node.Parent;
  104. while (nodeParent != null)
  105. {
  106. nodeParent.Checked = false;
  107. nodeParent = nodeParent.Parent;
  108. }
  109. }
  110. }
  111. anime.Nodes.Add(node);
  112. int index = 0;
  113. foreach (TreeNode node1 in anime.Nodes) {
  114. if (!node1.Checked)
  115. index++;
  116. }
  117. if (index == 0) {
  118. anime.Checked = true;
  119. }
  120. if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
  121. continue;
  122. RecursiveDataLeft(((PdnMenuItem)collection[i]).DropDownItems, node);
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// 确定
  128. /// </summary>
  129. /// <param name="sender"></param>
  130. /// <param name="e"></param>
  131. private void button1_Click(object sender, EventArgs e)
  132. {
  133. TreeNodeCollection nodes = this.animeLeft.Nodes;
  134. string str = "";
  135. foreach (TreeNode node in nodes)
  136. {
  137. if (node.GetType() != typeof(ToolStripSeparator))
  138. {
  139. if (node.Checked)
  140. {
  141. if (str == "")
  142. {
  143. str += node.Tag.ToString();
  144. }
  145. else
  146. {
  147. str += "," + node.Tag.ToString();
  148. }
  149. }
  150. if (node.Nodes.Count > 0)
  151. {
  152. bool nodeHasChildChecked;
  153. str = RecursionMenuId(node, str, out nodeHasChildChecked);
  154. if (!node.Checked && nodeHasChildChecked)
  155. {
  156. if (str == "")
  157. {
  158. str += node.Tag.ToString();
  159. }
  160. else
  161. {
  162. str += "," + node.Tag.ToString();
  163. }
  164. }
  165. }
  166. }
  167. }
  168. //FileStream fs = new FileStream(txtPath, FileMode.Create);
  169. //StreamWriter sw = new StreamWriter(fs);
  170. StreamWriter sw = new StreamWriter(txtPath, false);
  171. sw.Write(str);
  172. sw.Close();
  173. //fs.Close();
  174. sw.Dispose();
  175. //fs.Dispose();
  176. this.Close();
  177. }
  178. /// <summary>
  179. /// 递归获取所有选中的菜单id
  180. /// </summary>
  181. /// <param name="node"></param>
  182. /// <param name="str"></param>
  183. /// <returns></returns>
  184. private string RecursionMenuId(TreeNode node, string str, out bool hasChildChecked)
  185. {
  186. hasChildChecked = false;
  187. if (node.Nodes.Count > 0)
  188. {
  189. foreach (TreeNode tn in node.Nodes)
  190. {
  191. if (tn.Checked)
  192. {
  193. if (str == "")
  194. {
  195. str += tn.Tag.ToString();
  196. }
  197. else
  198. {
  199. str += "," + tn.Tag.ToString();
  200. }
  201. hasChildChecked = true;
  202. }
  203. if (tn.Nodes.Count > 0)
  204. {
  205. bool tnHasChildChecked;
  206. str = RecursionMenuId(tn, str, out tnHasChildChecked);
  207. if (!tn.Checked && tnHasChildChecked)
  208. {
  209. if (str == "")
  210. {
  211. str += tn.Tag.ToString();
  212. }
  213. else
  214. {
  215. str += "," + tn.Tag.ToString();
  216. }
  217. hasChildChecked = true;
  218. }
  219. }
  220. }
  221. }
  222. return str;
  223. }
  224. /// <summary>
  225. /// 取消
  226. /// </summary>
  227. /// <param name="sender"></param>
  228. /// <param name="e"></param>
  229. private void button2_Click(object sender, EventArgs e)
  230. {
  231. this.Close();
  232. }
  233. private string SpliceCheckedChildIds(TreeNode node, string childIds)
  234. {
  235. if (node.Nodes.Count > 0)
  236. {
  237. for (int i = 0; i < node.Nodes.Count; i++)
  238. {
  239. if (node.Nodes[i].GetType() != typeof(ToolStripSeparator))
  240. {
  241. if (node.Nodes[i].Checked)
  242. {
  243. if (childIds.Equals(""))
  244. {
  245. childIds = node.Nodes[i].Tag.ToString();
  246. }
  247. else
  248. {
  249. childIds += "," + node.Nodes[i].Tag.ToString();
  250. }
  251. if (node.Nodes[i].Nodes.Count > 0)
  252. {
  253. SpliceCheckedChildIds(node.Nodes[i], childIds);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. return childIds;
  260. }
  261. private void treeView1_MouseClick(object sender, MouseEventArgs e)
  262. {
  263. //TreeNode node = treeView1.GetNodeAt(new Point(e.X, e.Y));
  264. //if (node != null)
  265. //{
  266. // ChangeChild(node, node.Checked);//影响子节点
  267. // ChangeParent(node);//影响父节点
  268. //}
  269. }
  270. //递归子节点跟随其全选或全不选
  271. private void ChangeChild(TreeNode node, bool state)
  272. {
  273. node.Checked = state;
  274. foreach (TreeNode tn in node.Nodes)
  275. ChangeChild(tn, state);
  276. }
  277. //递归父节点跟随其全选或全不选
  278. private void ChangeParent(TreeNode node)
  279. {
  280. if (node.Parent != null)
  281. {
  282. //兄弟节点被选中的个数
  283. int brotherNodeCheckedCount = 0;
  284. //遍历该节点的兄弟节点
  285. foreach (TreeNode tn in node.Parent.Nodes)
  286. {
  287. if (tn.Checked == true)
  288. brotherNodeCheckedCount++;
  289. }
  290. //兄弟节点全没选,其父节点也不选
  291. if (brotherNodeCheckedCount == 0)
  292. {
  293. node.Parent.Checked = false;
  294. ChangeParent(node.Parent);
  295. }
  296. //兄弟节点只要有一个被选,其父节点也被选
  297. if (brotherNodeCheckedCount >= 1)
  298. {
  299. node.Parent.Checked = true;
  300. ChangeParent(node.Parent);
  301. }
  302. }
  303. }
  304. /*private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
  305. {
  306. if (e.Node != null)
  307. {
  308. this.treeView1.AfterCheck -= treeView1_AfterCheck;
  309. ChangeChild(e.Node, e.Node.Checked);//影响子节点
  310. ChangeParent(e.Node);//影响父节点
  311. this.treeView1.AfterCheck += treeView1_AfterCheck;
  312. }
  313. }*/
  314. private bool nextCheck(TreeNode n) //判断同级的节点是否全选
  315. {
  316. foreach (TreeNode tn in n.Parent.Nodes)
  317. {
  318. if (tn.Checked == false) return false;
  319. }
  320. return true;
  321. }
  322. private bool nextNotCheck(TreeNode n) //判断同级的节点是否全不选
  323. {
  324. if (n.Checked == true)
  325. {
  326. return false;
  327. }
  328. if (n.NextNode == null)
  329. {
  330. return true;
  331. }
  332. return this.nextNotCheck(n.NextNode);
  333. }
  334. private void cycleChild(TreeNode tn, bool check) //遍历节点下的子节点
  335. {
  336. if (tn.Nodes.Count != 0)
  337. {
  338. foreach (TreeNode child in tn.Nodes)
  339. {
  340. child.Checked = check;
  341. if (child.Nodes.Count != 0)
  342. {
  343. cycleChild(child, check);
  344. }
  345. }
  346. }
  347. else
  348. return;
  349. }
  350. private void cycleParent(TreeNode tn, bool check) //遍历节点上的父节点
  351. {
  352. if (tn.Parent != null)
  353. {
  354. if (nextCheck(tn))
  355. {
  356. tn.Parent.Checked = true;
  357. }
  358. else
  359. {
  360. tn.Parent.Checked = false;
  361. }
  362. cycleParent(tn.Parent, check);
  363. }
  364. return;
  365. }
  366. // afterCheck
  367. private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) //当选中或取消选中树节点上的复选框时发生
  368. {
  369. //要求父节点被勾选,则子节点全部被勾选;父节点不被勾选,则子节点不全不被勾选
  370. if (e.Node.Checked == true)
  371. {
  372. if (e.Action != TreeViewAction.Unknown)
  373. {
  374. cycleChild(e.Node, true);
  375. }
  376. if (e.Node.Parent != null)
  377. {
  378. if (nextCheck(e.Node))
  379. {
  380. cycleParent(e.Node, true);
  381. }
  382. else
  383. {
  384. cycleParent(e.Node, false);
  385. }
  386. }
  387. }
  388. if (e.Node.Checked == false)
  389. {
  390. if (e.Action != TreeViewAction.Unknown)
  391. {
  392. cycleChild(e.Node, false); //中间节点不选中则子节点全部不选中
  393. cycleParent(e.Node, false); //父节点不选中
  394. }
  395. // bCheck = false;
  396. }
  397. return;
  398. }
  399. int nodeClicks = 0;
  400. TreeViewHitTestInfo hitInfo = null;
  401. private void treeView1_MouseDown(object sender, MouseEventArgs e)
  402. {
  403. hitInfo = treeView1.HitTest(e.Location);
  404. nodeClicks = e.Clicks;
  405. }
  406. private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  407. {
  408. if (nodeClicks > 1 && hitInfo.Location == TreeViewHitTestLocations.Label)
  409. {
  410. e.Cancel = true;
  411. }
  412. }
  413. #region 控件
  414. /// <summary>
  415. /// Required designer variable.
  416. /// </summary>
  417. private System.ComponentModel.IContainer components = null;
  418. /// <summary>
  419. /// Clean up any resources being used.
  420. /// </summary>
  421. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  422. protected override void Dispose(bool disposing)
  423. {
  424. if (disposing && (components != null))
  425. {
  426. components.Dispose();
  427. }
  428. base.Dispose(disposing);
  429. }
  430. #region Windows Form Designer generated code
  431. /// <summary>
  432. /// Required method for Designer support - do not modify
  433. /// the contents of this method with the code editor.
  434. /// </summary>
  435. private void InitializeComponent()
  436. {
  437. this.groupBox1 = new System.Windows.Forms.GroupBox();
  438. this.button2 = new System.Windows.Forms.Button();
  439. this.button1 = new System.Windows.Forms.Button();
  440. this.groupBox2 = new System.Windows.Forms.GroupBox();
  441. this.treeView1 = new SmartCoalApplication.Core.CustomControl.TreeViewEnhanced();
  442. this.label1 = new System.Windows.Forms.Label();
  443. this.groupBox1.SuspendLayout();
  444. this.groupBox2.SuspendLayout();
  445. this.SuspendLayout();
  446. //
  447. // groupBox1
  448. //
  449. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  450. | System.Windows.Forms.AnchorStyles.Right)));
  451. this.groupBox1.Controls.Add(this.button2);
  452. this.groupBox1.Controls.Add(this.button1);
  453. this.groupBox1.Location = new System.Drawing.Point(13, 13);
  454. this.groupBox1.Name = "groupBox1";
  455. this.groupBox1.Size = new System.Drawing.Size(301, 58);
  456. this.groupBox1.TabIndex = 1;
  457. this.groupBox1.TabStop = false;
  458. this.groupBox1.Text = "操作";
  459. //
  460. // button2
  461. //
  462. this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  463. this.button2.Location = new System.Drawing.Point(220, 20);
  464. this.button2.Name = "button2";
  465. this.button2.Size = new System.Drawing.Size(75, 23);
  466. this.button2.TabIndex = 1;
  467. this.button2.Text = "取消";
  468. this.button2.UseVisualStyleBackColor = true;
  469. this.button2.Click += new System.EventHandler(this.button2_Click);
  470. //
  471. // button1
  472. //
  473. this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  474. this.button1.Location = new System.Drawing.Point(138, 20);
  475. this.button1.Name = "button1";
  476. this.button1.Size = new System.Drawing.Size(75, 23);
  477. this.button1.TabIndex = 0;
  478. this.button1.Text = "确定";
  479. this.button1.UseVisualStyleBackColor = true;
  480. this.button1.Click += new System.EventHandler(this.button1_Click);
  481. //
  482. // groupBox2
  483. //
  484. this.groupBox2.Controls.Add(this.treeView1);
  485. this.groupBox2.Location = new System.Drawing.Point(13, 78);
  486. this.groupBox2.Name = "groupBox2";
  487. this.groupBox2.Size = new System.Drawing.Size(301, 467);
  488. this.groupBox2.TabIndex = 2;
  489. this.groupBox2.TabStop = false;
  490. this.groupBox2.Text = "可用模块";
  491. //
  492. // treeView1
  493. //
  494. this.treeView1.Location = new System.Drawing.Point(7, 21);
  495. this.treeView1.Name = "treeView1";
  496. this.treeView1.Size = new System.Drawing.Size(288, 440);
  497. this.treeView1.TabIndex = 0;
  498. this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
  499. this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
  500. this.treeView1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseClick);
  501. this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
  502. //
  503. // label1
  504. //
  505. this.label1.AutoSize = true;
  506. this.label1.Location = new System.Drawing.Point(13, 551);
  507. this.label1.Name = "label1";
  508. this.label1.Size = new System.Drawing.Size(125, 12);
  509. this.label1.TabIndex = 3;
  510. this.label1.Text = "更改模块后请重启软件";
  511. //
  512. // ModuleManageDialog
  513. //
  514. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  515. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  516. this.ClientSize = new System.Drawing.Size(326, 577);
  517. this.Controls.Add(this.label1);
  518. this.Controls.Add(this.groupBox2);
  519. this.Controls.Add(this.groupBox1);
  520. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  521. this.MaximizeBox = false;
  522. this.MinimizeBox = false;
  523. this.Name = "ModuleManageDialog";
  524. this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  525. this.Text = "模块管理";
  526. this.Controls.SetChildIndex(this.groupBox1, 0);
  527. this.Controls.SetChildIndex(this.groupBox2, 0);
  528. this.Controls.SetChildIndex(this.label1, 0);
  529. this.groupBox1.ResumeLayout(false);
  530. this.groupBox2.ResumeLayout(false);
  531. this.ResumeLayout(false);
  532. this.PerformLayout();
  533. }
  534. #endregion
  535. private System.Windows.Forms.GroupBox groupBox1;
  536. private System.Windows.Forms.Button button2;
  537. private System.Windows.Forms.Button button1;
  538. private System.Windows.Forms.GroupBox groupBox2;
  539. private TreeViewEnhanced treeView1;
  540. private System.Windows.Forms.Label label1;
  541. #endregion
  542. }
  543. }