ToolBarContextMenu.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. function ToolBarContextMenu() {
  20. }
  21. ToolBarContextMenu.getSubMenu = function(menu, categories) {
  22. if (isNull(categories) || categories.length===0) {
  23. return menu;
  24. }
  25. var m;
  26. var actions = menu.actions();
  27. for (var k=0; k<actions.length; k++) {
  28. var action = actions[k];
  29. m = action.menu();
  30. if (!isNull(m) && m.title===categories[0]) {
  31. categories.shift();
  32. return ToolBarContextMenu.getSubMenu(m, categories);
  33. }
  34. }
  35. // menu not found: create it:
  36. m = menu;
  37. for (var i=0; i<categories.length; i++) {
  38. m = m.addMenu(categories[i]);
  39. }
  40. return m;
  41. };
  42. ToolBarContextMenu.sort = function(a,b) {
  43. var catA = a.property("Category");
  44. var catB = b.property("Category");
  45. if (isNull(catA) && !isNull(catB)) {
  46. return -1;
  47. }
  48. if (!isNull(catA) && isNull(catB)) {
  49. return 1;
  50. }
  51. if (!isNull(catA) && !isNull(catB)) {
  52. return catA.toString().localeCompare(catB.toString());
  53. }
  54. // no category:
  55. var titleA = a.windowTitle.replace("&", "");
  56. var titleB = b.windowTitle.replace("&", "");
  57. return titleA.localeCompare(titleB);
  58. };
  59. ToolBarContextMenu.createMenu = function(menu) {
  60. var appWin = RMainWindowQt.getMainWindow();
  61. var tbs = appWin.getToolBars();
  62. var dws = appWin.getDockWidgets();
  63. var i, a, m, cat;
  64. dws.sort(ToolBarContextMenu.sort);
  65. tbs.sort(ToolBarContextMenu.sort);
  66. for (i = 0; i<dws.length; i++) {
  67. var dw = dws[i];
  68. cat = dw.property("Category");
  69. if (isNull(cat)) {
  70. cat = [];
  71. }
  72. cat.unshift(qsTr("Widgets"));
  73. m = ToolBarContextMenu.getSubMenu(menu, cat);
  74. if (!isNull(m)) {
  75. a = dw.toggleViewAction();
  76. RGuiAction.addToWidget(a, m);
  77. }
  78. }
  79. for (i = 0; i<tbs.length; i++) {
  80. var tb = tbs[i];
  81. cat = tb.property("Category");
  82. if (isNull(cat)) {
  83. cat = [];
  84. }
  85. cat.unshift(qsTr("Toolbars"));
  86. m = ToolBarContextMenu.getSubMenu(menu, cat);
  87. if (!isNull(m)) {
  88. a = tb.toggleViewAction();
  89. RGuiAction.addToWidget(a, m);
  90. }
  91. }
  92. };
  93. ToolBarContextMenu.init = function(basePath) {
  94. var appWin = RMainWindowQt.getMainWindow();
  95. appWin.toolBarContextMenu.connect(ToolBarContextMenu.createMenu);
  96. };