2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ConfigDialog.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program 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 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2013-05-24 00:38:29 +02:00
|
|
|
import os
|
2012-12-05 16:38:14 +02:00
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from PyQt4.QtCore import Qt, QEvent
|
|
|
|
from PyQt4.QtGui import QFileDialog, QDialog, QIcon, QStyle, QStandardItemModel, QStandardItem, QMessageBox, QStyledItemDelegate, QLineEdit, QSpinBox, QDoubleSpinBox, QWidget, QToolButton, QHBoxLayout
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
2014-06-13 06:25:50 +02:00
|
|
|
from processing.core.Processing import Processing
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.ui.ui_DlgConfig import Ui_DlgConfig
|
2012-09-15 18:25:25 +03:00
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
#import processing.resources_rc
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-01 17:56:18 +02:00
|
|
|
class ConfigDialog(QDialog, Ui_DlgConfig):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def __init__(self, toolbox):
|
2012-12-01 17:56:18 +02:00
|
|
|
QDialog.__init__(self)
|
|
|
|
self.setupUi(self)
|
2012-09-15 18:25:25 +03:00
|
|
|
self.toolbox = toolbox
|
2012-12-01 17:56:18 +02:00
|
|
|
self.groupIcon = QIcon()
|
2013-10-01 20:52:22 +03:00
|
|
|
self.groupIcon.addPixmap(self.style().standardPixmap(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
QStyle.SP_DirClosedIcon), QIcon.Normal, QIcon.Off)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.groupIcon.addPixmap(self.style().standardPixmap(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
QStyle.SP_DirOpenIcon), QIcon.Normal, QIcon.On)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-01 20:22:21 +02:00
|
|
|
if hasattr(self.searchBox, 'setPlaceholderText'):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.searchBox.setPlaceholderText(self.tr('Search...'))
|
2012-12-01 20:22:21 +02:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
self.model = QStandardItemModel()
|
|
|
|
self.tree.setModel(self.model)
|
|
|
|
|
|
|
|
self.delegate = SettingDelegate()
|
|
|
|
self.tree.setItemDelegateForColumn(1, self.delegate)
|
|
|
|
|
2012-12-05 16:49:49 +02:00
|
|
|
self.searchBox.textChanged.connect(self.fillTree)
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2012-12-01 17:56:18 +02:00
|
|
|
self.fillTree()
|
2013-05-01 23:45:20 +02:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
self.tree.expanded.connect(self.adjustColumns)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def fillTree(self):
|
|
|
|
self.items = {}
|
2012-12-05 16:38:14 +02:00
|
|
|
self.model.clear()
|
2013-10-01 20:52:22 +03:00
|
|
|
self.model.setHorizontalHeaderLabels([self.tr('Setting'),
|
|
|
|
self.tr('Value')])
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2013-06-03 21:25:22 +02:00
|
|
|
text = unicode(self.searchBox.text())
|
2013-08-12 20:44:27 +02:00
|
|
|
settings = ProcessingConfig.getSettings()
|
2012-12-05 16:38:14 +02:00
|
|
|
|
|
|
|
rootItem = self.model.invisibleRootItem()
|
2014-10-03 21:56:24 +03:00
|
|
|
priorityKeys = [self.tr('General'), self.tr('Models'), self.tr('Scripts')]
|
2013-04-21 15:12:00 +02:00
|
|
|
for group in priorityKeys:
|
2012-12-05 16:38:14 +02:00
|
|
|
groupItem = QStandardItem(group)
|
2013-08-12 20:44:27 +02:00
|
|
|
icon = ProcessingConfig.getGroupIcon(group)
|
2012-12-05 16:38:14 +02:00
|
|
|
groupItem.setIcon(icon)
|
|
|
|
groupItem.setEditable(False)
|
|
|
|
emptyItem = QStandardItem()
|
|
|
|
emptyItem.setEditable(False)
|
|
|
|
rootItem.insertRow(0, [groupItem, emptyItem])
|
2012-09-15 18:25:25 +03:00
|
|
|
for setting in settings[group]:
|
|
|
|
if setting.hidden:
|
|
|
|
continue
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
if text == '' or text.lower() in setting.description.lower():
|
2012-12-05 16:38:14 +02:00
|
|
|
labelItem = QStandardItem(setting.description)
|
|
|
|
labelItem.setIcon(icon)
|
|
|
|
labelItem.setEditable(False)
|
2013-09-16 15:58:30 +03:00
|
|
|
self.items[setting] = SettingItem(setting)
|
|
|
|
groupItem.insertRow(0, [labelItem, self.items[setting]])
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
if text != '':
|
2012-12-05 16:38:14 +02:00
|
|
|
self.tree.expand(groupItem.index())
|
2013-05-01 23:45:20 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
providersItem = QStandardItem(self.tr('Providers'))
|
|
|
|
icon = QIcon(':/processing/images/alg.png')
|
2012-12-05 16:38:14 +02:00
|
|
|
providersItem.setIcon(icon)
|
|
|
|
providersItem.setEditable(False)
|
|
|
|
emptyItem = QStandardItem()
|
|
|
|
emptyItem.setEditable(False)
|
|
|
|
rootItem.insertRow(0, [providersItem, emptyItem])
|
2013-04-21 15:12:00 +02:00
|
|
|
for group in settings.keys():
|
|
|
|
if group in priorityKeys:
|
|
|
|
continue
|
2012-12-05 16:38:14 +02:00
|
|
|
|
|
|
|
groupItem = QStandardItem(group)
|
2013-08-12 20:44:27 +02:00
|
|
|
icon = ProcessingConfig.getGroupIcon(group)
|
2012-12-05 16:38:14 +02:00
|
|
|
groupItem.setIcon(icon)
|
|
|
|
groupItem.setEditable(False)
|
2013-04-21 15:12:00 +02:00
|
|
|
for setting in settings[group]:
|
|
|
|
if setting.hidden:
|
|
|
|
continue
|
2012-12-01 17:56:18 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
if text == '' or text.lower() in setting.description.lower():
|
2012-12-05 16:38:14 +02:00
|
|
|
labelItem = QStandardItem(setting.description)
|
|
|
|
labelItem.setIcon(icon)
|
|
|
|
labelItem.setEditable(False)
|
2013-09-16 15:58:30 +03:00
|
|
|
self.items[setting] = SettingItem(setting)
|
|
|
|
groupItem.insertRow(0, [labelItem, self.items[setting]])
|
2012-12-05 16:38:14 +02:00
|
|
|
|
2014-02-13 13:25:44 +07:00
|
|
|
emptyItem = QStandardItem()
|
|
|
|
emptyItem.setEditable(False)
|
2012-12-05 16:38:14 +02:00
|
|
|
providersItem.appendRow([groupItem, emptyItem])
|
|
|
|
|
|
|
|
self.tree.sortByColumn(0, Qt.AscendingOrder)
|
|
|
|
self.adjustColumns()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-01 17:56:18 +02:00
|
|
|
def accept(self):
|
2012-09-15 18:25:25 +03:00
|
|
|
for setting in self.items.keys():
|
2012-12-05 16:38:14 +02:00
|
|
|
if isinstance(setting.value, bool):
|
2013-10-01 20:52:22 +03:00
|
|
|
setting.value = self.items[setting].checkState() == Qt.Checked
|
2012-12-05 16:38:14 +02:00
|
|
|
elif isinstance(setting.value, (float, int, long)):
|
2014-03-24 20:00:23 +02:00
|
|
|
value = unicode(self.items[setting].text())
|
2012-09-15 18:25:25 +03:00
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
setting.value = value
|
|
|
|
except ValueError:
|
2013-10-01 20:52:22 +03:00
|
|
|
QMessageBox.critical(self, self.tr('Wrong value'),
|
2014-10-03 21:56:24 +03:00
|
|
|
self.tr('Wrong parameter value:\n%1') % value)
|
2012-09-15 18:25:25 +03:00
|
|
|
return
|
|
|
|
else:
|
2014-03-24 20:00:23 +02:00
|
|
|
setting.value = unicode(self.items[setting].text())
|
2014-04-23 18:24:23 +02:00
|
|
|
setting.save()
|
2014-06-13 06:25:50 +02:00
|
|
|
Processing.updateAlgsList()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-01 17:56:18 +02:00
|
|
|
QDialog.accept(self)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
def adjustColumns(self):
|
|
|
|
self.tree.resizeColumnToContents(0)
|
|
|
|
self.tree.resizeColumnToContents(1)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
class SettingItem(QStandardItem):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
def __init__(self, setting):
|
|
|
|
QStandardItem.__init__(self)
|
2012-09-15 18:25:25 +03:00
|
|
|
self.setting = setting
|
2012-12-05 16:38:14 +02:00
|
|
|
|
|
|
|
if isinstance(setting.value, bool):
|
|
|
|
self.setCheckable(True)
|
|
|
|
self.setEditable(False)
|
2012-09-15 18:25:25 +03:00
|
|
|
if setting.value:
|
2012-12-05 16:38:14 +02:00
|
|
|
self.setCheckState(Qt.Checked)
|
|
|
|
else:
|
|
|
|
self.setCheckState(Qt.Unchecked)
|
|
|
|
else:
|
|
|
|
self.setData(setting.value, Qt.EditRole)
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
class SettingDelegate(QStyledItemDelegate):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
QStyledItemDelegate.__init__(self, parent)
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def createEditor(
|
|
|
|
self,
|
|
|
|
parent,
|
|
|
|
options,
|
|
|
|
index,
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
):
|
2012-12-05 16:38:14 +02:00
|
|
|
value = self.convertValue(index.model().data(index, Qt.EditRole))
|
|
|
|
if isinstance(value, (int, long)):
|
|
|
|
spnBox = QSpinBox(parent)
|
|
|
|
spnBox.setRange(-999999999, 999999999)
|
|
|
|
return spnBox
|
|
|
|
elif isinstance(value, float):
|
|
|
|
spnBox = QDoubleSpinBox(parent)
|
2013-09-16 15:58:30 +03:00
|
|
|
spnBox.setRange(-999999999.999999, 999999999.999999)
|
2012-12-05 16:38:14 +02:00
|
|
|
spnBox.setDecimals(6)
|
|
|
|
return spnBox
|
|
|
|
elif isinstance(value, (str, unicode)):
|
|
|
|
if os.path.isdir(value):
|
|
|
|
return FileDirectorySelector(parent)
|
|
|
|
elif os.path.isfile(value):
|
|
|
|
return FileDirectorySelector(parent, True)
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2012-12-05 16:38:14 +02:00
|
|
|
return FileDirectorySelector(parent, True)
|
|
|
|
|
|
|
|
def setEditorData(self, editor, index):
|
|
|
|
value = self.convertValue(index.model().data(index, Qt.EditRole))
|
|
|
|
if isinstance(value, (int, long)):
|
|
|
|
editor.setValue(value)
|
|
|
|
elif isinstance(value, float):
|
|
|
|
editor.setValue(value)
|
|
|
|
elif isinstance(value, (str, unicode)):
|
|
|
|
editor.setText(value)
|
|
|
|
|
|
|
|
def setModelData(self, editor, model, index):
|
|
|
|
value = self.convertValue(index.model().data(index, Qt.EditRole))
|
|
|
|
if isinstance(value, (int, long)):
|
|
|
|
model.setData(index, editor.value(), Qt.EditRole)
|
|
|
|
elif isinstance(value, float):
|
|
|
|
model.setData(index, editor.value(), Qt.EditRole)
|
|
|
|
elif isinstance(value, (str, unicode)):
|
|
|
|
model.setData(index, editor.text(), Qt.EditRole)
|
|
|
|
|
|
|
|
def sizeHint(self, option, index):
|
|
|
|
return QSpinBox().sizeHint()
|
|
|
|
|
|
|
|
def eventFilter(self, editor, event):
|
|
|
|
if event.type() == QEvent.FocusOut and hasattr(editor, 'canFocusOut'):
|
|
|
|
if not editor.canFocusOut:
|
|
|
|
return False
|
|
|
|
return QStyledItemDelegate.eventFilter(self, editor, event)
|
|
|
|
|
|
|
|
def convertValue(self, value):
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
if value is None:
|
2014-10-22 01:21:45 +02:00
|
|
|
return ""
|
2012-12-05 16:38:14 +02:00
|
|
|
try:
|
|
|
|
return int(value)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
return float(value)
|
|
|
|
except ValueError:
|
|
|
|
return unicode(value)
|
|
|
|
|
|
|
|
|
|
|
|
class FileDirectorySelector(QWidget):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-05 16:38:14 +02:00
|
|
|
def __init__(self, parent=None, selectFile=False):
|
|
|
|
QWidget.__init__(self, parent)
|
|
|
|
|
|
|
|
# create gui
|
|
|
|
self.btnSelect = QToolButton()
|
2013-10-01 20:52:22 +03:00
|
|
|
self.btnSelect.setText(self.tr('...'))
|
2012-12-05 16:38:14 +02:00
|
|
|
self.lineEdit = QLineEdit()
|
|
|
|
self.hbl = QHBoxLayout()
|
|
|
|
self.hbl.setMargin(0)
|
|
|
|
self.hbl.setSpacing(0)
|
|
|
|
self.hbl.addWidget(self.lineEdit)
|
|
|
|
self.hbl.addWidget(self.btnSelect)
|
|
|
|
|
|
|
|
self.setLayout(self.hbl)
|
|
|
|
|
|
|
|
self.canFocusOut = False
|
|
|
|
self.selectFile = selectFile
|
|
|
|
|
|
|
|
self.setFocusPolicy(Qt.StrongFocus)
|
|
|
|
self.btnSelect.clicked.connect(self.select)
|
|
|
|
|
|
|
|
def select(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
lastDir = ''
|
2012-12-05 16:38:14 +02:00
|
|
|
if not self.selectFile:
|
|
|
|
selectedPath = QFileDialog.getExistingDirectory(None,
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
self.tr('Select directory'), lastDir,
|
|
|
|
QFileDialog.ShowDirsOnly)
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2012-12-05 16:38:14 +02:00
|
|
|
selectedPath = QFileDialog.getOpenFileName(None,
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
self.tr('Select file'), lastDir, self.tr('All files (*.*)')
|
|
|
|
)
|
2012-12-05 16:38:14 +02:00
|
|
|
|
|
|
|
if not selectedPath:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.lineEdit.setText(selectedPath)
|
2014-02-12 18:58:14 +01:00
|
|
|
self.canFocusOut = True
|
2012-12-05 16:38:14 +02:00
|
|
|
|
|
|
|
def text(self):
|
|
|
|
return self.lineEdit.text()
|
|
|
|
|
|
|
|
def setText(self, value):
|
|
|
|
self.lineEdit.setText(value)
|