LineAngle.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/Draw/Line/Line.js");
  20. /**
  21. * \class LineAngle
  22. * \brief Line from point and angle.
  23. * \ingroup ecma_draw_line
  24. */
  25. function LineAngle(guiAction) {
  26. Line.call(this, guiAction);
  27. this.pos = undefined;
  28. this.angle = undefined;
  29. this.len = undefined;
  30. this.referencePoint = undefined;
  31. this.fixedAngle = false;
  32. if (!isNull(guiAction)) {
  33. this.setUiOptions(["../Line.ui", "LineAngle.ui"]);
  34. }
  35. }
  36. LineAngle.State = {
  37. SettingPosition : 0
  38. };
  39. LineAngle.ReferencePoint = {
  40. Start : 0,
  41. Middle : 1,
  42. End : 2
  43. };
  44. LineAngle.prototype = new Line();
  45. LineAngle.prototype.beginEvent = function() {
  46. Line.prototype.beginEvent.call(this);
  47. this.setState(LineAngle.State.SettingPosition);
  48. };
  49. LineAngle.prototype.setState = function(state) {
  50. Line.prototype.setState.call(this, state);
  51. this.setCrosshairCursor();
  52. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  53. var appWin = RMainWindowQt.getMainWindow();
  54. this.setCommandPrompt(qsTr("Position"));
  55. this.setLeftMouseTip(qsTr("Specify position"));
  56. this.setRightMouseTip(EAction.trCancel);
  57. EAction.showSnapTools();
  58. this.typeChanged();
  59. };
  60. LineAngle.prototype.coordinateEvent = function(event) {
  61. var di = this.getDocumentInterface();
  62. this.pos = event.getModelPosition();
  63. di.setRelativeZero(this.pos);
  64. var op = this.getOperation(false);
  65. if (!isNull(op)) {
  66. di.applyOperation(op);
  67. }
  68. };
  69. LineAngle.prototype.coordinateEventPreview = function(event) {
  70. this.pos = event.getModelPosition();
  71. var op = this.getOperation(false);
  72. if (!isNull(op)) {
  73. this.getDocumentInterface().previewOperation(op);
  74. }
  75. };
  76. LineAngle.prototype.getOperation = function(preview) {
  77. if (isNull(this.pos) ||
  78. isNull(this.angle)) {
  79. return undefined;
  80. }
  81. var p1;
  82. var p2;
  83. // for rays / xlines, always use start point as reference
  84. // with length = 1.0:
  85. var referencePoint;
  86. var length;
  87. if (this.isRayOrXLine()) {
  88. referencePoint = LineAngle.ReferencePoint.Start;
  89. length = 1.0;
  90. }
  91. else {
  92. if (isNull(this.len) ||
  93. isNull(this.referencePoint)) {
  94. return undefined;
  95. }
  96. referencePoint = this.referencePoint;
  97. length = this.len;
  98. }
  99. switch (referencePoint) {
  100. case LineAngle.ReferencePoint.Start:
  101. p1 = this.pos;
  102. p2 = this.pos.operator_add(
  103. RVector.createPolar(+length, this.angle)
  104. );
  105. break;
  106. case LineAngle.ReferencePoint.Middle:
  107. p1 = this.pos.operator_add(
  108. RVector.createPolar(-length/2, this.angle)
  109. );
  110. p2 = this.pos.operator_add(
  111. RVector.createPolar(+length/2, this.angle)
  112. );
  113. break;
  114. case LineAngle.ReferencePoint.End:
  115. p1 = this.pos.operator_add(
  116. RVector.createPolar(-length, this.angle)
  117. );
  118. p2 = this.pos;
  119. break;
  120. default:
  121. return undefined;
  122. }
  123. var line = this.createLineEntity(this.getDocument(), p1, p2);
  124. if (isXLineEntity(line) && this.fixedAngle) {
  125. line.setFixedAngle(true);
  126. }
  127. return new RAddObjectOperation(line, this.getToolTitle());
  128. };
  129. LineAngle.prototype.slotAngleChanged = function(value) {
  130. this.angle = value;
  131. this.updatePreview(true);
  132. };
  133. LineAngle.prototype.slotLengthChanged = function(value) {
  134. this.len = value;
  135. this.updatePreview(true);
  136. };
  137. LineAngle.prototype.slotReferencePointChanged = function(value) {
  138. this.referencePoint = value;
  139. this.updatePreview(true);
  140. };
  141. LineAngle.prototype.typeChanged = function() {
  142. var optionsToolBar = EAction.getOptionsToolBar();
  143. var ws = ["LengthLabel", "Length", "ReferencePointLabel", "ReferencePoint"];
  144. for (var i=0; i<ws.length; i++) {
  145. var w = optionsToolBar.findChild(ws[i]);
  146. if (!isNull(w)) {
  147. w.enabled = !this.isRayOrXLine();
  148. }
  149. }
  150. };