using PaintDotNet.Base.SettingModel;
using PaintDotNet.Base.CommTool;
using PaintDotNet.Data.Param;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PaintDotNet.Setting
{
///
/// 设置->工作流程
///
internal class WorkFlowSettingDialog : PdnBaseForm
{
#region 控件
private GroupBox groupBox1;
private Button cancelBtn;
private Button saveBtn;
private Button button2;
private Button button1;
private GroupBox groupBox2;
private Button button3;
private GroupBox groupBox3;
private Button button4;
private Button button5;
private Button button6;
private TreeView treeView1;
private TreeView treeView2;
#endregion
///
/// 工作空间
///
private AppWorkspace appWorkspace;
///
/// 新增、重命名的弹窗
///
private CreateNameDialog createNameDialog;
///
/// 工作流程model
///
private WorkFlowModel workFlowModel = Startup.instance.workFlowModel;
///
/// 右侧树形菜单
///
private ToolStripItemCollection collection;
///
/// 是否新建工作流程
///
private bool create = true;
private string txtPath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\ModuleConfig.txt";
private string[] menuIdArr;
public WorkFlowSettingDialog(AppWorkspace appWorkspace)
{
this.appWorkspace = appWorkspace;
InitializeComponent();
InitializeLanguageText();
InitVisibleMenuId();
this.button3.Enabled = false;
this.button4.Enabled = false;
InitializeTreeEvent();
InitializeLeftTreeData();
InitializeRightTreeData();
}
///
/// 获取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 InitializeTreeEvent()
{
this.treeView1.HideSelection = false;
this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.treeView1.DrawNode += new DrawTreeNodeEventHandler(this.treeView_DrawNode);
this.treeView1.Invalidated += new InvalidateEventHandler(this.treeView1_InvalidateEvent);
this.treeView2.HideSelection = false;
this.treeView2.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.treeView2.DrawNode += new DrawTreeNodeEventHandler(this.treeView_DrawNode);
this.treeView2.Invalidated += new InvalidateEventHandler(this.treeView2_InvalidateEvent);
}
///
/// treeView1的重绘事件
///
///
///
private void treeView1_InvalidateEvent(object sender, InvalidateEventArgs e)
{
if (this.treeView2.SelectedNode != null)
{
if (this.treeView2.SelectedNode.Tag == null)
this.button3.Enabled = true;
}
if (this.treeView1.SelectedNode != null)
{
if (this.treeView1.SelectedNode.Nodes.Count > 0)
{
this.button3.Enabled = false;
}
}
}
///
/// treeView2的重绘事件
///
///
///
private void treeView2_InvalidateEvent(object sender, InvalidateEventArgs e)
{
this.button1.Enabled = true;
if (this.treeView1.SelectedNode != null)
{
if (this.treeView1.SelectedNode.Nodes.Count == 0)
{
this.button3.Enabled = true;
}
}
this.button4.Enabled = false;
this.cancelBtn.Enabled = true;
if (this.treeView2.SelectedNode != null)
{
if (this.treeView2.SelectedNode.Tag != null)
{
this.button1.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = true;
this.cancelBtn.Enabled = false;
}
}
}
///
/// 自定义绘制
/// 参考https://www.cnblogs.com/JiYF/p/6693503.html
///
///
///
private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
/**用默认颜色
e.DrawDefault = true;
return;
**/
//以下是自定义颜色
if ((e.State & TreeNodeStates.Selected) != 0)
{
//演示为绿底白字
e.Graphics.FillRectangle(Brushes.Green, e.Node.Bounds);
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0));
}
else
{
e.DrawDefault = true;
}
if ((e.State & TreeNodeStates.Focused) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = e.Node.Bounds;
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
}
///
/// 初始化左侧工作流程菜单
///
private void InitializeLeftTreeData()
{
if (workFlowModel != null && workFlowModel.Flows != null)
{
for (int i = 0; i < this.workFlowModel.Flows.Count; i++)
{
TreeNode anime = new TreeNode(this.workFlowModel.Flows[i].Name);
for (int j = 0; j < this.workFlowModel.Flows[i].Menus.Count; j++)
{
TreeNode child = new TreeNode();
child.Text = this.workFlowModel.Flows[i].Menus[j].Name;
child.Name = this.workFlowModel.Flows[i].Menus[j].Description;
child.Tag = this.workFlowModel.Flows[i].Menus[j].Id;
anime.Nodes.Add(child);
}
this.treeView2.Nodes.Add(anime);
}
}
}
///
/// 初始化右侧树形菜单数据
///
private void InitializeRightTreeData()
{
this.collection = this.appWorkspace.ToolBar.MainMenu.Items;
TreeNode anime = new TreeNode(PdnResources.GetString("Menu.menu.Text"));
this.RecursiveData(collection, anime);
anime.Expand();
this.treeView1.Nodes.Add(anime);
}
///
/// 递归进行数据组织
///
private void RecursiveData(ToolStripItemCollection collection, TreeNode anime)
{
//for (int i = 0; i < toolStripItemCollection.Count; i++)
//{
// //排除掉最近打开的文件,或者可以用数字id判断更准确
// if (!toolStripItemCollection[i].Name.Equals("OpenRecent") && !toolStripItemCollection[i].Name.Equals("CameraSelection"))
// {
// if (toolStripItemCollection[i].GetType() != typeof(ToolStripSeparator) && ((PdnMenuItem)toolStripItemCollection[i]).CanShowInSenseShield)
// {
// TreeNode node = new TreeNode();
// node.Name = toolStripItemCollection[i].Name;
// node.Text = toolStripItemCollection[i].Text;
// node.Tag = ((PdnMenuItem)toolStripItemCollection[i]).MenuId;
// anime.Nodes.Add(node);
// RecursiveData(((PdnMenuItem)toolStripItemCollection[i]).DropDownItems, node);
// }
// }
//}
for (int i = 0; i < collection.Count; i++)
{
TreeNode node = new TreeNode(/*collection[i].Text*/);
if (collection[i] is PdnMenuItem)
{
PdnMenuItem item = (PdnMenuItem)collection[i];
if (!item.CanShowInSenseShield)
continue;
node.Tag = item.MenuId;
if (menuIdArr != null && menuIdArr.Length > 0)
{
if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
{
node.Name = collection[i].Name;
node.Text = collection[i].Text;
node.Tag = ((PdnMenuItem)collection[i]).MenuId;
node.Checked = true;
}
else
{
node.Checked = false;
}
}
if (node.Checked)
{
anime.Nodes.Add(node);
}
if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
continue;
RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, node);
}
}
}
private void InitializeLanguageText()
{
this.Text = PdnResources.GetString("Menu.Setting.WorkFlowSetting.Text");
this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
this.button2.Text = PdnResources.GetString("Menu.File.Save.Text");
this.button1.Text = PdnResources.GetString("Menu.Edit.Delete.Text");
this.cancelBtn.Text = PdnResources.GetString("Menu.Rename.text");
this.saveBtn.Text = PdnResources.GetString("Menu.Add.text");
this.groupBox2.Text = PdnResources.GetString("Menu.Set.workprocess.Theworkflowismaintained.text");
this.button3.Text = "< " + PdnResources.GetString("Menu.Addto.text");
this.groupBox3.Text = PdnResources.GetString("Menu.Availablefunctions.text");
this.button4.Text = PdnResources.GetString("Menu.Moveout.text") + " >";
this.button5.Text = PdnResources.GetString("Menu.LabelAction.MoveUpAction.Text");
this.button6.Text = PdnResources.GetString("Menu.LabelAction.MoveDownAction.Text");
}
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.cancelBtn = new System.Windows.Forms.Button();
this.saveBtn = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.treeView2 = new System.Windows.Forms.TreeView();
this.button3 = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.treeView1 = new System.Windows.Forms.TreeView();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.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.Controls.Add(this.cancelBtn);
this.groupBox1.Controls.Add(this.saveBtn);
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(508, 58);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "操作";
//
// button2
//
this.button2.Location = new System.Drawing.Point(420, 19);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "保存";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(339, 19);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "删除";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// cancelBtn
//
this.cancelBtn.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.cancelBtn.Location = new System.Drawing.Point(258, 19);
this.cancelBtn.Name = "cancelBtn";
this.cancelBtn.Size = new System.Drawing.Size(75, 23);
this.cancelBtn.TabIndex = 1;
this.cancelBtn.Text = "重命名";
this.cancelBtn.UseVisualStyleBackColor = true;
this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click);
//
// saveBtn
//
this.saveBtn.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.saveBtn.Location = new System.Drawing.Point(177, 19);
this.saveBtn.Name = "saveBtn";
this.saveBtn.Size = new System.Drawing.Size(75, 23);
this.saveBtn.TabIndex = 0;
this.saveBtn.Text = "新增";
this.saveBtn.UseVisualStyleBackColor = true;
this.saveBtn.Click += new System.EventHandler(this.saveBtn_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.treeView2);
this.groupBox2.Location = new System.Drawing.Point(13, 77);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(200, 483);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "已维护工作流程";
//
// treeView2
//
this.treeView2.Location = new System.Drawing.Point(7, 21);
this.treeView2.Name = "treeView2";
this.treeView2.Size = new System.Drawing.Size(187, 456);
this.treeView2.TabIndex = 0;
this.treeView2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView2_MouseDown);
//
// button3
//
this.button3.Location = new System.Drawing.Point(230, 100);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 5;
this.button3.Text = "添加";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.treeView1);
this.groupBox3.Location = new System.Drawing.Point(320, 77);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(200, 483);
this.groupBox3.TabIndex = 6;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "可用功能";
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(7, 21);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(187, 456);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
//
// button4
//
this.button4.Location = new System.Drawing.Point(230, 130);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 7;
this.button4.Text = "移出";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(230, 160);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 8;
this.button5.Text = "向上移动";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(230, 190);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 9;
this.button6.Text = "向下移动";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// WorkFlowSettingDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.ClientSize = new System.Drawing.Size(533, 572);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.button3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "WorkFlowSettingDialog";
this.Text = "工作流程";
this.Controls.SetChildIndex(this.groupBox1, 0);
this.Controls.SetChildIndex(this.groupBox2, 0);
this.Controls.SetChildIndex(this.button3, 0);
this.Controls.SetChildIndex(this.groupBox3, 0);
this.Controls.SetChildIndex(this.button4, 0);
this.Controls.SetChildIndex(this.button5, 0);
this.Controls.SetChildIndex(this.button6, 0);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
///
/// 新建
///
///
///
private void saveBtn_Click(object sender, EventArgs e)
{
create = true;
createNameDialog = new CreateNameDialog(this);
createNameDialog.StartPosition = FormStartPosition.CenterParent;
createNameDialog.ShowDialog();
}
///
/// 重命名
///
///
///
private void cancelBtn_Click(object sender, EventArgs e)
{
if (this.treeView2.SelectedNode != null)
{
create = false;
createNameDialog = new CreateNameDialog(this);
createNameDialog.SetTextBoxValue(this.treeView2.SelectedNode.Text);
createNameDialog.StartPosition = FormStartPosition.CenterParent;
createNameDialog.ShowDialog();
}
else
MessageBox.Show(PdnResources.GetString("Menu.leaseselectaworkflowthathasbeenma.Text"));
}
///
/// 删除
///
///
///
private void button1_Click(object sender, EventArgs e)
{
if (this.treeView2.SelectedNode != null)
{
if (MessageBox.Show(PdnResources.GetString("Menu.reyousureyouwanttodeletetheselectedwo.Text") + "?", PdnResources.GetString("Menu.Thisdeletioncannotberecovered.text"), MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.treeView2.Nodes.Remove(this.treeView2.SelectedNode);
}
}
else
MessageBox.Show(PdnResources.GetString("Menu.erearenomaintainedworkflowstodel.Text"));
}
///
/// 保存
///
///
///
private void button2_Click(object sender, EventArgs e)
{
workFlowModel.Flows.Clear();
for (int i = 0; i < this.treeView2.Nodes.Count; i++)
{
WorkFlowModel.Flow flow = new WorkFlowModel.Flow();
flow.Menus = new List();
flow.Name = this.treeView2.Nodes[i].Text;
if (this.treeView2.Nodes[i].Nodes.Count > 0)
{
for (int j = 0; j < this.treeView2.Nodes[i].Nodes.Count; j++)
{
WorkFlowModel.Flow.Item item = new WorkFlowModel.Flow.Item();
item.Id = (int)(this.treeView2.Nodes[i].Nodes[j].Tag);
item.Name = this.treeView2.Nodes[i].Nodes[j].Text;
item.Description = this.treeView2.Nodes[i].Nodes[j].Name;
flow.Menus.Add(item);
}
}
workFlowModel.Flows.Add(flow);
}
string userInfoXml = XmlSerializeHelper.XmlSerialize(workFlowModel);
string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\WorkFlow.xml";
if (FileOperationHelper.WriteStringToFile(userInfoXml, filePath, FileMode.Create))
{
Startup.instance.workFlowModel = workFlowModel;
if (this.appWorkspace.workFlowDialog != null)
{
this.appWorkspace.workFlowDialog.InitializeWorkFlow();
}
this.Close();
}
else
{
MessageBox.Show(PdnResources.GetString("Menu.Workflowsavefailed.Text"));
}
}
///
/// 获取新建窗口里面输入的名称
///
///
public override void GetCreateName(string name)
{
if (!name.Equals(""))
{
if (create) //新增
{
this.treeView2.Nodes.Add(new TreeNode(name));
}
else //重命名
{
if (this.treeView2.SelectedNode != null)
{
this.treeView2.SelectedNode.Text = name;
}
}
createNameDialog.Close();
}
else
{
MessageBox.Show(PdnResources.GetString("Menu.leaseenterworkflowname.Text"));
}
}
///
/// 添加
///
///
///
private void button3_Click(object sender, EventArgs e)
{
if (this.treeView1.SelectedNode != null && this.treeView2.SelectedNode != null)
{
TreeNode child = new TreeNode();
child.Text = this.treeView1.SelectedNode.Text;//显示的名称 多语言
child.Name = this.treeView1.SelectedNode.Name;//唯一标识 英文
child.Tag = this.treeView1.SelectedNode.Tag;//唯一标识 数字
this.treeView2.SelectedNode.Nodes.Add(child);
}
}
///
/// 移出
///
///
///
private void button4_Click(object sender, EventArgs e)
{
if (this.treeView2.SelectedNode != null)
{
this.treeView2.Nodes.Remove(this.treeView2.SelectedNode);
this.treeView2.Refresh();
}
}
///
/// 向上移动
///
///
///
private void button5_Click(object sender, EventArgs e)
{
if (this.treeView2.SelectedNode != null)
{
if (this.treeView2.SelectedNode.Parent != null)
{
int index = this.treeView2.SelectedNode.Parent.Nodes.IndexOf(this.treeView2.SelectedNode);
if (index > 0)
{
TreeNode prenode = (TreeNode)this.treeView2.SelectedNode.PrevNode.Clone();
this.treeView2.SelectedNode.Parent.Nodes.Insert(index + 1, prenode);
this.treeView2.Nodes.Remove(this.treeView2.SelectedNode.PrevNode);
}
}
else
{
int index = this.treeView2.Nodes.IndexOf(this.treeView2.SelectedNode);
if (index > 0)
{
TreeNode prenode = (TreeNode)this.treeView2.SelectedNode.PrevNode.Clone();
this.treeView2.Nodes.Insert(index + 1, prenode);
this.treeView2.Nodes.Remove(this.treeView2.SelectedNode.PrevNode);
}
}
this.treeView2.Refresh();
}
}
///
/// 向下移动
///
///
///
private void button6_Click(object sender, EventArgs e)
{
if (this.treeView2.SelectedNode != null)
{
if (this.treeView2.SelectedNode.Parent != null)
{
int index = this.treeView2.SelectedNode.Parent.Nodes.IndexOf(this.treeView2.SelectedNode);
if (index < this.treeView2.SelectedNode.Parent.Nodes.Count - 1)
{
TreeNode prenode = (TreeNode)this.treeView2.SelectedNode.NextNode.Clone();
this.treeView2.SelectedNode.Parent.Nodes.Insert(index, prenode);
this.treeView2.Nodes.Remove(this.treeView2.SelectedNode.NextNode);
}
}
else
{
int index = this.treeView2.Nodes.IndexOf(this.treeView2.SelectedNode);
if (index < this.treeView2.Nodes.Count - 1)
{
TreeNode prenode = (TreeNode)this.treeView2.SelectedNode.NextNode.Clone();
this.treeView2.Nodes.Insert(index, prenode);
this.treeView2.Nodes.Remove(this.treeView2.SelectedNode.NextNode);
}
}
this.treeView2.Refresh();
}
}
///
/// 左侧工作流程鼠标按下事件
///
///
///
private void treeView2_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as TreeView) != null)
{
this.treeView2.SelectedNode = this.treeView2.GetNodeAt(e.X, e.Y);
this.treeView2.Refresh();
}
}
///
/// 右侧功能列表鼠标按下事件
///
///
///
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as TreeView) != null)
{
this.treeView1.SelectedNode = this.treeView1.GetNodeAt(e.X, e.Y);
this.treeView1.Refresh();
}
}
}
}