EditText.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * \ingroup ecma_modify
  21. *
  22. * \brief This module contains the ECMAScript implementation of the text
  23. * editing tool.
  24. */
  25. include("scripts/Modify/Modify.js");
  26. include("scripts/Draw/Text/TextDialog/TextDialog.js");
  27. /**
  28. * \class EditText
  29. * \brief Edit text.
  30. * \ingroup ecma_modify
  31. */
  32. function EditText(guiAction) {
  33. Modify.call(this, guiAction);
  34. this.entity = undefined;
  35. }
  36. EditText.prototype = new Modify();
  37. EditText.State = {
  38. ChoosingEntity : 0
  39. };
  40. EditText.prototype.beginEvent = function() {
  41. Modify.prototype.beginEvent.call(this);
  42. this.setState(EditText.State.ChoosingEntity);
  43. };
  44. EditText.prototype.setState = function(state) {
  45. Modify.prototype.setState.call(this, state);
  46. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  47. this.setCrosshairCursor();
  48. var appWin = RMainWindowQt.getMainWindow();
  49. switch (this.state) {
  50. case EditText.State.ChoosingEntity:
  51. var tr = qsTr("Choose text")
  52. this.setLeftMouseTip(tr);
  53. this.setCommandPrompt(tr);
  54. break;
  55. }
  56. this.setRightMouseTip(EAction.trCancel);
  57. };
  58. EditText.prototype.pickEntity = function(event, preview) {
  59. var doc = this.getDocument();
  60. var entityId = this.getEntityId(event, preview);
  61. var entity = doc.queryEntity(entityId);
  62. if (isNull(entity)) {
  63. this.entity = undefined;
  64. this.updatePreview();
  65. return;
  66. }
  67. switch (this.state) {
  68. case EditText.State.ChoosingEntity:
  69. if (isTextEntity(entity) ||
  70. isAttributeEntity(entity) ||
  71. isAttributeDefinitionEntity(entity)) {
  72. this.entity = entity;
  73. }
  74. if (preview) {
  75. this.updatePreview();
  76. }
  77. else {
  78. EditText.editText(entity);
  79. }
  80. break;
  81. }
  82. };
  83. EditText.prototype.getHighlightedEntities = function() {
  84. var ret = [];
  85. if (isEntity(this.entity)) {
  86. ret.push(this.entity.getId());
  87. }
  88. return ret;
  89. };
  90. /**
  91. * This static method may also be accessed by other tools to edit the
  92. * given text entity in a text dialog.
  93. */
  94. EditText.editText = function(entity) {
  95. if (!isTextEntity(entity) &&
  96. !isAttributeEntity(entity) &&
  97. !isAttributeDefinitionEntity(entity)) {
  98. return;
  99. }
  100. var dialog = new TextDialog();
  101. var textData = dialog.show(entity.getData());
  102. if (isNull(textData)) {
  103. // dialog canceled:
  104. return;
  105. }
  106. var newEntity = entity.clone();
  107. newEntity.setData(textData);
  108. var op = new RModifyObjectOperation(newEntity);
  109. if (!isNull(op)) {
  110. var di = EAction.getDocumentInterface();
  111. di.applyOperation(op);
  112. di.selectEntity(entity.getId());
  113. }
  114. };