using PaintDotNet.Base.CommTool;
using PaintDotNet.Base.SettingModel;
using PaintDotNet.CustomControl;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace PaintDotNet.Setting
{
///
/// 设置->模块管理
///
internal class ModuleManageDialog : PdnBaseForm
{
private AppWorkspace appWorkspace;
private ToolStripItemCollection collectionLeft;
private TreeNode animeLeft;
private string txtPath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\ModuleConfig.txt";
private string[] menuIdArr;
public ModuleManageDialog(AppWorkspace appWorkspace)
{
this.appWorkspace = appWorkspace;
this.ShowInTaskbar = false;
InitializeComponent();
InitializeLanguageText();
this.treeView1.CheckBoxes = true;
InitVisibleMenuId();
InitLeftTreeViewData();
this.Text = PdnResources.GetString("Menu.Setting.ModuleSetting.Text");
}
private void InitializeLanguageText()
{
this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
this.button2.Text = PdnResources.GetString("ConfirmLanguageDialog.CancelTB.ActionText");
this.button1.Text = PdnResources.GetString("Menu.application.text");
this.groupBox2.Text = PdnResources.GetString("Menu.Set.Focusparams.Availablemodules.text");
this.label1.Text = PdnResources.GetString("Menu.Set.Modulemanagement.Restartthesngs.text");
}
///
/// 获取txt文件中已保存的菜单可用id
///
private void InitVisibleMenuId()
{
if (System.IO.File.Exists(txtPath))
{
string str = System.IO.File.ReadAllText(txtPath);
if (str.IndexOf(',') != -1)
{
menuIdArr = str.Split(',');
}
else
{
if (!string.IsNullOrEmpty(str))
{
menuIdArr = new string[] { str };
}
}
}
}
///
/// 初始化左侧treeview数据
///
private void InitLeftTreeViewData()
{
this.collectionLeft = this.appWorkspace.ToolBar.MainMenu.Items;
this.animeLeft = new TreeNode(PdnResources.GetString("Menu.menu.Text"));
this.animeLeft.Checked = true;
this.RecursiveDataLeft(collectionLeft, animeLeft);
this.animeLeft.Expand();
this.treeView1.Nodes.Add(animeLeft);
}
///
/// 左侧递归进行数据组织
///
private void RecursiveDataLeft(ToolStripItemCollection collection, TreeNode anime)
{
for (int i = 0; i < collection.Count; i++)
{
TreeNode node = new TreeNode(collection[i].Text);
if (collection[i] is PdnMenuItem)
{
if (!(collection[i] is PdnMenuItem)) continue;
PdnMenuItem item = (PdnMenuItem)collection[i];
if (!item.CanShowInSenseShield)
continue;
node.Tag = item.MenuId;
if (menuIdArr != null && menuIdArr.Count() > 0)
{
if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
{
node.Checked = true;
}
else
{
node.Checked = false;
//兄弟节点只要有一个没选,其父节点也不选//#20756
TreeNode nodeParent = anime;// node.Parent;
while (nodeParent != null)
{
nodeParent.Checked = false;
nodeParent = nodeParent.Parent;
}
}
}
//if (Startup.instance.moduleConfigModel.items.Find(a => a.ParentId == item.MenuId) != null
// || Startup.instance.moduleConfigModel.items.Find(a => a.ChildIds.Split(',').Contains(item.MenuId.ToString())) != null)
//{
// node.Checked = true;
//}
anime.Nodes.Add(node);
if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
continue;
RecursiveDataLeft(((PdnMenuItem)collection[i]).DropDownItems, node);
}
}
}
///
/// 确定
///
///
///
private void button1_Click(object sender, EventArgs e)
{
TreeNodeCollection nodes = this.animeLeft.Nodes;
string str = "";
foreach (TreeNode node in nodes)
{
if (node.Checked)
{
if (str == "")
{
str += node.Tag.ToString();
}
else
{
str += "," + node.Tag.ToString();
}
}
if (node.Nodes.Count > 0)
{
bool nodeHasChildChecked;
str = RecursionMenuId(node, str, out nodeHasChildChecked);
if (!node.Checked && nodeHasChildChecked)
{
if (str == "")
{
str += node.Tag.ToString();
}
else
{
str += "," + node.Tag.ToString();
}
}
}
}
//FileStream fs = new FileStream(txtPath, FileMode.Create);
//StreamWriter sw = new StreamWriter(fs);
StreamWriter sw = new StreamWriter(txtPath, false);
sw.Write(str);
sw.Close();
//fs.Close();
sw.Dispose();
//fs.Dispose();
this.Close();
}
///
/// 递归获取所有选中的菜单id
///
///
///
///
private string RecursionMenuId(TreeNode node, string str, out bool hasChildChecked)
{
hasChildChecked = false;
if (node.Nodes.Count > 0)
{
foreach (TreeNode tn in node.Nodes)
{
if (tn.Checked)
{
if (str == "")
{
str += tn.Tag.ToString();
}
else
{
str += "," + tn.Tag.ToString();
}
hasChildChecked = true;
}
if (tn.Nodes.Count > 0)
{
bool tnHasChildChecked;
str = RecursionMenuId(tn, str, out tnHasChildChecked);
if (!tn.Checked && tnHasChildChecked)
{
if (str == "")
{
str += tn.Tag.ToString();
}
else
{
str += "," + tn.Tag.ToString();
}
hasChildChecked = true;
}
}
}
}
return str;
}
///
/// 取消
///
///
///
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
//递归父节点跟随其全选或全不选
private void ChangeParent(TreeNode node)
{
if (node.Parent != null)
{
//兄弟节点被选中的个数
int brotherNodeCheckedCount = 0;
//遍历该节点的兄弟节点
foreach (TreeNode tn in node.Parent.Nodes)
{
if (tn.Checked == true)
brotherNodeCheckedCount++;
}
//兄弟节点全没选,其父节点也不选
if (brotherNodeCheckedCount == 0)
{
node.Parent.Checked = false;
ChangeParent(node.Parent);
}
//兄弟节点只要有一个被选,其父节点也被选
if (brotherNodeCheckedCount >= 1)
{
node.Parent.Checked = true;
ChangeParent(node.Parent);
}
}
}
/*private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node != null)
{
this.treeView1.AfterCheck -= treeView1_AfterCheck;
ChangeChild(e.Node, e.Node.Checked);//影响子节点
ChangeParent(e.Node);//影响父节点
this.treeView1.AfterCheck += treeView1_AfterCheck;
}
}*/
private bool nextCheck(TreeNode n) //判断同级的节点是否全选
{
foreach (TreeNode tn in n.Parent.Nodes)
{
if (tn.Checked == false) return false;
}
return true;
}
private bool nextNotCheck(TreeNode n) //判断同级的节点是否全不选
{
if (n.Checked == true)
{
return false;
}
if (n.NextNode == null)
{
return true;
}
return this.nextNotCheck(n.NextNode);
}
private void cycleChild(TreeNode tn, bool check) //遍历节点下的子节点
{
if (tn.Nodes.Count != 0)
{
foreach (TreeNode child in tn.Nodes)
{
child.Checked = check;
if (child.Nodes.Count != 0)
{
cycleChild(child, check);
}
}
}
else
return;
}
private void cycleParent(TreeNode tn, bool check) //遍历节点上的父节点
{
if (tn.Parent != null)
{
if (nextCheck(tn))
{
tn.Parent.Checked = true;
}
else
{
tn.Parent.Checked = false;
}
cycleParent(tn.Parent, check);
}
return;
}
// afterCheck
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) //当选中或取消选中树节点上的复选框时发生
{
//要求父节点被勾选,则子节点全部被勾选;父节点不被勾选,则子节点不全不被勾选
if (e.Node.Checked == true)
{
if (e.Action != TreeViewAction.Unknown)
{
cycleChild(e.Node, true);
}
if (e.Node.Parent != null)
{
if (nextCheck(e.Node))
{
cycleParent(e.Node, true);
}
else
{
cycleParent(e.Node, false);
}
}
}
if (e.Node.Checked == false)
{
if (e.Action != TreeViewAction.Unknown)
{
cycleChild(e.Node, false); //中间节点不选中则子节点全部不选中
cycleParent(e.Node, false); //父节点不选中
}
// bCheck = false;
}
return;
}
int nodeClicks = 0;
TreeViewHitTestInfo hitInfo = null;
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
hitInfo = treeView1.HitTest(e.Location);
nodeClicks = e.Clicks;
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (nodeClicks > 1 && hitInfo.Location == TreeViewHitTestLocations.Label)
{
e.Cancel = true;
}
}
#region 控件
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.treeView1 = new TreeViewEnhanced();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(13, 13);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(301, 58);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "操作";
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(220, 20);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "取消";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(138, 20);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.treeView1);
this.groupBox2.Location = new System.Drawing.Point(13, 78);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(301, 467);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "可用模块";
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(7, 21);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(288, 440);
this.treeView1.TabIndex = 0;
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 551);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(125, 12);
this.label1.TabIndex = 3;
this.label1.Text = "更改模块后请重启软件";
//
// ModuleManageDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(326, 577);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "ModuleManageDialog";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.Text = "模块管理";
this.Controls.SetChildIndex(this.groupBox1, 0);
this.Controls.SetChildIndex(this.groupBox2, 0);
this.Controls.SetChildIndex(this.label1, 0);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox2;
private TreeViewEnhanced treeView1;
private System.Windows.Forms.Label label1;
#endregion
}
}