Rotate.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 Rotate
  22. * \brief Rotates selected entities. Supports multiple copies.
  23. * \ingroup ecma_modify
  24. */
  25. function Rotate(guiAction) {
  26. Transform.call(this, guiAction);
  27. this.centerPoint = undefined;
  28. this.referencePoint = undefined;
  29. this.targetPoint = undefined;
  30. this.angle = undefined;
  31. this.angleByMouse = false;
  32. this.useDialog = RSettings.getBoolValue("Rotate/UseDialog", true);
  33. if (!this.useDialog) {
  34. this.setUiOptions("Rotate.ui");
  35. }
  36. }
  37. Rotate.prototype = new Transform();
  38. Rotate.includeBasePath = includeBasePath;
  39. Rotate.getPreferencesCategory = function() {
  40. return [qsTr("Modify"), qsTr("Rotate")];
  41. };
  42. Rotate.State = {
  43. SettingCenterPoint : 0,
  44. SettingReferencePoint : 1,
  45. SettingTargetPoint : 2
  46. };
  47. Rotate.prototype.beginEvent = function() {
  48. Transform.prototype.beginEvent.call(this);
  49. if (!this.verifySelection()) {
  50. return;
  51. }
  52. this.setState(Rotate.State.SettingCenterPoint);
  53. };
  54. Rotate.prototype.setState = function(state) {
  55. Transform.prototype.setState.call(this, state);
  56. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  57. this.setCrosshairCursor();
  58. var appWin = RMainWindowQt.getMainWindow();
  59. switch (this.state) {
  60. case Rotate.State.SettingCenterPoint:
  61. this.referencePoint = undefined;
  62. this.targetPoint = undefined;
  63. var trCenterPoint = qsTr("Center point");
  64. this.setCommandPrompt(trCenterPoint);
  65. this.setLeftMouseTip(trCenterPoint);
  66. this.setRightMouseTip(EAction.trCancel);
  67. break;
  68. case Rotate.State.SettingReferencePoint:
  69. this.referencePoint = undefined;
  70. this.targetPoint = undefined;
  71. this.angle = undefined;
  72. var trReferencePoint = qsTr("Reference point");
  73. this.setCommandPrompt(trReferencePoint);
  74. this.setLeftMouseTip(trReferencePoint);
  75. this.setRightMouseTip(EAction.trBack);
  76. break;
  77. case Rotate.State.SettingTargetPoint:
  78. this.targetPoint = undefined;
  79. this.angle = undefined;
  80. var trTargetPoint = qsTr("Target point");
  81. this.setCommandPrompt(trTargetPoint);
  82. this.setLeftMouseTip(trTargetPoint);
  83. this.setRightMouseTip(EAction.trBack);
  84. break;
  85. }
  86. EAction.showSnapTools();
  87. };
  88. Rotate.prototype.escapeEvent = function() {
  89. switch (this.state) {
  90. case Rotate.State.SettingCenterPoint:
  91. Transform.prototype.escapeEvent.call(this);
  92. break;
  93. case Rotate.State.SettingReferencePoint:
  94. this.setState(Rotate.State.SettingCenterPoint);
  95. break;
  96. case Rotate.State.SettingTargetPoint:
  97. this.setState(Rotate.State.SettingReferencePoint);
  98. break;
  99. }
  100. };
  101. Rotate.prototype.pickCoordinate = function(event, preview) {
  102. var di = this.getDocumentInterface();
  103. var op;
  104. switch (this.state) {
  105. case Rotate.State.SettingCenterPoint:
  106. this.centerPoint = event.getModelPosition();
  107. if (preview) {
  108. this.updatePreview();
  109. }
  110. else {
  111. if (this.useDialog) {
  112. this.setState(-1);
  113. if (!this.showDialog()) {
  114. // dialog canceled:
  115. this.terminate();
  116. return;
  117. }
  118. }
  119. // define angle with mouse:
  120. if (this.angleByMouse===true) {
  121. di.setRelativeZero(this.centerPoint);
  122. this.setState(Rotate.State.SettingReferencePoint);
  123. }
  124. // angle defined in dialog:
  125. else {
  126. op = this.getOperation(false);
  127. if (!isNull(op)) {
  128. di.applyOperation(op);
  129. di.setRelativeZero(this.centerPoint);
  130. this.terminate();
  131. }
  132. }
  133. }
  134. break;
  135. case Rotate.State.SettingReferencePoint:
  136. this.referencePoint = event.getModelPosition();
  137. if (preview) {
  138. this.updatePreview();
  139. }
  140. else {
  141. this.setState(Rotate.State.SettingTargetPoint);
  142. }
  143. break;
  144. case Rotate.State.SettingTargetPoint:
  145. this.targetPoint = event.getModelPosition();
  146. if (preview) {
  147. this.updatePreview();
  148. }
  149. else {
  150. op = this.getOperation(false);
  151. if (!isNull(op)) {
  152. di.applyOperation(op);
  153. di.setRelativeZero(this.targetPoint);
  154. this.setState(-1);
  155. this.terminate();
  156. }
  157. }
  158. break;
  159. }
  160. };
  161. /**
  162. * Shows the rotation dialog.
  163. *
  164. * \return True if the dialog was accepted, false if it was canceled.
  165. */
  166. Rotate.prototype.showDialog = function() {
  167. var dialog = WidgetFactory.createDialog(Rotate.includeBasePath, "RotateDialog.ui");
  168. WidgetFactory.restoreState(dialog);
  169. if (!dialog.exec()) {
  170. destr(dialog);
  171. EAction.activateMainWindow();
  172. return false;
  173. }
  174. var widgets = getWidgets(dialog);
  175. if (widgets["DeleteOriginal"].checked===true) {
  176. this.copies = 0;
  177. }
  178. else if (widgets["KeepOriginal"].checked===true) {
  179. this.copies = 1;
  180. }
  181. else {
  182. this.copies = widgets["NumberOfCopies"].getValue();
  183. }
  184. if (widgets["AngleByMouse"].checked===true) {
  185. this.angleByMouse = true;
  186. this.angle = undefined;
  187. }
  188. else {
  189. this.angleByMouse = false;
  190. this.angle = widgets["Angle"].getValue();
  191. }
  192. this.useCurrentAttributes = (widgets["UseCurrentAttributes"].checked===true);
  193. WidgetFactory.saveState(dialog);
  194. destr(dialog);
  195. EAction.activateMainWindow();
  196. return true;
  197. }
  198. Rotate.prototype.getOperation = function(preview) {
  199. if (!isValidVector(this.centerPoint)) {
  200. return undefined;
  201. }
  202. if (this.angleByMouse===true) {
  203. if (!isValidVector(this.referencePoint) || !isValidVector(this.targetPoint)) {
  204. return undefined;
  205. }
  206. var a1 = this.centerPoint.getAngleTo(this.referencePoint);
  207. var a2 = this.centerPoint.getAngleTo(this.targetPoint);
  208. this.angle = RMath.getAngleDifference(a1, a2);
  209. }
  210. if (!isNumber(this.angle)) {
  211. return undefined;
  212. }
  213. return Transform.prototype.getOperation.call(this, preview);
  214. }
  215. /**
  216. * Callback function for Transform.getOperation.
  217. */
  218. Rotate.prototype.transform = function(entity, k, op, preview, flags) {
  219. entity.rotate(this.angle * k, this.centerPoint);
  220. op.addObject(entity, flags);
  221. }
  222. Rotate.prototype.getAuxPreview = function() {
  223. if (!isValidVector(this.centerPoint)) {
  224. return undefined;
  225. }
  226. var ret = [];
  227. if (this.state==Rotate.State.SettingReferencePoint || this.state==Rotate.State.SettingTargetPoint) {
  228. if (isValidVector(this.referencePoint)) {
  229. ret.push(new RLine(this.centerPoint, this.referencePoint));
  230. }
  231. }
  232. if (this.state==Rotate.State.SettingTargetPoint) {
  233. if (isValidVector(this.targetPoint)) {
  234. ret.push(new RLine(this.centerPoint, this.targetPoint));
  235. }
  236. }
  237. return ret;
  238. };
  239. Rotate.prototype.slotAngleChanged = function(v) {
  240. this.angle = v;
  241. };
  242. Rotate.prototype.slotAngleByMouseChanged = function(v) {
  243. this.angleByMouse = v;
  244. };