ToggleGrid.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/View/View.js");
  20. /**
  21. * Helper class to update grid button status when focus changes
  22. * (e.g. other view is activated).
  23. */
  24. function ToggleGridFocusListener(action) {
  25. RViewFocusListenerAdapter.call(this);
  26. this.action = action;
  27. }
  28. ToggleGridFocusListener.prototype = new RViewFocusListenerAdapter();
  29. ToggleGridFocusListener.prototype.updateFocus = function(view) {
  30. if (isNull(view)) {
  31. this.action.checked = false;
  32. return;
  33. }
  34. var doc = view.getDocument();
  35. if (isNull(doc)) {
  36. this.action.checked = false;
  37. return;
  38. }
  39. var gridOn = view.isGridVisible();
  40. this.action.checked = gridOn;
  41. };
  42. /**
  43. * \class ToggleGrid
  44. * \brief Toggles grid visibility.
  45. * \ingroup ecma_view
  46. */
  47. function ToggleGrid(guiAction) {
  48. View.call(this, guiAction);
  49. }
  50. ToggleGrid.prototype = new View();
  51. ToggleGrid.prototype.beginEvent = function() {
  52. View.prototype.beginEvent.call(this);
  53. var view = this.getDocumentInterface().getLastKnownViewWithFocus();
  54. if (!isNull(view)) {
  55. var gridOn = view.isGridVisible();
  56. view.setGridVisible(!gridOn);
  57. view.regenerate();
  58. }
  59. this.terminate();
  60. };
  61. ToggleGrid.prototype.finishEvent = function() {
  62. View.prototype.finishEvent.call(this);
  63. var view = this.getDocumentInterface().getLastKnownViewWithFocus();
  64. if (!isNull(view)) {
  65. var guiAction = this.getGuiAction();
  66. if (!isNull(guiAction)) {
  67. guiAction.setChecked(view.isGridVisible());
  68. }
  69. }
  70. };
  71. ToggleGrid.init = function(basePath) {
  72. var action = new RGuiAction(qsTr("&Grid"), RMainWindowQt.getMainWindow());
  73. action.setScriptFile(basePath + "/ToggleGrid.js");
  74. action.setRequiresDocument(true);
  75. action.setIcon(basePath + "/ToggleGrid.svg");
  76. action.setDefaultShortcut(new QKeySequence("g,r"));
  77. action.setNoState(true);
  78. action.setGroupSortOrder(3200);
  79. action.setSortOrder(100);
  80. action.setWidgetNames(["ViewMenu", "!ViewToolBar", "ViewToolsPanel", "ViewMatrixPanel"]);
  81. var appWin = EAction.getMainWindow();
  82. var fl = new ToggleGridFocusListener(action);
  83. appWin.addViewFocusListener(fl);
  84. };