GraphicsList.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. using System.Security.Permissions;
  11. namespace PaintDotNet.Annotation
  12. {
  13. using DrawList = List<DrawObject>;
  14. public interface DocumentDirtyObserver
  15. {
  16. void IsDirty(GraphicsList gList);
  17. }
  18. /// <summary>
  19. /// 绘制对象的数组
  20. /// 每个surface保持一个自己的list
  21. /// </summary>
  22. [Serializable]
  23. public class GraphicsList : ISerializable
  24. {
  25. public DrawList graphicsList;
  26. private List<DocumentDirtyObserver> observers;
  27. private bool dirty;
  28. private const string entryCount = "Count";
  29. private const string entryType = "Type";
  30. public GraphicsList()
  31. {
  32. this.graphicsList = new DrawList();
  33. this.observers = new List<DocumentDirtyObserver>();
  34. this.dirty = false;
  35. }
  36. public GraphicsList(ISurfaceBox surfaceBox)
  37. {
  38. this.graphicsList = new DrawList();
  39. this.observers = new List<DocumentDirtyObserver>();
  40. this.dirty = false;
  41. }
  42. #region Serialization Support
  43. /// <summary>
  44. /// Save object to serialization stream
  45. /// </summary>
  46. /// <param name="info"></param>
  47. /// <param name="context"></param>
  48. [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  49. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  50. {
  51. info.AddValue(entryCount, graphicsList.Count);
  52. int i = 0;
  53. foreach (DrawObject o in graphicsList)
  54. {
  55. info.AddValue(
  56. String.Format(CultureInfo.InvariantCulture,
  57. "{0}{1}",
  58. entryType, i),
  59. o.GetType().FullName);
  60. o.SaveToStream(info, i);
  61. i++;
  62. }
  63. }
  64. #endregion
  65. #region Other functions
  66. protected void NotifyDirty()
  67. {
  68. if (this.dirty)
  69. {
  70. foreach (DocumentDirtyObserver obs in this.observers)
  71. {
  72. obs.IsDirty(this);
  73. }
  74. }
  75. }
  76. public void Draw(Graphics g)
  77. {
  78. int n = graphicsList.Count;
  79. DrawObject o;
  80. int n3 = 0;
  81. // Enumerate list in reverse order to get first
  82. // object on the top of Z-order.
  83. for (int i = n - 1; i >= 0; i--)
  84. {
  85. o = graphicsList[i];
  86. //如果是视场,则只绘制一次
  87. if (o.objectType == DrawClass.View)
  88. {
  89. if (n3 == 0)
  90. {
  91. o.Draw(g);
  92. n3++;
  93. }
  94. }
  95. else
  96. {
  97. o.Draw(g);
  98. }
  99. if (o.Selected == true)
  100. {
  101. o.DrawTracker(g);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Dump (for debugging)
  107. /// </summary>
  108. public void Dump()
  109. {
  110. Trace.WriteLine("");
  111. foreach (DrawObject o in graphicsList)
  112. {
  113. o.Dump();
  114. }
  115. }
  116. /// <summary>
  117. /// Clear all objects in the list
  118. /// </summary>
  119. /// <returns>
  120. /// true if at least one object is deleted
  121. /// </returns>
  122. public bool Clear()
  123. {
  124. bool result = (graphicsList.Count > 0);
  125. graphicsList.Clear();
  126. return result;
  127. }
  128. /// <summary>
  129. /// Tells if the document is dirty.
  130. /// </summary>
  131. public bool Dirty
  132. {
  133. get
  134. {
  135. return this.dirty;
  136. }
  137. set
  138. {
  139. this.dirty = value;
  140. if (this.dirty)
  141. {
  142. NotifyDirty();
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// Count and this [nIndex] allow to read all graphics objects
  148. /// from GraphicsList in the loop.
  149. /// </summary>
  150. public int Count
  151. {
  152. get
  153. {
  154. return graphicsList.Count;
  155. }
  156. }
  157. public DrawObject this[int index]
  158. {
  159. get
  160. {
  161. if (index < 0 || index >= graphicsList.Count)
  162. return null;
  163. return graphicsList[index];
  164. }
  165. }
  166. public Size GetSize()
  167. {
  168. Size size = new Size(0, 0);
  169. foreach (DrawObject obj in graphicsList)
  170. {
  171. RectangleF objRect = obj.Rectangle;
  172. size.Width = (int)Math.Max(size.Width, objRect.X + objRect.Width);
  173. size.Height = (int)Math.Max(size.Height, objRect.Y + objRect.Height);
  174. }
  175. size.Width += 20;
  176. size.Height += 20;
  177. return size;
  178. }
  179. #region Selection
  180. /// <summary>
  181. /// SelectedCount and GetSelectedObject allow to read
  182. /// selected objects in the loop
  183. /// </summary>
  184. public int SelectionCount
  185. {
  186. get
  187. {
  188. int n = 0;
  189. foreach (DrawObject o in Selection)
  190. {
  191. n++;
  192. }
  193. return n;
  194. }
  195. }
  196. public IEnumerable<DrawObject> Selection
  197. {
  198. get
  199. {
  200. foreach (DrawObject o in graphicsList)
  201. {
  202. if (o.Selected)
  203. {
  204. yield return o;
  205. }
  206. }
  207. }
  208. }
  209. DrawObject LastDrawObject = null;
  210. /// <summary>
  211. /// 用于修改视场,获得选中的视场
  212. /// 因为修改视场判断了只能修改一个,
  213. /// 所以找到一个被选中就返回
  214. /// </summary>
  215. public DrawObject SelectDrawObject
  216. {
  217. get
  218. {
  219. if (graphicsList.Count((o) => o.Selected) == 1)
  220. return graphicsList.Find((o) => o.Selected);
  221. else
  222. return null;
  223. }
  224. }
  225. public event EventHandler SelectChanged;
  226. public virtual void OnSelectChanged()
  227. {
  228. if (LastDrawObject == SelectDrawObject) return;
  229. LastDrawObject = SelectDrawObject;
  230. SelectChanged?.Invoke(this, EventArgs.Empty);
  231. }
  232. public void SelectInRectangle(Rectangle rectangle)
  233. {
  234. UnselectAll();
  235. foreach (DrawObject o in graphicsList)
  236. {
  237. if (o.IntersectsWith(rectangle) && !o.isLocked)
  238. o.Selected = true;
  239. }
  240. }
  241. public void UnselectAll()
  242. {
  243. foreach (DrawObject o in graphicsList)
  244. {
  245. o.Selected = false;
  246. }
  247. }
  248. public void SelectAll()
  249. {
  250. foreach (DrawObject o in graphicsList)
  251. {
  252. if (!o.isLocked)
  253. {
  254. o.Selected = true;
  255. }
  256. }
  257. }
  258. #endregion
  259. #region 增删改
  260. public void Add(DrawObject obj)
  261. {
  262. obj.SelectedChanged += OnSelectChanged;
  263. graphicsList.Insert(0, obj);
  264. this.Dirty = true;
  265. }
  266. /// <summary>
  267. /// Insert object to specified place.
  268. /// Used for Undo.
  269. /// </summary>
  270. public void Insert(int index, DrawObject obj)
  271. {
  272. if (index >= 0 && index < graphicsList.Count)
  273. {
  274. graphicsList.Insert(index, obj);
  275. }
  276. }
  277. /// <summary>
  278. /// Replace object in specified place.
  279. /// Used for Undo.
  280. /// </summary>
  281. public void Replace(int index, DrawObject obj)
  282. {
  283. if (index >= 0 && index < graphicsList.Count)
  284. {
  285. graphicsList.RemoveAt(index);
  286. graphicsList.Insert(index, obj);
  287. }
  288. }
  289. /// <summary>
  290. /// Remove object by index.
  291. /// Used for Undo.
  292. /// </summary>
  293. public void RemoveAt(int index)
  294. {
  295. graphicsList.RemoveAt(index);
  296. }
  297. public void RemoveObj(DrawObject obj)
  298. {
  299. graphicsList.Remove(obj);
  300. }
  301. public void RemoveByIds(List<int> ids)
  302. {
  303. for (int i = 0; i < ids.Count; i++)
  304. {
  305. graphicsList.RemoveAll(u => u.ID == ids[i]);
  306. }
  307. }
  308. /// <summary>
  309. /// Delete last added object from the list
  310. /// (used for Undo operation).
  311. /// </summary>
  312. public void DeleteLastAddedObject()
  313. {
  314. if (graphicsList.Count > 0)
  315. {
  316. graphicsList.RemoveAt(0);
  317. }
  318. }
  319. /// <summary>
  320. /// 删除选中,目前应用在视场
  321. /// </summary>
  322. /// <returns>
  323. /// true if at least one object is deleted
  324. /// </returns>
  325. public bool DeleteSelection(DrawClass type)
  326. {
  327. bool result = false;
  328. int n = graphicsList.Count;
  329. for (int i = n - 1; i >= 0; i--)
  330. {
  331. if (((DrawObject)graphicsList[i]).Selected)
  332. {
  333. //这里用来区分选择的是标注、测量、还是视场
  334. //比如选择的是测量,用删除视场的菜单就不应该生效
  335. if (type == ((DrawObject)graphicsList[i]).objectType)
  336. {
  337. graphicsList.RemoveAt(i);
  338. result = true;
  339. }
  340. }
  341. }
  342. return result;
  343. }
  344. #endregion
  345. /// <summary>
  346. /// 删除某种类型的工具
  347. /// </summary>
  348. /// <param name="type"></param>
  349. /// <returns></returns>
  350. public bool DeleteDrawClass(DrawClass type)
  351. {
  352. bool result = false;
  353. int n = graphicsList.Count;
  354. for (int i = n - 1; i >= 0; i--)
  355. {
  356. if (type == ((DrawObject)graphicsList[i]).objectType)
  357. {
  358. graphicsList.RemoveAt(i);
  359. result = true;
  360. }
  361. }
  362. return result;
  363. }
  364. public bool DeleteDrawClass(DrawToolType type)
  365. {
  366. bool result = false;
  367. int n = graphicsList.Count;
  368. for (int i = n - 1; i >= 0; i--)
  369. {
  370. if (type == ((DrawObject)graphicsList[i]).drawToolType)
  371. {
  372. graphicsList.RemoveAt(i);
  373. result = true;
  374. }
  375. }
  376. return result;
  377. }
  378. /// <summary>
  379. /// 清空视场
  380. /// </summary>
  381. /// <returns></returns>
  382. public void DeleteAllView()
  383. {
  384. int n = graphicsList.Count;
  385. for (int i = n - 1; i >= 0; i--)
  386. {
  387. if (DrawClass.View == ((DrawObject)graphicsList[i]).objectType)
  388. {
  389. graphicsList.RemoveAt(i);
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// 向上移动
  395. /// </summary>
  396. /// <returns></returns>
  397. public bool MoveSelectionToMoveUp()
  398. {
  399. int n = graphicsList.Count;
  400. for (int i = 0; i < n; i++)
  401. {
  402. if ((graphicsList[i]).Selected)
  403. {
  404. if (i > 0)
  405. {
  406. graphicsList.Insert(i - 1, graphicsList[i]);
  407. graphicsList.RemoveAt(i + 1);
  408. }
  409. }
  410. }
  411. return true;
  412. }
  413. /// <summary>
  414. /// 向下移动
  415. /// </summary>
  416. /// <returns></returns>
  417. public bool MoveSelectionToMoveDown()
  418. {
  419. int n = graphicsList.Count;
  420. for (int i = 0; i < n; i++)
  421. {
  422. if ((graphicsList[i]).Selected)
  423. {
  424. if (i < n - 1)
  425. {
  426. graphicsList.Insert(i + 2, graphicsList[i]);
  427. graphicsList.RemoveAt(i);
  428. break;
  429. }
  430. }
  431. }
  432. return true;
  433. }
  434. /// <summary>
  435. /// 移动到最上
  436. /// Move selected items to front (beginning of the list)
  437. /// </summary>
  438. /// <returns>
  439. /// true if at least one object is moved
  440. /// </returns>
  441. public bool MoveSelectionToFront()
  442. {
  443. int n;
  444. int i;
  445. DrawList tempList;
  446. tempList = new DrawList();
  447. n = graphicsList.Count;
  448. // Read source list in reverse order, add every selected item
  449. // to temporary list and remove it from source list
  450. for (i = n - 1; i >= 0; i--)
  451. {
  452. if ((graphicsList[i]).Selected)
  453. {
  454. tempList.Add(graphicsList[i]);
  455. graphicsList.RemoveAt(i);
  456. }
  457. }
  458. // Read temporary list in direct order and insert every item
  459. // to the beginning of the source list
  460. n = tempList.Count;
  461. for (i = 0; i < n; i++)
  462. {
  463. graphicsList.Insert(0, tempList[i]);
  464. }
  465. return (n > 0);
  466. }
  467. /// <summary>
  468. /// 移动到最下
  469. /// Move selected items to back (end of the list)
  470. /// </summary>
  471. /// <returns>
  472. /// true if at least one object is moved
  473. /// </returns>
  474. public bool MoveSelectionToBack()
  475. {
  476. int n;
  477. int i;
  478. DrawList tempList;
  479. tempList = new DrawList();
  480. n = graphicsList.Count;
  481. // Read source list in reverse order, add every selected item
  482. // to temporary list and remove it from source list
  483. for (i = n - 1; i >= 0; i--)
  484. {
  485. if ((graphicsList[i]).Selected)
  486. {
  487. tempList.Add(graphicsList[i]);
  488. graphicsList.RemoveAt(i);
  489. }
  490. }
  491. // Read temporary list in reverse order and add every item
  492. // to the end of the source list
  493. n = tempList.Count;
  494. for (i = n - 1; i >= 0; i--)
  495. {
  496. graphicsList.Add(tempList[i]);
  497. }
  498. return (n > 0);
  499. }
  500. #endregion
  501. /// <summary>
  502. /// 判断是否存在视场
  503. /// </summary>
  504. /// <returns></returns>
  505. public bool IsExsitView()
  506. {
  507. if (this.graphicsList != null && this.graphicsList.Count > 0
  508. && this.graphicsList.FindAll(a => a.objectType == DrawClass.View).Count > 0)
  509. {
  510. return true;
  511. }
  512. return false;
  513. }
  514. /// <summary>
  515. /// 获取画布内指定大类的集合
  516. /// </summary>
  517. /// <returns></returns>
  518. public List<DrawObject> GetDrawClassList(DrawClass drawClass)
  519. {
  520. return this.graphicsList.FindAll(a => a.objectType == drawClass);
  521. }
  522. /// <summary>
  523. /// 检测当前画板内是否存在指定的类型
  524. /// </summary>
  525. /// <param name="drawToolType"></param>
  526. /// <returns></returns>
  527. public bool IsExsitSpecificObject(DrawToolType drawToolType)
  528. {
  529. if (this.graphicsList != null && this.graphicsList.Count > 0)
  530. return this.graphicsList.FindAll(a => a.drawToolType == drawToolType).Count > 0;
  531. return false;
  532. }
  533. /// <summary>
  534. /// 获取画板内指定类型的集合
  535. /// </summary>
  536. /// <param name="drawToolType"></param>
  537. /// <returns></returns>
  538. public List<DrawObject> GetDrawToolTypeList(DrawToolType drawToolType)
  539. {
  540. return this.graphicsList.FindAll(a => a.drawToolType == drawToolType);
  541. }
  542. public DrawObject GetById(int id)
  543. {
  544. return this.graphicsList.Find(a => a.ID == id);
  545. }
  546. public List<LabelMeasureDataModel> GetLabelsOrMeasureData(DrawClass drawClass)
  547. {
  548. List<LabelMeasureDataModel> list = new List<LabelMeasureDataModel>();
  549. foreach (DrawObject o in graphicsList)
  550. {
  551. if (o.objectType == drawClass)
  552. {
  553. LabelMeasureDataModel labelData = new LabelMeasureDataModel();
  554. labelData.drawClass = o.objectType.ToString();
  555. labelData.drawToolType = o.drawToolType.ToString();
  556. labelData.points = o.GetPoints();
  557. labelData.style = o.GetStyle();
  558. labelData.content = o.GetContent();
  559. list.Add(labelData);
  560. }
  561. }
  562. return list;
  563. }
  564. }
  565. }