DimOrdinate.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/Dimension/Dimension.js");
  20. /**
  21. * \class DimOrdinate
  22. * \brief Draw ordinate dimension.
  23. * \ingroup ecma_draw_dimension
  24. */
  25. function DimOrdinate(guiAction) {
  26. Dimension.call(this, guiAction);
  27. this.data = new RDimOrdinateData();
  28. this.data.setOrigin(new RVector(0,0));
  29. this.setUiOptions("../Dimension.ui");
  30. }
  31. DimOrdinate.prototype = new Dimension();
  32. DimOrdinate.State = {
  33. SettingDefiningPoint : 0,
  34. SettingLeaderEndPoint : 1
  35. };
  36. DimOrdinate.prototype.beginEvent = function() {
  37. Dimension.prototype.beginEvent.call(this);
  38. this.setState(DimOrdinate.State.SettingDefiningPoint);
  39. };
  40. DimOrdinate.prototype.setState = function(state) {
  41. var tr;
  42. Dimension.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 DimOrdinate.State.SettingDefiningPoint:
  48. this.data.setDefiningPoint(RVector.invalid);
  49. this.data.setLeaderEndPoint(RVector.invalid);
  50. tr = qsTr("Feature location");
  51. this.setCommandPrompt(tr);
  52. this.setLeftMouseTip(tr);
  53. this.setRightMouseTip(EAction.trCancel);
  54. break;
  55. case DimOrdinate.State.SettingLeaderEndPoint:
  56. this.data.setLeaderEndPoint(RVector.invalid);
  57. tr = qsTr("Endpoint of leader");
  58. this.setCommandPrompt(tr);
  59. this.setLeftMouseTip(tr);
  60. this.setRightMouseTip(EAction.trBack);
  61. break;
  62. }
  63. EAction.showSnapTools();
  64. };
  65. DimOrdinate.prototype.escapeEvent = function() {
  66. switch (this.state) {
  67. case DimOrdinate.State.SettingDefiningPoint:
  68. EAction.prototype.escapeEvent.call(this);
  69. break;
  70. case DimOrdinate.State.SettingLeaderEndPoint:
  71. this.setState(DimOrdinate.State.SettingDefiningPoint);
  72. break;
  73. }
  74. };
  75. DimOrdinate.prototype.pickCoordinate = function(event, preview) {
  76. var di = this.getDocumentInterface();
  77. switch (this.state) {
  78. case DimOrdinate.State.SettingDefiningPoint:
  79. this.data.setDefiningPoint(event.getModelPosition());
  80. if (preview) {
  81. this.updatePreview();
  82. }
  83. else {
  84. di.setRelativeZero(event.getModelPosition());
  85. this.setState(DimOrdinate.State.SettingLeaderEndPoint);
  86. }
  87. break;
  88. case DimOrdinate.State.SettingLeaderEndPoint:
  89. this.data.setLeaderEndPoint(event.getModelPosition());
  90. var dx = this.data.getLeaderEndPoint().x-this.data.getDefiningPoint().x;
  91. var dy = this.data.getLeaderEndPoint().y-this.data.getDefiningPoint().y;
  92. if (Math.abs(dx) > Math.abs(dy)) {
  93. this.data.setMeasuringYAxis();
  94. }
  95. else {
  96. this.data.setMeasuringXAxis();
  97. }
  98. if (preview) {
  99. this.updatePreview();
  100. }
  101. else {
  102. var op = this.getOperation(false);
  103. if (!isNull(op)) {
  104. di.applyOperation(op);
  105. di.setRelativeZero(event.getModelPosition());
  106. this.setState(DimOrdinate.State.SettingDefiningPoint);
  107. }
  108. }
  109. break;
  110. }
  111. };
  112. DimOrdinate.prototype.getOperation = function(preview) {
  113. if (!this.data.isValid()) {
  114. return undefined;
  115. }
  116. var doc = this.getDocument();
  117. var factor = this.getFactor();
  118. var scaled_data = this.data;
  119. scaled_data.setLinearFactor(factor);
  120. var entity = new RDimOrdinateEntity(doc, scaled_data);
  121. if (!isEntity(entity)) {
  122. return undefined;
  123. }
  124. return new RAddObjectOperation(entity, this.getToolTitle());
  125. };