HatchDialog.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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("../../WidgetFactory.js");
  20. /**
  21. * \class HatchDialog
  22. * \brief Hatch creation or editing dialog.
  23. * \ingroup ecma_draw_hatch
  24. */
  25. function HatchDialog() {
  26. this.dialog = undefined;
  27. }
  28. HatchDialog.basePath = includeBasePath;
  29. HatchDialog.prototype.show = function(hatchDataIn) {
  30. var doc = EAction.getDocument();
  31. if (isNull(doc)) {
  32. return;
  33. }
  34. this.dialog = WidgetFactory.createDialog(HatchDialog.basePath, "HatchDialog.ui", EAction.getMainWindow());
  35. var radioSolid = this.dialog.findChild("SolidFill");
  36. var radioHatch = this.dialog.findChild("HatchPattern");
  37. var comboPattern = this.dialog.findChild("Pattern");
  38. var editAngle = this.dialog.findChild("Angle");
  39. var editScale = this.dialog.findChild("Scale");
  40. var groupPattern = this.dialog.findChild("PatternGroup");
  41. if (radioSolid.checked) {
  42. groupPattern.enabled = false;
  43. }
  44. editAngle.valueChanged.connect(this, this.patternChanged);
  45. radioSolid.toggled.connect(this, this.patternChanged);
  46. //comboPattern["activated(QString)"].connect(this, "patternChanged");
  47. comboPattern["currentIndexChanged(int)"].connect(this, this.patternChanged);
  48. var previewDoc = new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
  49. //previewDoc.setMeasurement(doc.getMeasurement());
  50. this.previewDi = new RDocumentInterface(previewDoc);
  51. this.previewDi.setNotifyListeners(false);
  52. var layout = this.dialog.findChild("PreviewLayout");
  53. // keep this include here to make sure command line tools
  54. // can use Hatch.js without requiring a GUI:
  55. include("scripts/Widgets/AutoZoomView/AutoZoomView.js");
  56. var view = new AutoZoomView(this.dialog);
  57. view.objectName = "PreviewView";
  58. view.setPaintOrigin(false);
  59. layout.addWidget(view, 0, 0);
  60. view.setScene(new RGraphicsSceneQt(this.previewDi));
  61. view.disableGestures();
  62. view.setMargin(10);
  63. var patternNames;
  64. if (RUnit.isMetric(EAction.getDocument().getUnit())) {
  65. patternNames = RPatternListMetric.getNames();
  66. }
  67. else {
  68. patternNames = RPatternListImperial.getNames();
  69. }
  70. for (var i=0; i<patternNames.length; i++) {
  71. var patternName = patternNames[i];
  72. comboPattern.addItem(patternName);
  73. }
  74. if (!isNull(hatchDataIn)) {
  75. // initialize dialog from given hatch data (hatchDataIn):
  76. var rbSolidFill = this.dialog.findChild("SolidFill");
  77. var rbHatchPattern = this.dialog.findChild("HatchPattern");
  78. if (hatchDataIn.isSolid()) {
  79. rbSolidFill.checked = true;
  80. }
  81. else {
  82. rbHatchPattern.checked = true;
  83. }
  84. var cbPattern = this.dialog.findChild("Pattern");
  85. var idx = cbPattern.findText(hatchDataIn.getPatternName(), Qt.MatchFixedString);
  86. // if (idx===-1) {
  87. // cbPattern.currentIndex = 0;
  88. // }
  89. // else {
  90. cbPattern.currentIndex = idx;
  91. // }
  92. var leAngle = this.dialog.findChild("Angle");
  93. leAngle.setValue(hatchDataIn.getAngle());
  94. var leScale = this.dialog.findChild("Scale");
  95. leScale.setValue(hatchDataIn.getScale());
  96. }
  97. else {
  98. // initialize dialog from previous values (ini file):
  99. WidgetFactory.restoreState(this.dialog);
  100. }
  101. this.dialog.show();
  102. this.patternChanged();
  103. var res = this.dialog.exec();
  104. destr(this.previewDi);
  105. if (!res) {
  106. destr(this.dialog);
  107. EAction.activateMainWindow();
  108. return undefined;
  109. }
  110. WidgetFactory.saveState(this.dialog);
  111. // analyze dialog input:
  112. var hatchDataOut = isNull(hatchDataIn) ? new RHatchData() : hatchDataIn;
  113. hatchDataOut.setDocument(EAction.getDocument());
  114. hatchDataOut.setAngle(editAngle.getValue());
  115. hatchDataOut.setScale(editScale.getValue());
  116. hatchDataOut.setSolid(radioSolid.checked);
  117. hatchDataOut.setPatternName(radioSolid.checked ? "SOLID" : comboPattern.currentText);
  118. destr(this.dialog);
  119. EAction.activateMainWindow();
  120. return hatchDataOut;
  121. };
  122. HatchDialog.prototype.patternChanged = function() {
  123. this.previewDi.clear();
  124. var previewDoc = this.previewDi.getDocument();
  125. var metric = EAction.getDocument().isMetric();
  126. var radioSolid = this.dialog.findChild("SolidFill");
  127. var comboPattern = this.dialog.findChild("Pattern");
  128. var editAngle = this.dialog.findChild("Angle");
  129. var editScale = this.dialog.findChild("Scale");
  130. var patternName = comboPattern.currentText;
  131. var angle = editAngle.getValue();
  132. if (isNaN(angle)) {
  133. angle = 0.0;
  134. }
  135. var scale = editScale.getValue();
  136. if (isNaN(scale)) {
  137. scale = 1.0;
  138. }
  139. // size of preview square:
  140. var size = 30.0;
  141. if (radioSolid.checked) {
  142. //size = 30.0;
  143. patternName = "SOLID";
  144. }
  145. else {
  146. var pattern;
  147. if (metric) {
  148. previewDoc.setUnit(RS.Millimeter);
  149. previewDoc.setMeasurement(RS.Metric);
  150. pattern = RPatternListMetric.get(patternName);
  151. if (patternName.startsWith("AR-") /*|| patternName.startsWith("ACAD_")*/) {
  152. size = 900.0;
  153. }
  154. }
  155. else {
  156. size = 2;
  157. previewDoc.setUnit(RS.Inch);
  158. previewDoc.setMeasurement(RS.Imperial);
  159. pattern = RPatternListImperial.get(patternName);
  160. if (patternName.startsWith("ACAD_")) {
  161. size = 30.0;
  162. }
  163. if (patternName.startsWith("AR-") /*|| patternName.startsWith("ACAD_")*/) {
  164. size = 30.0;
  165. }
  166. }
  167. if (isNull(pattern)) {
  168. return;
  169. }
  170. }
  171. var data = new RHatchData(radioSolid.checked, 1.0, angle, patternName);
  172. data.setDocument(previewDoc);
  173. data.setLineweight(RLineweight.Weight015);
  174. var bgColor = RSettings.getColor("GraphicsViewColors/BackgroundColor", new RColor("black"));
  175. if (bgColor.lightness()>200) {
  176. data.setColor(new RColor("black"));
  177. }
  178. else {
  179. data.setColor(new RColor("white"));
  180. }
  181. data.newLoop();
  182. var lines = [
  183. new RLine(new RVector(0,0), new RVector(size,0)),
  184. new RLine(new RVector(size,0), new RVector(size,size)),
  185. new RLine(new RVector(size,size), new RVector(0,size)),
  186. new RLine(new RVector(0,size), new RVector(0,0)),
  187. ];
  188. for (var i=0; i<lines.length; i++) {
  189. if (RSettings.getQtVersion() >= 0x060000) {
  190. data.addBoundary(lines[i].clone());
  191. }
  192. else {
  193. data.addBoundary(lines[i]);
  194. }
  195. }
  196. var hatch = new RHatchEntity(previewDoc, data);
  197. var op = new RAddObjectOperation(hatch, false, false);
  198. this.previewDi.applyOperation(op);
  199. this.previewDi.autoZoom();
  200. };