Text.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_text Text Drawing Tool
  21. * \ingroup ecma_draw
  22. *
  23. * \brief This module contains the ECMAScript implementation of the text
  24. * drawing tool.
  25. */
  26. include("scripts/Draw/Draw.js");
  27. include("scripts/WidgetFactory.js");
  28. include("TextDialog/TextDialog.js");
  29. /**
  30. * \class Text
  31. * \brief Insert text.
  32. * \ingroup ecma_draw_text
  33. */
  34. function Text(guiAction, mode) {
  35. if (isNull(mode)) {
  36. mode = TextDialog.Mode.Text;
  37. }
  38. Draw.call(this, guiAction);
  39. this.textData = undefined;
  40. this.mode = mode;
  41. this.setUiOptions("Text.ui");
  42. }
  43. Text.prototype = new Draw();
  44. Text.State = {
  45. SettingPosition : 0
  46. };
  47. Text.prototype.beginEvent = function() {
  48. Draw.prototype.beginEvent.call(this);
  49. var dialog = new TextDialog(this.mode);
  50. this.textData = dialog.show();
  51. if (RSettings.getQtVersion() >= 0x060000) {
  52. delete dialog;
  53. }
  54. if (isNull(this.textData)) {
  55. this.terminate();
  56. return;
  57. }
  58. var optionsToolBar = EAction.getOptionsToolBar();
  59. var contentsEdit = optionsToolBar.findChild("Contents");
  60. WidgetFactory.initLineEdit(contentsEdit, false);
  61. contentsEdit.text = this.textData.getEscapedText();
  62. var heightEdit = optionsToolBar.findChild("Height");
  63. heightEdit.setValue(this.textData.getTextHeight());
  64. var angleEdit = optionsToolBar.findChild("Angle");
  65. angleEdit.setValue(this.textData.getAngle());
  66. this.setState(Text.State.SettingPosition);
  67. };
  68. Text.prototype.setState = function(state) {
  69. Draw.prototype.setState.call(this, state);
  70. this.setCrosshairCursor();
  71. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  72. var appWin = RMainWindowQt.getMainWindow();
  73. var tr = qsTr("Position");
  74. this.setLeftMouseTip(tr);
  75. this.setCommandPrompt(tr);
  76. this.setRightMouseTip(EAction.trCancel);
  77. EAction.showSnapTools();
  78. };
  79. Text.prototype.pickCoordinate = function(event, preview) {
  80. var di = this.getDocumentInterface();
  81. var pos = event.getModelPosition();
  82. if (!preview) {
  83. di.setRelativeZero(pos);
  84. }
  85. this.textData.setAlignmentPoint(pos);
  86. if (preview) {
  87. this.updatePreview();
  88. }
  89. else {
  90. this.applyOperation();
  91. }
  92. };
  93. Text.prototype.getOperation = function(preview) {
  94. if (isNull(this.textData)) {
  95. return undefined;
  96. }
  97. var text;
  98. if (isOfType(this.textData, RAttributeDefinitionData)) {
  99. text = new RAttributeDefinitionEntity(this.getDocument(), this.textData);
  100. }
  101. else {
  102. text = new RTextEntity(this.getDocument(), this.textData);
  103. }
  104. return new RAddObjectOperation(text, this.getToolTitle());
  105. };
  106. Text.prototype.slotContentsChanged = function(value) {
  107. if (isNull(this.textData)) {
  108. return;
  109. }
  110. this.textData.setText(value);
  111. this.updatePreview(true);
  112. };
  113. Text.prototype.slotHeightChanged = function(value) {
  114. if (isNull(this.textData)) {
  115. return;
  116. }
  117. this.textData.setTextHeight(value);
  118. this.updatePreview(true);
  119. };
  120. Text.prototype.slotAngleChanged = function(value) {
  121. if (isNull(this.textData)) {
  122. return;
  123. }
  124. this.textData.setAngle(value);
  125. this.updatePreview(true);
  126. };