Layer.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /**
  20. * \defgroup ecma_layer Layer Tools
  21. * \ingroup ecma_scripts
  22. *
  23. * \brief This module contains ECMAScript implementations of various
  24. * layer related tools.
  25. */
  26. include("scripts/EAction.js");
  27. /**
  28. * \class Layer
  29. * \brief Base class for all layer tools.
  30. * \ingroup ecma_layer
  31. */
  32. function Layer(guiAction) {
  33. EAction.call(this, guiAction);
  34. this.documentInterface = undefined;
  35. // if (!isNull(guiAction) && !isNull(guiAction.getDocumentInterface())) {
  36. // this.setDocumentInterface(guiAction.getDocumentInterface());
  37. // }
  38. }
  39. Layer.prototype = new EAction();
  40. Layer.includeBasePath = includeBasePath;
  41. Layer.prototype.beginEvent = function () {
  42. EAction.prototype.beginEvent.call(this);
  43. if (!isNull(this.getGuiAction()) && this.getGuiAction().objectName === "LayerToolsPanelAction") {
  44. EAction.showCadToolBarPanel("LayerToolsPanel");
  45. this.terminate();
  46. }
  47. };
  48. Layer.prototype.setDocumentInterface = function (di) {
  49. this.documentInterface = di;
  50. };
  51. Layer.prototype.getDocumentInterface = function () {
  52. if (!isNull(this.documentInterface)) {
  53. return this.documentInterface;
  54. }
  55. return EAction.prototype.getDocumentInterface.call(this);
  56. };
  57. Layer.prototype.getDocument = function () {
  58. if (!isNull(this.documentInterface)) {
  59. return this.documentInterface.getDocument();
  60. }
  61. return EAction.prototype.getDocument.call(this);
  62. };
  63. Layer.prototype.getCurrentLayer = function () {
  64. // get current or override document:
  65. var doc = this.getDocument();
  66. return doc.queryCurrentLayer();
  67. };
  68. Layer.getMenu = function () {
  69. var menu = EAction.getMenu(Layer.getTitle(), "LayerMenu");
  70. menu.setProperty("scriptFile", Layer.includeBasePath + "/Layer.js");
  71. return menu;
  72. };
  73. Layer.getToolBar = function () {
  74. var tb = EAction.getToolBar(Layer.getTitle(), "LayerToolBar");
  75. tb.visible = false;
  76. return tb;
  77. };
  78. Layer.getCadToolBarPanel = function () {
  79. var mtb = EAction.getMainCadToolBarPanel();
  80. var actionName = "LayerToolsPanelAction";
  81. if (!isNull(mtb) && mtb.findChild(actionName) == undefined) {
  82. var action = new RGuiAction(qsTr("Layer Tools"), mtb);
  83. action.setScriptFile(Layer.includeBasePath + "/Layer.js");
  84. action.objectName = actionName;
  85. action.setRequiresDocument(true);
  86. action.setIcon(Layer.includeBasePath + "/Layer.svg");
  87. //action.setStatusTip(qsTr("Show layer tools"));
  88. action.setDefaultShortcut(new QKeySequence("w,y"));
  89. action.setNoState();
  90. action.setDefaultCommands(["layermenu"]);
  91. action.setGroupSortOrder(60);
  92. action.setSortOrder(200);
  93. action.setWidgetNames(["!MainToolsPanel"]);
  94. }
  95. var tb = EAction.getCadToolBarPanel(
  96. Layer.getTitle(),
  97. "LayerToolsPanel",
  98. true
  99. );
  100. return tb;
  101. };
  102. Layer.getToolMatrixPanel = function () {
  103. RSettings.setValue("Layer/VisibleInMatrixPanel", false, false);
  104. return EAction.getToolMatrixPanel(Layer.getTitle(), "LayerMatrixPanel", 7000);
  105. };
  106. Layer.getTitle = function () {
  107. return qsTr("图层");
  108. // return qsTr("&Layer");
  109. };
  110. Layer.prototype.getTitle = function () {
  111. return Layer.getTitle();
  112. };
  113. Layer.init = function () {
  114. Layer.getMenu();
  115. Layer.getToolBar();
  116. Layer.getCadToolBarPanel();
  117. Layer.getToolMatrixPanel();
  118. };
  119. Layer.getShowFrozen = function () {
  120. if (hasPlugin("PROTOOLS")) {
  121. return RSettings.getBoolValue("LayerListPro/ShowFrozen", false);
  122. }
  123. else {
  124. return false;
  125. }
  126. };
  127. Layer.getFreezeLayer = function () {
  128. if (hasPlugin("PROTOOLS")) {
  129. return RSettings.getBoolValue("LayerListPro/FreezeLayer", true);
  130. }
  131. else {
  132. return true;
  133. }
  134. };
  135. /**
  136. * Show / hide all layers.
  137. *
  138. * \param show True to show, false to hide all
  139. * \param di RDocumentInterface
  140. * \param layerId ID of layer to exempt from the action
  141. * \param showProgress True to show progress information to progress listeners
  142. */
  143. Layer.showHide = function (show, di, layerId, showProgress) {
  144. if (showProgress === true) {
  145. if (show) {
  146. EAction.setProgressText(qsTr("Showing all layers"));
  147. }
  148. else {
  149. EAction.setProgressText(qsTr("Hiding all layers"));
  150. }
  151. }
  152. var showFrozen = Layer.getShowFrozen();
  153. var freezeLayer = Layer.getFreezeLayer();
  154. var operation = new RModifyObjectsOperation();
  155. var layers = di.getDocument().queryAllLayers();
  156. for (var l = 0; l < layers.length; ++l) {
  157. if (showProgress === true) {
  158. EAction.setProgress(100 / layers.length * l);
  159. }
  160. var layer = di.getDocument().queryLayer(layers[l]);
  161. if (layers[l] !== layerId) {
  162. if (showFrozen) {
  163. layer.setOff(!show);
  164. }
  165. else {
  166. if (freezeLayer) {
  167. layer.setFrozen(!show);
  168. }
  169. layer.setOff(!show);
  170. }
  171. } else {
  172. if (showFrozen) {
  173. layer.setOff(false);
  174. }
  175. else {
  176. layer.setOff(false);
  177. if (freezeLayer) {
  178. layer.setFrozen(false);
  179. }
  180. }
  181. }
  182. operation.addObject(layer);
  183. }
  184. di.applyOperation(operation);
  185. di.clearPreview();
  186. di.repaintViews();
  187. if (showProgress === true) {
  188. EAction.setProgressEnd();
  189. }
  190. };
  191. Layer.thawFreeze = function (freeze, di, layerId, showProgress) {
  192. if (showProgress === true) {
  193. if (freeze) {
  194. EAction.setProgressText(qsTr("Thawing all layers"));
  195. }
  196. else {
  197. EAction.setProgressText(qsTr("Freezing all layers"));
  198. }
  199. }
  200. var operation = new RModifyObjectsOperation();
  201. var layers = di.getDocument().queryAllLayers();
  202. for (var l = 0; l < layers.length; ++l) {
  203. if (showProgress === true) {
  204. EAction.setProgress(100 / layers.length * l);
  205. }
  206. var layer = di.getDocument().queryLayer(layers[l]);
  207. if (layers[l] !== layerId) {
  208. layer.setFrozen(!freeze);
  209. } else {
  210. layer.setFrozen(false);
  211. }
  212. operation.addObject(layer);
  213. }
  214. di.applyOperation(operation);
  215. di.clearPreview();
  216. di.repaintViews();
  217. if (showProgress === true) {
  218. EAction.setProgressEnd();
  219. }
  220. };
  221. Layer.lockUnlock = function (lock, di, showProgress) {
  222. if (showProgress === true) {
  223. if (lock) {
  224. EAction.setProgressText(qsTr("Locking all layers"));
  225. }
  226. else {
  227. EAction.setProgressText(qsTr("Unlocking all layers"));
  228. }
  229. }
  230. var operation = new RModifyObjectsOperation();
  231. operation.setTransactionType(RTransaction.LayerLockStatusChange);
  232. var layers = di.getDocument().queryAllLayers();
  233. for (var l = 0; l < layers.length; ++l) {
  234. if (showProgress === true) {
  235. EAction.setProgress(50 / layers.length * l);
  236. }
  237. var layer = di.getDocument().queryLayer(layers[l]);
  238. layer.setLocked(lock);
  239. operation.addObject(layer);
  240. }
  241. di.applyOperation(operation);
  242. di.clearPreview();
  243. if (showProgress === true) {
  244. EAction.setProgress(90);
  245. }
  246. di.repaintViews();
  247. if (showProgress === true) {
  248. EAction.setProgress(100);
  249. }
  250. if (showProgress === true) {
  251. EAction.setProgressEnd();
  252. }
  253. };
  254. Layer.removeAll = function (di, showProgress) {
  255. if (showProgress === true) {
  256. EAction.setProgressText(qsTr("remove all layers"));
  257. }
  258. var operation = new RModifyObjectsOperation();
  259. operation.setTransactionType(RTransaction.LayerLockStatusChange);
  260. var layers = di.getDocument().queryAllLayers();
  261. for (var l = 0; l < layers.length; ++l) {
  262. if (showProgress === true) {
  263. EAction.setProgress(50 / layers.length * l);
  264. }
  265. // 最后一个不删除
  266. if (l != layers.length - 1) {
  267. var layer = di.getDocument().queryLayer(layers[l]);
  268. var operation = new RDeleteObjectOperation(layer);
  269. di.applyOperation(operation);
  270. }
  271. }
  272. di.applyOperation(operation);
  273. di.clearPreview();
  274. if (showProgress === true) {
  275. EAction.setProgress(90);
  276. }
  277. di.repaintViews();
  278. if (showProgress === true) {
  279. EAction.setProgress(100);
  280. }
  281. if (showProgress === true) {
  282. EAction.setProgressEnd();
  283. }
  284. };
  285. /**
  286. * Can be reimplemented in derived classes to provide an advanced dialog.
  287. */
  288. Layer.prototype.createLayerDialog = function (di, layer) {
  289. return new LayerDialog(di, layer);
  290. };