-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
pluginsettings.py
89 lines (68 loc) · 3.42 KB
/
pluginsettings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
# (C) 2014 Minoru Akagi
# SPDX-License-Identifier: GPL-2.0-or-later
# begin: 2014-01-10
from configparser import ConfigParser
import os
from PyQt5.QtCore import Qt, QDir, QSettings
from PyQt5.QtWidgets import QDialog, QFileDialog, QAbstractItemView, QHeaderView, QTableWidgetItem
from .utils import logMessage, pluginDir
from .ui.settingsdialog import Ui_SettingsDialog
class SettingsDialog(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent)
# Set up the user interface from Designer.
self.ui = ui = Ui_SettingsDialog()
ui.setupUi(self)
ui.lineEdit_BrowserPath.setPlaceholderText("Leave this empty to use your default browser")
ui.pushButton_Browse.clicked.connect(self.browseClicked)
# load settings
settings = QSettings()
ui.lineEdit_BrowserPath.setText(settings.value("/Qgis2threejs/browser", "", type=str))
enabled_plugins = QSettings().value("/Qgis2threejs/plugins", "", type=str).split(",")
# initialize plugin table widget
plugin_dir = QDir(pluginDir("plugins"))
plugins = plugin_dir.entryList(QDir.Dirs | QDir.NoSymLinks | QDir.NoDotAndDotDot)
tableWidget = ui.tableWidget_Plugins
tableWidget.setColumnCount(1)
tableWidget.setHorizontalHeaderLabels(["Name"])
tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
headerView = tableWidget.horizontalHeader()
headerView.setSectionResizeMode(QHeaderView.Stretch)
self.plugin_metadata = []
for i, name in enumerate(plugins):
if name[0] == "_": # skip __pycache__ dir.
continue
parser = ConfigParser()
try:
with open(os.path.join(plugin_dir.absoluteFilePath(name), "metadata.txt"), "r", encoding="utf-8") as f:
parser.read_file(f)
metadata = dict(parser.items("general"))
self.plugin_metadata.append(metadata)
except Exception as e:
logMessage("Unable to read metadata of plugin: {} ({})".format(name, e), error=True)
tableWidget.setRowCount(len(self.plugin_metadata))
for i, metadata in enumerate(self.plugin_metadata):
item = QTableWidgetItem(metadata.get("name", name))
item.setCheckState(Qt.Checked if name in enabled_plugins else Qt.Unchecked)
tableWidget.setItem(i, 0, item)
tableWidget.selectionModel().currentRowChanged.connect(self.pluginSelectionChanged)
def pluginSelectionChanged(self, current, previous):
metadata = self.plugin_metadata[current.row()]
self.ui.textBrowser_Plugin.setHtml(metadata.get("description"))
def accept(self):
settings = QSettings()
# general settings
settings.setValue("/Qgis2threejs/browser", self.ui.lineEdit_BrowserPath.text())
# plugins
enabled_plugins = []
for i, metadata in enumerate(self.plugin_metadata):
item = self.ui.tableWidget_Plugins.item(i, 0)
if item.checkState() == Qt.Checked:
enabled_plugins.append(metadata["id"])
settings.setValue("/Qgis2threejs/plugins", ",".join(enabled_plugins))
QDialog.accept(self)
def browseClicked(self):
filename, _ = QFileDialog.getOpenFileName(self, self.tr("Select browser"))
if filename != "":
self.ui.lineEdit_BrowserPath.setText(filename)