ExDeleteObject.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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("../ModifyExamples.js");
  20. /**
  21. * \ingroup ecma_misc_examples_modifyexamples
  22. * \class ExDeleteObject
  23. */
  24. function ExDeleteObject(guiAction) {
  25. ModifyExamples.call(this, guiAction);
  26. }
  27. ExDeleteObject.prototype = new ModifyExamples();
  28. ExDeleteObject.prototype.beginEvent = function() {
  29. ModifyExamples.prototype.beginEvent.call(this);
  30. var di = this.getDocumentInterface();
  31. var document = this.getDocument();
  32. var op, entity, id;
  33. // add line entity,
  34. // document takes ownership of entity:
  35. op = new RAddObjectsOperation();
  36. entity = new RLineEntity(document, new RLineData(new RVector(0,0), new RVector(100,100)));
  37. op.addObject(entity);
  38. di.applyOperation(op);
  39. // after applying the operation, the ID of the newly added entity is known
  40. // and can be stored as reference for the added entity:
  41. id = entity.getId();
  42. EAction.handleUserMessage(qsTr("Added line with ID %1".arg(id)));
  43. // query a copy of the stored entity from the document
  44. // and use it to delete the entity:
  45. entity = document.queryEntity(id);
  46. op = new RDeleteObjectsOperation();
  47. op.deleteObject(entity);
  48. di.applyOperation(op);
  49. EAction.handleUserMessage(qsTr("Deleted line with ID %1").arg(id));
  50. EAction.handleUserMessage(qsTr("You can use <i>Edit > Undo</i> to restore the line entity."), false);
  51. this.terminate();
  52. };
  53. /**
  54. * Adds a menu for this action.
  55. */
  56. ExDeleteObject.init = function(basePath) {
  57. var action = new RGuiAction(qsTr("Add and Delete Line"), RMainWindowQt.getMainWindow());
  58. action.setRequiresDocument(true);
  59. action.setScriptFile(basePath + "/ExDeleteObject.js");
  60. action.setGroupSortOrder(78200);
  61. action.setSortOrder(100);
  62. action.setWidgetNames(["ModifyExamplesMenu"]);
  63. };