Circle2P.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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("../Circle.js");
  20. /**
  21. * \class Circle2P
  22. * \brief Circle from two diagonally opposite points.
  23. * \ingroup ecma_draw_circle
  24. */
  25. function Circle2P(guiAction) {
  26. Circle.call(this, guiAction);
  27. this.point1 = undefined;
  28. this.point2 = undefined;
  29. }
  30. Circle2P.prototype = new Circle();
  31. Circle2P.State = {
  32. SettingPoint1 : 0,
  33. SettingPoint2 : 1
  34. };
  35. Circle2P.prototype.beginEvent = function() {
  36. Circle.prototype.beginEvent.call(this);
  37. this.setState(Circle2P.State.SettingPoint1);
  38. };
  39. Circle2P.prototype.setState = function(state) {
  40. Circle.prototype.setState.call(this, state);
  41. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  42. this.setCrosshairCursor();
  43. var appWin = RMainWindowQt.getMainWindow();
  44. switch (this.state) {
  45. case Circle2P.State.SettingPoint1:
  46. this.point1 = undefined;
  47. this.point2 = undefined;
  48. var trFirstPoint = qsTr("First point");
  49. this.setCommandPrompt(trFirstPoint);
  50. this.setLeftMouseTip(trFirstPoint);
  51. this.setRightMouseTip(EAction.trCancel);
  52. break;
  53. case Circle2P.State.SettingPoint2:
  54. var trSecondPoint = qsTr("Second point");
  55. this.setCommandPrompt(trSecondPoint);
  56. this.setLeftMouseTip(trSecondPoint);
  57. this.setRightMouseTip(EAction.trBack);
  58. break;
  59. }
  60. EAction.showSnapTools();
  61. };
  62. Circle2P.prototype.escapeEvent = function() {
  63. switch (this.state) {
  64. case Circle2P.State.SettingPoint1:
  65. EAction.prototype.escapeEvent.call(this);
  66. break;
  67. case Circle2P.State.SettingPoint2:
  68. this.setState(Circle2P.State.SettingPoint1);
  69. break;
  70. }
  71. };
  72. Circle2P.prototype.pickCoordinate = function(event, preview) {
  73. this.error = "";
  74. var di = this.getDocumentInterface();
  75. switch (this.state) {
  76. case Circle2P.State.SettingPoint1:
  77. this.point1 = event.getModelPosition();
  78. if (!preview) {
  79. di.setRelativeZero(this.point1);
  80. this.setState(Circle2P.State.SettingPoint2);
  81. }
  82. break;
  83. case Circle2P.State.SettingPoint2:
  84. this.point2 = event.getModelPosition();
  85. if (preview) {
  86. this.updatePreview();
  87. }
  88. else {
  89. var op = this.getOperation(false);
  90. if (!isNull(op)) {
  91. di.applyOperation(op);
  92. di.setRelativeZero(this.point2);
  93. this.setState(Circle2P.State.SettingPoint1);
  94. }
  95. }
  96. break;
  97. }
  98. if (!preview && this.error.length!==0) {
  99. EAction.handleUserWarning(this.error);
  100. }
  101. };
  102. Circle2P.prototype.getOperation = function(preview) {
  103. var shape = this.getCircle2P(preview);
  104. if (isNull(shape)) {
  105. return undefined;
  106. }
  107. var doc = this.getDocument();
  108. var entity = new RCircleEntity(doc, new RCircleData(shape));
  109. if (!isEntity(entity)) {
  110. return undefined;
  111. }
  112. return new RAddObjectOperation(entity, this.getToolTitle());
  113. };
  114. Circle2P.prototype.getCircle2P = function(preview) {
  115. if (isNull(this.point1) || isNull(this.point2)) {
  116. return undefined;
  117. }
  118. var distance = this.point1.getDistanceTo(this.point2);
  119. if (distance<RS.PointTolerance) {
  120. if (!preview) {
  121. this.error = qsTr("The two points are identical");
  122. }
  123. return undefined;
  124. }
  125. return RCircle.createFrom2Points(this.point1, this.point2);
  126. };