Leader.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/Draw/Dimension/Dimension.js");
  20. /**
  21. * \class Leader
  22. * \brief Draw leader, segment by segment.
  23. * \ingroup ecma_draw_dimension
  24. */
  25. function Leader(guiAction) {
  26. Dimension.call(this, guiAction);
  27. this.arrowHead = true;
  28. this.leaderEntity = undefined;
  29. this.segment = undefined;
  30. this.arrowBlockId = RBlock.INVALID_ID;
  31. this.lastPoint = undefined;
  32. this.setUiOptions("Leader.ui");
  33. }
  34. Leader.prototype = new Dimension();
  35. Leader.State = {
  36. SettingFirstVertex : 0,
  37. SettingNextVertex : 1
  38. };
  39. Leader.prototype.beginEvent = function() {
  40. Dimension.prototype.beginEvent.call(this);
  41. this.setState(Leader.State.SettingFirstVertex);
  42. this.updateButtonStates();
  43. };
  44. Leader.prototype.setState = function(state) {
  45. Dimension.prototype.setState.call(this, state);
  46. this.setCrosshairCursor();
  47. var appWin = RMainWindowQt.getMainWindow();
  48. switch (this.state) {
  49. case Leader.State.SettingFirstVertex:
  50. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  51. var trFirstVertex = qsTr("Arrow position");
  52. this.setCommandPrompt(trFirstVertex);
  53. this.setLeftMouseTip(trFirstVertex);
  54. this.setRightMouseTip(EAction.trCancel);
  55. this.segment = undefined;
  56. EAction.showSnapTools();
  57. break;
  58. case Leader.State.SettingNextVertex:
  59. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  60. var trNextVertex = qsTr("Next vertex");
  61. this.setCommandPrompt(trNextVertex);
  62. this.setLeftMouseTip(trNextVertex);
  63. this.setRightMouseTip(EAction.trDone);
  64. EAction.showSnapTools();
  65. break;
  66. }
  67. };
  68. Leader.prototype.showUiOptions = function(resume) {
  69. Draw.prototype.showUiOptions.call(this, resume);
  70. this.updateButtonStates();
  71. };
  72. Leader.prototype.escapeEvent = function() {
  73. var di = this.getDocumentInterface();
  74. switch (this.state) {
  75. case Leader.State.SettingFirstVertex:
  76. EAction.prototype.escapeEvent.call(this);
  77. break;
  78. case Leader.State.SettingNextVertex:
  79. if (!isNull(this.leaderEntity)) {
  80. // remove leader with one or zero vertices:
  81. if (this.leaderEntity.countVertices()<=1) {
  82. var op = new RDeleteObjectOperation(this.leaderEntity, false);
  83. this.applyOperation(op);
  84. }
  85. }
  86. this.setState(Leader.State.SettingFirstVertex);
  87. this.updateButtonStates();
  88. break;
  89. }
  90. };
  91. Leader.prototype.pickCoordinate = function(event, preview) {
  92. var point, op;
  93. var di = this.getDocumentInterface();
  94. var document = this.getDocument();
  95. switch (this.state) {
  96. // set first vertex, create leader entity:
  97. case Leader.State.SettingFirstVertex:
  98. if (!preview) {
  99. point = event.getModelPosition();
  100. var leader = new RLeaderEntity(document, new RLeaderData());
  101. leader.appendVertex(point);
  102. op = new RAddObjectOperation(leader, this.getToolTitle());
  103. this.applyOperation(op);
  104. di.setRelativeZero(point);
  105. if (!isNull(this.leaderEntity)) {
  106. this.setState(Leader.State.SettingNextVertex);
  107. }
  108. }
  109. break;
  110. // set next vertex:
  111. case Leader.State.SettingNextVertex:
  112. point = event.getModelPosition();
  113. // if we have at least one vertex:
  114. if (!isNull(this.leaderEntity) && this.leaderEntity.countVertices()>0) {
  115. var appendPoint = this.leaderEntity.getEndPoint();
  116. this.segment = new RLine(appendPoint, point);
  117. this.lastPoint = point;
  118. // append vertex:
  119. if (!preview) {
  120. this.leaderEntity.appendVertex(point);
  121. this.leaderEntity.setArrowHead(this.arrowHead);
  122. }
  123. }
  124. if (preview) {
  125. this.updatePreview();
  126. }
  127. else {
  128. op = this.getOperation(false);
  129. if (!isNull(op)) {
  130. this.applyOperation(op);
  131. di.setRelativeZero(point);
  132. //this.uncheckArcSegment();
  133. }
  134. }
  135. break;
  136. }
  137. if (!preview) {
  138. this.updateButtonStates();
  139. }
  140. };
  141. Leader.prototype.getOperation = function(preview) {
  142. if (isNull(this.leaderEntity)) {
  143. return undefined;
  144. }
  145. // for preview, only add current segment:
  146. if (preview) {
  147. if (isNull(this.segment)) {
  148. return undefined;
  149. }
  150. if (this.leaderEntity.countVertices()>1) {
  151. var entity = new RLineEntity(this.getDocument(), new RLineData(this.segment));
  152. entity.copyAttributesFrom(getPtr(this.leaderEntity));
  153. this.initEntity(entity, preview);
  154. return new RAddObjectOperation(entity, false);
  155. }
  156. else {
  157. // for the first segment, we preview a real leader for the sake
  158. // of displaying the arrow correctly:
  159. if (this.arrowBlockId!==RBlock.INVALID_ID) {
  160. this.leaderEntity.setDimLeaderBlockId(this.arrowBlockId);
  161. }
  162. var ld = this.leaderEntity.clone();
  163. ld.appendVertex(this.segment.getEndPoint());
  164. ld.setArrowHead(this.arrowHead);
  165. this.initEntity(ld, preview);
  166. return new RAddObjectOperation(ld, this.getUseCurrentAttributes(), false);
  167. }
  168. }
  169. else {
  170. // add segment (not undoable as only adding the whole leader can be undone):
  171. this.initEntity(this.leaderEntity, preview);
  172. return new RAddObjectOperation(this.leaderEntity, this.getToolTitle(), this.getUseCurrentAttributes(), false);
  173. }
  174. };
  175. /**
  176. * Can be overwritten to initialize the added entity.
  177. */
  178. Leader.prototype.initEntity = function(entity, preview) {
  179. };
  180. Leader.prototype.getUseCurrentAttributes = function() {
  181. return true;
  182. };
  183. /**
  184. * Updates the leader in storage and makes sure that this.leaderEntity
  185. * points to new new clone of the original entity.
  186. */
  187. Leader.prototype.applyOperation = function(op) {
  188. this.leaderEntity = undefined;
  189. var di = this.getDocumentInterface();
  190. var document = this.getDocument();
  191. var transaction = di.applyOperation(op);
  192. // find out ID of leader, that was added to the document:
  193. var ids = transaction.getAffectedObjects();
  194. for (var i=0; i<ids.length; ++i) {
  195. var id = ids[i];
  196. var entity = document.queryEntity(id);
  197. if (isLeaderEntity(entity)) {
  198. this.leaderEntity = entity;
  199. break;
  200. }
  201. }
  202. this.postOperation(transaction);
  203. };
  204. /**
  205. * Called when user clicks the 'Undo' button to remove the last added vertex.
  206. */
  207. Leader.prototype.slotUndo = function() {
  208. if (isNull(this.leaderEntity)) {
  209. return;
  210. }
  211. var di = this.getDocumentInterface();
  212. if (this.leaderEntity.countVertices() >= 2) {
  213. this.leaderEntity.removeLastVertex();
  214. di.clearPreview();
  215. var op = this.getOperation(false);
  216. if (!isNull(op)) {
  217. this.applyOperation(op);
  218. }
  219. }
  220. this.updateButtonStates();
  221. };
  222. Leader.prototype.slotArrowHeadChanged = function(v) {
  223. var di = this.getDocumentInterface();
  224. this.arrowHead = v;
  225. if (!isNull(this.leaderEntity)) {
  226. this.leaderEntity.setArrowHead(this.arrowHead);
  227. }
  228. di.clearPreview();
  229. var op = this.getOperation(false);
  230. if (!isNull(op)) {
  231. if (this.leaderEntity.countVertices() >= 2) {
  232. this.applyOperation(op);
  233. }
  234. }
  235. this.updatePreview();
  236. };
  237. /**
  238. * Updates the state (enabled / disabled) of the undo button
  239. * depending on the current progress.
  240. */
  241. Leader.prototype.updateButtonStates = function() {
  242. if (isNull(this.leaderEntity)) {
  243. return;
  244. }
  245. var w = objectFromPath("MainWindow::Options::Undo");
  246. if (!isNull(w)) {
  247. if (this.leaderEntity.countVertices() > 1) {
  248. w.enabled = true;
  249. } else {
  250. w.enabled = false;
  251. }
  252. }
  253. };