ModuleManageDialog.cs 21 KB

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