DrawBasedOnRectangleSize.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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("Draw.js");
  20. include("scripts/Widgets/OptionsToolBar/OptionsToolBar.js");
  21. /**
  22. * \class DrawBasedOnRectangleSize
  23. *
  24. * \brief Base class for drawing tools that draw something based on
  25. * a rectangular shape with given width, height and angle. The tool
  26. * also supports reference points at the corners, top, left, right,
  27. * bottom and middle.
  28. *
  29. * \ingroup ecma_draw
  30. */
  31. function DrawBasedOnRectangleSize(guiAction) {
  32. EAction.call(this, guiAction);
  33. this.dialogUiFile = undefined;
  34. this.useDialog = RSettings.getBoolValue("DrawBasedOnRectangleSize/UseDialog", false);
  35. this.dialog = undefined;
  36. this.pos = undefined;
  37. this.width = 1;
  38. this.height = 1;
  39. this.angle = 0;
  40. this.corners = [];
  41. // [ObjectName], [Shown Text], offset vector
  42. var tl = [ "TopLeft", qsTr("Top Left"), new RVector(-1, 1) ];
  43. var t = [ "Top", qsTr("Top"), new RVector( 0, 1) ];
  44. var tr = [ "TopRight", qsTr("Top Right"), new RVector( 1, 1) ];
  45. var l = [ "Left", qsTr("Left"), new RVector(-1, 0) ];
  46. var m = [ "Middle", qsTr("Middle"), new RVector( 0, 0) ];
  47. var r = [ "Right", qsTr("Right"), new RVector( 1, 0) ];
  48. var bl = [ "BottomLeft", qsTr("Bottom Left"), new RVector(-1, -1) ];
  49. var b = [ "Bottom", qsTr("Bottom"), new RVector( 0, -1) ];
  50. var br = [ "BottomRight", qsTr("Bottom Right"), new RVector( 1, -1) ];
  51. if (RSettings.getBoolValue("DrawBasedOnRectangleSize/ReversedShortcuts", false)===true) {
  52. this.referencePoints = [
  53. tl, t, tr,
  54. l, m, r,
  55. bl, b, br,
  56. ];
  57. }
  58. else {
  59. // fits with number pad:
  60. this.referencePoints = [
  61. bl, b, br,
  62. l, m, r,
  63. tl, t, tr,
  64. ];
  65. }
  66. this.referencePointIndex = undefined;
  67. }
  68. DrawBasedOnRectangleSize.prototype = new EAction();
  69. DrawBasedOnRectangleSize.includeBasePath = includeBasePath;
  70. DrawBasedOnRectangleSize.prototype.beginEvent = function() {
  71. EAction.prototype.beginEvent.call(this);
  72. this.setState(0);
  73. // commented out to disable preview initially
  74. //this.simulateMouseMoveEvent();
  75. if (this.useDialog) {
  76. if (!this.showDialog()) {
  77. this.terminate();
  78. return;
  79. }
  80. }
  81. };
  82. DrawBasedOnRectangleSize.prototype.finishEvent = function() {
  83. EAction.prototype.finishEvent.call(this);
  84. };
  85. DrawBasedOnRectangleSize.prototype.setState = function(state) {
  86. EAction.prototype.setState.call(this, state);
  87. this.setCrosshairCursor();
  88. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  89. var appWin = RMainWindowQt.getMainWindow();
  90. var trPosition = qsTr("Position");
  91. this.setCommandPrompt(trPosition);
  92. this.setLeftMouseTip(trPosition);
  93. this.setRightMouseTip(EAction.trCancel);
  94. EAction.showSnapTools();
  95. };
  96. DrawBasedOnRectangleSize.prototype.initUiOptions = function(resume, restoreFromSettings) {
  97. EAction.prototype.initUiOptions.call(this, resume, restoreFromSettings);
  98. this.referencePointIndex = RSettings.getIntValue(this.settingsGroup + "/ReferencePoint", 4);
  99. var optionsToolBar = EAction.getOptionsToolBar();
  100. var refPointCombo = optionsToolBar.findChild("ReferencePoint");
  101. if (isNull(refPointCombo)) {
  102. return;
  103. }
  104. refPointCombo.blockSignals(true);
  105. refPointCombo.clear();
  106. if (isNull(this.shortcuts)) {
  107. this.shortcuts = [];
  108. }
  109. for (var i=0; i<this.referencePoints.length; i++) {
  110. var shortcut = "Ctrl+%1".arg(i+1);
  111. var shortcutLabel = shortcut;
  112. if (RS.getSystemId()==="osx") {
  113. shortcutLabel = shortcutLabel.replace("Ctrl+", "\u2318");
  114. }
  115. refPointCombo.addItem(
  116. this.referencePoints[i][1] + " (" + shortcutLabel + ")",
  117. this.referencePoints[i][2]
  118. );
  119. if (isNull(this.shortcuts[i])) {
  120. this.shortcuts[i] = new QShortcut(new QKeySequence(shortcut), refPointCombo, 0, 0, Qt.WindowShortcut);
  121. var keyReactor = new KeyReactor(i);
  122. this.shortcuts[i].activated.connect(keyReactor, keyReactor.activated);
  123. }
  124. }
  125. if (isNull(this.referencePointIndex) ||
  126. this.referencePointIndex<0 ||
  127. this.referencePointIndex>this.referencePoints.length-1) {
  128. this.referencePointIndex = 4;
  129. }
  130. refPointCombo.blockSignals(false);
  131. refPointCombo.currentIndex = this.referencePointIndex;
  132. };
  133. DrawBasedOnRectangleSize.prototype.pickCoordinate = function(event, preview) {
  134. var di = this.getDocumentInterface();
  135. this.pos = event.getModelPosition();
  136. if (preview) {
  137. this.updatePreview();
  138. }
  139. else {
  140. var op = this.getOperation(preview);
  141. if (!isNull(op)) {
  142. di.applyOperation(op);
  143. di.setRelativeZero(this.pos);
  144. }
  145. }
  146. };
  147. DrawBasedOnRectangleSize.prototype.updatePreview = function(clear) {
  148. if (!isNull(this.dialog)) {
  149. return;
  150. }
  151. EAction.prototype.updatePreview.call(this, clear);
  152. };
  153. DrawBasedOnRectangleSize.prototype.getOperation = function(preview) {
  154. var i;
  155. if (!isNumber(this.angle)) {
  156. this.angle = 0;
  157. }
  158. if (isNull(this.pos) || isNull(this.width) || isNull(this.height)) {
  159. return;
  160. }
  161. var x = this.pos.x;
  162. var y = this.pos.y;
  163. var w2 = this.width / 2;
  164. var h2 = this.height / 2;
  165. // create corners (centered)
  166. this.corners = [
  167. new RVector(x - w2, y - h2),
  168. new RVector(x + w2, y - h2),
  169. new RVector(x + w2, y + h2),
  170. new RVector(x - w2, y + h2)
  171. ];
  172. if (isNull(this.referencePointIndex) ||
  173. this.referencePointIndex<0 || this.referencePointIndex>this.referencePoints.length-1) {
  174. return undefined;
  175. }
  176. var referencePoint = this.referencePoints[this.referencePointIndex][2];
  177. // apply reference point vector
  178. for (i = 0; i < this.corners.length; ++i) {
  179. this.corners[i] = new RVector(
  180. this.corners[i].x - w2 * referencePoint.x,
  181. this.corners[i].y - h2 * referencePoint.y
  182. );
  183. }
  184. // apply angle:
  185. if (this.angle!==0.0) {
  186. for (i = 0; i < this.corners.length; ++i) {
  187. this.corners[i].rotate(this.angle, this.pos);
  188. }
  189. }
  190. var op = new RAddObjectsOperation();
  191. op.setText(this.getToolTitle());
  192. var shapes = this.getShapes(this.corners);
  193. for (i=0; i<shapes.length; ++i) {
  194. var e = shapeToEntity(this.getDocument(), shapes[i]);
  195. if (isNull(e)) {
  196. continue;
  197. }
  198. op.addObject(e);
  199. }
  200. return op;
  201. };
  202. DrawBasedOnRectangleSize.prototype.getShapes = function(corners) {
  203. return [];
  204. };
  205. DrawBasedOnRectangleSize.prototype.slotWidthChanged = function(value) {
  206. this.width = value;
  207. this.updateOkButton();
  208. this.updatePreview(true);
  209. };
  210. DrawBasedOnRectangleSize.prototype.slotHeightChanged = function(value) {
  211. this.height = value;
  212. this.updateOkButton();
  213. this.updatePreview(true);
  214. };
  215. DrawBasedOnRectangleSize.prototype.slotAngleChanged = function(value) {
  216. this.angle = value;
  217. this.updateOkButton();
  218. this.updatePreview(true);
  219. };
  220. DrawBasedOnRectangleSize.prototype.slotReferencePointChanged = function(value) {
  221. var optionsToolBar = EAction.getOptionsToolBar();
  222. var refPointCombo = optionsToolBar.findChild("ReferencePoint");
  223. this.referencePointIndex = refPointCombo.currentIndex;
  224. this.updatePreview(true);
  225. };
  226. DrawBasedOnRectangleSize.prototype.slotReset = function() {
  227. var optionsToolBar = EAction.getOptionsToolBar();
  228. if (isNull(optionsToolBar)) {
  229. return;
  230. }
  231. var refPointCombo = optionsToolBar.findChild("ReferencePoint");
  232. if (!isNull(refPointCombo)) {
  233. refPointCombo.currentIndex = 4;
  234. }
  235. var angleEdit = optionsToolBar.findChild("Angle");
  236. if (!isNull(angleEdit)) {
  237. angleEdit.setValue(0.0);
  238. }
  239. this.updatePreview(true);
  240. };
  241. DrawBasedOnRectangleSize.prototype.validate = function() {
  242. return isNumberGreaterZero(this.width) && isNumberGreaterZero(this.height) && isNumber(this.angle);
  243. };
  244. DrawBasedOnRectangleSize.prototype.updateOkButton = function() {
  245. if (isNull(this.dialog)) {
  246. return;
  247. }
  248. var buttonBox = this.dialog.findChild("ButtonBox");
  249. if (isNull(buttonBox)) {
  250. return;
  251. }
  252. buttonBox.button(QDialogButtonBox.Ok).enabled = this.validate();
  253. };
  254. /**
  255. * Reacts to an assigned shortcut for the given index of the reference point combo box.
  256. */
  257. function KeyReactor(i) {
  258. this.i = i;
  259. }
  260. KeyReactor.prototype.activated = function() {
  261. var optionsToolBar = EAction.getOptionsToolBar();
  262. var refPointCombo = optionsToolBar.findChild("ReferencePoint");
  263. refPointCombo.currentIndex = this.i;
  264. };