123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 |
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base.SettingModel;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Globalization;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Security.Permissions;
- namespace PaintDotNet.Annotation
- {
- using DrawList = List<DrawObject>;
- public interface DocumentDirtyObserver
- {
- void IsDirty(GraphicsList gList);
- }
- /// <summary>
- /// 绘制对象的数组
- /// 每个surface保持一个自己的list
- /// </summary>
- [Serializable]
- public class GraphicsList : ISerializable
- {
- public DrawList graphicsList;
- private List<DocumentDirtyObserver> observers;
- private bool dirty;
- private const string entryCount = "Count";
- private const string entryType = "Type";
- public GraphicsList()
- {
- this.graphicsList = new DrawList();
- this.observers = new List<DocumentDirtyObserver>();
- this.dirty = false;
- }
- public GraphicsList(ISurfaceBox surfaceBox)
- {
- this.graphicsList = new DrawList();
- this.observers = new List<DocumentDirtyObserver>();
- this.dirty = false;
- }
- #region Serialization Support
- /// <summary>
- /// Save object to serialization stream
- /// </summary>
- /// <param name="info"></param>
- /// <param name="context"></param>
- [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
- public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
- {
- info.AddValue(entryCount, graphicsList.Count);
- int i = 0;
- foreach (DrawObject o in graphicsList)
- {
- info.AddValue(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryType, i),
- o.GetType().FullName);
- o.SaveToStream(info, i);
- i++;
- }
- }
- #endregion
- #region Other functions
- protected void NotifyDirty()
- {
- if (this.dirty)
- {
- foreach (DocumentDirtyObserver obs in this.observers)
- {
- obs.IsDirty(this);
- }
- }
- }
- public void Draw(Graphics g)
- {
- int n = graphicsList.Count;
- DrawObject o;
- int n3 = 0;
- // Enumerate list in reverse order to get first
- // object on the top of Z-order.
- for (int i = n - 1; i >= 0; i--)
- {
- o = graphicsList[i];
- //如果是视场,则只绘制一次
- if (o.objectType == DrawClass.View)
- {
- if (n3 == 0)
- {
- o.Draw(g);
- n3++;
- }
- }
- else
- {
- o.Draw(g);
- }
- if (o.Selected == true)
- {
- o.DrawTracker(g);
- }
- }
- }
- /// <summary>
- /// Dump (for debugging)
- /// </summary>
- public void Dump()
- {
- Trace.WriteLine("");
- foreach (DrawObject o in graphicsList)
- {
- o.Dump();
- }
- }
- /// <summary>
- /// Clear all objects in the list
- /// </summary>
- /// <returns>
- /// true if at least one object is deleted
- /// </returns>
- public bool Clear()
- {
- bool result = (graphicsList.Count > 0);
- graphicsList.Clear();
- return result;
- }
- /// <summary>
- /// Tells if the document is dirty.
- /// </summary>
- public bool Dirty
- {
- get
- {
- return this.dirty;
- }
- set
- {
- this.dirty = value;
- if (this.dirty)
- {
- NotifyDirty();
- }
- }
- }
- /// <summary>
- /// Count and this [nIndex] allow to read all graphics objects
- /// from GraphicsList in the loop.
- /// </summary>
- public int Count
- {
- get
- {
- return graphicsList.Count;
- }
- }
- public DrawObject this[int index]
- {
- get
- {
- if (index < 0 || index >= graphicsList.Count)
- return null;
- return graphicsList[index];
- }
- }
- public Size GetSize()
- {
- Size size = new Size(0, 0);
- foreach (DrawObject obj in graphicsList)
- {
- RectangleF objRect = obj.Rectangle;
- size.Width = (int)Math.Max(size.Width, objRect.X + objRect.Width);
- size.Height = (int)Math.Max(size.Height, objRect.Y + objRect.Height);
- }
- size.Width += 20;
- size.Height += 20;
- return size;
- }
- #region Selection
- /// <summary>
- /// SelectedCount and GetSelectedObject allow to read
- /// selected objects in the loop
- /// </summary>
- public int SelectionCount
- {
- get
- {
- int n = 0;
- foreach (DrawObject o in Selection)
- {
- n++;
- }
- return n;
- }
- }
- public IEnumerable<DrawObject> Selection
- {
- get
- {
- foreach (DrawObject o in graphicsList)
- {
- if (o.Selected)
- {
- yield return o;
- }
- }
- }
- }
- DrawObject LastDrawObject = null;
- /// <summary>
- /// 用于修改视场,获得选中的视场
- /// 因为修改视场判断了只能修改一个,
- /// 所以找到一个被选中就返回
- /// </summary>
- public DrawObject SelectDrawObject
- {
- get
- {
- if (graphicsList.Count((o) => o.Selected) == 1)
- return graphicsList.Find((o) => o.Selected);
- else
- return null;
- }
- }
- public event EventHandler SelectChanged;
- public virtual void OnSelectChanged()
- {
- if (LastDrawObject == SelectDrawObject) return;
- LastDrawObject = SelectDrawObject;
- SelectChanged?.Invoke(this, EventArgs.Empty);
- }
- public void SelectInRectangle(Rectangle rectangle)
- {
- UnselectAll();
- foreach (DrawObject o in graphicsList)
- {
- if (o.IntersectsWith(rectangle) && !o.isLocked)
- o.Selected = true;
- }
- }
- public void UnselectAll()
- {
- foreach (DrawObject o in graphicsList)
- {
- o.Selected = false;
- }
- }
- public void SelectAll()
- {
- foreach (DrawObject o in graphicsList)
- {
- if (!o.isLocked)
- {
- o.Selected = true;
- }
- }
- }
- #endregion
- #region 增删改
- public void Add(DrawObject obj)
- {
- obj.SelectedChanged += OnSelectChanged;
- graphicsList.Insert(0, obj);
- this.Dirty = true;
- }
- /// <summary>
- /// Insert object to specified place.
- /// Used for Undo.
- /// </summary>
- public void Insert(int index, DrawObject obj)
- {
- if (index >= 0 && index < graphicsList.Count)
- {
- graphicsList.Insert(index, obj);
- }
- }
- /// <summary>
- /// Replace object in specified place.
- /// Used for Undo.
- /// </summary>
- public void Replace(int index, DrawObject obj)
- {
- if (index >= 0 && index < graphicsList.Count)
- {
- graphicsList.RemoveAt(index);
- graphicsList.Insert(index, obj);
- }
- }
- /// <summary>
- /// Remove object by index.
- /// Used for Undo.
- /// </summary>
- public void RemoveAt(int index)
- {
- graphicsList.RemoveAt(index);
- }
- public void RemoveObj(DrawObject obj)
- {
- graphicsList.Remove(obj);
- }
- public void RemoveByIds(List<int> ids)
- {
- for (int i = 0; i < ids.Count; i++)
- {
- graphicsList.RemoveAll(u => u.ID == ids[i]);
- }
- }
- /// <summary>
- /// Delete last added object from the list
- /// (used for Undo operation).
- /// </summary>
- public void DeleteLastAddedObject()
- {
- if (graphicsList.Count > 0)
- {
- graphicsList.RemoveAt(0);
- }
- }
- /// <summary>
- /// 删除选中,目前应用在视场
- /// </summary>
- /// <returns>
- /// true if at least one object is deleted
- /// </returns>
- public bool DeleteSelection(DrawClass type)
- {
- bool result = false;
- int n = graphicsList.Count;
- for (int i = n - 1; i >= 0; i--)
- {
- if (((DrawObject)graphicsList[i]).Selected)
- {
- //这里用来区分选择的是标注、测量、还是视场
- //比如选择的是测量,用删除视场的菜单就不应该生效
- if (type == ((DrawObject)graphicsList[i]).objectType)
- {
- graphicsList.RemoveAt(i);
- result = true;
- }
- }
- }
- return result;
- }
- #endregion
- /// <summary>
- /// 删除某种类型的工具
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public bool DeleteDrawClass(DrawClass type)
- {
- bool result = false;
- int n = graphicsList.Count;
- for (int i = n - 1; i >= 0; i--)
- {
- if (type == ((DrawObject)graphicsList[i]).objectType)
- {
- graphicsList.RemoveAt(i);
- result = true;
- }
- }
- return result;
- }
- public bool DeleteDrawClass(DrawToolType type)
- {
- bool result = false;
- int n = graphicsList.Count;
- for (int i = n - 1; i >= 0; i--)
- {
- if (type == ((DrawObject)graphicsList[i]).drawToolType)
- {
- graphicsList.RemoveAt(i);
- result = true;
- }
- }
- return result;
- }
- /// <summary>
- /// 清空视场
- /// </summary>
- /// <returns></returns>
- public void DeleteAllView()
- {
- int n = graphicsList.Count;
- for (int i = n - 1; i >= 0; i--)
- {
- if (DrawClass.View == ((DrawObject)graphicsList[i]).objectType)
- {
- graphicsList.RemoveAt(i);
- }
- }
- }
- /// <summary>
- /// 向上移动
- /// </summary>
- /// <returns></returns>
- public bool MoveSelectionToMoveUp()
- {
- int n = graphicsList.Count;
- for (int i = 0; i < n; i++)
- {
- if ((graphicsList[i]).Selected)
- {
- if (i > 0)
- {
- graphicsList.Insert(i - 1, graphicsList[i]);
- graphicsList.RemoveAt(i + 1);
- }
- }
- }
- return true;
- }
- /// <summary>
- /// 向下移动
- /// </summary>
- /// <returns></returns>
- public bool MoveSelectionToMoveDown()
- {
- int n = graphicsList.Count;
- for (int i = 0; i < n; i++)
- {
- if ((graphicsList[i]).Selected)
- {
- if (i < n - 1)
- {
- graphicsList.Insert(i + 2, graphicsList[i]);
- graphicsList.RemoveAt(i);
- break;
- }
- }
- }
- return true;
- }
- /// <summary>
- /// 移动到最上
- /// Move selected items to front (beginning of the list)
- /// </summary>
- /// <returns>
- /// true if at least one object is moved
- /// </returns>
- public bool MoveSelectionToFront()
- {
- int n;
- int i;
- DrawList tempList;
- tempList = new DrawList();
- n = graphicsList.Count;
- // Read source list in reverse order, add every selected item
- // to temporary list and remove it from source list
- for (i = n - 1; i >= 0; i--)
- {
- if ((graphicsList[i]).Selected)
- {
- tempList.Add(graphicsList[i]);
- graphicsList.RemoveAt(i);
- }
- }
- // Read temporary list in direct order and insert every item
- // to the beginning of the source list
- n = tempList.Count;
- for (i = 0; i < n; i++)
- {
- graphicsList.Insert(0, tempList[i]);
- }
- return (n > 0);
- }
- /// <summary>
- /// 移动到最下
- /// Move selected items to back (end of the list)
- /// </summary>
- /// <returns>
- /// true if at least one object is moved
- /// </returns>
- public bool MoveSelectionToBack()
- {
- int n;
- int i;
- DrawList tempList;
- tempList = new DrawList();
- n = graphicsList.Count;
- // Read source list in reverse order, add every selected item
- // to temporary list and remove it from source list
- for (i = n - 1; i >= 0; i--)
- {
- if ((graphicsList[i]).Selected)
- {
- tempList.Add(graphicsList[i]);
- graphicsList.RemoveAt(i);
- }
- }
- // Read temporary list in reverse order and add every item
- // to the end of the source list
- n = tempList.Count;
- for (i = n - 1; i >= 0; i--)
- {
- graphicsList.Add(tempList[i]);
- }
- return (n > 0);
- }
- #endregion
- /// <summary>
- /// 判断是否存在视场
- /// </summary>
- /// <returns></returns>
- public bool IsExsitView()
- {
- if (this.graphicsList != null && this.graphicsList.Count > 0
- && this.graphicsList.FindAll(a => a.objectType == DrawClass.View).Count > 0)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 获取画布内指定大类的集合
- /// </summary>
- /// <returns></returns>
- public List<DrawObject> GetDrawClassList(DrawClass drawClass)
- {
- return this.graphicsList.FindAll(a => a.objectType == drawClass);
- }
- /// <summary>
- /// 检测当前画板内是否存在指定的类型
- /// </summary>
- /// <param name="drawToolType"></param>
- /// <returns></returns>
- public bool IsExsitSpecificObject(DrawToolType drawToolType)
- {
- if (this.graphicsList != null && this.graphicsList.Count > 0)
- return this.graphicsList.FindAll(a => a.drawToolType == drawToolType).Count > 0;
- return false;
- }
- /// <summary>
- /// 获取画板内指定类型的集合
- /// </summary>
- /// <param name="drawToolType"></param>
- /// <returns></returns>
- public List<DrawObject> GetDrawToolTypeList(DrawToolType drawToolType)
- {
- return this.graphicsList.FindAll(a => a.drawToolType == drawToolType);
- }
- public DrawObject GetById(int id)
- {
- return this.graphicsList.Find(a => a.ID == id);
- }
- public List<LabelMeasureDataModel> GetLabelsOrMeasureData(DrawClass drawClass)
- {
- List<LabelMeasureDataModel> list = new List<LabelMeasureDataModel>();
- foreach (DrawObject o in graphicsList)
- {
- if (o.objectType == drawClass)
- {
- LabelMeasureDataModel labelData = new LabelMeasureDataModel();
- labelData.drawClass = o.objectType.ToString();
- labelData.drawToolType = o.drawToolType.ToString();
- labelData.points = o.GetPoints();
- labelData.style = o.GetStyle();
- labelData.content = o.GetContent();
- list.Add(labelData);
- }
- }
- return list;
- }
- }
- }
|