Mirror.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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("../Transform.js");
  20. /**
  21. * \class Mirror
  22. * \brief Mirrors selected entities.
  23. * \ingroup ecma_modify
  24. */
  25. function Mirror(guiAction) {
  26. Transform.call(this, guiAction);
  27. this.axisPoint1 = undefined;
  28. this.axisPoint2 = undefined;
  29. this.useDialog = RSettings.getBoolValue("Mirror/UseDialog", true);
  30. if (!this.useDialog) {
  31. this.setUiOptions("Mirror.ui");
  32. }
  33. }
  34. Mirror.prototype = new Transform();
  35. Mirror.includeBasePath = includeBasePath;
  36. Mirror.getPreferencesCategory = function() {
  37. return [qsTr("Modify"), qsTr("Mirror")];
  38. };
  39. Mirror.State = {
  40. SettingAxisPoint1 : 0,
  41. SettingAxisPoint2 : 1
  42. };
  43. Mirror.prototype.beginEvent = function() {
  44. Transform.prototype.beginEvent.call(this);
  45. if (!this.verifySelection()) {
  46. return;
  47. }
  48. this.setState(Mirror.State.SettingAxisPoint1);
  49. };
  50. Mirror.prototype.setState = function(state) {
  51. Transform.prototype.setState.call(this, state);
  52. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  53. this.setCrosshairCursor();
  54. var appWin = RMainWindowQt.getMainWindow();
  55. switch (this.state) {
  56. case Mirror.State.SettingAxisPoint1:
  57. var trAxisPoint1 = qsTr("First point of symmetry axis");
  58. this.setCommandPrompt(trAxisPoint1);
  59. this.setLeftMouseTip(trAxisPoint1);
  60. this.setRightMouseTip(EAction.trCancel);
  61. break;
  62. case Mirror.State.SettingAxisPoint2:
  63. var trAxisPoint2 = qsTr("Second point of symmetry axis");
  64. this.setCommandPrompt(trAxisPoint2);
  65. this.setLeftMouseTip(trAxisPoint2);
  66. this.setRightMouseTip(EAction.trBack);
  67. break;
  68. }
  69. EAction.showSnapTools();
  70. };
  71. Mirror.prototype.escapeEvent = function() {
  72. switch (this.state) {
  73. case Mirror.State.SettingAxisPoint1:
  74. Transform.prototype.escapeEvent.call(this);
  75. break;
  76. case Mirror.State.SettingAxisPoint2:
  77. this.setState(Mirror.State.SettingAxisPoint1);
  78. break;
  79. }
  80. };
  81. Mirror.prototype.pickCoordinate = function(event, preview) {
  82. var di = this.getDocumentInterface();
  83. switch (this.state) {
  84. case Mirror.State.SettingAxisPoint1:
  85. if (!preview) {
  86. this.axisPoint1 = event.getModelPosition();
  87. di.setRelativeZero(this.axisPoint1);
  88. this.setState(Mirror.State.SettingAxisPoint2);
  89. }
  90. break;
  91. case Mirror.State.SettingAxisPoint2:
  92. this.axisPoint2 = event.getModelPosition();
  93. if (preview) {
  94. this.updatePreview();
  95. }
  96. else {
  97. if (this.useDialog) {
  98. this.setState(-1);
  99. if (!this.showDialog()) {
  100. // dialog canceled:
  101. this.terminate();
  102. return;
  103. }
  104. }
  105. var op = this.getOperation(false);
  106. if (!isNull(op)) {
  107. di.applyOperation(op);
  108. di.setRelativeZero(this.axisPoint2);
  109. this.terminate();
  110. }
  111. }
  112. break;
  113. }
  114. };
  115. /**
  116. * Shows the translation dialog.
  117. *
  118. * \return True if the dialog was accepted, false if it was canceled.
  119. */
  120. Mirror.prototype.showDialog = function() {
  121. var dialog = WidgetFactory.createDialog(Mirror.includeBasePath, "MirrorDialog.ui");
  122. WidgetFactory.restoreState(dialog);
  123. if (!dialog.exec()) {
  124. destr(dialog);
  125. EAction.activateMainWindow();
  126. return false;
  127. }
  128. var widgets = getWidgets(dialog);
  129. if (widgets["DeleteOriginal"].checked===true) {
  130. this.copies = 0;
  131. this.copy = false;
  132. }
  133. else if (widgets["KeepOriginal"].checked===true) {
  134. this.copies = 1;
  135. this.copy = true;
  136. }
  137. this.useCurrentAttributes = (widgets["UseCurrentAttributes"].checked===true);
  138. WidgetFactory.saveState(dialog);
  139. destr(dialog);
  140. EAction.activateMainWindow();
  141. return true;
  142. };
  143. /**
  144. * Callback function for Transform.getOperation.
  145. */
  146. Mirror.prototype.transform = function(entity, k, op, preview, flags) {
  147. entity.mirror(this.axisPoint1, this.axisPoint2);
  148. op.addObject(entity, flags);
  149. };
  150. Mirror.prototype.getAuxPreview = function() {
  151. if (!isValidVector(this.axisPoint1) || !isValidVector(this.axisPoint1)) {
  152. return undefined;
  153. }
  154. var ret = [];
  155. ret.push(new RLine(this.axisPoint1, this.axisPoint2));
  156. return ret;
  157. };
  158. Mirror.prototype.slotCopyChanged = function(checked) {
  159. Transform.prototype.slotCopyChanged.call(this, checked);
  160. if (this.copy) {
  161. this.copies = 1;
  162. }
  163. else {
  164. this.copies = 0;
  165. }
  166. };