SnapDistanceManual.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2011-2013 by Robert S. 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/Snap/Snap.js");
  20. /**
  21. * \class SnapDistanceManual
  22. * \ingroup ecma_snap
  23. * \brief Snap to point with distance to specified point.
  24. */
  25. function SnapDistanceManual(guiAction) {
  26. Snap.call(this, guiAction);
  27. this.State = {
  28. PickingPoint1 : 0,
  29. PickingPoint2 : 1
  30. };
  31. this.state = this.State.PickingPoint1;
  32. this.setCommandPrompt(qsTr("Specify first point to measure distance from"));
  33. this.point1 = undefined;
  34. this.point2 = undefined;
  35. this.setUiOptions("SnapDistanceManual.ui");
  36. }
  37. SnapDistanceManual.prototype = new Snap();
  38. SnapDistanceManual.prototype.beginEvent = function() {
  39. Snap.prototype.beginEvent.call(this);
  40. this.di = this.getDocumentInterface();
  41. this.di.setSnap(new RSnapAuto());
  42. this.di.setClickMode(RAction.PickCoordinate);
  43. this.base = this.getOverrideBase();
  44. if (isNull(this.base)) {
  45. qWarning("No base action to override");
  46. this.terminate();
  47. return;
  48. }
  49. if (this.base.getClickMode() !== RAction.PickCoordinate) {
  50. qWarning("No coordinate requested at this point");
  51. this.terminate();
  52. }
  53. };
  54. SnapDistanceManual.prototype.coordinateEvent = function(event) {
  55. if (this.state===this.State.PickingPoint1) {
  56. this.point1 = event.getModelPosition();
  57. this.state = this.State.PickingPoint2;
  58. this.di.setRelativeZero(this.point1);
  59. this.setCommandPrompt(qsTr("Second point"));
  60. }
  61. else {
  62. this.point2 = event.getModelPosition();
  63. this.generateEvent(event, false);
  64. this.terminate();
  65. }
  66. };
  67. SnapDistanceManual.prototype.coordinateEventPreview = function(event) {
  68. if (this.state===this.State.PickingPoint2) {
  69. this.point2 = event.getModelPosition();
  70. this.generateEvent(event, true);
  71. }
  72. };
  73. SnapDistanceManual.prototype.generateEvent = function(event, preview) {
  74. var dist2p = this.point1.getDistanceTo(this.point2);
  75. var ang = this.point1.getAngleTo(this.point2);
  76. var dist = objectFromPath("MainWindow::Options::Distance");
  77. var value = RMath.eval(dist.text);
  78. var tc = objectFromPath("MainWindow::Options::TypeCombo");
  79. if (tc.currentIndex === 1) { // Percent
  80. dist = dist2p * value / 100;
  81. } else if (tc.currentIndex === 2) { // Fraction
  82. dist = dist2p * value;
  83. } else { // Distance
  84. dist = value;
  85. }
  86. var v = new RVector();
  87. v.setPolar(dist, ang);
  88. var pos = this.point1.operator_add(v);
  89. pos = this.restrict(pos);
  90. if (!isNull(this.base)) {
  91. if (!preview) {
  92. this.base.coordinateEvent(
  93. new RCoordinateEvent(
  94. pos,
  95. event.getGraphicsScene(),
  96. event.getGraphicsView()
  97. )
  98. );
  99. } else {
  100. this.base.coordinateEventPreview(
  101. new RCoordinateEvent(
  102. pos,
  103. event.getGraphicsScene(),
  104. event.getGraphicsView()
  105. )
  106. );
  107. var line = new RLine(this.point1, this.point2);
  108. this.di.addAuxShapeToPreview(line);
  109. this.di.setCursorPosition(pos);
  110. }
  111. }
  112. };