GraphicsList.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. using SmartCoalApplication.Annotation.Enum;
  2. using SmartCoalApplication.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 SmartCoalApplication.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. public void DrawPicture(Graphics g)
  113. {
  114. int n = graphicsList.Count;
  115. DrawObject o;
  116. int n3 = 0;
  117. // Enumerate list in reverse order to get first
  118. // object on the top of Z-order.
  119. for (int i = n - 1; i >= 0; i--)
  120. {
  121. o = graphicsList[i];
  122. //如果是视场,则只绘制一次
  123. if (o.objectType == DrawClass.View)
  124. {
  125. if (n3 == 0)
  126. {
  127. o.DrawPicture(g,o.Rectangle);
  128. n3++;
  129. }
  130. }
  131. else
  132. {
  133. o.Draw(g);
  134. }
  135. if (o.Selected == true)
  136. {
  137. o.DrawTracker(g);
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Dump (for debugging)
  143. /// </summary>
  144. public void Dump()
  145. {
  146. Trace.WriteLine("");
  147. foreach (DrawObject o in graphicsList)
  148. {
  149. o.Dump();
  150. }
  151. }
  152. /// <summary>
  153. /// Clear all objects in the list
  154. /// </summary>
  155. /// <returns>
  156. /// true if at least one object is deleted
  157. /// </returns>
  158. public bool Clear()
  159. {
  160. bool result = (graphicsList.Count > 0);
  161. graphicsList.Clear();
  162. return result;
  163. }
  164. /// <summary>
  165. /// Tells if the document is dirty.
  166. /// </summary>
  167. public bool Dirty
  168. {
  169. get
  170. {
  171. return this.dirty;
  172. }
  173. set
  174. {
  175. this.dirty = value;
  176. if (this.dirty)
  177. {
  178. NotifyDirty();
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// Count and this [nIndex] allow to read all graphics objects
  184. /// from GraphicsList in the loop.
  185. /// </summary>
  186. public int Count
  187. {
  188. get
  189. {
  190. return graphicsList.Count;
  191. }
  192. }
  193. public DrawObject this[int index]
  194. {
  195. get
  196. {
  197. if (index < 0 || index >= graphicsList.Count)
  198. return null;
  199. return graphicsList[index];
  200. }
  201. }
  202. public Size GetSize()
  203. {
  204. Size size = new Size(0, 0);
  205. foreach (DrawObject obj in graphicsList)
  206. {
  207. RectangleF objRect = obj.Rectangle;
  208. size.Width = (int)Math.Max(size.Width, objRect.X + objRect.Width);
  209. size.Height = (int)Math.Max(size.Height, objRect.Y + objRect.Height);
  210. }
  211. size.Width += 20;
  212. size.Height += 20;
  213. return size;
  214. }
  215. /// <summary>
  216. /// SelectedCount and GetSelectedObject allow to read
  217. /// selected objects in the loop
  218. /// </summary>
  219. public int SelectionCount
  220. {
  221. get
  222. {
  223. int n = 0;
  224. foreach (DrawObject o in Selection)
  225. {
  226. n++;
  227. }
  228. return n;
  229. }
  230. }
  231. public IEnumerable<DrawObject> Selection
  232. {
  233. get
  234. {
  235. foreach (DrawObject o in graphicsList)
  236. {
  237. if (o.Selected)
  238. {
  239. yield return o;
  240. }
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 用于修改视场,获得选中的视场
  246. /// 因为修改视场判断了只能修改一个,
  247. /// 所以找到一个被选中就返回
  248. /// </summary>
  249. public DrawObject SelectDrawObject
  250. {
  251. get
  252. {
  253. foreach (DrawObject o in graphicsList)
  254. {
  255. if (o.Selected)
  256. {
  257. return o;
  258. }
  259. }
  260. return null;
  261. }
  262. }
  263. public void Add(DrawObject obj)
  264. {
  265. obj.SelectedChanged += new EventHandler(this.childSelected_SelectedChanged);
  266. // insert to the top of z-order
  267. graphicsList.Insert(0, obj);
  268. this.Dirty = true;
  269. }
  270. public void AddRange(List<DrawObject> obj)
  271. {
  272. //obj.SelectedChanged += new EventHandler(this.childSelected_SelectedChanged);
  273. // insert to the top of z-order
  274. graphicsList.AddRange(obj);
  275. this.Dirty = true;
  276. }
  277. private void childSelected_SelectedChanged(object sender, EventArgs e)
  278. {
  279. OnSelectChanged();
  280. }
  281. /// <summary>
  282. /// Insert object to specified place.
  283. /// Used for Undo.
  284. /// </summary>
  285. public void Insert(int index, DrawObject obj)
  286. {
  287. if (index >= 0 && index < graphicsList.Count)
  288. {
  289. graphicsList.Insert(index, obj);
  290. }
  291. }
  292. /// <summary>
  293. /// Replace object in specified place.
  294. /// Used for Undo.
  295. /// </summary>
  296. public void Replace(int index, DrawObject obj)
  297. {
  298. if (index >= 0 && index < graphicsList.Count)
  299. {
  300. graphicsList.RemoveAt(index);
  301. graphicsList.Insert(index, obj);
  302. }
  303. }
  304. /// <summary>
  305. /// Remove object by index.
  306. /// Used for Undo.
  307. /// </summary>
  308. public void RemoveAt(int index)
  309. {
  310. graphicsList.RemoveAt(index);
  311. }
  312. public void RemoveObj(DrawObject obj)
  313. {
  314. graphicsList.Remove(obj);
  315. }
  316. public void RemoveByIds(List<int> ids)
  317. {
  318. for (int i = 0; i < ids.Count; i++)
  319. {
  320. graphicsList.RemoveAll(u => u.ID == ids[i]);
  321. }
  322. }
  323. /// <summary>
  324. /// Delete last added object from the list
  325. /// (used for Undo operation).
  326. /// </summary>
  327. public void DeleteLastAddedObject()
  328. {
  329. if (graphicsList.Count > 0)
  330. {
  331. graphicsList.RemoveAt(0);
  332. }
  333. }
  334. public void SelectInRectangle(Rectangle rectangle)
  335. {
  336. UnselectAll();
  337. foreach (DrawObject o in graphicsList)
  338. {
  339. if (o.IntersectsWith(rectangle) && o.canMove)
  340. o.Selected = true;
  341. }
  342. }
  343. public void UnselectAll()
  344. {
  345. foreach (DrawObject o in graphicsList)
  346. {
  347. o.Selected = false;
  348. }
  349. }
  350. public void SelectAll()
  351. {
  352. foreach (DrawObject o in graphicsList)
  353. {
  354. if (o.canMove)
  355. {
  356. o.Selected = true;
  357. }
  358. }
  359. }
  360. /// <summary>
  361. /// 删除选中,目前应用在视场
  362. /// </summary>
  363. /// <returns>
  364. /// true if at least one object is deleted
  365. /// </returns>
  366. public bool DeleteSelection(DrawClass type)
  367. {
  368. bool result = false;
  369. int n = graphicsList.Count;
  370. for (int i = n - 1; i >= 0; i--)
  371. {
  372. if (((DrawObject)graphicsList[i]).Selected)
  373. {
  374. //这里用来区分选择的是标注、测量、还是视场
  375. //比如选择的是测量,用删除视场的菜单就不应该生效
  376. if(type == ((DrawObject)graphicsList[i]).objectType)
  377. {
  378. graphicsList.RemoveAt(i);
  379. result = true;
  380. }
  381. }
  382. }
  383. return result;
  384. }
  385. /// <summary>
  386. /// 删除某种类型的工具
  387. /// </summary>
  388. /// <param name="type"></param>
  389. /// <returns></returns>
  390. public bool DeleteDrawClass(DrawClass type)
  391. {
  392. bool result = false;
  393. int n = graphicsList.Count;
  394. for (int i = n - 1; i >= 0; i--)
  395. {
  396. if (type == ((DrawObject)graphicsList[i]).objectType)
  397. {
  398. graphicsList.RemoveAt(i);
  399. result = true;
  400. }
  401. }
  402. return result;
  403. }
  404. public bool DeleteDrawClass(DrawToolType type)
  405. {
  406. bool result = false;
  407. int n = graphicsList.Count;
  408. for (int i = n - 1; i >= 0; i--)
  409. {
  410. if (type == ((DrawObject)graphicsList[i]).drawToolType)
  411. {
  412. graphicsList.RemoveAt(i);
  413. result = true;
  414. }
  415. }
  416. return result;
  417. }
  418. /// <summary>
  419. /// 清空视场
  420. /// </summary>
  421. /// <returns></returns>
  422. public void DeleteAllView()
  423. {
  424. int n = graphicsList.Count;
  425. for (int i = n - 1; i >= 0; i--)
  426. {
  427. if (DrawClass.View == ((DrawObject)graphicsList[i]).objectType)
  428. {
  429. graphicsList.RemoveAt(i);
  430. }
  431. }
  432. }
  433. /// <summary>
  434. /// 向上移动
  435. /// </summary>
  436. /// <returns></returns>
  437. public bool MoveSelectionToMoveUp()
  438. {
  439. int n = graphicsList.Count;
  440. for (int i = 0; i < n; i++)
  441. {
  442. if ((graphicsList[i]).Selected)
  443. {
  444. if (i > 0)
  445. {
  446. graphicsList.Insert(i - 1, graphicsList[i]);
  447. graphicsList.RemoveAt(i + 1);
  448. }
  449. }
  450. }
  451. return true;
  452. }
  453. /// <summary>
  454. /// 向下移动
  455. /// </summary>
  456. /// <returns></returns>
  457. public bool MoveSelectionToMoveDown()
  458. {
  459. int n = graphicsList.Count;
  460. for (int i = 0; i < n; i++)
  461. {
  462. if ((graphicsList[i]).Selected)
  463. {
  464. if (i < n - 1)
  465. {
  466. graphicsList.Insert(i + 2, graphicsList[i]);
  467. graphicsList.RemoveAt(i);
  468. break;
  469. }
  470. }
  471. }
  472. return true;
  473. }
  474. /// <summary>
  475. /// 移动到最上
  476. /// Move selected items to front (beginning of the list)
  477. /// </summary>
  478. /// <returns>
  479. /// true if at least one object is moved
  480. /// </returns>
  481. public bool MoveSelectionToFront()
  482. {
  483. int n;
  484. int i;
  485. DrawList tempList;
  486. tempList = new DrawList();
  487. n = graphicsList.Count;
  488. // Read source list in reverse order, add every selected item
  489. // to temporary list and remove it from source list
  490. for (i = n - 1; i >= 0; i--)
  491. {
  492. if ((graphicsList[i]).Selected)
  493. {
  494. tempList.Add(graphicsList[i]);
  495. graphicsList.RemoveAt(i);
  496. }
  497. }
  498. // Read temporary list in direct order and insert every item
  499. // to the beginning of the source list
  500. n = tempList.Count;
  501. for (i = 0; i < n; i++)
  502. {
  503. graphicsList.Insert(0, tempList[i]);
  504. }
  505. return (n > 0);
  506. }
  507. /// <summary>
  508. /// 移动到最下
  509. /// Move selected items to back (end of the list)
  510. /// </summary>
  511. /// <returns>
  512. /// true if at least one object is moved
  513. /// </returns>
  514. public bool MoveSelectionToBack()
  515. {
  516. int n;
  517. int i;
  518. DrawList tempList;
  519. tempList = new DrawList();
  520. n = graphicsList.Count;
  521. // Read source list in reverse order, add every selected item
  522. // to temporary list and remove it from source list
  523. for (i = n - 1; i >= 0; i--)
  524. {
  525. if ((graphicsList[i]).Selected)
  526. {
  527. tempList.Add(graphicsList[i]);
  528. graphicsList.RemoveAt(i);
  529. }
  530. }
  531. // Read temporary list in reverse order and add every item
  532. // to the end of the source list
  533. n = tempList.Count;
  534. for (i = n - 1; i >= 0; i--)
  535. {
  536. graphicsList.Add(tempList[i]);
  537. }
  538. return (n > 0);
  539. }
  540. #endregion
  541. /// <summary>
  542. /// 判断是否存在视场
  543. /// </summary>
  544. /// <returns></returns>
  545. public bool IsExsitView()
  546. {
  547. if (this.graphicsList!=null && this.graphicsList.Count>0
  548. && this.graphicsList.FindAll(a => a.objectType == DrawClass.View).Count>0)
  549. {
  550. return true;
  551. }
  552. return false;
  553. }
  554. /// <summary>
  555. /// 获取画布内指定大类的集合
  556. /// </summary>
  557. /// <returns></returns>
  558. public List<DrawObject> GetDrawClassList(DrawClass drawClass)
  559. {
  560. return this.graphicsList.FindAll(a => a.objectType == drawClass);
  561. }
  562. /// <summary>
  563. /// 检测当前画板内是否存在指定的类型
  564. /// </summary>
  565. /// <param name="drawToolType"></param>
  566. /// <returns></returns>
  567. public bool IsExsitSpecificObject(DrawToolType drawToolType)
  568. {
  569. if (this.graphicsList != null && this.graphicsList.Count > 0)
  570. return this.graphicsList.FindAll(a => a.drawToolType == drawToolType).Count > 0;
  571. return false;
  572. }
  573. /// <summary>
  574. /// 获取画板内指定类型的集合
  575. /// </summary>
  576. /// <param name="drawToolType"></param>
  577. /// <returns></returns>
  578. public List<DrawObject> GetDrawToolTypeList(DrawToolType drawToolType)
  579. {
  580. return this.graphicsList.FindAll(a => a.drawToolType == drawToolType);
  581. }
  582. public DrawObject GetById(int id)
  583. {
  584. return this.graphicsList.Find(a => a.ID == id);
  585. }
  586. public List<LabelMeasureDataModel> GetLabelsOrMeasureData(DrawClass drawClass)
  587. {
  588. List<LabelMeasureDataModel> list = new List<LabelMeasureDataModel>();
  589. foreach (DrawObject o in graphicsList)
  590. {
  591. if(o.objectType == drawClass)
  592. {
  593. LabelMeasureDataModel labelData = new LabelMeasureDataModel();
  594. labelData.drawClass = o.objectType.ToString();
  595. labelData.drawToolType = o.drawToolType.ToString();
  596. labelData.points = o.GetPoints();
  597. labelData.style = o.GetStyle();
  598. labelData.content = o.GetContent();
  599. list.Add(labelData);
  600. }
  601. }
  602. return list;
  603. }
  604. }
  605. }