InfoDistancePP.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/Information/Information.js");
  20. /**
  21. * \class InfoDistancePP
  22. * \ingroup ecma_information
  23. * \brief Measure distance from point to point.
  24. */
  25. function InfoDistancePP(guiAction) {
  26. Information.call(this, guiAction);
  27. this.point1 = undefined;
  28. this.point2 = undefined;
  29. this.setUiOptions(InfoDistancePP.includeBasePath + "/../Information.ui");
  30. }
  31. InfoDistancePP.prototype = new Information();
  32. InfoDistancePP.includeBasePath = includeBasePath;
  33. InfoDistancePP.State = {
  34. SettingFirstPoint : 0,
  35. SettingSecondPoint : 1
  36. };
  37. InfoDistancePP.prototype.beginEvent = function() {
  38. Information.prototype.beginEvent.call(this);
  39. this.setState(InfoDistancePP.State.SettingFirstPoint);
  40. };
  41. InfoDistancePP.prototype.setState = function(state) {
  42. Information.prototype.setState.call(this, state);
  43. var di = this.getDocumentInterface();
  44. di.setClickMode(RAction.PickCoordinate);
  45. this.setCrosshairCursor();
  46. var appWin = RMainWindowQt.getMainWindow();
  47. switch (this.state) {
  48. case InfoDistancePP.State.SettingFirstPoint:
  49. var trFirstPoint = qsTr("First point");
  50. this.setCommandPrompt(trFirstPoint);
  51. this.setLeftMouseTip(trFirstPoint);
  52. this.setRightMouseTip(EAction.trCancel);
  53. break;
  54. case InfoDistancePP.State.SettingSecondPoint:
  55. this.point2 = undefined;
  56. var trSecondPoint = qsTr("Second point");
  57. this.setCommandPrompt(trSecondPoint);
  58. this.setLeftMouseTip(trSecondPoint);
  59. this.setRightMouseTip(EAction.trDone);
  60. break;
  61. }
  62. EAction.showSnapTools();
  63. };
  64. InfoDistancePP.prototype.escapeEvent = function() {
  65. switch (this.state) {
  66. case InfoDistancePP.State.SettingFirstPoint:
  67. EAction.prototype.escapeEvent.call(this);
  68. break;
  69. case InfoDistancePP.State.SettingSecondPoint:
  70. this.setState(InfoDistancePP.State.SettingFirstPoint);
  71. break;
  72. }
  73. };
  74. InfoDistancePP.prototype.pickCoordinate = function(event, preview) {
  75. var appWin = EAction.getMainWindow();
  76. var di = this.getDocumentInterface();
  77. var op;
  78. switch (this.state) {
  79. case InfoDistancePP.State.SettingFirstPoint:
  80. if (!preview) {
  81. this.point1 = event.getModelPosition();
  82. this.point2 = undefined;
  83. di.setRelativeZero(this.point1);
  84. this.setState(InfoDistancePP.State.SettingSecondPoint);
  85. }
  86. break;
  87. case InfoDistancePP.State.SettingSecondPoint:
  88. this.point2 = event.getModelPosition();
  89. if (this.addToDrawing) {
  90. op = this.getOperation(preview);
  91. if (preview) {
  92. di.previewOperation(op);
  93. }
  94. else {
  95. di.applyOperation(op);
  96. di.setRelativeZero(this.point2);
  97. }
  98. }
  99. if (!preview) {
  100. var line = new RLine(this.point1, this.point2);
  101. var distance = line.getLength();
  102. var resultMessage = qsTr("Distance:") + " " + this.formatLinearResultCmd(distance);
  103. var dx = Math.abs(line.getEndPoint().x - line.getStartPoint().x);
  104. var dy = Math.abs(line.getEndPoint().y - line.getStartPoint().y);
  105. var angle = line.getAngle();
  106. resultMessage += ",\n" + qsTr("Delta X:") + " " + this.formatLinearResultCmd(dx);
  107. resultMessage += ",\n" + qsTr("Delta Y:") + " " + this.formatLinearResultCmd(dy);
  108. resultMessage += ",\n" + qsTr("Angle:") + " " + this.formatAngularResultCmd(angle);
  109. if (this.autoTerminate) {
  110. this.updateLineEdit(distance);
  111. this.setNoState(false);
  112. this.terminate();
  113. return;
  114. }
  115. else {
  116. this.setState(InfoDistancePP.State.SettingFirstPoint);
  117. }
  118. appWin.handleUserInfo(resultMessage);
  119. }
  120. break;
  121. }
  122. // keep showing preview after 2nd point has been set:
  123. if (!this.addToDrawing) {
  124. if (!isNull(this.point1) && !isNull(this.point2)) {
  125. op = this.getOperation(preview);
  126. if (preview) {
  127. di.previewOperation(op);
  128. }
  129. else {
  130. destr(op);
  131. }
  132. }
  133. }
  134. };
  135. InfoDistancePP.prototype.getOperation = function(preview) {
  136. var op = new RAddObjectsOperation();
  137. op.setText(this.getToolTitle());
  138. this.addInfoLine(op, this.point1, this.point2, preview);
  139. return op;
  140. };