CircleCP.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 CircleCP
  22. * \brief Circle from center point and point on circle line.
  23. * \ingroup ecma_draw_circle
  24. */
  25. function CircleCP(guiAction) {
  26. Circle.call(this, guiAction);
  27. }
  28. CircleCP.prototype = new Circle();
  29. CircleCP.State = {
  30. SettingCenter : 0,
  31. SettingRadius : 1
  32. };
  33. CircleCP.prototype.beginEvent = function() {
  34. Circle.prototype.beginEvent.call(this);
  35. this.setState(CircleCP.State.SettingCenter);
  36. };
  37. CircleCP.prototype.setState = function(state) {
  38. Circle.prototype.setState.call(this, state);
  39. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  40. this.setCrosshairCursor();
  41. var appWin = RMainWindowQt.getMainWindow();
  42. switch (this.state) {
  43. case CircleCP.State.SettingCenter:
  44. this.center = undefined;
  45. this.radius = undefined;
  46. var trCenter = qsTr("Center");
  47. this.setCommandPrompt(trCenter);
  48. this.setLeftMouseTip(trCenter);
  49. this.setRightMouseTip(EAction.trCancel);
  50. break;
  51. case CircleCP.State.SettingRadius:
  52. var trPointOnCircle = qsTr("Point on circle or radius");
  53. this.setCommandPrompt(trPointOnCircle);
  54. this.setLeftMouseTip(trPointOnCircle);
  55. this.setRightMouseTip(EAction.trBack);
  56. break;
  57. }
  58. EAction.showSnapTools();
  59. };
  60. CircleCP.prototype.escapeEvent = function() {
  61. switch (this.state) {
  62. case CircleCP.State.SettingCenter:
  63. EAction.prototype.escapeEvent.call(this);
  64. break;
  65. case CircleCP.State.SettingRadius:
  66. this.setState(CircleCP.State.SettingCenter);
  67. break;
  68. }
  69. };
  70. CircleCP.prototype.pickCoordinate = function(event, preview) {
  71. var di = this.getDocumentInterface();
  72. switch (this.state) {
  73. case CircleCP.State.SettingCenter:
  74. if (!preview) {
  75. this.center = event.getModelPosition();
  76. di.setRelativeZero(this.center);
  77. this.setState(CircleCP.State.SettingRadius);
  78. }
  79. break;
  80. case CircleCP.State.SettingRadius:
  81. this.radius = this.center.getDistanceTo(event.getModelPosition());
  82. if (preview) {
  83. this.updatePreview();
  84. }
  85. else {
  86. di.setRelativeZero(event.getModelPosition());
  87. var op = this.getOperation(false);
  88. if (!isNull(op)) {
  89. di.applyOperation(op);
  90. this.setState(CircleCP.State.SettingCenter);
  91. }
  92. }
  93. break;
  94. }
  95. };
  96. CircleCP.prototype.getOperation = function(preview) {
  97. if (isNull(this.center) || !isNumber(this.radius)) {
  98. return undefined;
  99. }
  100. if (this.state != CircleCP.State.SettingRadius) {
  101. return undefined;
  102. }
  103. var circle = new RCircleEntity(
  104. this.getDocument(),
  105. new RCircleData(
  106. this.center,
  107. this.radius
  108. )
  109. );
  110. return new RAddObjectOperation(circle, this.getToolTitle());
  111. };
  112. CircleCP.prototype.applyCommand = function(event, preview) {
  113. var di = this.getDocumentInterface();
  114. var cmd = event.getCommand();
  115. var pos = RMath.parseCoordinate(cmd);
  116. if (pos.isValid()) {
  117. return;
  118. }
  119. var value = RMath.eval(cmd);
  120. if (pos.isValid()) {
  121. return;
  122. }
  123. switch (this.state) {
  124. case CircleCP.State.SettingRadius:
  125. event.accept();
  126. this.radius = value;
  127. if (preview) {
  128. this.updatePreview(true);
  129. }
  130. else {
  131. var op = this.getOperation(false);
  132. if (!isNull(op)) {
  133. di.applyOperation(op);
  134. this.setState(CircleCP.State.SettingCenter);
  135. }
  136. }
  137. break;
  138. }
  139. };