Paste.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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("scripts/Edit/Edit.js");
  20. /**
  21. * \class Paste
  22. * \brief Pastes the clipboard contents into the drawing.
  23. * The target point is specified by the user.
  24. * \ingroup ecma_edit
  25. */
  26. function Paste(guiAction) {
  27. Edit.call(this, guiAction);
  28. this.setUiOptions(Paste.includeBasePath + "/Paste.ui");
  29. this.boundary = undefined;
  30. this.offset = new RVector(0,0);
  31. this.scale = 1.0;
  32. this.rotation = 0.0;
  33. this.flipHorizontal = false;
  34. this.flipVertical = false;
  35. this.toCurrentLayer = false;
  36. this.overwriteLayers = false;
  37. this.overwriteBlocks = false;
  38. this.sourceDocument = RDocument.getClipboard();
  39. }
  40. Paste.prototype = new Edit();
  41. Paste.includeBasePath = includeBasePath;
  42. Paste.State = {
  43. SettingPosition : 0
  44. };
  45. Paste.getPreferencesCategory = function() {
  46. return [qsTr("Edit"), qsTr("Paste")];
  47. };
  48. Paste.prototype.beginEvent = function() {
  49. // always reset paste options to defaults:
  50. if (RSettings.getBoolValue("Paste/ResetOnToolStart", false)===true) {
  51. RSettings.setValue("Paste/Scale", 1.0, true);
  52. RSettings.setValue("Paste/Rotation", 0.0, true);
  53. RSettings.setValue("Paste/FlipHorizontal", false, true);
  54. RSettings.setValue("Paste/FlipVertical", false, true);
  55. RSettings.setValue("Paste/OverwriteBlocks", false, true);
  56. RSettings.setValue("Paste/OverwriteLayers", false, true);
  57. RSettings.setValue("Paste/ToCurrentLayer", false, true);
  58. }
  59. Edit.prototype.beginEvent.call(this);
  60. this.di = this.getDocumentInterface();
  61. this.setState(Paste.State.SettingPosition);
  62. };
  63. Paste.prototype.initUiOptions = function(resume, optionsToolBar) {
  64. Edit.prototype.initUiOptions.call(this, resume, optionsToolBar);
  65. var combo = optionsToolBar.findChild("Scale");
  66. if (!isNull(combo)) {
  67. combo.setCompleter(null);
  68. }
  69. combo = optionsToolBar.findChild("Rotation");
  70. if (!isNull(combo)) {
  71. combo.setCompleter(null);
  72. }
  73. };
  74. Paste.prototype.setState = function(state) {
  75. Edit.prototype.setState.call(this, state);
  76. if (this.state===Paste.State.SettingPosition) {
  77. this.setCrosshairCursor();
  78. this.di.setClickMode(RAction.PickCoordinate);
  79. var trPosition = qsTr("Position");
  80. this.setCommandPrompt(trPosition);
  81. this.setLeftMouseTip(trPosition);
  82. this.setRightMouseTip(EAction.trCancel);
  83. EAction.showSnapTools();
  84. }
  85. };
  86. Paste.prototype.pickCoordinate = function(event, preview) {
  87. if (this.state===Paste.State.SettingPosition) {
  88. this.offset = event.getModelPosition();
  89. if (preview) {
  90. this.updatePreview();
  91. }
  92. else {
  93. var op = this.getOperation();
  94. if (!isNull(op)) {
  95. this.di.applyOperation(op);
  96. this.di.clearPreview();
  97. this.di.setRelativeZero(this.offset);
  98. this.di.repaintViews();
  99. this.postInsertion();
  100. }
  101. }
  102. }
  103. };
  104. /**
  105. * Called after every insert. Can be used by inheriting classes to increment a
  106. * counter, preprocess the inserted document, etc.
  107. */
  108. Paste.prototype.postInsertion = function() {
  109. };
  110. /**
  111. * Called to perform additional initialization of the RPasteOperation.
  112. */
  113. Paste.prototype.initOperation = function(op) {
  114. };
  115. Paste.prototype.getOperation = function(preview) {
  116. if (!isVector(this.offset) || !isNumber(this.scale) || !isNumber(this.rotation)) {
  117. return undefined;
  118. }
  119. var op = new RPasteOperation(this.sourceDocument);
  120. op.setText(this.getToolTitle());
  121. op.setOffset(this.offset);
  122. op.setScale(this.scale);
  123. op.setRotation(this.rotation);
  124. op.setFlipHorizontal(this.flipHorizontal);
  125. op.setFlipVertical(this.flipVertical);
  126. op.setToCurrentLayer(this.toCurrentLayer);
  127. op.setOverwriteLayers(this.overwriteLayers);
  128. op.setOverwriteBlocks(this.overwriteBlocks);
  129. this.initOperation(op);
  130. var unitFactor = RUnit.convert(1.0, this.sourceDocument.getUnit(), this.getDocument().getUnit());
  131. this.boundary = op.getBoundary(unitFactor);
  132. return op;
  133. };
  134. Paste.prototype.getAuxPreview = function() {
  135. var ret = [];
  136. if (!isNull(this.boundary)) {
  137. ret.push(this.boundary);
  138. }
  139. return ret;
  140. };
  141. Paste.prototype.slotScaleChanged = function(value) {
  142. this.scale = value;
  143. this.updatePreview(true);
  144. };
  145. Paste.prototype.slotRotationChanged = function(value) {
  146. this.rotation = value;
  147. this.updatePreview(true);
  148. };
  149. Paste.prototype.slotFlipHorizontalChanged = function(value) {
  150. this.flipHorizontal = value;
  151. this.updatePreview(true);
  152. };
  153. Paste.prototype.slotFlipVerticalChanged = function(value) {
  154. this.flipVertical = value;
  155. this.updatePreview(true);
  156. };
  157. Paste.prototype.slotToCurrentLayerChanged = function(value) {
  158. this.toCurrentLayer = value;
  159. this.updatePreview(true);
  160. };
  161. Paste.prototype.slotOverwriteLayersChanged = function(value) {
  162. this.overwriteLayers = value;
  163. this.updatePreview(true);
  164. };
  165. Paste.prototype.slotOverwriteBlocksChanged = function(value) {
  166. this.overwriteBlocks = value;
  167. this.updatePreview(true);
  168. };