Counter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright (c) 2015 by Robert S. All rights reserved.
  3. * 2020 by Andrew Mustun (refactor, add number format instead of precision)
  4. *
  5. * This file is part of the QCAD project.
  6. *
  7. * QCAD is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * QCAD is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with QCAD.
  19. */
  20. /**
  21. * \class Counter
  22. * \ingroup ecma_misc_draw
  23. *
  24. * \brief Implementation of the auto increment number text tool.
  25. */
  26. include("scripts/EAction.js");
  27. include("numeral.js");
  28. /**
  29. * \class Counter
  30. * \brief Auto increment numbers.
  31. * \ingroup ecma_misc_miscdraw
  32. */
  33. function Counter(guiAction) {
  34. EAction.call(this, guiAction);
  35. this.position = undefined;
  36. this.start = 1;
  37. this.increment = 1;
  38. this.numberFormat = "0";
  39. this.prefix = "";
  40. this.suffix = "";
  41. this.setUiOptions("Counter.ui");
  42. }
  43. Counter.prototype = new EAction();
  44. Counter.State = {
  45. SettingPosition : 0
  46. };
  47. Counter.prototype.beginEvent = function() {
  48. EAction.prototype.beginEvent.call(this);
  49. this.setState(Counter.State.SettingPosition);
  50. };
  51. Counter.prototype.setState = function(state) {
  52. EAction.prototype.setState.call(this, state);
  53. this.setCrosshairCursor();
  54. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  55. var appWin = RMainWindowQt.getMainWindow();
  56. var trPosition = qsTr("Position");
  57. this.setCommandPrompt(trPosition);
  58. this.setLeftMouseTip(trPosition);
  59. this.setRightMouseTip(EAction.trCancel);
  60. EAction.showSnapTools();
  61. };
  62. Counter.prototype.pickCoordinate = function(event, preview) {
  63. var di = this.getDocumentInterface();
  64. this.position = event.getModelPosition();
  65. var op = this.getOperation();
  66. if (preview) {
  67. di.previewOperation(op);
  68. }
  69. else {
  70. di.applyOperation(op);
  71. di.setRelativeZero(this.position);
  72. // set start number to start number + increment
  73. this.start = this.start + this.increment;
  74. var optionsToolBar = EAction.getOptionsToolBar();
  75. if (!isNull(optionsToolBar)) {
  76. var leStart = optionsToolBar.findChild("Start");
  77. leStart.text = (this.start).toFixed(4);
  78. }
  79. }
  80. };
  81. Counter.prototype.getOperation = function() {
  82. if (!isVector(this.position)) {
  83. return undefined;
  84. }
  85. var di = this.getDocumentInterface();
  86. var doc = di.getDocument();
  87. var pos = this.position;
  88. var str;
  89. if (this.numberFormat.length>0) {
  90. var num = numeral(this.start);
  91. str = num.format(this.numberFormat);
  92. }
  93. else {
  94. str = this.start.toFixed(0);
  95. }
  96. str = this.prefix + str + this.suffix;
  97. var fontName = RSettings.getStringValue("TextDialog/Font", "Arial");
  98. var fontHeight = RSettings.getDoubleValue("TextDialog/Height", 1.0);
  99. var fontWidth = RSettings.getDoubleValue("TextDialog/Width", 0.0);
  100. var fontValign = RSettings.getIntValue("TextDialog/VAlignment", 0);
  101. var fontHalign = RSettings.getIntValue("TextDialog/HAlignment", 0);
  102. var fontAngle = RSettings.getDoubleValue("TextDialog/Angle", 0.0);
  103. var fontBold = RSettings.getBoolValue("TextDialog/Bold", false);
  104. var fontItalic = RSettings.getBoolValue("TextDialog/Italic", false);
  105. var fontLineSpacingFactor = RSettings.getDoubleValue("TextDialog/LineSpacingFactor", 1.0);
  106. var td = new RTextData();
  107. td.setAlignmentPoint(pos);
  108. td.setVAlign(fontValign);
  109. td.setHAlign(fontHalign);
  110. td.setText(str);
  111. td.setTextHeight(fontHeight);
  112. td.setTextWidth(fontWidth);
  113. td.setFontName(fontName);
  114. td.setAngle(deg2rad(fontAngle));
  115. td.setBold(fontBold);
  116. td.setItalic(fontItalic);
  117. td.setLineSpacingFactor(fontLineSpacingFactor);
  118. var text = new RTextEntity(doc, td);
  119. var op = new RAddObjectOperation(text, this.getToolTitle());
  120. return op;
  121. };
  122. Counter.prototype.slotStartChanged = function(value) {
  123. this.start = value;
  124. this.updatePreview(true);
  125. };
  126. Counter.prototype.slotIncrementChanged = function(value) {
  127. this.increment = value;
  128. };
  129. Counter.prototype.slotNumberFormatChanged = function(value) {
  130. this.numberFormat = value;
  131. this.updatePreview(true);
  132. };
  133. Counter.prototype.slotPrefixChanged = function(value) {
  134. this.prefix = value;
  135. this.updatePreview(true);
  136. };
  137. Counter.prototype.slotSuffixChanged = function(value) {
  138. this.suffix = value;
  139. this.updatePreview(true);
  140. };