InfoArea.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 InfoArea
  22. * \ingroup ecma_information
  23. * \brief Measure area.
  24. */
  25. function InfoArea(guiAction) {
  26. Information.call(this, guiAction);
  27. this.polyline = undefined;
  28. this.setUiOptions("../Information.ui");
  29. }
  30. InfoArea.prototype = new Information();
  31. InfoArea.State = {
  32. SettingPoint : 0,
  33. Done : 1
  34. };
  35. InfoArea.prototype.beginEvent = function() {
  36. Information.prototype.beginEvent.call(this);
  37. this.reset();
  38. };
  39. InfoArea.prototype.escapeEvent = function() {
  40. switch (this.state) {
  41. case InfoArea.State.SettingPoint:
  42. this.slotCalculate();
  43. break;
  44. case InfoArea.State.Done:
  45. EAction.prototype.escapeEvent.call(this);
  46. break;
  47. }
  48. };
  49. InfoArea.prototype.reset = function() {
  50. this.polyline = new RPolyline();
  51. this.polyline.setClosed(true);
  52. this.setState(InfoArea.State.SettingPoint);
  53. };
  54. InfoArea.prototype.setState = function(state) {
  55. Information.prototype.setState.call(this, state);
  56. var di = this.getDocumentInterface();
  57. di.setClickMode(RAction.PickCoordinate);
  58. this.setCrosshairCursor();
  59. var trSelectPoint = qsTr("Set next polygon corner");
  60. var appWin = RMainWindowQt.getMainWindow();
  61. switch (this.state) {
  62. case InfoArea.State.SettingPoint:
  63. this.setCommandPrompt(trSelectPoint);
  64. this.setLeftMouseTip(trSelectPoint);
  65. this.setRightMouseTip(EAction.trDone);
  66. break;
  67. case InfoArea.State.Done:
  68. this.setCommandPrompt(trSelectPoint);
  69. this.setLeftMouseTip(trSelectPoint);
  70. this.setRightMouseTip(EAction.trCancel);
  71. break;
  72. }
  73. EAction.showSnapTools();
  74. };
  75. InfoArea.prototype.pickCoordinate = function(event, preview) {
  76. var di = this.getDocumentInterface();
  77. var doc = this.getDocument();
  78. var pos = event.getModelPosition();
  79. switch (this.state) {
  80. case InfoArea.State.Done:
  81. if (!preview) {
  82. this.reset();
  83. }
  84. this.updatePreview();
  85. if (preview) {
  86. break;
  87. }
  88. case InfoArea.State.SettingPoint:
  89. this.point1 = pos;
  90. this.polyline.appendVertex(pos);
  91. if (!preview) {
  92. di.setRelativeZero(pos);
  93. }
  94. if (preview) {
  95. this.updatePreview();
  96. this.polyline.removeLastVertex();
  97. }
  98. break;
  99. }
  100. };
  101. InfoArea.prototype.getOperation = function(preview) {
  102. if (isNull(this.polyline)) {
  103. return undefined;
  104. }
  105. var di = this.getDocumentInterface();
  106. var op = new RAddObjectsOperation();
  107. op.setText(this.getToolTitle());
  108. this.addShape(op, this.polyline, preview);
  109. var view = di.getLastKnownViewWithFocus();
  110. view = getRGraphicsView(view);
  111. var areaText = this.getAreaText();
  112. view.clearTextLabels();
  113. var c = this.polyline.getLastVertex();
  114. if (c.isValid()) {
  115. this.addTextLabel(op, view, c, areaText, preview);
  116. }
  117. return op;
  118. };
  119. InfoArea.prototype.getAreaText = function(rounded) {
  120. return this.formatAreaResult(this.getArea(), rounded);
  121. };
  122. InfoArea.prototype.getArea = function() {
  123. var pts = this.polyline.getVertices();
  124. var area = 0;
  125. var nPts = pts.length;
  126. var j = nPts - 1;
  127. var p1;
  128. var p2;
  129. for (var i = 0; i < nPts; j = i++) {
  130. p1 = pts[i];
  131. p2 = pts[j];
  132. area += p1.x * p2.y;
  133. area -= p1.y * p2.x;
  134. }
  135. area /= 2;
  136. return Math.abs(area);
  137. };
  138. InfoArea.prototype.getCircumference = function() {
  139. var pts = this.polyline.getVertices();
  140. var circ = 0;
  141. var nPts = pts.length;
  142. var j = nPts - 1;
  143. var p1;
  144. var p2;
  145. for (var i = 0; i < nPts; j = i++) {
  146. p1 = pts[i];
  147. p2 = pts[j];
  148. circ += p1.getDistanceTo(p2);
  149. }
  150. return circ;
  151. };
  152. InfoArea.prototype.slotCalculate = function() {
  153. var di = this.getDocumentInterface();
  154. var doc = di.getDocument();
  155. if (isNull(this.polyline) || this.polyline.countVertices()===0) {
  156. this.polyline = undefined;
  157. this.setState(InfoArea.State.Done);
  158. // di.clearPreview();
  159. // di.regenerateViews();
  160. // di.repaintViews();
  161. return;
  162. }
  163. var areaText = this.getAreaText();
  164. var info = qsTr("Polygon area:") + " " + areaText +
  165. ", " + qsTr("circumference:") + " " + this.getCircumference();
  166. if (this.addToDrawing) {
  167. //var di = this.getDocumentInterface();
  168. var op = this.getOperation(false);
  169. if (!isNull(op)) {
  170. di.applyOperation(op);
  171. }
  172. this.polyline = undefined;
  173. }
  174. this.setState(InfoArea.State.Done);
  175. di.clearPreview();
  176. this.updatePreview();
  177. di.regenerateViews();
  178. di.repaintViews();
  179. EAction.getMainWindow().handleUserInfo(info);
  180. };