BlockDialog.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 BlockDialog
  22. * \ingroup ecma_block
  23. *
  24. * Creates a dialog to edit the attributes of a block (currently name only).
  25. *
  26. * \param document RDocument the block is in.
  27. * \param block RBlock object to edit or undefined to create and return a new block.
  28. */
  29. function BlockDialog(document, block, allowOverwrite, allowSameName) {
  30. if (isNull(allowOverwrite)) {
  31. allowOverwrite = true;
  32. }
  33. if (isNull(allowSameName)) {
  34. allowSameName = false;
  35. }
  36. this.dialog = null;
  37. this.document = document;
  38. this.block = block;
  39. this.allowOverwrite = allowOverwrite;
  40. this.allowSameName = allowSameName;
  41. }
  42. BlockDialog.includeBasePath = includeBasePath;
  43. BlockDialog.prototype.show = function() {
  44. var lt;
  45. this.dialog = WidgetFactory.createDialog(BlockDialog.includeBasePath, "BlockDialog.ui", EAction.getMainWindow());
  46. var widgets = getWidgets(this.dialog);
  47. var leBlockName = widgets["BlockName"];
  48. var rx = new RegExp("[^<>/\\\\\":;\?\*|,=`]{1,255}");
  49. this.validator = new QRegExpValidator(rx, leBlockName);
  50. leBlockName.setValidator(this.validator);
  51. leBlockName.textChanged.connect(this, this.validate);
  52. var creatingBlock = isNull(this.block);
  53. if (!creatingBlock) {
  54. // user is editing an existing block:
  55. // init dialog to show attributes of given block:
  56. leBlockName.text = this.block.getName();
  57. if (leBlockName.text.startsWith("*")) {
  58. leBlockName.enabled = false;
  59. }
  60. if (!this.allowSameName) {
  61. // creating copy of block:
  62. // select name:
  63. leBlockName.selectAll();
  64. }
  65. }
  66. this.dialog.show();
  67. if (creatingBlock) {
  68. var c = 0;
  69. while (!this.validate()) {
  70. ++c;
  71. leBlockName.text = qsTr("block", "default block name prefix") + " " + c;
  72. }
  73. leBlockName.selectAll();
  74. }
  75. if (!this.dialog.exec()) {
  76. destr(this.dialog);
  77. EAction.activateMainWindow();
  78. return undefined;
  79. }
  80. var text = leBlockName.text.trim();
  81. if (!creatingBlock) {
  82. // rename block:
  83. destr(this.dialog);
  84. EAction.activateMainWindow();
  85. this.block.setName(text);
  86. return this.block;
  87. }
  88. else {
  89. // create new block:
  90. var block = new RBlock(this.document, text, new RVector(0,0));
  91. destr(this.dialog);
  92. EAction.activateMainWindow();
  93. return block;
  94. }
  95. };
  96. /**
  97. * Block name validation.
  98. */
  99. BlockDialog.prototype.validate = function() {
  100. var widgets = getWidgets(this.dialog);
  101. var leBlockName = widgets["BlockName"];
  102. return BlockDialog.validate(this.block, leBlockName.text, this.document, this.dialog, this.validator, this.allowOverwrite, this.allowSameName);
  103. };
  104. BlockDialog.validate = function(block, blockName, document, dialog, validator, allowOverwrite, allowSameName) {
  105. var creatingBlock = isNull(block);
  106. var widgets = getWidgets(dialog);
  107. var leBlockName = widgets["BlockName"];
  108. if (isNull(leBlockName)) {
  109. return false;
  110. }
  111. var message = widgets["Message"];
  112. var pos = 0;
  113. var acceptable = true;
  114. message.clear();
  115. if (leBlockName.text!==leBlockName.text.trim()) {
  116. message.text = "<font color='red'>" + qsTr("Leading or trailing spaces.") + "</font>";
  117. acceptable = false;
  118. }
  119. if (leBlockName.text.isEmpty()) {
  120. message.text = "<font color='red'>" + qsTr("Block name is empty.") + "</font>";
  121. acceptable = false;
  122. }
  123. else {
  124. if (validator.validate(leBlockName.text, pos) != QValidator.Acceptable) {
  125. message.text = "<font color='red'>" + qsTr("Block name is invalid.") + "</font>";
  126. acceptable = false;
  127. }
  128. }
  129. var ret = acceptable;
  130. // block already exists:
  131. if (document.hasBlock(leBlockName.text)) {
  132. if (creatingBlock && allowOverwrite===true) {
  133. // warning: overwriting an existing block:
  134. message.text += "<font color='red'>" + qsTr("Block \"%1\" already exists<br>and will be overwritten.").arg(leBlockName.text.toString()) + "</font>";
  135. acceptable = true;
  136. }
  137. else if ((isNull(block) || block.getName().toLowerCase() !== leBlockName.text.toLowerCase()) && !allowSameName) {
  138. // error: renaming existing block to existing block name (not allowed):
  139. message.text = "<font color='red'>" + qsTr("Block already exists.") + "</font>";
  140. acceptable = false;
  141. }
  142. ret = false;
  143. }
  144. if (!isNull(widgets["ButtonBox"])) {
  145. widgets["ButtonBox"].button(QDialogButtonBox.Ok).enabled = acceptable;
  146. }
  147. return ret;
  148. };