SelectLayer.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/library.js");
  20. include("scripts/Layer/Layer.js");
  21. /**
  22. * \class SelectLayer
  23. * \ingroup ecma_layer
  24. * \brief Select entities based on layer.
  25. */
  26. function SelectLayer(guiAction) {
  27. Layer.call(this, guiAction);
  28. this.select = true;
  29. }
  30. SelectLayer.prototype = new Layer();
  31. SelectLayer.prototype.beginEvent = function() {
  32. Layer.prototype.beginEvent.call(this);
  33. var doc = this.getDocument();
  34. var layer = doc.queryCurrentLayer();
  35. this.selectLayer(layer);
  36. this.terminate();
  37. };
  38. SelectLayer.prototype.selectLayer = function(layer) {
  39. return this.selectLayers([layer]);
  40. };
  41. SelectLayer.prototype.selectLayers = function(layers) {
  42. var ret = true;
  43. var doc = this.getDocument();
  44. var di = this.getDocumentInterface();
  45. var num;
  46. var ids = [];
  47. for (var i=0; i<layers.length; i++) {
  48. var layer = layers[i];
  49. if (layer.isOffOrFrozen()) {
  50. ret = false;
  51. }
  52. else {
  53. ids = ids.concat(doc.queryLayerEntities(layer.getId()));
  54. }
  55. }
  56. if (ids.length>0) {
  57. if (this.select) {
  58. num = di.selectEntities(ids, true);
  59. EAction.handleUserMessage(qsTr("%1 entities added to selection.").arg(num));
  60. }
  61. else {
  62. num = di.deselectEntities(ids);
  63. EAction.handleUserMessage(qsTr("%1 entities removed from selection.").arg(num));
  64. }
  65. }
  66. return ret;
  67. };