/** * 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/Widgets/Widgets.js"); include("scripts/WidgetFactory.js"); /** * Block list widget class (QWidget). */ function RBlockListQt(parent, addListener, showHeader) { RTreeWidget.call(this, parent); if (isNull(addListener)) { addListener = true; } if (isNull(showHeader)) { showHeader = false; } this.di = undefined; if (!showHeader) { this.header().close(); } this.iconSize = new QSize(16, 16); this.indentation = 0; this.rootIsDecorated = false; this.setSelectableColumn(2); this.columnCount = 3; this.header().stretchLastSection = false; if (RSettings.isQt(5)) { this.header().minimumSectionSize = 22; this.header().setSectionResizeMode(BlockList.colName, QHeaderView.Stretch); this.header().setSectionResizeMode(BlockList.colVisible, QHeaderView.Interactive); this.header().setSectionResizeMode(BlockList.colEdit, QHeaderView.Interactive); } else { this.header().setResizeMode(BlockList.colName, QHeaderView.Stretch); this.header().setResizeMode(BlockList.colVisible, QHeaderView.Interactive); this.header().setResizeMode(BlockList.colEdit, QHeaderView.Interactive); } this.setColumnWidth(BlockList.colVisible, 22); this.setColumnWidth(BlockList.colEdit, 22); var self = this; if (addListener) { var appWin = EAction.getMainWindow(); var adapter = new RBlockListenerAdapter(); appWin.addBlockListener(adapter); adapter.blocksUpdated.connect(function(di) { self.updateBlocks(di); }); adapter.currentBlockSet.connect(function(di) { self.updateCurrentBlock(di); }); adapter.blocksCleared.connect(function() { self.clearBlocks(); }); } this.itemDoubleClicked.connect(function(item, col) { self.editBlock(item, col); }); this.itemColumnClicked.connect(function(item, col) { self.itemColumnClickedSlot(item, col); }); this.itemSelectionChanged.connect(function() { self.blockActivated(); }); this.basePath = includeBasePath; this.currentBlockItem = undefined; } RBlockListQt.prototype = new RTreeWidget(); RBlockListQt.getWidget = function() { var appWin = EAction.getMainWindow(); return appWin.findChild("BlockList"); }; /** * Add an additional action to be used in the context menu. * Can be used by plugins to hook into the block list context menu. */ RBlockListQt.addContextMenuScriptFile = function(scriptFile, blockList) { //var blockList = RBlockListQt.getWidget(); if (isNull(blockList)) { return; } var scriptFiles = RBlockListQt.getContextMenuScriptFiles(blockList); scriptFiles.push(scriptFile); blockList.setProperty("ContextMenuScriptFiles", scriptFiles); }; RBlockListQt.getContextMenuScriptFiles = function(blockList) { //var blockList = RBlockListQt.getWidget(); if (isNull(blockList)) { return []; } var scriptFiles = blockList.property("ContextMenuScriptFiles"); if (isNull(scriptFiles)) { scriptFiles = []; } return scriptFiles; }; RBlockListQt.prototype.contextMenuEvent = function(e) { var item = this.itemAt(e.pos()); if (!isNull(item)) { this.setCurrentItem(item); } var menu = new QMenu(); RGuiAction.getByScriptFile("scripts/Block/ToggleBlockVisibility/ToggleBlockVisibility.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/ShowAllBlocks/ShowAllBlocks.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/HideAllBlocks/HideAllBlocks.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/AddBlock/AddBlock.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/RemoveBlock/RemoveBlock.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/RenameBlock/RenameBlock.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/EditBlock/EditBlock.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/InsertBlock/InsertBlock.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/SelectBlockReferences/SelectBlockReferences.js").addToMenu(menu); RGuiAction.getByScriptFile("scripts/Block/DeselectBlockReferences/DeselectBlockReferences.js").addToMenu(menu); RBlockListQt.complementContextMenu(menu, RBlockListQt.getWidget()); menu.exec(QCursor.pos()); e.ignore(); }; RBlockListQt.complementContextMenu = function(menu, blockListWidget) { // add additional context menu actions provided by plugins: var scriptFiles = RBlockListQt.getContextMenuScriptFiles(blockListWidget); for (var i=0; i0) { title = layoutName + " (" + title + ")"; } return title; }; RBlockListQt.prototype.createBlockItem = function(block) { var item = new QTreeWidgetItem(); var name = block.getName(); var flags = makeQtItemFlags(Qt.ItemIsSelectable, Qt.ItemIsEnabled); item.setFlags(flags); var title = RBlockListQt.getBlockTitle(block); item.setText(BlockList.colName, title); item.setData(BlockList.colName, Qt.UserRole, name); this.updateItemIcons(item, block); return item; }; RBlockListQt.prototype.updateItemIcons = function(item, block) { //var iconName = autoIconPath(BlockList.includeBasePath + "/Visible%1.svg".arg(Number(!block.isFrozen()))); item.setIcon(BlockList.colVisible, BlockList.iconVisible[Number(!block.isFrozen())]); var doc = this.di.getDocument(); var currentBlockId = doc.getCurrentBlockId(); //iconName = autoIconPath(BlockList.includeBasePath + "/Edit%1.svg".arg(Number(block.getId()===currentBlockId))); item.setIcon(BlockList.colEdit, BlockList.iconEdit[Number(block.getId()===currentBlockId)]); }; /** * Called when no document is active. Clears the block list. */ RBlockListQt.prototype.clearBlocks = function() { this.clear(); }; /** * Called when the user clicks the icon beside the block name to hide * all block inserts of that block. */ RBlockListQt.prototype.itemColumnClickedSlot = function(item, column) { if (isNull(this.di) || isNull(item)) { return; } var doc = this.di.getDocument(); var block = doc.queryBlock(this.getBlockName(item)); if (isFunction(block.isNull) && block.isNull()) { return; } if (column===BlockList.colVisible) { block.setFrozen(!block.isFrozen()); var op = new RModifyObjectOperation(block, false); this.di.applyOperation(op); //this.updateBlocks(this.di); } else if (column===BlockList.colEdit) { this.setCurrentItem(item); this.editBlock(); //this.updateBlocks(this.di); } this.blockActivated(); }; /** * Called when a block name is activated (clicked, highlighted) in the block list. */ RBlockListQt.prototype.blockActivated = function() { if (isNull(this.di)) { this.enableActions(false); return; } var list = this.selectedItems(); if (list.length===0) { this.enableActions(false); return; } var item = list[0]; if (isNull(item)) { this.enableActions(false); return; } var document = this.di.getDocument(); if (isNull(document)) { this.enableActions(false); return; } var currentBlock = document.queryCurrentBlock(); if (isNull(currentBlock)) { this.enableActions(false); return; } var blockName = this.getBlockName(item); // prevent removal or insertion of current block: var insertable = currentBlock.getName().toLowerCase()!==blockName.toLowerCase() && !blockName.startsWith("*"); var renamable = document.getBlockId(blockName)!==document.getModelSpaceBlockId(); var removable = document.getBlockId(blockName)!==document.getModelSpaceBlockId(); this.enableActions(insertable, renamable, removable); }; /** * \return The block name for the given tree widget item. */ RBlockListQt.prototype.getBlockName = function(item) { return item.data(BlockList.colName, Qt.UserRole); }; /** * \return The list item for the block with the given block name. */ RBlockListQt.getItem = function(widget, blockName) { for (var i=0; i0x060000) { layout.addWidget(blockList); } else { layout.addWidget(blockList, 1, 0); } RSettings.setValue("BlockList/AlternatingRowColor", new RColor(230, 235, 250), false); WidgetFactory.initList(blockList, "BlockList"); var button; button = formWidget.findChild("ShowAllBlocks"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/ShowAllBlocks/ShowAllBlocks.js")); button = formWidget.findChild("HideAllBlocks"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/HideAllBlocks/HideAllBlocks.js")); button = formWidget.findChild("AddBlock"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/AddBlock/AddBlock.js")); button = formWidget.findChild("RemoveBlock"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/RemoveBlock/RemoveBlock.js")); button = formWidget.findChild("RenameBlock"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/RenameBlock/RenameBlock.js")); button = formWidget.findChild("EditBlock"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/EditBlock/EditBlock.js")); button = formWidget.findChild("InsertBlock"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/InsertBlock/InsertBlock.js")); // button = formWidget.findChild("CreateBlock"); // button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/CreateBlock/CreateBlock.js")); button = formWidget.findChild("CloseBlock"); button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/EditMainDrawing/EditMainDrawing.js")); var dock = new RDockWidget(qsTr("Block List"), appWin); dock.objectName = "BlockListDock"; //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); }); dock.visible = false; };