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