RemoveBlock.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/Block/Block.js");
  20. include("scripts/library.js");
  21. include("scripts/Widgets/BlockList/BlockList.js");
  22. /**
  23. * \class RemoveBlock
  24. * \ingroup ecma_block
  25. * \brief Remove the selected block.
  26. */
  27. function RemoveBlock(guiAction) {
  28. Block.call(this, guiAction);
  29. }
  30. RemoveBlock.prototype = new Block();
  31. RemoveBlock.prototype.beginEvent = function() {
  32. Block.prototype.beginEvent.call(this);
  33. var blockList = EAction.getMainWindow().findChild("BlockList");
  34. if (isNull(blockList)) {
  35. this.terminate();
  36. return;
  37. }
  38. var item = BlockList.getActiveItem();
  39. if (isNull(item)) {
  40. this.terminate();
  41. return;
  42. }
  43. var di = this.getDocumentInterface();
  44. var doc = di.getDocument();
  45. var block = doc.queryBlock(item.data(BlockList.colName, Qt.UserRole));
  46. if (isNull(block)) {
  47. this.terminate();
  48. return;
  49. }
  50. if (block.getId()===doc.getCurrentBlockId()) {
  51. // deleting current block: activate model space:
  52. Block.editBlock(di, doc.getBlockName(doc.getModelSpaceBlockId()));
  53. }
  54. var op = new RDeleteObjectsOperation();
  55. op.setText(this.getToolTitle());
  56. op.deleteObject(block);
  57. if (block.hasLayout()) {
  58. var layout = doc.queryLayout(block.getLayoutId());
  59. if (!isNull(layout)) {
  60. op.deleteObject(layout);
  61. }
  62. }
  63. di.applyOperation(op);
  64. di.clearPreview();
  65. di.repaintViews();
  66. this.terminate();
  67. };