Snap.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  20. * \defgroup ecma_snap Snap Tools
  21. * \ingroup ecma_scripts
  22. *
  23. * \brief This module contains ECMAScript implementations of various
  24. * snap tools.
  25. */
  26. include("scripts/EAction.js");
  27. /**
  28. * \class Snap
  29. * \brief Base class for all snap tools.
  30. * \ingroup ecma_snap
  31. */
  32. function Snap(guiAction) {
  33. EAction.call(this, guiAction);
  34. }
  35. Snap.prototype = new EAction();
  36. Snap.includeBasePath = includeBasePath;
  37. Snap.getMenu = function() {
  38. // var menu = EAction.getMenu(Snap.getTitle(), "SnapMenu");
  39. // menu.setProperty("scriptFile", Snap.includeBasePath + "/Snap.js");
  40. // return menu;
  41. };
  42. Snap.getToolBar = function() {
  43. var tb = EAction.getToolBar(Snap.getTitle(), "SnapToolBar");
  44. tb.visible = false;
  45. return tb;
  46. };
  47. Snap.getCadToolBarPanel = function() {
  48. var mtb = EAction.getMainCadToolBarPanel();
  49. var actionName = "SnapToolsPanelAction";
  50. if (!isNull(mtb) && mtb.findChild(actionName)==undefined) {
  51. var action = new RGuiAction(qsTr("Snap Tools"), mtb);
  52. action.setScriptFile(Snap.includeBasePath + "/Snap.js");
  53. action.objectName = actionName;
  54. action.setRequiresDocument(true);
  55. action.setIcon(Snap.includeBasePath + "/Snap.svg");
  56. //action.setStatusTip(qsTr("Show snap tools"));
  57. action.setNoState();
  58. action.setDefaultCommands(["snapmenu"]);
  59. action.setGroupSortOrder(40);
  60. action.setSortOrder(400);
  61. action.setWidgetNames(["!MainToolsPanel"]);
  62. }
  63. var tb = EAction.getCadToolBarPanel(
  64. Snap.getTitle(),
  65. "SnapToolsPanel",
  66. true
  67. );
  68. return tb;
  69. };
  70. Snap.getToolMatrixPanel = function() {
  71. return EAction.getToolMatrixPanel(Snap.getTitle(), "SnapMatrixPanel", 5000);
  72. };
  73. Snap.getTitle = function() {
  74. return qsTr("&Snap");
  75. };
  76. Snap.prototype.getTitle = function() {
  77. return Snap.getTitle();
  78. };
  79. Snap.init = function() {
  80. Snap.getMenu();
  81. Snap.getToolBar();
  82. Snap.getCadToolBarPanel();
  83. Snap.getToolMatrixPanel();
  84. };
  85. Snap.prototype.beginEvent = function() {
  86. if (!this.hasNoState()) {
  87. EAction.prototype.beginEvent.call(this);
  88. return;
  89. }
  90. if (!isNull(this.getGuiAction())) {
  91. this.getGuiAction().setChecked(true);
  92. }
  93. if (!isNull(this.getGuiAction()) && this.getGuiAction().objectName==="SnapToolsPanelAction") {
  94. EAction.showCadToolBarPanel("SnapToolsPanel");
  95. this.terminate();
  96. }
  97. };
  98. Snap.prototype.finishEvent = function() {
  99. if (!this.hasNoState()) {
  100. EAction.prototype.finishEvent.call(this);
  101. return;
  102. }
  103. if (!isNull(this.getGuiAction()) && !isDeleted(this.getGuiAction())) {
  104. if (this.getGuiAction().getGroup().isEmpty()) {
  105. this.getGuiAction().setChecked(false);
  106. }
  107. if (this.getGuiAction().isOverride()) {
  108. this.getGuiAction().setChecked(false);
  109. }
  110. }
  111. };
  112. Snap.prototype.restrict = function(coord) {
  113. var di = this.getDocumentInterface();
  114. if (isNull(di)) {
  115. return coord;
  116. }
  117. var currentSnapRestriction = di.getSnapRestriction();
  118. if (!isNull(currentSnapRestriction)) {
  119. return currentSnapRestriction.restrictSnap(coord, di.getRelativeZero());
  120. }
  121. return coord;
  122. };