LineFreehand.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/Draw/Line/Line.js");
  20. /**
  21. * \class LineFreehand
  22. * \brief Freehand line.
  23. * \ingroup ecma_draw_line
  24. */
  25. function LineFreehand(guiAction) {
  26. Line.call(this, guiAction);
  27. this.pointList = [];
  28. }
  29. LineFreehand.prototype = new Line();
  30. LineFreehand.State = {
  31. Idle : 0,
  32. Drawing : 1
  33. };
  34. LineFreehand.prototype.beginEvent = function() {
  35. Line.prototype.beginEvent.call(this);
  36. this.setState(LineFreehand.State.Idle);
  37. };
  38. LineFreehand.prototype.setState = function(state) {
  39. Line.prototype.setState.call(this, state);
  40. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  41. this.setCrosshairCursor();
  42. var appWin = RMainWindowQt.getMainWindow();
  43. switch (this.state) {
  44. case LineFreehand.State.Idle:
  45. var trStart = qsTr("Press to start drawing");
  46. this.setCommandPrompt(trStart);
  47. this.setLeftMouseTip(trStart);
  48. this.setRightMouseTip(EAction.trCancel);
  49. this.pointList = [];
  50. break;
  51. case LineFreehand.State.Drawing:
  52. var trStop = qsTr("Release to stop drawing");
  53. this.setCommandPrompt(trStop);
  54. this.setLeftMouseTip(trStop);
  55. this.setRightMouseTip("");
  56. break;
  57. }
  58. };
  59. LineFreehand.prototype.escapeEvent = function() {
  60. switch (this.state) {
  61. case LineFreehand.State.Idle:
  62. EAction.prototype.escapeEvent.call(this);
  63. break;
  64. }
  65. };
  66. LineFreehand.prototype.mousePressEvent = function(event) {
  67. if (event.button() == Qt.LeftButton
  68. && event.modifiers().valueOf() != Qt.ControlModifier.valueOf()) {
  69. if (this.state == LineFreehand.State.Idle) {
  70. this.setState(LineFreehand.State.Drawing);
  71. }
  72. }
  73. };
  74. LineFreehand.prototype.mouseMoveEvent = function(event) {
  75. if (event.buttons().valueOf() & Qt.LeftButton.valueOf() &&
  76. event.modifiers().valueOf() != Qt.ControlModifier.valueOf()) {
  77. if (this.state == LineFreehand.State.Drawing) {
  78. this.pointList.push(event.getModelPosition());
  79. this.updatePreview();
  80. }
  81. }
  82. };
  83. LineFreehand.prototype.mouseReleaseEvent = function(event) {
  84. if (event.button() == Qt.LeftButton) {
  85. if (this.state == LineFreehand.State.Drawing) {
  86. var di = this.getDocumentInterface();
  87. var op = this.getOperation(false);
  88. if (!isNull(op)) {
  89. di.applyOperation(op);
  90. }
  91. this.setState(LineFreehand.State.Idle);
  92. }
  93. }
  94. Line.prototype.mouseReleaseEvent.call(this, event);
  95. };
  96. LineFreehand.prototype.getOperation = function(preview) {
  97. var op = new RAddObjectsOperation();
  98. op.setText(this.getToolTitle());
  99. op.setLimitPreview(false);
  100. for (var i=0; i<this.pointList.length-1; ++i) {
  101. var line = new RLineEntity(
  102. this.getDocument(),
  103. new RLineData(
  104. this.pointList[i],
  105. this.pointList[i+1]
  106. )
  107. );
  108. op.addObject(line);
  109. }
  110. return op;
  111. };