GraphicsList.cs 17 KB

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