DrawBasedOnThreeCircles.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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("Draw.js");
  20. /**
  21. * \class DrawBasedOnThreeCircles
  22. * \brief Base class for drawing tools that draw something based on
  23. * three given circles or arcs.
  24. * \ingroup ecma_misc_draw
  25. */
  26. function DrawBasedOnThreeCircles(guiAction) {
  27. EAction.call(this, guiAction);
  28. this.entity1 = undefined;
  29. this.shape1 = undefined;
  30. this.pos1 = undefined;
  31. this.entity2 = undefined;
  32. this.shape2 = undefined;
  33. this.pos2 = undefined;
  34. this.entity3 = undefined;
  35. this.shape3 = undefined;
  36. this.pos3 = undefined;
  37. }
  38. DrawBasedOnThreeCircles.prototype = new EAction();
  39. DrawBasedOnThreeCircles.State = {
  40. ChoosingShape1 : 0,
  41. ChoosingShape2 : 1,
  42. ChoosingShape3 : 2
  43. };
  44. DrawBasedOnThreeCircles.prototype.beginEvent = function() {
  45. EAction.prototype.beginEvent.call(this);
  46. this.setState(DrawBasedOnThreeCircles.State.ChoosingShape1);
  47. };
  48. DrawBasedOnThreeCircles.prototype.setState = function(state) {
  49. EAction.prototype.setState.call(this, state);
  50. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  51. this.setCrosshairCursor();
  52. var appWin = RMainWindowQt.getMainWindow();
  53. switch (this.state) {
  54. case DrawBasedOnThreeCircles.State.ChoosingShape1:
  55. this.entity1 = undefined;
  56. this.shape1 = undefined;
  57. this.entity2 = undefined;
  58. this.shape2 = undefined;
  59. this.entity3 = undefined;
  60. this.shape3 = undefined;
  61. this.center = undefined;
  62. var trFirstEntity = qsTr("First arc or circle");
  63. this.setCommandPrompt(trFirstEntity);
  64. this.setLeftMouseTip(trFirstEntity);
  65. this.setRightMouseTip(EAction.trCancel);
  66. break;
  67. case DrawBasedOnThreeCircles.State.ChoosingShape2:
  68. this.entity2 = undefined;
  69. this.shape2 = undefined;
  70. this.entity3 = undefined;
  71. this.shape3 = undefined;
  72. this.center = undefined;
  73. var trSecondEntity = qsTr("Second arc or circle");
  74. this.setCommandPrompt(trSecondEntity);
  75. this.setLeftMouseTip(trSecondEntity);
  76. this.setRightMouseTip(EAction.trBack);
  77. break;
  78. case DrawBasedOnThreeCircles.State.ChoosingShape3:
  79. this.entity3 = undefined;
  80. this.shape3 = undefined;
  81. this.center = undefined;
  82. var trThirdEntity = qsTr("Third arc or circle");
  83. this.setCommandPrompt(trThirdEntity);
  84. this.setLeftMouseTip(trThirdEntity);
  85. this.setRightMouseTip(EAction.trBack);
  86. break;
  87. }
  88. };
  89. DrawBasedOnThreeCircles.prototype.escapeEvent = function() {
  90. switch (this.state) {
  91. case DrawBasedOnThreeCircles.State.ChoosingShape1:
  92. EAction.prototype.escapeEvent.call(this);
  93. break;
  94. case DrawBasedOnThreeCircles.State.ChoosingShape2:
  95. this.setState(DrawBasedOnThreeCircles.State.ChoosingShape1);
  96. break;
  97. case DrawBasedOnThreeCircles.State.ChoosingShape3:
  98. this.setState(DrawBasedOnThreeCircles.State.ChoosingShape2);
  99. break;
  100. }
  101. };
  102. DrawBasedOnThreeCircles.prototype.pickEntity = function(event, preview) {
  103. this.error = "";
  104. var di = this.getDocumentInterface();
  105. var doc = this.getDocument();
  106. var entityId = this.getEntityId(event, preview);
  107. var entity = doc.queryEntity(entityId);
  108. var pos = event.getModelPosition();
  109. if (isNull(entity)) {
  110. return;
  111. }
  112. var shape = entity.getClosestSimpleShape(pos);
  113. if (!isArcShape(shape) &&
  114. !isCircleShape(shape)) {
  115. if (!preview) {
  116. EAction.warnNotArcCircle();
  117. }
  118. return;
  119. }
  120. switch (this.state) {
  121. case DrawBasedOnThreeCircles.State.ChoosingShape1:
  122. this.entity1 = entity;
  123. this.shape1 = shape;
  124. this.pos1 = pos;
  125. if (preview) {
  126. this.updatePreview();
  127. }
  128. else {
  129. this.setState(DrawBasedOnThreeCircles.State.ChoosingShape2);
  130. }
  131. break;
  132. case DrawBasedOnThreeCircles.State.ChoosingShape2:
  133. this.entity2 = entity;
  134. this.shape2 = shape;
  135. this.pos2 = pos;
  136. if (preview) {
  137. this.updatePreview();
  138. }
  139. else {
  140. this.setState(DrawBasedOnThreeCircles.State.ChoosingShape3);
  141. }
  142. break;
  143. case DrawBasedOnThreeCircles.State.ChoosingShape3:
  144. this.entity3 = entity;
  145. this.shape3 = shape;
  146. this.pos3 = pos;
  147. if (preview) {
  148. this.updatePreview();
  149. }
  150. else {
  151. var op = this.getOperation(false);
  152. if (!isNull(op)) {
  153. di.applyOperation(op);
  154. this.setState(DrawBasedOnThreeCircles.State.ChoosingShape1);
  155. }
  156. }
  157. break;
  158. }
  159. if (!preview && this.error.length!==0) {
  160. EAction.handleUserWarning(this.error);
  161. }
  162. };
  163. DrawBasedOnThreeCircles.prototype.getOperation = function(preview) {
  164. var shapes = this.getShapes(preview);
  165. if (isNull(shapes)) {
  166. return undefined;
  167. }
  168. var doc = this.getDocument();
  169. var op = new RAddObjectsOperation();
  170. op.setText(this.getToolTitle());
  171. for (var i=0; i<shapes.length; i++) {
  172. var entity = shapeToEntity(doc, shapes[i]);
  173. if (!isEntity(entity)) {
  174. continue;
  175. }
  176. op.addObject(entity);
  177. }
  178. return op;
  179. };
  180. /**
  181. * Implemented by child classes.
  182. */
  183. DrawBasedOnThreeCircles.prototype.getShapes = function(preview) {
  184. return undefined;
  185. };
  186. DrawBasedOnThreeCircles.prototype.getHighlightedEntities = function() {
  187. var ret = [];
  188. if (isEntity(this.entity1)) {
  189. ret.push(this.entity1.getId());
  190. }
  191. if (isEntity(this.entity2)) {
  192. ret.push(this.entity2.getId());
  193. }
  194. if (isEntity(this.entity3)) {
  195. ret.push(this.entity3.getId());
  196. }
  197. return ret;
  198. };