LayerList.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/WidgetFactory.js");
  20. function RLayerListQt(parent) {
  21. RListWidget.call(this, parent);
  22. if (RS.getSystemId()==="osx") {
  23. this.setIconOffset(7);
  24. }
  25. this.iconSize = new QSize(32, 16);
  26. this.di = undefined;
  27. var appWin = EAction.getMainWindow();
  28. var adapter = new RLayerListenerAdapter();
  29. appWin.addLayerListener(adapter);
  30. if (RSettings.getQtVersion() < 0x060000) {
  31. adapter.layersUpdated.connect(this, "updateLayers");
  32. adapter.currentLayerSet.connect(this, "updateLayers");
  33. adapter.layersCleared.connect(this, "clearLayers");
  34. this.setProperty("listener", adapter);
  35. this.itemSelectionChanged.connect(this, "layerActivated");
  36. this.itemDoubleClicked.connect(this, "moveSelectionToLayer");
  37. this.iconClicked.connect(this, "iconClickedSlot");
  38. }
  39. else {
  40. adapter.layersUpdated.connect(this, this.updateLayers);
  41. adapter.currentLayerSet.connect(this, this.updateLayers);
  42. adapter.layersCleared.connect(this, this.clearLayers);
  43. this.setProperty("listener", adapter);
  44. this.itemSelectionChanged.connect(this, this.layerActivated);
  45. this.itemDoubleClicked.connect(this, this.moveSelectionToLayer);
  46. this.iconClicked.connect(this, this.iconClickedSlot);
  47. }
  48. this.basePath = includeBasePath;
  49. }
  50. RLayerListQt.prototype = new RListWidget();
  51. RLayerListQt.prototype.toString = function() {
  52. return "RLayerListQt()";
  53. };
  54. RLayerListQt.prototype.contextMenuEvent = function(e) {
  55. var item = this.itemAt(e.pos());
  56. if (!isNull(item)) {
  57. this.setCurrentItem(item);
  58. }
  59. var menu = new QMenu(this);
  60. menu.objectName = "ContextMenu";
  61. RGuiAction.getByScriptFile("scripts/Layer/ToggleLayerVisibility/ToggleLayerVisibility.js").addToMenu(menu);
  62. RGuiAction.getByScriptFile("scripts/Layer/ShowAllLayers/ShowAllLayers.js").addToMenu(menu);
  63. RGuiAction.getByScriptFile("scripts/Layer/HideAllLayers/HideAllLayers.js").addToMenu(menu);
  64. RGuiAction.getByScriptFile("scripts/Layer/ShowActiveLayer/ShowActiveLayer.js").addToMenu(menu);
  65. RGuiAction.getByScriptFile("scripts/Layer/AddLayer/AddLayer.js").addToMenu(menu);
  66. RGuiAction.getByScriptFile("scripts/Layer/RemoveLayer/RemoveLayer.js").addToMenu(menu);
  67. RGuiAction.getByScriptFile("scripts/Layer/EditLayer/EditLayer.js").addToMenu(menu);
  68. RGuiAction.getByScriptFile("scripts/Layer/SelectLayer/SelectLayer.js").addToMenu(menu);
  69. RGuiAction.getByScriptFile("scripts/Layer/DeselectLayer/DeselectLayer.js").addToMenu(menu);
  70. menu.exec(QCursor.pos());
  71. e.ignore();
  72. };
  73. RLayerListQt.prototype.updateLayers = function(documentInterface, previousLayerId) {
  74. this.di = documentInterface;
  75. var layer;
  76. var pos = this.verticalScrollBar().sliderPosition;
  77. this.clear();
  78. if (isNull(documentInterface)) {
  79. return;
  80. }
  81. var doc = documentInterface.getDocument();
  82. var result = doc.queryAllLayers();
  83. for ( var i = 0; i < result.length; ++i) {
  84. var id = result[i];
  85. layer = doc.queryLayer(id);
  86. if (isFunction(layer.isNull) && layer.isNull()) {
  87. continue;
  88. }
  89. var item = new QListWidgetItem(layer.getName(), this);
  90. var iconName = autoIconPath(this.basePath
  91. + "/layerstatus_%1%2.svg".arg(Number(layer.isFrozen()))
  92. .arg(Number(layer.isLocked())));
  93. item.setIcon(new QIcon(iconName));
  94. if (layer.isProtected()) {
  95. item.setData(Qt.UserRole, true);
  96. }
  97. this.addItem(item);
  98. }
  99. this.sortItems();
  100. layer = doc.queryCurrentLayer();
  101. if (!isFunction(layer.isNull) || !layer.isNull()) {
  102. var items = this.findItems(layer.getName(), Qt.MatchExactly);
  103. if (items.length !== 0) {
  104. this.blockSignals(true);
  105. this.setCurrentItem(items[0]);
  106. this.blockSignals(false);
  107. }
  108. }
  109. this.verticalScrollBar().sliderPosition = pos;
  110. };
  111. RLayerListQt.prototype.clearLayers = function() {
  112. this.clear();
  113. };
  114. RLayerListQt.prototype.iconClickedSlot = function(x, item) {
  115. if (isNull(this.di) || isNull(item)) {
  116. return;
  117. }
  118. var doc = this.di.getDocument();
  119. var layer = doc.queryLayer(item.text());
  120. if (isFunction(layer.isNull) && layer.isNull()) {
  121. return;
  122. }
  123. if (x < this.iconSize.width() / 2) {
  124. var off = !layer.isFrozen();
  125. layer.setFrozen(off);
  126. layer.setOff(off);
  127. } else {
  128. layer.setLocked(!layer.isLocked());
  129. }
  130. var op = new RModifyObjectOperation(layer);
  131. this.di.applyOperation(op);
  132. this.updateLayers(this.di);
  133. };
  134. RLayerListQt.prototype.moveSelectionToLayer = function() {
  135. if (isNull(this.di)) {
  136. return;
  137. }
  138. var list = this.selectedItems();
  139. if (list.length===0) {
  140. return;
  141. }
  142. var item = list[0];
  143. if (isNull(item)) {
  144. return;
  145. }
  146. if (!this.di.hasSelection()) {
  147. // no selection: edit layer:
  148. var a = RGuiAction.getByScriptFile("scripts/Layer/EditLayer/EditLayer.js");
  149. a.slotTrigger();
  150. return;
  151. }
  152. var op = new RChangePropertyOperation(REntity.PropertyLayer, item.text());
  153. this.di.applyOperation(op);
  154. };
  155. RLayerListQt.prototype.layerActivated = function() {
  156. if (isNull(this.di)) {
  157. return;
  158. }
  159. var list = this.selectedItems();
  160. if (list.length===0) {
  161. return;
  162. }
  163. var item = list[0];
  164. if (isNull(item)) {
  165. return;
  166. }
  167. this.blockSignals(true);
  168. this.di.setCurrentLayer(item.text());
  169. this.blockSignals(false);
  170. };
  171. function LayerList(guiAction) {
  172. EAction.call(this, guiAction);
  173. }
  174. LayerList.prototype = new EAction();
  175. LayerList.getPreferencesCategory = function() {
  176. return [ qsTr("Widgets"), qsTr("Layer List") ];
  177. };
  178. LayerList.applyPreferences = function(doc, mdiChild) {
  179. var appWin = RMainWindowQt.getMainWindow();
  180. appWin.notifyLayerListeners(EAction.getDocumentInterface(), []);
  181. //if (RSettings.getBoolValue("LayerList/AlternatingRowColors", false)===true) {
  182. var layerList = appWin.findChild("LayerList");
  183. WidgetFactory.initList(layerList, "LayerList");
  184. //}
  185. };
  186. /**
  187. * Shows / hides the layer list.
  188. */
  189. LayerList.prototype.beginEvent = function() {
  190. EAction.prototype.beginEvent.call(this);
  191. var appWin = RMainWindowQt.getMainWindow();
  192. var dock = appWin.findChild("LayerListDock");
  193. if (!RSettings.getOriginalArguments().contains("-no-show")) {
  194. dock.visible = !dock.visible;
  195. if (dock.visible) dock.raise();
  196. }
  197. };
  198. LayerList.prototype.finishEvent = function() {
  199. Widgets.prototype.finishEvent.call(this);
  200. var appWin = RMainWindowQt.getMainWindow();
  201. var dock = appWin.findChild("LayerListDock");
  202. this.getGuiAction().setChecked(dock.visible);
  203. };
  204. LayerList.uninit = function() {
  205. var action = RGuiAction.getByScriptFile("scripts/Widgets/LayerList/LayerList.js");
  206. action.visible = false;
  207. var appWin = RMainWindowQt.getMainWindow();
  208. var layerList = appWin.findChild("LayerList");
  209. if (!isNull(layerList)) {
  210. appWin.removeLayerListener(layerList.property("listener"));
  211. }
  212. var basicDock = appWin.findChild("LayerListDock");
  213. if (!isNull(basicDock)) {
  214. destr(basicDock);
  215. }
  216. LayerList.getPreferencesCategory = undefined;
  217. };
  218. LayerList.init = function(basePath) {
  219. var appWin = RMainWindowQt.getMainWindow();
  220. var action = new RGuiAction(qsTr("&Layer List"), appWin);
  221. action.setRequiresDocument(false);
  222. action.setScriptFile(basePath + "/LayerList.js");
  223. action.setIcon(basePath + "/LayerList.svg");
  224. action.setDefaultShortcut(new QKeySequence("g,y"));
  225. action.setDefaultCommands(["gy"]);
  226. action.setGroupSortOrder(3600);
  227. action.setSortOrder(100);
  228. action.setWidgetNames(["ViewMenu", "WidgetsToolBar", "ViewToolsPanel", "WidgetMatrixPanel"]);
  229. var formWidget = WidgetFactory.createWidget(basePath, "LayerList.ui");
  230. var layout = formWidget.findChild("verticalLayout");
  231. var layerList = new RLayerListQt(formWidget);
  232. layerList.objectName = "LayerList";
  233. layout.addWidget(layerList, 1, 0);
  234. RSettings.setValue("LayerList/AlternatingRowColor", new RColor(230, 235, 250), false);
  235. WidgetFactory.initList(layerList, "LayerList");
  236. layerList.itemSelectionChanged.connect(function() {
  237. var action = RGuiAction.getByScriptFile("scripts/Layer/RemoveLayer/RemoveLayer.js");
  238. if (isNull(action)) {
  239. return;
  240. }
  241. var list = layerList.selectedItems();
  242. if (list.length === 0) {
  243. return;
  244. }
  245. var item = list[0];
  246. if (isNull(item)) {
  247. return;
  248. }
  249. // protected layers:
  250. if (item.text() === "0" || item.data(Qt.UserRole)===true) {
  251. action.setEnabledOverride(false, 0);
  252. } else {
  253. action.setEnabledOverride(true, 1);
  254. }
  255. });
  256. var widgets = getWidgets(formWidget);
  257. widgets["ShowAll"].setDefaultAction(
  258. RGuiAction.getByScriptFile("scripts/Layer/ShowAllLayers/ShowAllLayers.js"));
  259. widgets["HideAll"].setDefaultAction(
  260. RGuiAction.getByScriptFile("scripts/Layer/HideAllLayers/HideAllLayers.js"));
  261. widgets["Add"].setDefaultAction(
  262. RGuiAction.getByScriptFile("scripts/Layer/AddLayer/AddLayer.js"));
  263. widgets["Remove"].setDefaultAction(
  264. RGuiAction.getByScriptFile("scripts/Layer/RemoveLayer/RemoveLayer.js"));
  265. widgets["Edit"].setDefaultAction(
  266. RGuiAction.getByScriptFile("scripts/Layer/EditLayer/EditLayer.js"));
  267. var dock = new RDockWidget(qsTr("Layer List"), appWin);
  268. dock.objectName = "LayerListDock";
  269. //dock.setProperty("Category", Widgets.getListContextMenuCategory());
  270. dock.setWidget(formWidget);
  271. appWin.addDockWidget(Qt.RightDockWidgetArea, dock);
  272. dock.shown.connect(function() { action.setChecked(true); });
  273. dock.hidden.connect(function() { action.setChecked(false); });
  274. };