using PaintDotNet.Annotation.Enum;
using PaintDotNet.Annotation.FieldView;
using PaintDotNet.Base.CommTool;
using PaintDotNet.DbOpreate.DbBll;
using PaintDotNet.DbOpreate.DbModel;
using PaintDotNet.Base.SettingModel.LVMModel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace PaintDotNet.FieldView
{
internal class OpenViewDialog : PdnBaseForm
{
#region 控件
private GroupBox groupBox1;
private GroupBox groupBox2;
private GroupBox groupBox3;
private GroupBox groupBox4;
private ListView listView1;
private Panel panel1;
private PictureBox pictureBox1;
private AppWorkspace appWorkspace;
private Button button4;
private Button button3;
private Button button2;
private Button button1;
private Label label1;
#endregion
///
/// 重命名
///
private EditViewDialog createNameDialog;
///
/// 选定的视场在listview的items内的下标
///
private int index = -1;
///
/// 视场数据
///
private List list;
///
/// 选定的视场数据的model
///
private ViewListModel viewListModel;
public OpenViewDialog(AppWorkspace appWorkspace)
{
this.appWorkspace = appWorkspace;
InitializeComponent();
InitializeLanguageText();
InitListViewHeader();
InitViewData();
}
private void InitListViewHeader()
{
this.listView1.View = View.Details;
ColumnHeader header0 = new ColumnHeader();
header0.Text = "";
header0.Width = 25;
this.listView1.Columns.Add(header0);
ColumnHeader header = new ColumnHeader();
header.Text = PdnResources.GetString("Menu.viewname.text");
header.Width = 150;
this.listView1.Columns.Add(header);
}
private void InitViewData()
{
this.listView1.Items.Clear();
list = mic_view_infos_BLL.FindAll();
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
mic_view_infos view = list[i];
ListViewItem item = new ListViewItem();
item.Tag = view.id;
item.Text = "";
item.SubItems.Add(view.view_name);
this.listView1.Items.Add(item);
}
}
}
private void InitializeLanguageText()
{
this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
this.button4.Text = PdnResources.GetString("Menu.Rename.text");
this.button3.Text = PdnResources.GetString("Menu.viewsetting.Openview.Openrecord.text");
this.button2.Text = PdnResources.GetString("Menu.viewsetting.Openview.DeleteRecord.text");
this.button1.Text = PdnResources.GetString("Menu.Applyall.text");
this.groupBox2.Text = PdnResources.GetString("Menu.viewsetting.Openview.List.text");
this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text");
this.groupBox4.Text = PdnResources.GetString("Menu.viewsetting.Openview.brief.text");
this.Text = PdnResources.GetString("Menu.ViewSetting.OpenFieldOfView.Text");
}
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.listView1 = new System.Windows.Forms.ListView();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button4);
this.groupBox1.Controls.Add(this.button3);
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(526, 46);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "操作";
//
// button4
//
this.button4.Location = new System.Drawing.Point(445, 14);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 1;
this.button4.Text = "重命名";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(364, 14);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "打开记录";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(283, 14);
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.Location = new System.Drawing.Point(202, 14);
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.listView1);
this.groupBox2.Location = new System.Drawing.Point(13, 66);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(200, 295);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "列表";
//
// listView1
//
this.listView1.FullRowSelect = true;
this.listView1.HideSelection = false;
this.listView1.Location = new System.Drawing.Point(7, 21);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(187, 268);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listView1_ItemSelectionChanged);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.panel1);
this.groupBox3.Location = new System.Drawing.Point(220, 66);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(319, 295);
this.groupBox3.TabIndex = 3;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "预览";
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(7, 21);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(306, 268);
this.panel1.TabIndex = 0;
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Black;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(300, 262);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.label1);
this.groupBox4.Location = new System.Drawing.Point(13, 368);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(526, 64);
this.groupBox4.TabIndex = 4;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "简介";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(3, 17);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 0;
//
// OpenViewDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.ClientSize = new System.Drawing.Size(551, 444);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "OpenViewDialog";
this.Text = "打开视场";
this.Controls.SetChildIndex(this.groupBox1, 0);
this.Controls.SetChildIndex(this.groupBox2, 0);
this.Controls.SetChildIndex(this.groupBox3, 0);
this.Controls.SetChildIndex(this.groupBox4, 0);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.ResumeLayout(false);
}
///
/// listView1 ItemSelectionChanged事件
///
///
///
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
listView1.FullRowSelect = true;
if (this.listView1.SelectedItems.Count > 0)
{
listView1.SelectedItems[0].SubItems[0].ForeColor = Color.Blue;
//先清除原有格式
foreach (ListViewItem item in listView1.Items)
{
item.ForeColor = Color.Black;
}
foreach (ListViewItem item in listView1.Items)
{
item.BackColor = Color.White;
Font f = new Font(Control.DefaultFont, FontStyle.Regular);
item.Font = f;
}
//加粗字体
Font f2 = new Font(Control.DefaultFont, FontStyle.Bold);
listView1.SelectedItems[0].SubItems[0].Font = f2;
//设置选中行背景颜色
listView1.SelectedItems[0].BackColor = Color.LightBlue;
listView1.SelectedItems[0].Selected = true;
//赋值当前选中索引
this.index = e.ItemIndex;
//处理UI
this.label1.Text = " " + list.Find(a => a.id == (int)(listView1.SelectedItems[0].Tag)).view_descrption;
//获得model
this.viewListModel = XmlSerializeHelper.DESerializer(list.Find(a => a.id == (int)(listView1.SelectedItems[0].Tag)).view_xml);
//绘制右侧预览
this.pictureBox1.Image = this.panel1_Paint(viewListModel);
}
}
///
/// 绘制视场到图片
///
///
private Bitmap panel1_Paint(ViewListModel viewListModel)
{
Region a = null;
for (int i = 0; i < viewListModel.Views.Count; i++)
{
if (viewListModel.Views[i].Type != null)
{
GraphicsPath path = new GraphicsPath();
DrawToolType type = (DrawToolType)Enum.Parse(typeof(DrawToolType), viewListModel.Views[i].Type);
if (type == DrawToolType.ViewCircle)
{
path.AddEllipse(viewListModel.Views[i].Rectangle);
}
else if (type == DrawToolType.ViewOval)
{
path.AddClosedCurve(viewListModel.Views[i].Points.ToArray());
}
else
{
path.AddPolygon(viewListModel.Views[i].Points.ToArray());
}
if (a == null)
{
a = new Region(path);
}
else
{
if ((CombineMode)Enum.Parse(typeof(CombineMode), viewListModel.Views[i].CombineMode) == CombineMode.Union)
a.Union(path);
else
a.Exclude(path);
}
}
}
int width = (int)viewListModel.RectangleFW;
int height = (int)viewListModel.RectangleFH;
Rectangle rect = new Rectangle(0, 0, width, height);
Bitmap dstBitmap = new Bitmap(width, height);
BitmapData dstBmData = dstBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
IntPtr dstScan = dstBmData.Scan0;
unsafe
{
byte* dstP = (byte*)(void*)dstScan;
int dstOffset = dstBmData.Stride - width * 4;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++, dstP += 4)
{
if (a.IsVisible(new Point(x + (int)(viewListModel.RectangleFX), y + (int)(viewListModel.RectangleFY))))
{
dstP[0] = 255;
dstP[1] = 255;
dstP[2] = 255;
dstP[3] = 255;
}
else
{
dstP[0] = 0;
dstP[1] = 0;
dstP[2] = 0;
dstP[3] = 255;
}
}
dstP += dstOffset;
}
}
dstBitmap.UnlockBits(dstBmData);
return dstBitmap;
}
///
/// 应用全部
///
///
///
private void button1_Click(object sender, EventArgs e)
{
if (viewListModel != null && viewListModel.Views.Count > 0
&& this.appWorkspace.DocumentWorkspaces != null
&& this.appWorkspace.DocumentWorkspaces.Length > 0)
{
foreach (DocumentWorkspace dw in this.appWorkspace.DocumentWorkspaces)
{
for (int i = 0; i < viewListModel.Views.Count; i++)
{
DrawToolType type = (DrawToolType)Enum.Parse(typeof(DrawToolType), viewListModel.Views[i].Type);
ViewBase line = null;
switch (type)
{
case DrawToolType.ViewOval:
line = new ViewOval(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewCircle:
line = new ViewCircle(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewRectangle:
line = new ViewRectangle(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewTriangle:
line = new ViewTriangle(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewSquare:
line = new ViewSquare(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewRectangleEx:
line = new ViewRectangleEx(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewPolygon:
line = new ViewPolygon(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewTriangleEx:
line = new ViewTriangleEx(viewListModel.Views[i].Points);
break;
}
if (line != null)
{
line.ISurfaceBox = dw;
line.combineMode = (CombineMode)Enum.Parse(typeof(CombineMode), viewListModel.Views[i].CombineMode);
line.Selected = true;
dw.AddGraphicsFromForm(line);
}
}
}
}
}
///
/// 删除记录
///
///
///
private void button2_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0)
{
DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.Areyousuretodeletethefie.text") + "?", PdnResources.GetString("Menu.ensure.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.OK)
{
int id = (int)(this.listView1.SelectedItems[0].Tag);
if (id > 0)
{
//删除数据库文件
mic_view_infos_BLL.Del(id);
InitViewData();
}
}
}
}
///
/// 打开记录
///
///
///
private void button3_Click(object sender, EventArgs e)
{
//将所有的视场反向还原到surface中
if (viewListModel != null && viewListModel.Views.Count > 0 && this.appWorkspace.ActiveDocumentWorkspace != null
&& this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface != null
&& this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null)
{
for (int i = 0; i < viewListModel.Views.Count; i++)
{
DrawToolType type = (DrawToolType)Enum.Parse(typeof(DrawToolType), viewListModel.Views[i].Type);
ViewBase line = null;
switch (type)
{
case DrawToolType.ViewOval:
line = new ViewOval(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewCircle:
line = new ViewCircle(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewRectangle:
line = new ViewRectangle(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewRectangleEx:
line = new ViewRectangleEx(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewTriangle:
line = new ViewTriangle(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewSquare:
line = new ViewSquare(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewPolygon:
line = new ViewPolygon(viewListModel.Views[i].Points);
break;
case DrawToolType.ViewTriangleEx:
line = new ViewTriangleEx(viewListModel.Views[i].Points);
break;
}
if (line != null)
{
line.ISurfaceBox = this.appWorkspace.ActiveDocumentWorkspace;
line.combineMode = (CombineMode)Enum.Parse(typeof(CombineMode), viewListModel.Views[i].CombineMode);
line.Selected = true;
this.appWorkspace.ActiveDocumentWorkspace.AddGraphicsFromForm(line);
}
}
}
}
///
/// 重命名
///
///
///
private void button4_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0)
{
createNameDialog = new EditViewDialog(this);
createNameDialog.Text = PdnResources.GetString("Menu.Rename.text");
createNameDialog.textBox1.Text = list[this.index].view_name;
createNameDialog.richTextBox1.Text = list[this.index].view_descrption;
createNameDialog.StartPosition = FormStartPosition.CenterParent;
createNameDialog.ShowDialog();
}
}
///
/// 另存为弹窗 确定保存
///
///
public override void GetCreateName(string name, string desc)
{
mic_view_infos model = list[this.index];
mic_view_infos query = list.Find(a => a.view_name.Equals(name));
if (query != null)
{
if (model.id != query.id)
{
MessageBox.Show(PdnResources.GetString("Menu.viewnamerepeated,pleaserename.text"));
return;
}
}
model.view_name = name;
model.view_descrption = desc;
mic_view_infos_BLL.Update(model);
createNameDialog.Close();
InitViewData();
}
}
}