ExThreePoints.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. //! [main]
  20. //! [include]
  21. include("scripts/EAction.js");
  22. //! [include]
  23. //! [constructor]
  24. /**
  25. * \class ExThreePoints
  26. * \ingroup ecma_misc_examples_draw
  27. */
  28. function ExThreePoints(guiAction) {
  29. EAction.call(this, guiAction);
  30. }
  31. //! [constructor]
  32. //! [State]
  33. ExThreePoints.State = {
  34. SettingPosition : 0
  35. };
  36. //! [State]
  37. //! [inheritance]
  38. ExThreePoints.prototype = new EAction();
  39. //! [inheritance]
  40. //! [setState]
  41. ExThreePoints.prototype.setState = function(state) {
  42. EAction.prototype.setState.call(this, state);
  43. // set crosshair cursor for choosing the coordinate:
  44. this.setCrosshairCursor();
  45. // we are interested in coordinates the user clicks
  46. // (as opposed to entities):
  47. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  48. // status bar user information:
  49. var appWin = RMainWindowQt.getMainWindow();
  50. var tr = qsTr("Position");
  51. this.setLeftMouseTip(tr);
  52. this.setCommandPrompt(tr);
  53. this.setRightMouseTip(EAction.trCancel);
  54. // show the snap toolbar, so the user can choose an alternative
  55. // snap tool if desired:
  56. EAction.showSnapTools();
  57. };
  58. //! [setState]
  59. //! [beginEvent]
  60. ExThreePoints.prototype.beginEvent = function() {
  61. EAction.prototype.beginEvent.call(this);
  62. this.setState(ExThreePoints.State.SettingPosition);
  63. };
  64. //! [beginEvent]
  65. //! [coordinateEvent]
  66. ExThreePoints.prototype.coordinateEvent = function(event) {
  67. // the exact position that was clicked or
  68. // entered by the user:
  69. var pos = event.getModelPosition();
  70. // move relative zero point to that position:
  71. this.getDocumentInterface().setRelativeZero(pos);
  72. // create an operation for adding objects:
  73. var op = new RAddObjectsOperation();
  74. for (var i=0; i<3; i++) {
  75. // create a point entity and add it to the operation:
  76. var point = new RPointEntity(
  77. this.getDocument(),
  78. new RPointData(new RVector(pos.x + i, pos.y))
  79. );
  80. op.addObject(point);
  81. }
  82. // apply the operation to the current drawing:
  83. this.getDocumentInterface().applyOperation(op);
  84. };
  85. //! [coordinateEvent]
  86. //! [init]
  87. ExThreePoints.init = function(basePath) {
  88. var action = new RGuiAction(qsTr("Three Points"), RMainWindowQt.getMainWindow());
  89. action.setRequiresDocument(true);
  90. action.setScriptFile(basePath + "/ExThreePoints.js");
  91. //action.setIcon(basePath + "/ExThreePoints.svg");
  92. action.setStatusTip(qsTr("Draw three points"));
  93. action.setDefaultShortcut(new QKeySequence("p,3"));
  94. action.setDefaultCommands(["point3"]);
  95. action.setGroupSortOrder(73100);
  96. action.setSortOrder(400);
  97. action.setWidgetNames(["DrawExamplesMenu"]);
  98. };
  99. //! [init]
  100. //! [main]