OffsetThrough.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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("../Offset/Offset.js");
  20. /**
  21. * \class OffsetThrough
  22. * \brief Offset through given position.
  23. * \ingroup ecma_modify
  24. */
  25. function OffsetThrough(guiAction) {
  26. Offset.call(this, guiAction);
  27. this.entity = undefined;
  28. this.shape = undefined;
  29. this.distance = undefined;
  30. if (!isNull(guiAction)) {
  31. this.setUiOptions("OffsetThrough.ui");
  32. }
  33. }
  34. OffsetThrough.prototype = new Offset();
  35. OffsetThrough.State = {
  36. ChoosingEntity : 0,
  37. SettingPos : 1
  38. };
  39. OffsetThrough.prototype.beginEvent = function() {
  40. EAction.prototype.beginEvent.call(this);
  41. this.setState(OffsetThrough.State.ChoosingEntity);
  42. };
  43. OffsetThrough.prototype.initState = function() {
  44. this.setCrosshairCursor();
  45. var tr;
  46. var appWin = RMainWindowQt.getMainWindow();
  47. switch (this.state) {
  48. case OffsetThrough.State.ChoosingEntity:
  49. this.entity = undefined;
  50. this.shape = undefined;
  51. this.distance = undefined;
  52. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  53. tr = this.getLeftMouseTip();
  54. this.setLeftMouseTip(tr);
  55. this.setCommandPrompt(tr);
  56. this.setRightMouseTip(EAction.trCancel);
  57. break;
  58. case OffsetThrough.State.SettingPos:
  59. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  60. tr = qsTr("Specify point");
  61. this.setLeftMouseTip(tr);
  62. this.setCommandPrompt(tr);
  63. this.setRightMouseTip(EAction.trBack);
  64. EAction.showSnapTools();
  65. break;
  66. }
  67. };
  68. OffsetThrough.prototype.escapeEvent = function() {
  69. switch (this.state) {
  70. case OffsetThrough.State.ChoosingEntity:
  71. EAction.prototype.escapeEvent.call(this);
  72. break;
  73. case OffsetThrough.State.SettingPos:
  74. this.setState(OffsetThrough.State.ChoosingEntity);
  75. break;
  76. }
  77. };
  78. OffsetThrough.prototype.pickEntity = function(event, preview) {
  79. var di = this.getDocumentInterface();
  80. var doc = this.getDocument();
  81. var entityId = this.getEntityId(event, preview);
  82. var entity = doc.queryEntity(entityId);
  83. var pos = event.getModelPosition();
  84. if (isNull(entity)) {
  85. this.entity = undefined;
  86. return;
  87. }
  88. switch (this.state) {
  89. case OffsetThrough.State.ChoosingEntity:
  90. var shape;
  91. if (this.segmentMode) {
  92. // parallel to polyline segment:
  93. shape = entity.getClosestSimpleShape(pos);
  94. }
  95. else {
  96. shape = entity.getClosestShape(pos);
  97. }
  98. if (this.isShapeSupported(shape)) {
  99. this.entity = entity;
  100. this.shape = shape;
  101. if (preview) {
  102. this.updatePreview();
  103. }
  104. else {
  105. this.setState(OffsetThrough.State.SettingPos);
  106. }
  107. }
  108. else {
  109. if (!preview) {
  110. this.warnUnsupportedShape();
  111. break;
  112. }
  113. }
  114. break;
  115. }
  116. };
  117. OffsetThrough.prototype.pickCoordinate = function(event, preview) {
  118. var di = this.getDocumentInterface();
  119. switch (this.state) {
  120. case OffsetThrough.State.SettingPos:
  121. this.pos = event.getModelPosition();
  122. this.distance = this.getDistance(this.pos, this.shape);
  123. if (preview) {
  124. this.updatePreview();
  125. }
  126. else {
  127. var op = this.getOperation(false);
  128. if (!isNull(op)) {
  129. di.applyOperation(op);
  130. }
  131. }
  132. break;
  133. }
  134. };
  135. OffsetThrough.prototype.getDistance = function(pos, shape) {
  136. return shape.getDistanceTo(pos, false);
  137. };