/** * 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("../AddOn.js"); include("Edit.js"); function AbstractPreferences(guiAction, appPreferences, initialClassName) { Edit.call(this, guiAction); if (isNull(appPreferences)) { appPreferences = true; } this.appPreferences = appPreferences; this.restartWarningShown = false; this.initialClassName = initialClassName; } AbstractPreferences.prototype = new Edit(); AbstractPreferences.includeBasePath = includeBasePath; AbstractPreferences.prototype.beginEvent = function() { Edit.prototype.beginEvent.call(this); this.dialog = this.createWidget(AbstractPreferences.includeBasePath + "/AbstractPreferences.ui"); // TODO: Qt 5: add this flag (?) //var flags = new Qt.WindowFlags(Qt.WindowTitleHint); //this.dialog.setWindowFlags(flags); this.treeWidget = this.dialog.findChild("twCategory"); var title; if (this.appPreferences) { title = qsTr("Application Preferences"); } else { title = qsTr("Drawing Preferences"); } this.dialog.setWindowTitle(title); this.treeWidget.setHeaderLabel(title); this.pageWidget = this.dialog.findChild("stwPage"); this.filterWidget = this.dialog.findChild("leFilter"); this.titleWidget = this.dialog.findChild("lbTitle"); var splitter = this.dialog.findChild("splitter"); splitter.setStretchFactor(0, 1); splitter.setStretchFactor(1, 4); // connections: this.treeWidget.itemSelectionChanged.connect(this, this.showPage); this.filterWidget.textChanged.connect(this, this.filterTree); var btApply = this.dialog.findChild("buttonBox").button(QDialogButtonBox.Apply); btApply.clicked.connect(this, this.apply); this.addOns = AddOn.getAddOns(); AbstractPreferences.fillTreeWidget(this.addOns, this.treeWidget, this.appPreferences); this.treeWidget.expandAll(); if (!isNull(this.initialClassName)) { this.showPageFor(this.initialClassName); } if (this.dialog.exec() === QDialog.Accepted.valueOf()) { // apply calls save and apply: this.apply(); } this.uninit(); destr(this.dialog); EAction.activateMainWindow(); this.terminate(); }; /** * Initializes the navigation tree based on available add-ons with preferences. */ AbstractPreferences.fillTreeWidget = function(addOns, treeWidget, appPreferences) { for (var i = 0; i < addOns.length; ++i) { var addOn = addOns[i]; var className = addOn.getClassName(); var scriptFile = addOn.getFilePath(); if (appPreferences===false) { if (!scriptFile.contains("DrawingPreferences")) { continue; } } try { // include normally not needed var doInclude = false; if (isNull(global[className])) { doInclude = true; } if(doInclude) { include(addOn.getFilePath()); } } catch (e1) { qWarning("AppPreferences.js: Exception: ", e1); continue; } var cat; cat = undefined; try { if (!isNull(global[className]) && isFunction(global[className].getPreferencesCategory)) { cat = global[className].getPreferencesCategory(appPreferences); } if (isNull(cat)) { continue; } } catch (e2) { qWarning("AppPreferences.js: Add-on class not found: ", className, " exception: ", e2); continue; } if (!addOn.preferenceFileExists()) { continue; } // update tree var items = treeWidget.findItems(cat[0], Qt.MatchFixedString); var item; item = undefined; if (items.length === 0) { item = new QTreeWidgetItem(treeWidget, [ cat[0] ]); treeWidget.addTopLevelItem(item); if (cat.length === 1) { item.setData(0, Qt.UserRole, i); } } else { item = items[0]; } var parent = item; for (var x = 1; x < cat.length; ++x) { var subitem; subitem = undefined; for (var c = 0; c < parent.childCount(); ++c) { var child = parent.child(c); if (child.text(0) === cat[x]) { subitem = child; break; } } if (isNull(subitem)) { subitem = new QTreeWidgetItem(parent, [ cat[x] ]); parent.addChild(subitem); if (x == cat.length - 1) { subitem.setData(0, Qt.UserRole, i); } } parent = subitem; } } treeWidget.sortItems(0, Qt.AscendingOrder); }; /** * Applies the settings of all preference pages by calling * 'applyPreferences' for every add-on class. */ AbstractPreferences.prototype.apply = function() { var mdiChild, document; // force all settings to be reloaded from RSettings when they are // used next time: RSettings.resetCache(); this.save(); for (var i=0; i" + qsTr("These are default preferences for new drawings.") + "
" + qsTr("Changes will affect new drawings but NOT the current drawing.") + "
" + qsTr("Preferences of the current drawing can be changed under
Edit > Drawing Preferences.") + ""; warningLabel.visible = true; } } var pText = ""; while (!isNull(parent)) { pText += parent.text(0) + ": " + pText; parent = parent.parent(); } if (pText == "") { this.titleWidget.text = item.text(0); } else { this.titleWidget.text = pText + item.text(0); } var i = item.data(0, Qt.UserRole); var widget; if (!isNull(i)) { var addOn = this.addOns[i]; widget = addOn.getPreferenceWidget(); if (isNull(widget)) { var prefFile = addOn.getPreferenceFile(); widget = this.createWidget(prefFile); this.pageWidget.addWidget(widget); addOn.setPreferenceWidget(widget); // loads (initializes) the page: this.load(addOn); var treeWidget = this.treeWidget; widget.settingChangedEvent = function() { // mark changed category in bold: var font = treeWidget.currentItem().font(0); font.setBold(true); treeWidget.currentItem().setFont(0, font); }; var btReset = this.dialog.findChild("ResetToDefaults"); try { btReset.clicked.disconnect(this, this.reset); } catch (e) { // ignored: signal might not be connected } btReset.clicked.connect(this, this.reset); } } else { widget = this.pageWidget.findChild("empty"); } this.pageWidget.setCurrentWidget(widget); this.treeWidget.setCurrentItem(item); }; AbstractPreferences.prototype.showPageFor = function(className) { var flags = makeQtMatchFlags(Qt.MatchWildcard, Qt.MatchContains, Qt.MatchRecursive); var items = this.treeWidget.findItems("*", flags, 0); for (var i = 0; i < items.length; ++i) { var item = items[i]; var ix = item.data(0, Qt.UserRole); if (!isNull(ix)) { var addOn = this.addOns[ix]; if (addOn.getClassName()===className) { this.treeWidget.setCurrentItem(item); } } } }; /** * Called when user clicks reset button on preferences page. */ AbstractPreferences.prototype.reset = function() { var widget = this.pageWidget.currentWidget(); //qDebug("AbstractPreferences.js:", "reset(): widget.objectName:", widget.objectName); WidgetFactory.resetState(widget); var treeWidget = this.treeWidget; var font = treeWidget.currentItem().font(0); font.setBold(true); treeWidget.currentItem().setFont(0, font); widget.setProperty("hasChanged", true); }; /** * Called when the global or drawing unit changes. Notifies all loaded * add-on pages. */ AbstractPreferences.prototype.unitUpdated = function(unit) { for (var i=0; i