Translate.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 Translate
  22. * \brief Translates (moves or copies) selected entities. Supports multiple copies.
  23. * \ingroup ecma_modify
  24. */
  25. function Translate(guiAction) {
  26. Transform.call(this, guiAction);
  27. this.referencePoint = undefined;
  28. this.targetPoint = undefined;
  29. this.useDialog = RSettings.getBoolValue("Translate/UseDialog", true);
  30. if (!this.useDialog) {
  31. this.setUiOptions("Translate.ui");
  32. }
  33. }
  34. Translate.prototype = new Transform();
  35. Translate.includeBasePath = includeBasePath;
  36. Translate.getPreferencesCategory = function() {
  37. return [qsTr("Modify"), qsTr("Move/Copy")];
  38. };
  39. Translate.State = {
  40. SettingReferencePoint : 0,
  41. SettingTargetPoint : 1
  42. };
  43. Translate.prototype.beginEvent = function() {
  44. Transform.prototype.beginEvent.call(this);
  45. if (!this.verifySelection()) {
  46. return;
  47. }
  48. this.setState(Translate.State.SettingReferencePoint);
  49. };
  50. Translate.prototype.initState = function(state) {
  51. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  52. this.setCrosshairCursor();
  53. var appWin = RMainWindowQt.getMainWindow();
  54. switch (this.state) {
  55. case Translate.State.SettingReferencePoint:
  56. this.referencePoint = undefined;
  57. this.targetPoint = undefined;
  58. var trReferencePoint = qsTr("Reference point");
  59. this.setCommandPrompt(trReferencePoint);
  60. this.setLeftMouseTip(trReferencePoint);
  61. this.setRightMouseTip(EAction.trCancel);
  62. break;
  63. case Translate.State.SettingTargetPoint:
  64. this.targetPoint = undefined;
  65. var trTargetPoint = qsTr("Target point");
  66. this.setCommandPrompt(trTargetPoint);
  67. this.setLeftMouseTip(trTargetPoint);
  68. this.setRightMouseTip(EAction.trBack);
  69. break;
  70. }
  71. EAction.showSnapTools();
  72. };
  73. Translate.prototype.escapeEvent = function() {
  74. switch (this.state) {
  75. case Translate.State.SettingReferencePoint:
  76. Transform.prototype.escapeEvent.call(this);
  77. break;
  78. case Translate.State.SettingTargetPoint:
  79. this.setState(Translate.State.SettingReferencePoint);
  80. break;
  81. }
  82. };
  83. Translate.prototype.pickCoordinate = function(event, preview) {
  84. var di = this.getDocumentInterface();
  85. var op;
  86. switch (this.state) {
  87. case Translate.State.SettingReferencePoint:
  88. if (!preview) {
  89. this.referencePoint = event.getModelPosition();
  90. di.setRelativeZero(this.referencePoint);
  91. this.setState(Translate.State.SettingTargetPoint);
  92. }
  93. break;
  94. case Translate.State.SettingTargetPoint:
  95. this.targetPoint = event.getModelPosition();
  96. if (preview) {
  97. op = this.getOperation(true);
  98. if (!isNull(op)) {
  99. di.previewOperation(op);
  100. }
  101. }
  102. else {
  103. if (this.useDialog) {
  104. this.setState(-1);
  105. if (!this.showDialog()) {
  106. // dialog canceled:
  107. this.terminate();
  108. return;
  109. }
  110. }
  111. op = this.getOperation(false);
  112. if (!isNull(op)) {
  113. di.applyOperation(op);
  114. di.setRelativeZero(this.targetPoint);
  115. this.terminate();
  116. }
  117. }
  118. break;
  119. }
  120. };
  121. Translate.prototype.getOperation = function(preview, selectResult) {
  122. if (isNull(this.copies)) {
  123. return undefined;
  124. }
  125. if (isNull(selectResult)) {
  126. selectResult = true;
  127. }
  128. var di = this.getDocumentInterface();
  129. var op = new RModifyObjectsOperation();
  130. var offset = this.targetPoint.operator_subtract(this.referencePoint)
  131. var transformation = new RTranslation(offset);
  132. op.transformSelection(transformation, di, this.getCopies(), preview, selectResult, this.useCurrentAttributes);
  133. return op;
  134. };
  135. /**
  136. * Shows the translation dialog.
  137. *
  138. * \return True if the dialog was accepted, false if it was canceled.
  139. */
  140. Translate.prototype.showDialog = function() {
  141. var dialog = WidgetFactory.createDialog(Translate.includeBasePath, "TranslateDialog.ui");
  142. WidgetFactory.restoreState(dialog);
  143. if (!dialog.exec()) {
  144. destr(dialog);
  145. EAction.activateMainWindow();
  146. return false;
  147. }
  148. var widgets = getWidgets(dialog);
  149. if (widgets["DeleteOriginal"].checked===true) {
  150. this.copies = 0;
  151. }
  152. else if (widgets["KeepOriginal"].checked===true) {
  153. this.copies = 1;
  154. }
  155. else {
  156. this.copies = widgets["NumberOfCopies"].getValue();
  157. }
  158. this.useCurrentAttributes = (widgets["UseCurrentAttributes"].checked===true);
  159. WidgetFactory.saveState(dialog);
  160. destr(dialog);
  161. EAction.activateMainWindow();
  162. return true;
  163. };