| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- /**
- * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
- *
- * This file is part of the QCAD project.
- *
- * QCAD is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * QCAD is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with QCAD.
- */
- include("scripts/WidgetFactory.js");
- function RLayerListQt(parent) {
- RListWidget.call(this, parent);
- if (RS.getSystemId()==="osx") {
- this.setIconOffset(7);
- }
- this.iconSize = new QSize(32, 16);
- this.di = undefined;
- var appWin = EAction.getMainWindow();
- var adapter = new RLayerListenerAdapter();
- appWin.addLayerListener(adapter);
- if (RSettings.getQtVersion() < 0x060000) {
- adapter.layersUpdated.connect(this, "updateLayers");
- adapter.currentLayerSet.connect(this, "updateLayers");
- adapter.layersCleared.connect(this, "clearLayers");
- this.setProperty("listener", adapter);
- this.itemSelectionChanged.connect(this, "layerActivated");
- this.itemDoubleClicked.connect(this, "moveSelectionToLayer");
- this.iconClicked.connect(this, "iconClickedSlot");
- }
- else {
- adapter.layersUpdated.connect(this, this.updateLayers);
- adapter.currentLayerSet.connect(this, this.updateLayers);
- adapter.layersCleared.connect(this, this.clearLayers);
- this.setProperty("listener", adapter);
- this.itemSelectionChanged.connect(this, this.layerActivated);
- this.itemDoubleClicked.connect(this, this.moveSelectionToLayer);
- this.iconClicked.connect(this, this.iconClickedSlot);
- }
- this.basePath = includeBasePath;
- }
- RLayerListQt.prototype = new RListWidget();
- RLayerListQt.prototype.toString = function() {
- return "RLayerListQt()";
- };
- RLayerListQt.prototype.contextMenuEvent = function(e) {
- var item = this.itemAt(e.pos());
- if (!isNull(item)) {
- this.setCurrentItem(item);
- }
- var menu = new QMenu(this);
- menu.objectName = "ContextMenu";
- RGuiAction.getByScriptFile("scripts/Layer/ToggleLayerVisibility/ToggleLayerVisibility.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/ShowAllLayers/ShowAllLayers.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/HideAllLayers/HideAllLayers.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/ShowActiveLayer/ShowActiveLayer.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/AddLayer/AddLayer.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/RemoveLayer/RemoveLayer.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/EditLayer/EditLayer.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/SelectLayer/SelectLayer.js").addToMenu(menu);
- RGuiAction.getByScriptFile("scripts/Layer/DeselectLayer/DeselectLayer.js").addToMenu(menu);
- menu.exec(QCursor.pos());
- e.ignore();
- };
- RLayerListQt.prototype.updateLayers = function(documentInterface, previousLayerId) {
- this.di = documentInterface;
- var layer;
- var pos = this.verticalScrollBar().sliderPosition;
- this.clear();
- if (isNull(documentInterface)) {
- return;
- }
- var doc = documentInterface.getDocument();
- var result = doc.queryAllLayers();
- for ( var i = 0; i < result.length; ++i) {
- var id = result[i];
- layer = doc.queryLayer(id);
- if (isFunction(layer.isNull) && layer.isNull()) {
- continue;
- }
- var item = new QListWidgetItem(layer.getName(), this);
- var iconName = autoIconPath(this.basePath
- + "/layerstatus_%1%2.svg".arg(Number(layer.isFrozen()))
- .arg(Number(layer.isLocked())));
- item.setIcon(new QIcon(iconName));
- if (layer.isProtected()) {
- item.setData(Qt.UserRole, true);
- }
- this.addItem(item);
- }
- this.sortItems();
- layer = doc.queryCurrentLayer();
- if (!isFunction(layer.isNull) || !layer.isNull()) {
- var items = this.findItems(layer.getName(), Qt.MatchExactly);
- if (items.length !== 0) {
- this.blockSignals(true);
- this.setCurrentItem(items[0]);
- this.blockSignals(false);
- }
- }
- this.verticalScrollBar().sliderPosition = pos;
- };
- RLayerListQt.prototype.clearLayers = function() {
- this.clear();
- };
- RLayerListQt.prototype.iconClickedSlot = function(x, item) {
- if (isNull(this.di) || isNull(item)) {
- return;
- }
- var doc = this.di.getDocument();
- var layer = doc.queryLayer(item.text());
- if (isFunction(layer.isNull) && layer.isNull()) {
- return;
- }
- if (x < this.iconSize.width() / 2) {
- var off = !layer.isFrozen();
- layer.setFrozen(off);
- layer.setOff(off);
- } else {
- layer.setLocked(!layer.isLocked());
- }
- var op = new RModifyObjectOperation(layer);
- this.di.applyOperation(op);
- this.updateLayers(this.di);
- };
- RLayerListQt.prototype.moveSelectionToLayer = function() {
- if (isNull(this.di)) {
- return;
- }
- var list = this.selectedItems();
- if (list.length===0) {
- return;
- }
- var item = list[0];
- if (isNull(item)) {
- return;
- }
- if (!this.di.hasSelection()) {
- // no selection: edit layer:
- var a = RGuiAction.getByScriptFile("scripts/Layer/EditLayer/EditLayer.js");
- a.slotTrigger();
- return;
- }
- var op = new RChangePropertyOperation(REntity.PropertyLayer, item.text());
- this.di.applyOperation(op);
- };
- RLayerListQt.prototype.layerActivated = function() {
- if (isNull(this.di)) {
- return;
- }
- var list = this.selectedItems();
- if (list.length===0) {
- return;
- }
- var item = list[0];
- if (isNull(item)) {
- return;
- }
- this.blockSignals(true);
- this.di.setCurrentLayer(item.text());
- this.blockSignals(false);
- };
- function LayerList(guiAction) {
- EAction.call(this, guiAction);
- }
- LayerList.prototype = new EAction();
- LayerList.getPreferencesCategory = function() {
- return [ qsTr("Widgets"), qsTr("Layer List") ];
- };
- LayerList.applyPreferences = function(doc, mdiChild) {
- var appWin = RMainWindowQt.getMainWindow();
- appWin.notifyLayerListeners(EAction.getDocumentInterface(), []);
- //if (RSettings.getBoolValue("LayerList/AlternatingRowColors", false)===true) {
- var layerList = appWin.findChild("LayerList");
- WidgetFactory.initList(layerList, "LayerList");
- //}
- };
- /**
- * Shows / hides the layer list.
- */
- LayerList.prototype.beginEvent = function() {
- EAction.prototype.beginEvent.call(this);
- var appWin = RMainWindowQt.getMainWindow();
- var dock = appWin.findChild("LayerListDock");
- if (!RSettings.getOriginalArguments().contains("-no-show")) {
- dock.visible = !dock.visible;
- if (dock.visible) dock.raise();
- }
- };
- LayerList.prototype.finishEvent = function() {
- Widgets.prototype.finishEvent.call(this);
- var appWin = RMainWindowQt.getMainWindow();
- var dock = appWin.findChild("LayerListDock");
- this.getGuiAction().setChecked(dock.visible);
- };
- LayerList.uninit = function() {
- var action = RGuiAction.getByScriptFile("scripts/Widgets/LayerList/LayerList.js");
- action.visible = false;
- var appWin = RMainWindowQt.getMainWindow();
- var layerList = appWin.findChild("LayerList");
- if (!isNull(layerList)) {
- appWin.removeLayerListener(layerList.property("listener"));
- }
- var basicDock = appWin.findChild("LayerListDock");
- if (!isNull(basicDock)) {
- destr(basicDock);
- }
- LayerList.getPreferencesCategory = undefined;
- };
- LayerList.init = function(basePath) {
- var appWin = RMainWindowQt.getMainWindow();
- var action = new RGuiAction(qsTr("&Layer List"), appWin);
- action.setRequiresDocument(false);
- action.setScriptFile(basePath + "/LayerList.js");
- action.setIcon(basePath + "/LayerList.svg");
- action.setDefaultShortcut(new QKeySequence("g,y"));
- action.setDefaultCommands(["gy"]);
- action.setGroupSortOrder(3600);
- action.setSortOrder(100);
- action.setWidgetNames(["ViewMenu", "WidgetsToolBar", "ViewToolsPanel", "WidgetMatrixPanel"]);
- var formWidget = WidgetFactory.createWidget(basePath, "LayerList.ui");
- var layout = formWidget.findChild("verticalLayout");
- var layerList = new RLayerListQt(formWidget);
- layerList.objectName = "LayerList";
- layout.addWidget(layerList, 1, 0);
- RSettings.setValue("LayerList/AlternatingRowColor", new RColor(230, 235, 250), false);
- WidgetFactory.initList(layerList, "LayerList");
- layerList.itemSelectionChanged.connect(function() {
- var action = RGuiAction.getByScriptFile("scripts/Layer/RemoveLayer/RemoveLayer.js");
- if (isNull(action)) {
- return;
- }
- var list = layerList.selectedItems();
- if (list.length === 0) {
- return;
- }
- var item = list[0];
- if (isNull(item)) {
- return;
- }
- // protected layers:
- if (item.text() === "0" || item.data(Qt.UserRole)===true) {
- action.setEnabledOverride(false, 0);
- } else {
- action.setEnabledOverride(true, 1);
- }
- });
- var widgets = getWidgets(formWidget);
- widgets["ShowAll"].setDefaultAction(
- RGuiAction.getByScriptFile("scripts/Layer/ShowAllLayers/ShowAllLayers.js"));
- widgets["HideAll"].setDefaultAction(
- RGuiAction.getByScriptFile("scripts/Layer/HideAllLayers/HideAllLayers.js"));
- widgets["Add"].setDefaultAction(
- RGuiAction.getByScriptFile("scripts/Layer/AddLayer/AddLayer.js"));
- widgets["Remove"].setDefaultAction(
- RGuiAction.getByScriptFile("scripts/Layer/RemoveLayer/RemoveLayer.js"));
- widgets["Edit"].setDefaultAction(
- RGuiAction.getByScriptFile("scripts/Layer/EditLayer/EditLayer.js"));
- var dock = new RDockWidget(qsTr("Layer List"), appWin);
- dock.objectName = "LayerListDock";
- //dock.setProperty("Category", Widgets.getListContextMenuCategory());
- dock.setWidget(formWidget);
- appWin.addDockWidget(Qt.RightDockWidgetArea, dock);
- dock.shown.connect(function() { action.setChecked(true); });
- dock.hidden.connect(function() { action.setChecked(false); });
- };
|