Line.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_draw_line Line Drawing Tools
  21. * \ingroup ecma_draw
  22. *
  23. * \brief This module contains ECMAScript implementations of various line drawing tools.
  24. */
  25. include("scripts/Draw/Draw.js");
  26. /**
  27. * \class Line
  28. * \brief Base class for all line drawing tools.
  29. * \ingroup ecma_draw_line
  30. */
  31. function Line(guiAction) {
  32. Draw.call(this, guiAction);
  33. this.showAutoLineType = false;
  34. this.lineType = Line.LineType.Line;
  35. }
  36. Line.prototype = new Draw();
  37. Line.includeBasePath = includeBasePath;
  38. Line.LineType = {
  39. Auto : -1,
  40. Line : 0,
  41. XLine : 1,
  42. Ray : 2
  43. };
  44. Line.prototype.beginEvent = function() {
  45. Draw.prototype.beginEvent.call(this);
  46. if (!isNull(this.getGuiAction()) && this.getGuiAction().objectName==="LineToolsPanelAction") {
  47. EAction.showCadToolBarPanel("LineToolsPanel");
  48. this.terminate();
  49. }
  50. };
  51. Line.getMenu = function() {
  52. // var menu = EAction.getSubMenu(
  53. // Draw.getMenu(),
  54. // 20, 200,
  55. // Line.getTitle(),
  56. // "DrawLineMenu",
  57. // Line.includeBasePath + "/Line.svg"
  58. // );
  59. // menu.setProperty("scriptFile", Line.includeBasePath + "/Line.js");
  60. // return menu;
  61. };
  62. Line.getToolBar = function() {
  63. var tb = EAction.getToolBar(Line.getTitle(), "LineToolBar", Qt.TopToolBarArea, Draw.getContextMenuCategory());
  64. tb.visible = false;
  65. return tb;
  66. };
  67. Line.getCadToolBarPanel = function() {
  68. var mtb = Draw.getCadToolBarPanel();
  69. var actionName = "LineToolsPanelAction";
  70. if (!isNull(mtb) && mtb.findChild(actionName)==undefined) {
  71. var action = new RGuiAction(qsTr("Line Tools"), mtb);
  72. action.setScriptFile(Line.includeBasePath + "/Line.js");
  73. action.objectName = actionName;
  74. action.setRequiresDocument(true);
  75. action.setIcon(Line.includeBasePath + "/Line.svg");
  76. //action.setStatusTip(qsTr("Show line tools"));
  77. action.setDefaultShortcut(new QKeySequence("w,l"));
  78. action.setNoState();
  79. action.setDefaultCommands(["linemenu"]);
  80. action.setGroupSortOrder(20);
  81. action.setSortOrder(200);
  82. action.setWidgetNames(["MainToolsPanel"]);
  83. }
  84. var tb = EAction.getCadToolBarPanel(Line.getTitle(), "LineToolsPanel", true);
  85. return tb;
  86. };
  87. Line.getToolMatrixPanel = function() {
  88. return EAction.getToolMatrixPanel(Line.getTitle(), "LineMatrixPanel", 200);
  89. };
  90. Line.getTitle = function() {
  91. return qsTr("&Line");
  92. };
  93. Line.prototype.getTitle = function() {
  94. return Line.getTitle();
  95. };
  96. Line.getLineType = function(shape) {
  97. if (isLineShape(shape)) {
  98. return Line.LineType.Line;
  99. }
  100. if (isRayShape(shape)) {
  101. return Line.LineType.Ray;
  102. }
  103. if (isXLineShape(shape)) {
  104. return Line.LineType.XLine;
  105. }
  106. return undefined;
  107. };
  108. Line.createLineEntity = function(doc, p1, p2, lineType) {
  109. switch (lineType) {
  110. default:
  111. case Line.LineType.Line:
  112. return new RLineEntity(doc, new RLineData(p1, p2));
  113. case Line.LineType.XLine:
  114. return new RXLineEntity(doc, new RXLineData(p1, p2.operator_subtract(p1)));
  115. case Line.LineType.Ray:
  116. return new RRayEntity(doc, new RRayData(p1, p2.operator_subtract(p1)));
  117. }
  118. };
  119. Line.prototype.createLineEntity = function(doc, p1, p2) {
  120. return Line.createLineEntity(doc, p1, p2, this.lineType);
  121. };
  122. Line.prototype.initUiOptions = function(resume, optionsToolBar) {
  123. Draw.prototype.initUiOptions.call(this, resume, optionsToolBar);
  124. this.lineType = RSettings.getIntValue(this.settingsGroup + "/Type",
  125. this.showAutoLineType ? Line.LineType.Auto : Line.LineType.Line);
  126. var wn = "";
  127. switch (this.lineType) {
  128. case Line.LineType.Auto:
  129. wn = "TypeAuto";
  130. break;
  131. case Line.LineType.Line:
  132. wn = "TypeSegment";
  133. break;
  134. case Line.LineType.XLine:
  135. wn = "TypeXLine";
  136. break;
  137. case Line.LineType.Ray:
  138. wn = "TypeRay";
  139. break;
  140. }
  141. var w = optionsToolBar.findChild("TypeAutoAction");
  142. if (!isNull(w) && !this.showAutoLineType) {
  143. w.visible = false;
  144. if (wn==="TypeAuto") {
  145. wn = "TypeSegment";
  146. }
  147. }
  148. w = optionsToolBar.findChild(wn);
  149. if (!isNull(w)) {
  150. w.checked = true;
  151. }
  152. };
  153. Line.prototype.hideUiOptions = function(saveToSettings) {
  154. Draw.prototype.hideUiOptions.call(this, saveToSettings);
  155. RSettings.setValue(this.settingsGroup + "/Type", this.lineType);
  156. };
  157. /**
  158. * Implemented by line tool classes to be notified when type changed.
  159. */
  160. Line.prototype.typeChanged = function() {
  161. };
  162. Line.prototype.slotTypeAutoChanged = function(checked) {
  163. Line.slotTypeAutoChanged(this, checked);
  164. };
  165. Line.slotTypeAutoChanged = function(obj, checked) {
  166. if (checked) {
  167. obj.lineType = Line.LineType.Auto;
  168. obj.updatePreview(true);
  169. if (isFunction(obj.typeChanged)) {
  170. obj.typeChanged();
  171. }
  172. }
  173. };
  174. Line.prototype.slotTypeSegmentChanged = function(checked) {
  175. Line.slotTypeSegmentChanged(this, checked);
  176. };
  177. Line.slotTypeSegmentChanged = function(obj, checked) {
  178. if (checked) {
  179. obj.lineType = Line.LineType.Line;
  180. obj.updatePreview(true);
  181. if (isFunction(obj.typeChanged)) {
  182. obj.typeChanged();
  183. }
  184. }
  185. };
  186. Line.prototype.slotTypeRayChanged = function(checked) {
  187. Line.slotTypeRayChanged(this, checked);
  188. };
  189. Line.slotTypeRayChanged = function(obj, checked) {
  190. if (checked) {
  191. obj.lineType = Line.LineType.Ray;
  192. obj.updatePreview(true);
  193. if (isFunction(obj.typeChanged)) {
  194. obj.typeChanged();
  195. }
  196. }
  197. };
  198. Line.prototype.slotTypeXLineChanged = function(checked) {
  199. Line.slotTypeXLineChanged(this, checked);
  200. };
  201. Line.slotTypeXLineChanged = function(obj, checked) {
  202. if (checked) {
  203. obj.lineType = Line.LineType.XLine;
  204. obj.updatePreview(true);
  205. if (isFunction(obj.typeChanged)) {
  206. obj.typeChanged();
  207. }
  208. }
  209. };
  210. Line.prototype.isRayOrXLine = function() {
  211. return this.lineType===Line.LineType.Ray || this.lineType===Line.LineType.XLine;
  212. };
  213. Line.init = function() {
  214. Line.getMenu();
  215. Line.getToolBar();
  216. Line.getCadToolBarPanel();
  217. Line.getToolMatrixPanel();
  218. };