LayerDialog.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 LayerDialog
  22. * \ingroup ecma_layer
  23. *
  24. * Creates a dialog to edit the attributes of a layer.
  25. *
  26. * \param document RDocument the layer is in.
  27. * \param layer RLayer object to edit or undefined to show a dialog for a new layer.
  28. */
  29. function LayerDialog(documentInterface, layer) {
  30. this.dialog = null;
  31. this.documentInterface = documentInterface;
  32. this.document = undefined;
  33. if (!isNull(this.documentInterface)) {
  34. this.document = documentInterface.getDocument();
  35. }
  36. this.layer = layer;
  37. this.prefix = undefined;
  38. this.defaultName = qsTr("layer", "default layer name prefix") + " %1";
  39. this.defaultColor = RSettings.getColor("Layer/DefaultColor", new RColor("black"));
  40. }
  41. /**
  42. * Hook for derived classes to do additional initialization.
  43. */
  44. LayerDialog.prototype.initDialog = function(dialog, layer) {
  45. };
  46. /**
  47. * Hook for derived classes to store additional information back to layer.
  48. */
  49. LayerDialog.prototype.initLayer = function(dialog, layer) {
  50. };
  51. /**
  52. * Hook for derived classes to return complete layer name.
  53. */
  54. LayerDialog.prototype.getLayerName = function(dialog) {
  55. var leLayerName = dialog.findChild("LayerName");
  56. if (!isNull(this.prefix)) {
  57. return this.prefix + leLayerName.text;
  58. }
  59. return leLayerName.text;
  60. };
  61. LayerDialog.prototype.show = function() {
  62. this.dialog = WidgetFactory.createDialog("scripts/Layer", "LayerDialog.ui");
  63. var widgets = getWidgets(this.dialog);
  64. var leLayerName = widgets["LayerName"];
  65. var rx = new RegExp("[^<>/\\\\\":;\?\*|,=`]{1,255}");
  66. if (RSettings.getQtVersion()>=0x060000) {
  67. this.validator = new QRegularExpressionValidator(rx, leLayerName);
  68. }
  69. else {
  70. this.validator = new QRegExpValidator(rx, leLayerName);
  71. }
  72. leLayerName.setValidator(this.validator);
  73. var cbColor = widgets["Color"];
  74. cbColor.setColor(this.defaultColor);
  75. var cbLineweight = widgets["Lineweight"];
  76. cbLineweight.setLineweight(RSettings.getIntValue("Layer/DefaultLineweight", RLineweight.Weight025));
  77. var cbLinetype = widgets["Linetype"];
  78. cbLinetype.init(this.document);
  79. // init from existing layer:
  80. if (!isNull(this.layer)) {
  81. leLayerName.text = this.layer.getName();
  82. if (leLayerName.text === "0") {
  83. leLayerName.enabled = false;
  84. }
  85. else {
  86. // only allow user to edit part after prefix:
  87. if (!isNull(this.prefix)) {
  88. if (leLayerName.text.startsWith(this.prefix)) {
  89. leLayerName.text = leLayerName.text.substring(this.prefix.length);
  90. }
  91. }
  92. }
  93. // TODO: undeletable layers
  94. cbColor.setColor(this.layer.getColor());
  95. cbLineweight.setLineweight(this.layer.getLineweight());
  96. var ltP = this.document.queryLinetype(this.layer.getLinetypeId());
  97. if (!isNull(ltP)) {
  98. //lt = ltP.data();
  99. cbLinetype.setLinetypePattern(ltP.getPattern());
  100. }
  101. }
  102. // show prefix as part of the layer name label:
  103. var lLayerName = widgets["LayerNameLabel"];
  104. if (!isNull(this.prefix)) {
  105. lLayerName.text = lLayerName.text + " " + this.prefix;
  106. }
  107. leLayerName.textChanged.connect(this, this.validate);
  108. this.initDialog(this.dialog, this.layer);
  109. this.dialog.show();
  110. if (isNull(this.layer)) {
  111. var c = 1;
  112. do {
  113. leLayerName.text = this.defaultName.arg(c);
  114. ++c;
  115. } while (!this.validate());
  116. }
  117. else {
  118. this.validate();
  119. }
  120. leLayerName.selectAll();
  121. if (!this.dialog.exec()) {
  122. destr(this.dialog);
  123. EAction.activateMainWindow();
  124. return undefined;
  125. }
  126. var text = leLayerName.text.trim();
  127. if (!isNull(this.prefix)) {
  128. text = this.prefix + text;
  129. }
  130. var clr = cbColor.getColor();
  131. var lw = cbLineweight.getLineweight();
  132. var lt = cbLinetype.getLinetypePattern();
  133. var ltId = this.document.getLinetypeId(lt.getName());
  134. if (!isNull(this.layer)) {
  135. this.layer.setName(text);
  136. this.layer.setColor(clr);
  137. this.layer.setLinetypeId(ltId);
  138. this.layer.setLineweight(lw);
  139. this.initLayer(this.dialog, this.layer);
  140. return this.layer;
  141. }
  142. var layer = new RLayer(this.document, text, false, false, clr, ltId, lw);
  143. this.initLayer(this.dialog, layer);
  144. destr(this.dialog);
  145. EAction.activateMainWindow();
  146. return layer;
  147. };
  148. /**
  149. * Layer name validation.
  150. */
  151. LayerDialog.prototype.validate = function() {
  152. var layerName = this.getLayerName(this.dialog);
  153. return LayerDialog.validate(this.layer, layerName, this.document, this.dialog, this.validator);
  154. };
  155. LayerDialog.validate = function(layer, layerName, document, dialog, validator) {
  156. var widgets = getWidgets(dialog);
  157. var leLayerName = widgets["LayerName"];
  158. if (isNull(leLayerName)) {
  159. return false;
  160. }
  161. var pos = 0;
  162. var acceptable = true;
  163. var lMessage = widgets["Message"];
  164. if (!isNull(lMessage)) {
  165. lMessage.clear();
  166. }
  167. if (layerName!==layerName.trim()) {
  168. if (!isNull(lMessage)) {
  169. lMessage.text = "<font color='red'>" + qsTr("Leading or trailing spaces.") + "</font>";
  170. }
  171. acceptable = false;
  172. }
  173. if (validator.validate(layerName, pos) != QValidator.Acceptable) {
  174. if (!isNull(lMessage)) {
  175. lMessage.text = "<font color='red'>" + qsTr("Layer name is empty.") + "</font>";
  176. }
  177. acceptable = false;
  178. }
  179. // layer exists: check if name exists:
  180. if (document.hasLayer(layerName)) {
  181. // error if we're creating a new layer and the layer name is taken or
  182. // we're trying to rename a layer to an existing name
  183. if (isNull(layer) ||
  184. layer.getName().toLowerCase() !== layerName.toLowerCase()) {
  185. if (!isNull(lMessage)) {
  186. lMessage.text = "<font color='red'>" + qsTr("Layer already exists.") + "</font>";
  187. }
  188. acceptable = false;
  189. }
  190. }
  191. if (!isNull(widgets["ButtonBox"])) {
  192. widgets["ButtonBox"].button(QDialogButtonBox.Ok).enabled = acceptable;
  193. }
  194. return acceptable;
  195. };