Line2P.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/Line/Line.js");
  20. include("scripts/Snap/RestrictAngleLength/RestrictAngleLength.js");
  21. /**
  22. * \class Line2P
  23. * \brief Line from two points or sequence of lines.
  24. * \ingroup ecma_draw_line
  25. */
  26. function Line2P(guiAction) {
  27. Line.call(this, guiAction);
  28. // list of points drawn:
  29. this.pointList = [];
  30. // index of point that was drawn last, used for tool undo / redo:
  31. this.pointListIndex = -1;
  32. // list of entities drawn:
  33. this.entityIdList = [];
  34. this.point1 = undefined;
  35. this.point2 = undefined;
  36. this.setUiOptions(["../Line.ui", "Line2P.ui"]);
  37. }
  38. Line2P.prototype = new Line();
  39. Line2P.State = {
  40. SettingFirstPoint : 0,
  41. SettingNextPoint : 1
  42. };
  43. Line2P.prototype.beginEvent = function() {
  44. Line.prototype.beginEvent.call(this);
  45. this.setState(Line2P.State.SettingFirstPoint);
  46. this.updateButtonStates();
  47. };
  48. Line2P.prototype.setState = function(state) {
  49. Line.prototype.setState.call(this, state);
  50. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  51. this.setCrosshairCursor();
  52. var appWin = RMainWindowQt.getMainWindow();
  53. switch (this.state) {
  54. case Line2P.State.SettingFirstPoint:
  55. var trFirstPoint = qsTr("First point");
  56. this.setCommandPrompt(trFirstPoint);
  57. this.setLeftMouseTip(trFirstPoint);
  58. this.setRightMouseTip(EAction.trCancel);
  59. this.pointList = [];
  60. this.pointListIndex = -1;
  61. this.entityIdList = [];
  62. break;
  63. case Line2P.State.SettingNextPoint:
  64. var trNextPoint = qsTr("Next point");
  65. this.setCommandPrompt(trNextPoint);
  66. this.setLeftMouseTip(trNextPoint);
  67. this.setRightMouseTip(EAction.trDone);
  68. break;
  69. }
  70. EAction.showSnapTools();
  71. };
  72. Line2P.prototype.showUiOptions = function(resume, restoreFromSettings) {
  73. Draw.prototype.showUiOptions.call(this, resume, restoreFromSettings);
  74. var optionsToolBar = EAction.getOptionsToolBar();
  75. var w = optionsToolBar.findChild("Restrict");
  76. var guiAction = RGuiAction.getByScriptFile("scripts/Snap/RestrictAngleLength/RestrictAngleLength.js");
  77. w.setDefaultAction(guiAction);
  78. this.updateButtonStates();
  79. };
  80. Line2P.prototype.escapeEvent = function() {
  81. switch (this.state) {
  82. case Line2P.State.SettingFirstPoint:
  83. EAction.prototype.escapeEvent.call(this);
  84. return;
  85. case Line2P.State.SettingNextPoint:
  86. this.setState(Line2P.State.SettingFirstPoint);
  87. this.updateButtonStates();
  88. break;
  89. }
  90. };
  91. Line2P.prototype.keyPressEvent = function(event) {
  92. var di = this.getDocumentInterface();
  93. if ((event.key() === Qt.Key_Enter.valueOf()) || (event.key() === Qt.Key_Return.valueOf())) {
  94. if (this.state === Line2P.State.SettingFirstPoint) {
  95. this.point1 = di.getLastPosition();
  96. di.setRelativeZero(this.point1);
  97. this.setState(Line2P.State.SettingNextPoint);
  98. di.clearPreview();
  99. di.previewOperation(this.getOperation(true));
  100. } else {
  101. EAction.prototype.keyPressEvent(event);
  102. }
  103. } else {
  104. EAction.prototype.keyPressEvent(event);
  105. }
  106. };
  107. Line2P.prototype.pickCoordinate = function(event, preview) {
  108. var di = this.getDocumentInterface();
  109. switch (this.state) {
  110. case Line2P.State.SettingFirstPoint:
  111. if (!preview) {
  112. this.point1 = event.getModelPosition();
  113. this.pointList.splice(0, 0, this.point1);
  114. this.pointListIndex = 0;
  115. di.setRelativeZero(this.point1);
  116. this.setState(Line2P.State.SettingNextPoint);
  117. }
  118. break;
  119. case Line2P.State.SettingNextPoint:
  120. this.point2 = event.getModelPosition();
  121. if (preview) {
  122. this.updatePreview();
  123. }
  124. else {
  125. var op = this.getOperation(preview);
  126. if (!isNull(op)) {
  127. if (!this.isRayOrXLine()) {
  128. this.pointListIndex++;
  129. }
  130. var doc = this.getDocument();
  131. var trans = di.applyOperation(op);
  132. var id = this.getLineEntityId(trans);
  133. if (!this.isRayOrXLine()) {
  134. this.pointList.splice(this.pointListIndex, 0, this.point2);
  135. this.entityIdList.splice(this.pointListIndex-1, 0, id);
  136. di.setRelativeZero(this.point2);
  137. this.point1 = this.point2;
  138. }
  139. // qDebug("this.pointList: ", this.pointList);
  140. // qDebug("this.entityIdList: ", this.entityIdList);
  141. // qDebug("this.pointListIndex: ", this.pointListIndex);
  142. }
  143. }
  144. break;
  145. }
  146. if (!preview) {
  147. this.updateButtonStates();
  148. }
  149. };
  150. Line2P.prototype.getOperation = function(preview) {
  151. if (!isVector(this.point1) || !isVector(this.point2)) {
  152. return undefined;
  153. }
  154. var e = this.createLineEntity(this.getDocument(), this.point1, this.point2);
  155. return new RAddObjectOperation(e, this.getToolTitle());
  156. };
  157. Line2P.prototype.slotClose = function() {
  158. if (this.isRayOrXLine()) {
  159. return;
  160. }
  161. if (this.pointList.length >= 3) {
  162. this.point2 = this.pointList[0];
  163. this.getDocumentInterface().applyOperation(this.getOperation(false));
  164. this.pointList.push(this.point2);
  165. var di = this.getDocumentInterface();
  166. di.setRelativeZero(this.point2);
  167. this.setState(Line2P.State.SettingFirstPoint);
  168. }
  169. this.updateButtonStates();
  170. };
  171. Line2P.prototype.slotUndo = function() {
  172. if (this.isRayOrXLine()) {
  173. return;
  174. }
  175. if (this.pointListIndex > 0) {
  176. var di = this.getDocumentInterface();
  177. var doc = this.getDocument();
  178. this.pointListIndex--;
  179. this.point1 = this.pointList[this.pointListIndex];
  180. di.setRelativeZero(this.point1);
  181. var id = this.entityIdList[this.pointListIndex];
  182. var entity = doc.queryEntity(id);
  183. di.applyOperation(new RDeleteObjectOperation(entity));
  184. di.clearPreview();
  185. di.previewOperation(this.getOperation(true));
  186. // qDebug("undone: this.pointListIndex: ", this.pointListIndex);
  187. // qDebug("undone: this.pointList: ", this.pointList);
  188. // qDebug("undone: this.entityIdList: ", this.entityIdList);
  189. }
  190. this.updateButtonStates();
  191. };
  192. Line2P.prototype.slotRedo = function() {
  193. if (this.isRayOrXLine()) {
  194. return;
  195. }
  196. if (this.pointListIndex < this.pointList.length-1) {
  197. var di = this.getDocumentInterface();
  198. var doc = this.getDocument();
  199. this.pointListIndex++;
  200. this.point1 = this.pointList[this.pointListIndex];
  201. di.setRelativeZero(this.point1);
  202. var e = this.createLineEntity(doc, this.pointList[this.pointListIndex-1], this.pointList[this.pointListIndex]);
  203. var trans = di.applyOperation(new RAddObjectOperation(e));
  204. var id = this.getLineEntityId(trans);
  205. this.entityIdList[this.pointListIndex-1] = id;
  206. di.clearPreview();
  207. di.previewOperation(this.getOperation(true));
  208. // qDebug("redone: this.pointListIndex: ", this.pointListIndex);
  209. // qDebug("redone: this.pointList: ", this.pointList);
  210. // qDebug("redone: this.entityIdList: ", this.entityIdList);
  211. }
  212. this.updateButtonStates();
  213. };
  214. Line2P.prototype.updateButtonStates = function() {
  215. var optionsToolBar = EAction.getOptionsToolBar();
  216. var w = optionsToolBar.findChild("Undo");
  217. if (!isNull(w)) {
  218. if (this.pointListIndex > 0 && !this.isRayOrXLine()) {
  219. w.enabled = true;
  220. } else {
  221. w.enabled = false;
  222. }
  223. }
  224. w = optionsToolBar.findChild("Redo");
  225. if (!isNull(w)) {
  226. //qDebug("this.pointList: ", this.pointList);
  227. //qDebug("this.pointListIndex: ", this.pointListIndex);
  228. //qDebug("this.entityIdList.length: ", this.entityIdList.length);
  229. if (this.pointListIndex >= 0 &&
  230. this.pointListIndex < this.entityIdList.length &&
  231. !this.isRayOrXLine()) {
  232. w.enabled = true;
  233. } else {
  234. w.enabled = false;
  235. }
  236. }
  237. w = optionsToolBar.findChild("Close");
  238. if (!isNull(w)) {
  239. if (this.pointList.length > 2 && !this.isRayOrXLine()) {
  240. w.enabled = true;
  241. } else {
  242. w.enabled = false;
  243. }
  244. }
  245. };
  246. Line2P.prototype.getLineEntityId = function(trans) {
  247. var objIds = trans.getAffectedObjects();
  248. for (var i=0; i<objIds.length; i++) {
  249. var objId = objIds[i];
  250. var obj = this.getDocument().queryObjectDirect(objId);
  251. if (isLineEntity(obj)) {
  252. return obj.getId();
  253. }
  254. }
  255. return RObject.INVALID_ID;
  256. };
  257. /**
  258. * Allows commands to be entered in command line
  259. *
  260. * "Using the 'startsWith' function allows the user to enter only as many characters
  261. * as needed to distinguish between commands
  262. * In this case only the first character is needed. (But entering 'c', 'cl', 'clo', 'clos'
  263. * or 'close' would all invoke the close command. Similarly with undo and redo)"
  264. *
  265. */
  266. Line2P.prototype.commandEvent = function(event) {
  267. var str;
  268. var cmd = event.getCommand();
  269. cmd = cmd.toLowerCase();
  270. str = "close";
  271. if (str.startsWith(cmd)) {
  272. this.slotClose();
  273. event.accept();
  274. return;
  275. }
  276. str = "undo";
  277. if (str.startsWith(cmd)) {
  278. this.slotUndo();
  279. event.accept();
  280. return;
  281. }
  282. str = "redo";
  283. if (str.startsWith(cmd)) {
  284. this.slotRedo();
  285. event.accept();
  286. return;
  287. }
  288. };
  289. Line2P.prototype.typeChanged = function() {
  290. if (this.pointList.length!==0) {
  291. this.pointList = [this.pointList[this.pointList.length-1]];
  292. }
  293. this.pointListIndex = 0;
  294. this.entityIdList = [];
  295. this.updateButtonStates();
  296. };