TextAlignedLeader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. include("scripts/EAction.js");
  2. include("scripts/library.js");
  3. function TextAlignedLeader(guiAction) {
  4. EAction.call(this, guiAction);
  5. this.doc = undefined;
  6. this.di = undefined;
  7. this.op = undefined;
  8. this.firstVertex = undefined;
  9. this.entityId = undefined;
  10. };
  11. TextAlignedLeader.prototype = new EAction();
  12. TextAlignedLeader.State = {
  13. SettingFirstVertex: 0,
  14. ChoosingEntity: 1
  15. };
  16. // Handler for event beginEvent
  17. TextAlignedLeader.prototype.beginEvent = function () {
  18. EAction.prototype.beginEvent.call(this);
  19. this.doc = this.getDocument();
  20. this.di = this.getDocumentInterface();
  21. this.op = new RAddObjectsOperation();
  22. this.setState(TextAlignedLeader.State.SettingFirstVertex);
  23. };
  24. // Setter for peoperty State
  25. TextAlignedLeader.prototype.setState = function (state) {
  26. EAction.prototype.setState.call(this, state);
  27. this.setCrosshairCursor();
  28. var appWin = RMainWindowQt.getMainWindow();
  29. switch (this.state) {
  30. case TextAlignedLeader.State.SettingFirstVertex:
  31. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  32. var trFirstVertex = qsTr("Arrow position");
  33. this.setCommandPrompt(trFirstVertex);
  34. this.setLeftMouseTip(trFirstVertex);
  35. this.setRightMouseTip(EAction.trCancel);
  36. EAction.showSnapTools();
  37. break;
  38. case TextAlignedLeader.State.ChoosingEntity:
  39. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  40. var trSelectText = qsTr("Choose text");
  41. this.setCommandPrompt(trSelectText);
  42. this.setLeftMouseTip(trSelectText);
  43. this.setRightMouseTip(EAction.trCancel);
  44. break;
  45. };
  46. };
  47. // Handler for event pickCoordinate
  48. TextAlignedLeader.prototype.pickCoordinate = function (event, preview) {
  49. switch (this.state) {
  50. case TextAlignedLeader.State.SettingFirstVertex:
  51. if (!preview) {
  52. this.firstVertex = event.getModelPosition();
  53. if (!isNull(this.firstVertex)) {
  54. this.setState(TextAlignedLeader.State.ChoosingEntity);
  55. }
  56. }
  57. break;
  58. };
  59. };
  60. // Handler for event pickEntity
  61. TextAlignedLeader.prototype.pickEntity = function (event, preview) {
  62. this.entityId = this.getEntityId(event, preview);
  63. var pos = event.getModelPosition();
  64. switch (this.state) {
  65. case TextAlignedLeader.State.ChoosingEntity:
  66. if (preview) {
  67. this.updatePreview();
  68. }
  69. else {
  70. this.drawLeader(this.entityId);
  71. }
  72. break;
  73. };
  74. };
  75. TextAlignedLeader.prototype.getHighlightedEntities = function() {
  76. var ret = [];
  77. if (isEntity(this.doc.queryEntity(this.entityId)) && this.verifyEntity() == "ОК") {
  78. ret.push(this.entityId);
  79. }
  80. return ret;
  81. };
  82. TextAlignedLeader.prototype.verifyEntity = function () {
  83. if (this.entityId != -1 && isTextEntity(this.doc.queryEntity(this.entityId))) {
  84. return "ОК";
  85. }
  86. return "Choose text";
  87. };
  88. TextAlignedLeader.prototype.setLeftLeader = function (leader, vectorArr) {
  89. leader.appendVertex(this.firstVertex);
  90. leader.appendVertex(vectorArr[0]);
  91. leader.appendVertex(vectorArr[1]);
  92. return leader;
  93. };
  94. TextAlignedLeader.prototype.setRightLeader = function (leader, vectorArr) {
  95. leader.appendVertex(this.firstVertex);
  96. leader.appendVertex(vectorArr[1]);
  97. leader.appendVertex(vectorArr[0]);
  98. return leader;
  99. };
  100. TextAlignedLeader.prototype.drawLeader = function (entityId) {
  101. // checking the validity of an entity
  102. var msg = this.verifyEntity();
  103. if (msg != "ОК") {
  104. EAction.handleUserWarning(qsTr(msg));
  105. return;
  106. };
  107. var entity = this.doc.queryEntity(entityId);
  108. var td = entity.getData();
  109. // get the enlarged bounding box (10% of the text height)
  110. var box = td.getBoundingBox().grow(entity.getTextHeight() * 0.1);
  111. var vectorArr = box.getCorners2d();
  112. // delete 2 top corners
  113. vectorArr.pop();
  114. vectorArr.pop();
  115. // find the index of the point closest to the firstVertex
  116. var index = this.firstVertex.getClosestIndex2D(vectorArr);
  117. // choose the right or left leader depending on the firstVertex
  118. var leader = new RLeaderEntity(this.doc, new RLeaderData());
  119. if (index == 0) {
  120. this.op.addObject(this.setLeftLeader(leader, vectorArr));
  121. } else {
  122. this.op.addObject(this.setRightLeader(leader, vectorArr));
  123. }
  124. this.di.applyOperation(this.op);
  125. EAction.handleUserMessage(qsTr("Leader was created successfully"));
  126. this.terminate();
  127. };