Rotate2.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Rotate2
  22. * \brief Rotates and counter-rotates selected entities. Supports multiple copies.
  23. * \ingroup ecma_modify
  24. */
  25. function Rotate2(guiAction) {
  26. Transform.call(this, guiAction);
  27. this.primaryCenter = undefined;
  28. this.secondaryCenter = undefined;
  29. this.primaryAngle = 0.0;
  30. this.secondaryAngle = 0.0;
  31. this.setUiOptions("Rotate2.ui");
  32. }
  33. Rotate2.prototype = new Transform();
  34. Rotate2.includeBasePath = includeBasePath;
  35. Rotate2.State = {
  36. SettingPrimaryCenter : 0,
  37. SettingSecondaryCenter : 1
  38. };
  39. Rotate2.prototype.beginEvent = function() {
  40. Transform.prototype.beginEvent.call(this);
  41. if (!this.verifySelection()) {
  42. return;
  43. }
  44. this.setState(Rotate2.State.SettingPrimaryCenter);
  45. };
  46. Rotate2.prototype.setState = function(state) {
  47. Transform.prototype.setState.call(this, state);
  48. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  49. this.setCrosshairCursor();
  50. var appWin = RMainWindowQt.getMainWindow();
  51. switch (this.state) {
  52. case Rotate2.State.SettingPrimaryCenter:
  53. var trPrimaryCenter = qsTr("Center of primary rotation");
  54. this.setCommandPrompt(trPrimaryCenter);
  55. this.setLeftMouseTip(trPrimaryCenter);
  56. this.setRightMouseTip(EAction.trCancel);
  57. break;
  58. case Rotate2.State.SettingSecondaryCenter:
  59. var trSecondaryCenter = qsTr("Center of secondary rotation");
  60. this.setCommandPrompt(trSecondaryCenter);
  61. this.setLeftMouseTip(trSecondaryCenter);
  62. this.setRightMouseTip(EAction.trBack);
  63. break;
  64. }
  65. EAction.showSnapTools();
  66. };
  67. Rotate2.prototype.escapeEvent = function() {
  68. switch (this.state) {
  69. case Rotate2.State.SettingPrimaryCenter:
  70. Transform.prototype.escapeEvent.call(this);
  71. break;
  72. case Rotate2.State.SettingSecondaryCenter:
  73. this.setState(Rotate2.State.SettingPrimaryCenter);
  74. break;
  75. }
  76. };
  77. Rotate2.prototype.pickCoordinate = function(event, preview) {
  78. var di = this.getDocumentInterface();
  79. switch (this.state) {
  80. case Rotate2.State.SettingPrimaryCenter:
  81. if (!preview) {
  82. this.primaryCenter = event.getModelPosition();
  83. di.setRelativeZero(this.primaryCenter);
  84. this.setState(Rotate2.State.SettingSecondaryCenter);
  85. }
  86. break;
  87. case Rotate2.State.SettingSecondaryCenter:
  88. this.secondaryCenter = event.getModelPosition();
  89. if (preview) {
  90. di.previewOperation(this.getOperation(true));
  91. }
  92. else {
  93. this.setState(-1);
  94. if (!this.showDialog()) {
  95. // dialog canceled:
  96. this.terminate();
  97. return;
  98. }
  99. di.applyOperation(this.getOperation(false));
  100. di.setRelativeZero(this.secondaryCenter);
  101. this.terminate();
  102. }
  103. break;
  104. }
  105. };
  106. /**
  107. * Shows the rotation dialog.
  108. *
  109. * \return True if the dialog was accepted, false if it was canceled.
  110. */
  111. Rotate2.prototype.showDialog = function() {
  112. var dialog = WidgetFactory.createDialog(Rotate2.includeBasePath, "Rotate2Dialog.ui");
  113. WidgetFactory.restoreState(dialog);
  114. var widgets = getWidgets(dialog);
  115. // initialize dialog angles to angles used in tool options toolbar:
  116. widgets["PrimaryAngle"].setValue(this.primaryAngle);
  117. widgets["SecondaryAngle"].setValue(this.secondaryAngle);
  118. if (this.copies<0.5) {
  119. widgets["DeleteOriginal"].checked = true;
  120. }
  121. else if (this.copies<1.5) {
  122. widgets["KeepOriginal"].checked = true;
  123. }
  124. else {
  125. widgets["MultipleCopies"].checked = true;
  126. widgets["NumberOfCopies"].setValue(this.copies);
  127. }
  128. if (!dialog.exec()) {
  129. destr(dialog);
  130. EAction.activateMainWindow();
  131. return false;
  132. }
  133. if (widgets["DeleteOriginal"].checked===true) {
  134. this.copies = 0;
  135. }
  136. else if (widgets["KeepOriginal"].checked===true) {
  137. this.copies = 1;
  138. }
  139. else {
  140. this.copies = widgets["NumberOfCopies"].getValue();
  141. }
  142. this.primaryAngle = widgets["PrimaryAngle"].getValue();
  143. this.secondaryAngle = widgets["SecondaryAngle"].getValue();
  144. this.useCurrentAttributes = (widgets["UseCurrentAttributes"].checked===true);
  145. WidgetFactory.saveState(dialog);
  146. destr(dialog);
  147. EAction.activateMainWindow();
  148. return true;
  149. };
  150. /**
  151. * Callback function for Transform.getOperation.
  152. */
  153. Rotate2.prototype.transform = function(entity, k, op, preview, flags) {
  154. // qDebug("sc (transform): ", this.secondaryCenter);
  155. entity.rotate(this.primaryAngle * k, this.primaryCenter);
  156. var sc = this.secondaryCenter.copy();
  157. sc.rotate(this.primaryAngle * k, this.primaryCenter);
  158. entity.rotate(this.secondaryAngle * k, sc);
  159. op.addObject(entity, flags);
  160. };
  161. Rotate2.prototype.slotPrimaryAngleChanged = function(value) {
  162. this.primaryAngle = value;
  163. qDebug("this.primaryAngle: ", this.primaryAngle);
  164. if (this.primaryAngle>29.9) {
  165. debugger;
  166. }
  167. };
  168. Rotate2.prototype.slotSecondaryAngleChanged = function(value) {
  169. this.secondaryAngle = value;
  170. };
  171. Rotate2.prototype.slotNumberOfCopiesChanged = function(value) {
  172. this.copies = value;
  173. };