ShapePolygonPP.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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("../Shape.js");
  20. /**
  21. * \class ShapePolygonPP
  22. * \brief Polygon from two corner points and number of edges.
  23. * \ingroup ecma_draw_shape
  24. */
  25. function ShapePolygonPP(guiAction) {
  26. Shape.call(this, guiAction);
  27. this.corner1 = undefined;
  28. this.corner2 = undefined;
  29. this.setUiOptions(["../Shape.ui", "ShapePolygonPP.ui"]);
  30. }
  31. ShapePolygonPP.prototype = new Shape();
  32. ShapePolygonPP.State = {
  33. SettingCorner1 : 0,
  34. SettingCorner2 : 1
  35. };
  36. ShapePolygonPP.prototype.beginEvent = function() {
  37. Shape.prototype.beginEvent.call(this);
  38. this.setState(ShapePolygonPP.State.SettingCorner1);
  39. };
  40. ShapePolygonPP.prototype.setState = function(state) {
  41. Shape.prototype.setState.call(this, state);
  42. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  43. this.setCrosshairCursor();
  44. var appWin = RMainWindowQt.getMainWindow();
  45. switch (this.state) {
  46. case ShapePolygonPP.State.SettingCorner1:
  47. this.corner1 = undefined;
  48. this.corner2 = undefined;
  49. var trCorner1 = qsTr("First corner");
  50. this.setCommandPrompt(trCorner1);
  51. this.setLeftMouseTip(trCorner1);
  52. this.setRightMouseTip(EAction.trCancel);
  53. break;
  54. case ShapePolygonPP.State.SettingCorner2:
  55. var trCorner2 = qsTr("Corner point");
  56. this.setCommandPrompt(trCorner2);
  57. this.setLeftMouseTip(trCorner2);
  58. this.setRightMouseTip(EAction.trDone);
  59. break;
  60. }
  61. EAction.showSnapTools();
  62. };
  63. ShapePolygonPP.prototype.escapeEvent = function() {
  64. switch (this.state) {
  65. case ShapePolygonPP.State.SettingCorner1:
  66. EAction.prototype.escapeEvent.call(this);
  67. break;
  68. case ShapePolygonPP.State.SettingCorner2:
  69. this.setState(ShapePolygonPP.State.SettingCorner1);
  70. break;
  71. }
  72. };
  73. ShapePolygonPP.prototype.pickCoordinate = function(event, preview) {
  74. var di = this.getDocumentInterface();
  75. switch (this.state) {
  76. case ShapePolygonPP.State.SettingCorner1:
  77. this.corner1 = event.getModelPosition();
  78. if (!preview) {
  79. di.setRelativeZero(this.corner1);
  80. //di.repaintViews();
  81. this.setState(ShapePolygonPP.State.SettingCorner2);
  82. }
  83. break;
  84. case ShapePolygonPP.State.SettingCorner2:
  85. this.corner2 = event.getModelPosition();
  86. if (preview) {
  87. this.updatePreview();
  88. }
  89. else {
  90. var op = this.getOperation(false);
  91. if (!isNull(op)) {
  92. di.applyOperation(op);
  93. di.setRelativeZero(this.corner2);
  94. this.setState(ShapePolygonPP.State.SettingCorner1);
  95. }
  96. }
  97. break;
  98. }
  99. };
  100. ShapePolygonPP.prototype.getOperation = function(preview) {
  101. if (isNull(this.corner1) || isNull(this.corner2)) {
  102. return undefined;
  103. }
  104. var c = this.corner1;
  105. var corners = [];
  106. var len = this.corner1.getDistanceTo(this.corner2);
  107. var ang1 = this.corner1.getAngleTo(this.corner2);
  108. var ang = ang1;
  109. for (var n=1; n<=this.numberOfCorners; ++n) {
  110. var edge = new RVector();
  111. edge.setPolar(len, ang);
  112. corners.push(c.copy());
  113. c = c.operator_add(edge);
  114. // more precise than incrementing the angle:
  115. ang = ang1 + (2*Math.PI)/this.numberOfCorners * n;
  116. }
  117. var op = new RAddObjectsOperation();
  118. op.setText(this.getToolTitle());
  119. var shapes = this.getShapes(corners);
  120. for (var i=0; i<shapes.length; ++i) {
  121. var e = shapeToEntity(this.getDocument(), shapes[i]);
  122. if (isNull(e)) {
  123. continue;
  124. }
  125. op.addObject(e);
  126. }
  127. this.complementOperation(op, shapes);
  128. return op;
  129. };
  130. ShapePolygonPP.prototype.slotCornersChanged = function(value) {
  131. this.numberOfCorners = value;
  132. this.updatePreview(true);
  133. };