ClipboardOperation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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("Edit.js");
  20. /**
  21. * Base class for copy and cut.
  22. */
  23. function ClipboardOperation(guiAction) {
  24. Edit.call(this, guiAction);
  25. this.cut = false;
  26. this.chooseReferencePoint = false;
  27. this.offset = 0.0;
  28. }
  29. ClipboardOperation.prototype = new Edit();
  30. ClipboardOperation.prototype.beginEvent = function() {
  31. Edit.prototype.beginEvent.call(this);
  32. if (this.chooseReferencePoint) {
  33. this.setState(0);
  34. }
  35. else {
  36. this.applyOperation();
  37. var di = this.getDocumentInterface();
  38. di.setRelativeZero(di.getDocument().getSelectionBox().getCenter());
  39. this.terminate();
  40. }
  41. };
  42. ClipboardOperation.prototype.setState = function(state) {
  43. Edit.prototype.setState.call(this, state);
  44. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  45. this.setCrosshairCursor();
  46. var trReferencePoint = qsTr("Reference point");
  47. var appWin = RMainWindowQt.getMainWindow();
  48. this.setCommandPrompt(trReferencePoint);
  49. this.setLeftMouseTip(trReferencePoint);
  50. this.setRightMouseTip(EAction.trCancel);
  51. EAction.showSnapTools();
  52. };
  53. ClipboardOperation.prototype.coordinateEvent = function(event) {
  54. var di = this.getDocumentInterface();
  55. di.setRelativeZero(event.getModelPosition());
  56. this.offset = event.getModelPosition().getNegated();
  57. this.applyOperation();
  58. this.terminate();
  59. };
  60. /**
  61. * Center of the selection is the reference point by default.
  62. */
  63. ClipboardOperation.prototype.getOffset = function() {
  64. if (this.chooseReferencePoint) {
  65. return this.offset;
  66. }
  67. else {
  68. return this.getDocument().getSelectionBox().getCenter().getNegated();
  69. }
  70. };
  71. /**
  72. * \return the appropriate operation (copy or cut).
  73. */
  74. ClipboardOperation.prototype.getOperation = function(preview) {
  75. var op = new RCopyOperation(this.getOffset(), this.getDocument());
  76. return op;
  77. };
  78. /**
  79. * Applies the operation returned by this.getOperation.
  80. */
  81. ClipboardOperation.prototype.applyOperation = function() {
  82. var clipBoard = RDocumentInterface.getClipboard();
  83. clipBoard.getDocument().setUnit(this.getDocument().getUnit());
  84. clipBoard.applyOperation(this.getOperation(false));
  85. clipBoard.getDocument().setUnit(this.getDocument().getUnit());
  86. clipBoard.autoZoom();
  87. if (this.cut) {
  88. var op = new RDeleteSelectionOperation();
  89. op.setText(this.getToolTitle());
  90. this.getDocumentInterface().applyOperation(op);
  91. }
  92. };