ModuleManageDialog.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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.animeLeft.Checked = true;
  71. this.RecursiveDataLeft(collectionLeft, animeLeft);
  72. this.animeLeft.Expand();
  73. this.treeView1.Nodes.Add(animeLeft);
  74. }
  75. /// <summary>
  76. /// 左侧递归进行数据组织
  77. /// </summary>
  78. private void RecursiveDataLeft(ToolStripItemCollection collection, TreeNode anime)
  79. {
  80. for (int i = 0; i < collection.Count; i++)
  81. {
  82. TreeNode node = new TreeNode(collection[i].Text);
  83. if (collection[i] is PdnMenuItem)
  84. {
  85. if (!(collection[i] is PdnMenuItem)) continue;
  86. PdnMenuItem item = (PdnMenuItem)collection[i];
  87. if (!item.CanShowInSenseShield)
  88. continue;
  89. node.Tag = item.MenuId;
  90. if (menuIdArr != null && menuIdArr.Count() > 0)
  91. {
  92. if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
  93. {
  94. node.Checked = true;
  95. }
  96. else
  97. {
  98. node.Checked = false;
  99. //兄弟节点只要有一个没选,其父节点也不选//#20756
  100. TreeNode nodeParent = anime;// node.Parent;
  101. while (nodeParent != null)
  102. {
  103. nodeParent.Checked = false;
  104. nodeParent = nodeParent.Parent;
  105. }
  106. }
  107. }
  108. //if (Startup.instance.moduleConfigModel.items.Find(a => a.ParentId == item.MenuId) != null
  109. // || Startup.instance.moduleConfigModel.items.Find(a => a.ChildIds.Split(',').Contains(item.MenuId.ToString())) != null)
  110. //{
  111. // node.Checked = true;
  112. //}
  113. anime.Nodes.Add(node);
  114. if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
  115. continue;
  116. RecursiveDataLeft(((PdnMenuItem)collection[i]).DropDownItems, node);
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// 确定
  122. /// </summary>
  123. /// <param name="sender"></param>
  124. /// <param name="e"></param>
  125. private void button1_Click(object sender, EventArgs e)
  126. {
  127. TreeNodeCollection nodes = this.animeLeft.Nodes;
  128. string str = "";
  129. foreach (TreeNode node in nodes)
  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. //FileStream fs = new FileStream(txtPath, FileMode.Create);
  160. //StreamWriter sw = new StreamWriter(fs);
  161. StreamWriter sw = new StreamWriter(txtPath, false);
  162. sw.Write(str);
  163. sw.Close();
  164. //fs.Close();
  165. sw.Dispose();
  166. //fs.Dispose();
  167. this.Close();
  168. }
  169. /// <summary>
  170. /// 递归获取所有选中的菜单id
  171. /// </summary>
  172. /// <param name="node"></param>
  173. /// <param name="str"></param>
  174. /// <returns></returns>
  175. private string RecursionMenuId(TreeNode node, string str, out bool hasChildChecked)
  176. {
  177. hasChildChecked = false;
  178. if (node.Nodes.Count > 0)
  179. {
  180. foreach (TreeNode tn in node.Nodes)
  181. {
  182. if (tn.Checked)
  183. {
  184. if (str == "")
  185. {
  186. str += tn.Tag.ToString();
  187. }
  188. else
  189. {
  190. str += "," + tn.Tag.ToString();
  191. }
  192. hasChildChecked = true;
  193. }
  194. if (tn.Nodes.Count > 0)
  195. {
  196. bool tnHasChildChecked;
  197. str = RecursionMenuId(tn, str, out tnHasChildChecked);
  198. if (!tn.Checked && tnHasChildChecked)
  199. {
  200. if (str == "")
  201. {
  202. str += tn.Tag.ToString();
  203. }
  204. else
  205. {
  206. str += "," + tn.Tag.ToString();
  207. }
  208. hasChildChecked = true;
  209. }
  210. }
  211. }
  212. }
  213. return str;
  214. }
  215. /// <summary>
  216. /// 取消
  217. /// </summary>
  218. /// <param name="sender"></param>
  219. /// <param name="e"></param>
  220. private void button2_Click(object sender, EventArgs e)
  221. {
  222. this.Close();
  223. }
  224. //递归父节点跟随其全选或全不选
  225. private void ChangeParent(TreeNode node)
  226. {
  227. if (node.Parent != null)
  228. {
  229. //兄弟节点被选中的个数
  230. int brotherNodeCheckedCount = 0;
  231. //遍历该节点的兄弟节点
  232. foreach (TreeNode tn in node.Parent.Nodes)
  233. {
  234. if (tn.Checked == true)
  235. brotherNodeCheckedCount++;
  236. }
  237. //兄弟节点全没选,其父节点也不选
  238. if (brotherNodeCheckedCount == 0)
  239. {
  240. node.Parent.Checked = false;
  241. ChangeParent(node.Parent);
  242. }
  243. //兄弟节点只要有一个被选,其父节点也被选
  244. if (brotherNodeCheckedCount >= 1)
  245. {
  246. node.Parent.Checked = true;
  247. ChangeParent(node.Parent);
  248. }
  249. }
  250. }
  251. /*private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
  252. {
  253. if (e.Node != null)
  254. {
  255. this.treeView1.AfterCheck -= treeView1_AfterCheck;
  256. ChangeChild(e.Node, e.Node.Checked);//影响子节点
  257. ChangeParent(e.Node);//影响父节点
  258. this.treeView1.AfterCheck += treeView1_AfterCheck;
  259. }
  260. }*/
  261. private bool nextCheck(TreeNode n) //判断同级的节点是否全选
  262. {
  263. foreach (TreeNode tn in n.Parent.Nodes)
  264. {
  265. if (tn.Checked == false) return false;
  266. }
  267. return true;
  268. }
  269. private bool nextNotCheck(TreeNode n) //判断同级的节点是否全不选
  270. {
  271. if (n.Checked == true)
  272. {
  273. return false;
  274. }
  275. if (n.NextNode == null)
  276. {
  277. return true;
  278. }
  279. return this.nextNotCheck(n.NextNode);
  280. }
  281. private void cycleChild(TreeNode tn, bool check) //遍历节点下的子节点
  282. {
  283. if (tn.Nodes.Count != 0)
  284. {
  285. foreach (TreeNode child in tn.Nodes)
  286. {
  287. child.Checked = check;
  288. if (child.Nodes.Count != 0)
  289. {
  290. cycleChild(child, check);
  291. }
  292. }
  293. }
  294. else
  295. return;
  296. }
  297. private void cycleParent(TreeNode tn, bool check) //遍历节点上的父节点
  298. {
  299. if (tn.Parent != null)
  300. {
  301. if (nextCheck(tn))
  302. {
  303. tn.Parent.Checked = true;
  304. }
  305. else
  306. {
  307. tn.Parent.Checked = false;
  308. }
  309. cycleParent(tn.Parent, check);
  310. }
  311. return;
  312. }
  313. // afterCheck
  314. private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) //当选中或取消选中树节点上的复选框时发生
  315. {
  316. //要求父节点被勾选,则子节点全部被勾选;父节点不被勾选,则子节点不全不被勾选
  317. if (e.Node.Checked == true)
  318. {
  319. if (e.Action != TreeViewAction.Unknown)
  320. {
  321. cycleChild(e.Node, true);
  322. }
  323. if (e.Node.Parent != null)
  324. {
  325. if (nextCheck(e.Node))
  326. {
  327. cycleParent(e.Node, true);
  328. }
  329. else
  330. {
  331. cycleParent(e.Node, false);
  332. }
  333. }
  334. }
  335. if (e.Node.Checked == false)
  336. {
  337. if (e.Action != TreeViewAction.Unknown)
  338. {
  339. cycleChild(e.Node, false); //中间节点不选中则子节点全部不选中
  340. cycleParent(e.Node, false); //父节点不选中
  341. }
  342. // bCheck = false;
  343. }
  344. return;
  345. }
  346. int nodeClicks = 0;
  347. TreeViewHitTestInfo hitInfo = null;
  348. private void treeView1_MouseDown(object sender, MouseEventArgs e)
  349. {
  350. hitInfo = treeView1.HitTest(e.Location);
  351. nodeClicks = e.Clicks;
  352. }
  353. private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  354. {
  355. if (nodeClicks > 1 && hitInfo.Location == TreeViewHitTestLocations.Label)
  356. {
  357. e.Cancel = true;
  358. }
  359. }
  360. #region 控件
  361. /// <summary>
  362. /// Required designer variable.
  363. /// </summary>
  364. private System.ComponentModel.IContainer components = null;
  365. /// <summary>
  366. /// Clean up any resources being used.
  367. /// </summary>
  368. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  369. protected override void Dispose(bool disposing)
  370. {
  371. if (disposing && (components != null))
  372. {
  373. components.Dispose();
  374. }
  375. base.Dispose(disposing);
  376. }
  377. #region Windows Form Designer generated code
  378. /// <summary>
  379. /// Required method for Designer support - do not modify
  380. /// the contents of this method with the code editor.
  381. /// </summary>
  382. private void InitializeComponent()
  383. {
  384. this.groupBox1 = new System.Windows.Forms.GroupBox();
  385. this.button2 = new System.Windows.Forms.Button();
  386. this.button1 = new System.Windows.Forms.Button();
  387. this.groupBox2 = new System.Windows.Forms.GroupBox();
  388. this.treeView1 = new TreeViewEnhanced();
  389. this.label1 = new System.Windows.Forms.Label();
  390. this.groupBox1.SuspendLayout();
  391. this.groupBox2.SuspendLayout();
  392. this.SuspendLayout();
  393. //
  394. // groupBox1
  395. //
  396. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  397. | System.Windows.Forms.AnchorStyles.Right)));
  398. this.groupBox1.Controls.Add(this.button2);
  399. this.groupBox1.Controls.Add(this.button1);
  400. this.groupBox1.Location = new System.Drawing.Point(13, 13);
  401. this.groupBox1.Name = "groupBox1";
  402. this.groupBox1.Size = new System.Drawing.Size(301, 58);
  403. this.groupBox1.TabIndex = 1;
  404. this.groupBox1.TabStop = false;
  405. this.groupBox1.Text = "操作";
  406. //
  407. // button2
  408. //
  409. this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  410. this.button2.Location = new System.Drawing.Point(220, 20);
  411. this.button2.Name = "button2";
  412. this.button2.Size = new System.Drawing.Size(75, 23);
  413. this.button2.TabIndex = 1;
  414. this.button2.Text = "取消";
  415. this.button2.UseVisualStyleBackColor = true;
  416. this.button2.Click += new System.EventHandler(this.button2_Click);
  417. //
  418. // button1
  419. //
  420. this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  421. this.button1.Location = new System.Drawing.Point(138, 20);
  422. this.button1.Name = "button1";
  423. this.button1.Size = new System.Drawing.Size(75, 23);
  424. this.button1.TabIndex = 0;
  425. this.button1.Text = "确定";
  426. this.button1.UseVisualStyleBackColor = true;
  427. this.button1.Click += new System.EventHandler(this.button1_Click);
  428. //
  429. // groupBox2
  430. //
  431. this.groupBox2.Controls.Add(this.treeView1);
  432. this.groupBox2.Location = new System.Drawing.Point(13, 78);
  433. this.groupBox2.Name = "groupBox2";
  434. this.groupBox2.Size = new System.Drawing.Size(301, 467);
  435. this.groupBox2.TabIndex = 2;
  436. this.groupBox2.TabStop = false;
  437. this.groupBox2.Text = "可用模块";
  438. //
  439. // treeView1
  440. //
  441. this.treeView1.Location = new System.Drawing.Point(7, 21);
  442. this.treeView1.Name = "treeView1";
  443. this.treeView1.Size = new System.Drawing.Size(288, 440);
  444. this.treeView1.TabIndex = 0;
  445. this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
  446. this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
  447. this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
  448. //
  449. // label1
  450. //
  451. this.label1.AutoSize = true;
  452. this.label1.Location = new System.Drawing.Point(13, 551);
  453. this.label1.Name = "label1";
  454. this.label1.Size = new System.Drawing.Size(125, 12);
  455. this.label1.TabIndex = 3;
  456. this.label1.Text = "更改模块后请重启软件";
  457. //
  458. // ModuleManageDialog
  459. //
  460. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  461. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  462. this.ClientSize = new System.Drawing.Size(326, 577);
  463. this.Controls.Add(this.label1);
  464. this.Controls.Add(this.groupBox2);
  465. this.Controls.Add(this.groupBox1);
  466. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  467. this.MaximizeBox = false;
  468. this.Name = "ModuleManageDialog";
  469. this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  470. this.Text = "模块管理";
  471. this.Controls.SetChildIndex(this.groupBox1, 0);
  472. this.Controls.SetChildIndex(this.groupBox2, 0);
  473. this.Controls.SetChildIndex(this.label1, 0);
  474. this.groupBox1.ResumeLayout(false);
  475. this.groupBox2.ResumeLayout(false);
  476. this.ResumeLayout(false);
  477. this.PerformLayout();
  478. }
  479. #endregion
  480. private System.Windows.Forms.GroupBox groupBox1;
  481. private System.Windows.Forms.Button button2;
  482. private System.Windows.Forms.Button button1;
  483. private System.Windows.Forms.GroupBox groupBox2;
  484. private TreeViewEnhanced treeView1;
  485. private System.Windows.Forms.Label label1;
  486. #endregion
  487. }
  488. }