DefaultAction.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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("scripts/EAction.js");
  20. include("scripts/Select/Select.js");
  21. include("scripts/Widgets/CadToolBar/CadToolBar.js");
  22. /**
  23. * \class DefaultAction
  24. * \brief Default action which is active when no other tool
  25. * or action is active. Implements basic entity selection
  26. * and drag and drop.
  27. */
  28. function DefaultAction(guiAction) {
  29. EAction.call(this, guiAction);
  30. }
  31. DefaultAction.prototype = new EAction();
  32. DefaultAction.State = {
  33. Neutral : 0,
  34. Dragging : 1, //*< Started dragging, minor distance, could be simple click at this stage
  35. SettingCorner2 : 2, //*< Setting 2nd corner of selection rectangle.
  36. MovingEntity : 3, //*< Dragging entity, mouse button is down.
  37. MovingEntityInBlock : 4, //*< Dragging entity inside block, mouse button is down.
  38. SettingEntity : 5, //*< Setting position of previously dragged entity, mouse button is up.
  39. MovingReference : 6, //*< Dragging reference point, mouse button is down.
  40. SettingReference : 7 //*< Setting position of previously dragged reference point, mouse button is up.
  41. };
  42. DefaultAction.prototype.beginEvent = function() {
  43. EAction.prototype.beginEvent.call(this);
  44. //this.snapRangePixels = RSettings.getSnapRange();
  45. //this.minSnapRangePixels = Math.min(this.snapRangePixels / 2, 10);
  46. this.pickRangePixels = RSettings.getPickRange();
  47. this.minPickRangePixels = Math.min(this.pickRangePixels / 2, 10);
  48. this.d1Model = RVector.invalid;
  49. this.d1Screen = RVector.invalid;
  50. this.d2Model = RVector.invalid;
  51. this.d2Screen = RVector.invalid;
  52. this.di = this.getDocumentInterface();
  53. this.document = this.di.getDocument();
  54. this.setState(DefaultAction.State.Neutral);
  55. this.blockRefId = RObject.INVALID_ID;
  56. this.entityInBlockId = RObject.INVALID_ID;
  57. this.optOutRelativeZeroResume = true;
  58. };
  59. DefaultAction.prototype.setState = function(state) {
  60. EAction.prototype.setState.call(this, state);
  61. var appWin = EAction.getMainWindow();
  62. if (this.state === DefaultAction.State.MovingReference
  63. || this.state === DefaultAction.State.SettingReference
  64. || this.state === DefaultAction.State.MovingEntity
  65. || this.state === DefaultAction.State.MovingEntityInBlock
  66. || this.state === DefaultAction.State.SettingEntity) {
  67. this.di.setClickMode(RAction.PickCoordinate);
  68. this.setCrosshairCursor();
  69. EAction.showSnapTools();
  70. } else {
  71. this.di.setClickMode(RAction.PickingDisabled);
  72. this.setArrowCursor();
  73. }
  74. var tr;
  75. switch (this.state) {
  76. case DefaultAction.State.Neutral:
  77. this.d1Model = RVector.invalid;
  78. this.d1Screen = RVector.invalid;
  79. this.d2Model = RVector.invalid;
  80. this.d2Screen = RVector.invalid;
  81. var ltip = qsTr("Select entity or region");
  82. if (this.di.hasSelection()) {
  83. ltip += "\n" + qsTr("Move entity or reference");
  84. }
  85. this.setLeftMouseTip(ltip);
  86. this.setRightMouseTip("");
  87. this.setCommandPrompt();
  88. break;
  89. case DefaultAction.State.Dragging:
  90. this.d2Model = RVector.invalid;
  91. this.d2Screen = RVector.invalid;
  92. break;
  93. case DefaultAction.State.SettingCorner2:
  94. tr = qsTr("Second corner");
  95. this.setLeftMouseTip(tr);
  96. this.setCommandPrompt(tr);
  97. this.setRightMouseTip("");
  98. break;
  99. case DefaultAction.State.MovingReference:
  100. case DefaultAction.State.SettingReference:
  101. tr = qsTr("Target point of reference point");
  102. this.setLeftMouseTip(tr);
  103. this.setCommandPrompt(tr);
  104. this.setRightMouseTip("");
  105. break;
  106. case DefaultAction.State.MovingEntity:
  107. case DefaultAction.State.SettingEntity:
  108. tr = qsTr("Target point of selection");
  109. this.setLeftMouseTip(tr);
  110. this.setCommandPrompt(tr);
  111. this.setRightMouseTip("");
  112. break;
  113. case DefaultAction.State.MovingEntityInBlock:
  114. this.setLeftMouseTip(
  115. qsTr("Move entity to desired location")
  116. );
  117. break;
  118. default:
  119. break;
  120. }
  121. };
  122. DefaultAction.prototype.suspendEvent = function() {
  123. if (!isNull(this.getGuiAction())) {
  124. this.getGuiAction().setChecked(false);
  125. }
  126. EAction.prototype.suspendEvent.call(this);
  127. };
  128. DefaultAction.prototype.resumeEvent = function() {
  129. EAction.prototype.resumeEvent.call(this);
  130. };
  131. DefaultAction.prototype.mouseMoveEvent = function(event) {
  132. // we're in the middle of panning:
  133. // do nothing:
  134. if (event.buttons().valueOf() === RS.MiddleButton ||
  135. (event.buttons().valueOf() === Qt.LeftButton.valueOf() &&
  136. event.modifiers().valueOf() === Qt.ControlModifier.valueOf())) {
  137. return;
  138. }
  139. // we're in the middle of choosing something from a context menu:
  140. // do nothing:
  141. if (this.waitingForContextMenu) {
  142. return;
  143. }
  144. var view, referencePoint, entityId, range;
  145. if (isNull(this.pickRangePixels)) {
  146. return;
  147. }
  148. view = event.getGraphicsView();
  149. switch (this.state) {
  150. case DefaultAction.State.Neutral:
  151. var screenPosition = event.getScreenPosition();
  152. referencePoint = view.getClosestReferencePoint(screenPosition, this.minPickRangePixels);
  153. if (referencePoint.isValid()) {
  154. this.highlightReferencePoint(referencePoint);
  155. } else {
  156. if (RSettings.getBoolValue("GraphicsView/HighlightEntity", true)===true) {
  157. entityId = EAction.getEntityIdUnderCursor(this.getDocumentInterface(), event, undefined, true);
  158. // range = view.mapDistanceFromView(this.pickRangePixels);
  159. // var strictRange = view.mapDistanceFromView(10);
  160. // RMouseEvent.setOriginalMousePos(event.globalPos());
  161. // entityId = this.di.getClosestEntity(event.getModelPosition(), range, strictRange, false);
  162. // RMouseEvent.resetOriginalMousePos();
  163. if (entityId !== RObject.INVALID_ID && this.document.isEntityEditable(entityId)) {
  164. this.highlightEntity(entityId);
  165. }
  166. }
  167. }
  168. break;
  169. case DefaultAction.State.Dragging:
  170. this.d2Model = event.getModelPosition();
  171. this.d2Screen = event.getScreenPosition();
  172. view = event.getGraphicsView();
  173. if (!this.d1Screen.equalsFuzzy(this.d2Screen, 10 /*this.minPickRangePixels*/)) {
  174. // if the dragging started on top of a reference point,
  175. // start moving the reference point:
  176. referencePoint = view.getClosestReferencePoint(this.d1Screen, this.minPickRangePixels);
  177. if (referencePoint.isValid() && !referencePoint.isIgnore() && !RSettings.getIgnoreAllReferencePoints() &&
  178. !referencePoint.isArrow()) {
  179. this.d1Model = referencePoint;
  180. this.di.setRelativeZero(this.d1Model);
  181. this.setState(DefaultAction.State.MovingReference);
  182. } else {
  183. // if the dragging started on top of an entity,
  184. // start moving the entity:
  185. entityId = view.getClosestEntity(this.d1Screen, this.minPickRangePixels, 10, false);
  186. // in block easy drag and drop:
  187. if (entityId !== RObject.INVALID_ID) {
  188. var doc = this.getDocument();
  189. if (!isNull(doc)) {
  190. var entity = doc.queryEntityDirect(entityId);
  191. if (isBlockReferenceEntity(entity)) {
  192. var blockId = entity.getReferencedBlockId();
  193. var block = doc.queryBlock(blockId);
  194. if (!isNull(block)) {
  195. range = view.mapDistanceFromView(this.pickRangePixels);
  196. if (Math.abs(entity.getScaleFactors().x)>0) {
  197. range /= Math.abs(entity.getScaleFactors().x);
  198. }
  199. // cursor, mapped to block coordinates:
  200. var pBlock = entity.mapToBlock(this.d1Model);
  201. var box = new RBox(pBlock, range);
  202. var candidateIds = doc.queryIntersectedEntitiesXY(box, true, false, blockId);
  203. var entityInBlockId;
  204. if (candidateIds.length===1) {
  205. entityInBlockId = candidateIds[0];
  206. }
  207. else {
  208. entityInBlockId = doc.queryClosestXY(candidateIds, pBlock, range*2, false, range/2);
  209. }
  210. var entityInBlock = doc.queryEntityDirect(entityInBlockId);
  211. if (!isNull(entityInBlock) && getInBlockEasyDragAndDrop(entityInBlock)) {
  212. var refP = entityInBlock.getReferencePoints();
  213. if (refP.length>0) {
  214. this.d1Model = refP[0]; //entity.mapToBlock(refP[0]);
  215. }
  216. else {
  217. this.d1Model = pBlock;
  218. }
  219. this.entityInBlockId = entityInBlockId;
  220. this.blockRefId = entityId;
  221. this.setState(DefaultAction.State.MovingEntityInBlock);
  222. var guiAction = RGuiAction.getByScriptFile("scripts/Snap/SnapFree/SnapFree.js");
  223. if (!isNull(guiAction)) {
  224. guiAction.slotTrigger();
  225. }
  226. this.di.setSnap(new RSnapFree());
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. if (entityId !== RObject.INVALID_ID && this.document.hasSelection()) {
  234. // move start point of dragging operation to closest
  235. // reference point:
  236. // TODO: use auto snap instead here (optional?):
  237. this.d1Model = view.getClosestReferencePoint(entityId, this.d1Screen);
  238. this.di.setRelativeZero(this.d1Model);
  239. this.setState(DefaultAction.State.MovingEntity);
  240. } else {
  241. // if the dragging started in an empty space,
  242. // start box selection:
  243. this.setState(DefaultAction.State.SettingCorner2);
  244. // make sure one mouse move is enough to get a visible preview
  245. // (for testing, one mouse move event has to be enough):
  246. this.mouseMoveEvent(event);
  247. }
  248. }
  249. }
  250. break;
  251. case DefaultAction.State.SettingCorner2:
  252. this.d2Model = event.getModelPosition();
  253. Select.previewSelectionBox(
  254. this.getDocumentInterface(),
  255. new RBox(this.d1Model, this.d2Model),
  256. this.d1Model.x > this.d2Model.x
  257. );
  258. break;
  259. // easy in block entity drag and drop (point mark labels):
  260. // case DefaultAction.State.MovingEntityInBlock:
  261. // this.moveEntityInBlock(event.getModelPosition(), true);
  262. // break;
  263. default:
  264. break;
  265. }
  266. };
  267. DefaultAction.prototype.mouseReleaseEvent = function(event) {
  268. if (this.waitingForContextMenu) {
  269. return;
  270. }
  271. var appWin = EAction.getMainWindow();
  272. var persistentSelection = RSettings.getBoolValue("GraphicsView/PersistentSelection", false);
  273. var entityId;
  274. var shiftPressed = isShiftPressed(event);
  275. var controlPressed = isControlPressed(event);
  276. var addToSelection = this.getAddToSelection(shiftPressed) || controlPressed || persistentSelection===true;
  277. if (event.button() === Qt.LeftButton) {
  278. switch (this.state) {
  279. case DefaultAction.State.Dragging:
  280. // handle clicks on reference points:
  281. this.d2Model = event.getModelPosition();
  282. this.d2Screen = event.getScreenPosition();
  283. var view = event.getGraphicsView();
  284. if (this.d1Screen.equalsFuzzy(this.d2Screen, 10)) {
  285. // mouse press and release at same position (click):
  286. var referencePoint = view.getClosestReferencePoint(this.d1Screen, this.minPickRangePixels);
  287. if (referencePoint.isValid() && !referencePoint.isIgnore() && !RSettings.getIgnoreAllReferencePoints()) {
  288. this.d1Model = referencePoint;
  289. var op = new RClickReferencePointOperation(this.d1Model);
  290. this.di.applyOperation(op);
  291. this.setState(DefaultAction.State.Neutral);
  292. return;
  293. }
  294. }
  295. // find selectable entity under cursor:
  296. entityId = this.getEntityId(event, false, true);
  297. //qDebug("entityId: ", entityId);
  298. //if (entityId!==RObject.INVALID_ID) qDebug(" parentId: ", this.document.queryEntityDirect(entityId).getParentId());
  299. if (entityId !== -1) {
  300. if (addToSelection && (this.document.isSelected(entityId) || this.document.isSelectedWorkingSet(entityId))) {
  301. this.deselectEntity(entityId);
  302. }
  303. else {
  304. this.selectEntity(entityId, addToSelection);
  305. }
  306. } else {
  307. if (!addToSelection) {
  308. if (persistentSelection===false) {
  309. this.di.clearSelection();
  310. }
  311. }
  312. }
  313. this.di.clearPreview();
  314. this.di.repaintViews();
  315. this.setState(DefaultAction.State.Neutral);
  316. break;
  317. case DefaultAction.State.SettingCorner2:
  318. this.di.clearPreview();
  319. this.d2Model = event.getModelPosition();
  320. // select entities:
  321. this.di.selectBoxXY(new RBox(this.d1Model, this.d2Model), addToSelection);
  322. this.setState(DefaultAction.State.Neutral);
  323. break;
  324. // easy in block entity drag and drop (point mark labels):
  325. // case DefaultAction.State.MovingEntityInBlock:
  326. // this.moveEntityInBlock(event.getModelPosition(), false);
  327. // break;
  328. default:
  329. break;
  330. }
  331. } else if (event.button() == Qt.RightButton) {
  332. var handled = false;
  333. if (this.state!==DefaultAction.State.Neutral && this.state!==DefaultAction.State.MovingEntityInBlock) {
  334. this.di.clearPreview();
  335. this.di.repaintViews();
  336. this.setState(DefaultAction.State.Neutral);
  337. handled = true;
  338. }
  339. else if (this.state===DefaultAction.State.Neutral) {
  340. var rightClickRange = RSettings.getIntValue("GraphicsView/RightClickRange", 10);
  341. entityId = EAction.getEntityIdUnderCursor(this.di, event, rightClickRange, true);
  342. var rightClickToDeselect = RSettings.getBoolValue("GraphicsView/RightClickToDeselect", false);
  343. // right-click on entity can be used to show context menu:
  344. if (entityId!==-1) {
  345. // show entity context menu:
  346. var rightClickEntityContextMenu = RSettings.getBoolValue("ContextMenu/RightClickEntityContextMenu", true);
  347. if (rightClickEntityContextMenu) {
  348. event.accept();
  349. appWin.showContextMenu(entityId, event.getModelPosition());
  350. handled = true;
  351. }
  352. // use right-click on entity to select entity:
  353. else if (rightClickToDeselect) {
  354. this.selectEntity(entityId, addToSelection);
  355. handled = true;
  356. }
  357. }
  358. else {
  359. // use right-click into empty area to deselect everything:
  360. if (rightClickToDeselect) {
  361. if (this.di.hasSelection()) {
  362. this.di.clearSelection();
  363. this.di.clearPreview();
  364. this.di.repaintViews();
  365. handled = true;
  366. }
  367. }
  368. }
  369. }
  370. if (!handled) {
  371. var cadToolBar = appWin.findChild("CadToolBar");
  372. if (cadToolBar.back()===true) {
  373. handled = true;
  374. }
  375. }
  376. if (!handled) {
  377. // show context menu for empty space:
  378. var rightClickContextMenu = RSettings.getBoolValue("ContextMenu/RightClickContextMenu", true);
  379. if (rightClickContextMenu===true) {
  380. event.accept();
  381. appWin.showContextMenu(RObject.INVALID_ID, event.getModelPosition());
  382. }
  383. }
  384. }
  385. };
  386. DefaultAction.prototype.mousePressEvent = function(event) {
  387. if (event.button() == Qt.LeftButton
  388. && event.modifiers().valueOf() != Qt.ControlModifier.valueOf()) {
  389. if (this.state == DefaultAction.State.Neutral) {
  390. this.d1Model = event.getModelPosition();
  391. this.d1Screen = event.getScreenPosition();
  392. this.setState(DefaultAction.State.Dragging);
  393. this.di.clearPreview();
  394. }
  395. }
  396. };
  397. DefaultAction.prototype.mouseDoubleClickEvent = function(event) {
  398. if (event.button() == Qt.LeftButton && (this.state===DefaultAction.State.Neutral || this.state===DefaultAction.State.Dragging)) {
  399. if (this.state===DefaultAction.State.Dragging) {
  400. // cancel dragging (double-click):
  401. this.setState(DefaultAction.State.Neutral);
  402. }
  403. var view = event.getGraphicsView();
  404. var range = view.mapDistanceFromView(this.pickRangePixels);
  405. var strictRange = view.mapDistanceFromView(10);
  406. var entityId = this.di.getClosestEntity(event.getModelPosition(), range, strictRange, false);
  407. if (entityId===RObject.INVALID_ID) {
  408. this.emptySpaceDoubleClicked();
  409. return;
  410. }
  411. this.entityDoubleClicked(entityId, event);
  412. }
  413. EAction.prototype.mouseDoubleClickEvent.call(this, event);
  414. };
  415. DefaultAction.prototype.emptySpaceDoubleClicked = function() {
  416. if (this.document.getCurrentViewportId()!==RObject.INVALID_ID) {
  417. this.di.unsetCurrentViewport();
  418. }
  419. };
  420. DefaultAction.prototype.escapeEvent = function(event) {
  421. this.di.clearPreview();
  422. this.di.repaintViews();
  423. var appWin = EAction.getMainWindow();
  424. var cadToolBar = appWin.findChild("CadToolBar");
  425. cadToolBar.back();
  426. this.setState(DefaultAction.State.Neutral);
  427. };
  428. DefaultAction.prototype.coordinateEvent = function(event) {
  429. // required to make sure invalid coordinate event goes through (needed for drag and drop):
  430. this.pickCoordinate(event, false);
  431. };
  432. DefaultAction.prototype.pickCoordinate = function(event, preview) {
  433. var op;
  434. var shiftPressed = isShiftPressed(event);
  435. var addToSelection = this.getAddToSelection(shiftPressed);
  436. var doc = this.getDocument();
  437. switch (this.state) {
  438. case DefaultAction.State.MovingReference:
  439. case DefaultAction.State.SettingReference:
  440. // find reference shape for lengthen, shorten when shift is pressed:
  441. var referenceShape = undefined;
  442. var d2 = event.getModelPosition();
  443. if (shiftPressed) {
  444. var view = event.getGraphicsView();
  445. var modelRange = view.mapDistanceFromView(this.minPickRangePixels);
  446. var referenceEntityId = this.di.getClosestEntity(this.d1Model, modelRange, 10, false, true);
  447. if (referenceEntityId!==RObject.INVALID_ID) {
  448. var e = doc.queryEntity(referenceEntityId);
  449. if (!isNull(e)) {
  450. referenceShape = e.getClosestSimpleShape(this.d1Model);
  451. }
  452. }
  453. if (!isNull(referenceShape)) {
  454. d2 = referenceShape.getClosestPointOnShape(d2, false);
  455. this.di.addAuxShapeToPreview(new RLine(this.d2Model, event.getModelPosition()));
  456. }
  457. }
  458. if (preview) {
  459. this.d2Model = d2;
  460. op = new RMoveReferencePointOperation(this.d1Model, this.d2Model, event.getModifiers());
  461. var scene = this.di.getGraphicsSceneWithFocus();
  462. op.setGraphicsScene(scene);
  463. this.di.previewOperation(op);
  464. }
  465. else {
  466. if (this.state===DefaultAction.State.MovingReference) {
  467. this.setState(DefaultAction.State.SettingReference);
  468. }
  469. else {
  470. this.d2Model = d2;
  471. op = new RMoveReferencePointOperation(this.d1Model, this.d2Model, event.getModifiers());
  472. var scene = this.di.getGraphicsSceneWithFocus();
  473. op.setGraphicsScene(scene);
  474. op.setText(qsTr("Move Reference Point"));
  475. this.di.applyOperation(op);
  476. this.di.clearPreview();
  477. this.di.repaintViews();
  478. CadToolBar.back();
  479. this.setState(DefaultAction.State.Neutral);
  480. }
  481. }
  482. break;
  483. case DefaultAction.State.MovingEntity:
  484. case DefaultAction.State.SettingEntity:
  485. if (preview) {
  486. this.d2Model = event.getModelPosition();
  487. op = new RMoveSelectionOperation(this.d1Model, this.d2Model);
  488. this.di.previewOperation(op);
  489. }
  490. else {
  491. if (this.state===DefaultAction.State.MovingEntity) {
  492. this.setState(DefaultAction.State.SettingEntity);
  493. }
  494. else {
  495. this.d2Model = event.getModelPosition();
  496. op = new RMoveSelectionOperation(this.d1Model, this.d2Model);
  497. op.setText(qsTr("Move Selection"));
  498. this.di.applyOperation(op);
  499. this.di.clearPreview();
  500. this.di.repaintViews();
  501. CadToolBar.back();
  502. this.setState(DefaultAction.State.Neutral);
  503. }
  504. }
  505. break;
  506. // easy in block entity drag and drop (point mark labels):
  507. case DefaultAction.State.MovingEntityInBlock:
  508. var doc = this.getDocument();
  509. if (isNull(doc)) {
  510. break;
  511. }
  512. var blockRef = doc.queryEntity(this.blockRefId);
  513. if (isNull(blockRef)) {
  514. break;
  515. }
  516. this.d2Model = blockRef.mapToBlock(event.getModelPosition());
  517. var entityInBlock = doc.queryEntity(this.entityInBlockId);
  518. entityInBlock.move(this.d2Model.operator_subtract(this.d1Model));
  519. op = new RAddObjectsOperation();
  520. op.setText(qsTr("Move Entity"));
  521. op.addObject(entityInBlock, false);
  522. if (preview) {
  523. this.di.previewOperation(op);
  524. }
  525. else {
  526. doc.removeFromSpatialIndex(blockRef);
  527. this.di.applyOperation(op);
  528. blockRef.update();
  529. doc.addToSpatialIndex(blockRef);
  530. this.setState(DefaultAction.State.Neutral);
  531. var guiAction = RGuiAction.getByScriptFile("scripts/Snap/SnapAuto/SnapAuto.js");
  532. if (!isNull(guiAction)) {
  533. guiAction.slotTrigger();
  534. }
  535. this.di.setSnap(new RSnapAuto());
  536. }
  537. break;
  538. default:
  539. break;
  540. }
  541. };
  542. /**
  543. * Called when the mouse cursor hovers over an entity.
  544. */
  545. DefaultAction.prototype.highlightEntity = function(entityId) {
  546. if (RSettings.getBoolValue("GraphicsView/HighlightEntity", true)===false) {
  547. return;
  548. }
  549. if (isNull(this.di)) {
  550. return;
  551. }
  552. this.di.highlightEntity(entityId);
  553. };
  554. /**
  555. * Called when the mouse cursor hovers over a reference point.
  556. */
  557. DefaultAction.prototype.highlightReferencePoint = function(referencePoint) {
  558. if (isNull(this.di)) {
  559. return;
  560. }
  561. this.di.highlightReferencePoint(referencePoint);
  562. };
  563. /**
  564. * Called when the user deselects a single entity.
  565. */
  566. DefaultAction.prototype.deselectEntity = function(entityId) {
  567. if (isNull(this.di)) {
  568. return;
  569. }
  570. this.di.deselectEntity(entityId);
  571. };
  572. /**
  573. * Called when the user selects a single entity.
  574. */
  575. DefaultAction.prototype.selectEntity = function(entityId, add) {
  576. if (isNull(this.di)) {
  577. return;
  578. }
  579. this.di.selectEntity(entityId, add);
  580. };
  581. /**
  582. * Called when the user double-clicks an entity.
  583. */
  584. DefaultAction.prototype.entityDoubleClicked = function(entityId, event) {
  585. if (isNull(this.document) || isNull(this.di)) {
  586. return;
  587. }
  588. var entity = this.document.queryEntity(entityId);
  589. if (isTextEntity(entity) ||
  590. isAttributeEntity(entity) ||
  591. isAttributeDefinitionEntity(entity)) {
  592. if (RSettings.getBoolValue("GraphicsView/DoubleClickEditText", true)===true) {
  593. include("scripts/Modify/EditText/EditText.js");
  594. EditText.editText(entity);
  595. }
  596. }
  597. else if (isBlockReferenceEntity(entity)) {
  598. // in block text editing with double-click:
  599. /*
  600. var blockId = entity.getReferencedBlockId();
  601. var block = this.document.queryBlock(blockId);
  602. if (!isNull(block)) {
  603. var view = event.getGraphicsView();
  604. var range = view.mapDistanceFromView(this.pickRangePixels);
  605. // cursor, mapped to block coordinates:
  606. var pBlock = entity.mapToBlock(event.getModelPosition());
  607. var box = new RBox(
  608. pBlock.operator_subtract(new RVector(range,range)),
  609. pBlock.operator_add(new RVector(range,range))
  610. );
  611. var res = this.document.queryIntersectedEntitiesXY(box, true, false, blockId);
  612. var entityInBlockId;
  613. if (res.length===1) {
  614. entityInBlockId = res[0];
  615. }
  616. else {
  617. entityInBlockId = this.document.queryClosestXY(res, pBlock, range*2, false);
  618. }
  619. var entityInBlock = this.document.queryEntity(entityInBlockId);
  620. if (!isNull(entityInBlock) && isTextBasedEntity(entityInBlock) && getInBlockTextEdit(entityInBlock)) {
  621. include("scripts/Modify/EditText/EditText.js");
  622. EditText.editText(entityInBlock);
  623. return;
  624. }
  625. }
  626. */
  627. // block editing with double click might be disabled globally:
  628. if (RSettings.getBoolValue("GraphicsView/DoubleClickEditBlock", true)===true) {
  629. // block editing with double click might be disabled for this entity:
  630. if (entity.getCustomBoolProperty("QCAD", "DoubleClickEditBlock", true)===true) {
  631. include("scripts/Block/Block.js");
  632. EAction.handleUserMessage(qsTr("Editing block \"%1\"<br>Choose <i>Block > Return to Main Drawing</i> when done").arg(entity.getReferencedBlockName()), false);
  633. Block.editBlock(this.di, entity.getReferencedBlockName());
  634. }
  635. }
  636. }
  637. else if (isHatchEntity(entity)) {
  638. if (RSettings.getBoolValue("GraphicsView/DoubleClickEditHatch", true)===true) {
  639. include("scripts/Modify/EditHatch/EditHatch.js");
  640. EditHatch.editHatch(entity);
  641. }
  642. }
  643. else if (isLineEntity(entity) ||
  644. isArcEntity(entity) ||
  645. isSplineEntity(entity) ||
  646. isPolylineEntity(entity) ||
  647. isEllipseEntity(entity)) {
  648. if (RSettings.getBoolValue("GraphicsView/DoubleClickSelectContour", true)===true) {
  649. include("scripts/Select/SelectContour/SelectContour.js");
  650. var tol = RSettings.getDoubleValue("GraphicsView/DoubleClickSelectContourTolerance", 0.001);
  651. var layerId = RObject.INVALID_ID;
  652. if (isAltPressed(event)) {
  653. // Alt: restrict to same layer:
  654. layerId = entity.getLayerId();
  655. }
  656. var matchingEntityIds = SelectContour.getConnectedEntities(this.document, entityId, tol, layerId);
  657. var add = isShiftPressed(event);
  658. if (entity.isSelected()) {
  659. this.di.selectEntities(matchingEntityIds, add);
  660. }
  661. else {
  662. this.di.deselectEntities(matchingEntityIds);
  663. }
  664. }
  665. }
  666. // TODO:
  667. // else if (isViewportEntity(entity)) {
  668. // this.di.setCurrentViewport(entity.data());
  669. // }
  670. };
  671. /**
  672. * \return True to add to selection (by default this is when shiftPressed is true).
  673. * May be reimplemented to allow other ways to add to selection (e.g. user interface button, etc.)
  674. */
  675. DefaultAction.prototype.getAddToSelection = function(shiftPressed) {
  676. return shiftPressed;
  677. };