2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.py
|
2012-10-05 23:28:47 +02:00
|
|
|
---------------------
|
|
|
|
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$'
|
|
|
|
|
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 os
|
|
|
|
|
|
|
|
from PyQt4.QtCore import QPyNullVariant, QCoreApplication, QSettings
|
|
|
|
from PyQt4.QtGui import QIcon
|
2015-11-05 09:16:00 +01:00
|
|
|
from processing.tools.system import defaultOutputFolder
|
2015-10-24 21:11:46 +02:00
|
|
|
import processing.tools.dataobjects
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-10-30 23:30:16 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
class ProcessingConfig:
|
|
|
|
|
2015-11-05 09:16:00 +01:00
|
|
|
OUTPUT_FOLDER = 'OUTPUTS_FOLDER'
|
2013-10-01 20:52:22 +03:00
|
|
|
RASTER_STYLE = 'RASTER_STYLE'
|
|
|
|
VECTOR_POINT_STYLE = 'VECTOR_POINT_STYLE'
|
|
|
|
VECTOR_LINE_STYLE = 'VECTOR_LINE_STYLE'
|
|
|
|
VECTOR_POLYGON_STYLE = 'VECTOR_POLYGON_STYLE'
|
|
|
|
SHOW_RECENT_ALGORITHMS = 'SHOW_RECENT_ALGORITHMS'
|
|
|
|
USE_SELECTED = 'USE_SELECTED'
|
|
|
|
USE_FILENAME_AS_LAYER_NAME = 'USE_FILENAME_AS_LAYER_NAME'
|
|
|
|
KEEP_DIALOG_OPEN = 'KEEP_DIALOG_OPEN'
|
|
|
|
SHOW_DEBUG_IN_DIALOG = 'SHOW_DEBUG_IN_DIALOG'
|
|
|
|
RECENT_ALGORITHMS = 'RECENT_ALGORITHMS'
|
|
|
|
PRE_EXECUTION_SCRIPT = 'PRE_EXECUTION_SCRIPT'
|
|
|
|
POST_EXECUTION_SCRIPT = 'POST_EXECUTION_SCRIPT'
|
|
|
|
SHOW_CRS_DEF = 'SHOW_CRS_DEF'
|
|
|
|
WARN_UNMATCHING_CRS = 'WARN_UNMATCHING_CRS'
|
2015-10-24 21:11:46 +02:00
|
|
|
DEFAULT_OUTPUT_RASTER_LAYER_EXT = 'DEFAULT_OUTPUT_RASTER_LAYER_EXT'
|
|
|
|
DEFAULT_OUTPUT_VECTOR_LAYER_EXT = 'DEFAULT_OUTPUT_VECTOR_LAYER_EXT'
|
2016-01-29 14:07:47 +01:00
|
|
|
SHOW_PROVIDERS_TOOLTIP = "SHOW_PROVIDERS_TOOLTIP"
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
settings = {}
|
2013-10-01 20:52:22 +03:00
|
|
|
settingIcons = {}
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def initialize():
|
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
|
|
|
icon = QIcon(os.path.dirname(__file__) + '/../images/alg.png')
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingConfig.settingIcons['General'] = icon
|
2015-08-22 14:29:41 +02:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
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
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.SHOW_DEBUG_IN_DIALOG,
|
|
|
|
ProcessingConfig.tr('Show extra info in Log panel'), True))
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.KEEP_DIALOG_OPEN,
|
|
|
|
ProcessingConfig.tr('Keep dialog open after running an algorithm'), False))
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.USE_SELECTED,
|
|
|
|
ProcessingConfig.tr('Use only selected features'), True))
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.USE_FILENAME_AS_LAYER_NAME,
|
|
|
|
ProcessingConfig.tr('Use filename as layer name'), False))
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.SHOW_RECENT_ALGORITHMS,
|
|
|
|
ProcessingConfig.tr('Show recently executed algorithms'), True))
|
2016-01-29 14:07:47 +01:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.SHOW_PROVIDERS_TOOLTIP,
|
|
|
|
ProcessingConfig.tr('Show tooltip when there are disabled providers'), True))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.OUTPUT_FOLDER,
|
2015-11-05 09:16:00 +01:00
|
|
|
ProcessingConfig.tr('Output folder'), defaultOutputFolder(),
|
2015-06-26 11:54:58 +02:00
|
|
|
valuetype=Setting.FOLDER))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.SHOW_CRS_DEF,
|
|
|
|
ProcessingConfig.tr('Show layer CRS definition in selection boxes'), True))
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.WARN_UNMATCHING_CRS,
|
|
|
|
ProcessingConfig.tr("Warn before executing if layer CRS's do not match"), True))
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.RASTER_STYLE,
|
2015-10-24 21:11:46 +02:00
|
|
|
ProcessingConfig.tr('Style for raster layers'), '',
|
|
|
|
valuetype=Setting.FILE))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.VECTOR_POINT_STYLE,
|
2015-10-24 21:11:46 +02:00
|
|
|
ProcessingConfig.tr('Style for point layers'), '',
|
|
|
|
valuetype=Setting.FILE))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.VECTOR_LINE_STYLE,
|
2015-10-24 21:11:46 +02:00
|
|
|
ProcessingConfig.tr('Style for line layers'), '',
|
|
|
|
valuetype=Setting.FILE))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.VECTOR_POLYGON_STYLE,
|
2015-10-24 21:11:46 +02:00
|
|
|
ProcessingConfig.tr('Style for polygon layers'), '',
|
|
|
|
valuetype=Setting.FILE))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.PRE_EXECUTION_SCRIPT,
|
2015-10-24 21:11:46 +02:00
|
|
|
ProcessingConfig.tr('Pre-execution script'), '',
|
|
|
|
valuetype=Setting.FILE))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.POST_EXECUTION_SCRIPT,
|
2015-10-24 21:11:46 +02:00
|
|
|
ProcessingConfig.tr('Post-execution script'), '',
|
|
|
|
valuetype=Setting.FILE))
|
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
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.RECENT_ALGORITHMS,
|
|
|
|
ProcessingConfig.tr('Recent algs'), '', hidden=True))
|
2015-10-24 21:11:46 +02:00
|
|
|
extensions = processing.tools.dataobjects.getSupportedOutputVectorLayerExtensions()
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
|
|
|
|
ProcessingConfig.tr('Default output vector layer extension'), extensions[0],
|
2016-01-30 09:33:24 +11:00
|
|
|
valuetype=Setting.SELECTION, options=extensions))
|
2015-10-24 21:11:46 +02:00
|
|
|
extensions = processing.tools.dataobjects.getSupportedOutputRasterLayerExtensions()
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.DEFAULT_OUTPUT_RASTER_LAYER_EXT,
|
|
|
|
ProcessingConfig.tr('Default output raster layer extension'), extensions[0],
|
2016-01-30 09:33:24 +11:00
|
|
|
valuetype=Setting.SELECTION, options=extensions))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def setGroupIcon(group, icon):
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.settingIcons[group] = icon
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getGroupIcon(group):
|
2014-10-09 17:34:39 +03:00
|
|
|
if group == ProcessingConfig.tr('General'):
|
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
|
|
|
return QIcon(os.path.dirname(__file__) + '/../images/alg.png')
|
2013-08-12 20:44:27 +02:00
|
|
|
if group in ProcessingConfig.settingIcons:
|
|
|
|
return ProcessingConfig.settingIcons[group]
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2015-08-22 14:29:41 +02:00
|
|
|
return QIcon(os.path.dirname(__file__) + '/../images/alg.png')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def addSetting(setting):
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.settings[setting.name] = setting
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def removeSetting(name):
|
2013-08-12 20:44:27 +02:00
|
|
|
del ProcessingConfig.settings[name]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getSettings():
|
2014-04-23 18:24:23 +02:00
|
|
|
'''Return settings as a dict with group names as keys and lists of settings as values'''
|
2013-10-01 20:52:22 +03:00
|
|
|
settings = {}
|
2013-08-12 20:44:27 +02:00
|
|
|
for setting in ProcessingConfig.settings.values():
|
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 setting.group not in settings:
|
2012-09-15 18:25:25 +03:00
|
|
|
group = []
|
|
|
|
settings[setting.group] = group
|
|
|
|
else:
|
|
|
|
group = settings[setting.group]
|
|
|
|
group.append(setting)
|
|
|
|
return settings
|
|
|
|
|
|
|
|
@staticmethod
|
2014-05-02 10:32:37 +02:00
|
|
|
def readSettings():
|
2013-08-12 20:44:27 +02:00
|
|
|
for setting in ProcessingConfig.settings.values():
|
2014-04-23 18:24:23 +02:00
|
|
|
setting.read()
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
@staticmethod
|
|
|
|
def getSetting(name):
|
2013-08-12 20:44:27 +02:00
|
|
|
if name in ProcessingConfig.settings.keys():
|
2014-05-08 18:48:25 +02:00
|
|
|
v = ProcessingConfig.settings[name].value
|
2016-03-14 20:26:58 +01:00
|
|
|
try:
|
|
|
|
if isinstance(v, QPyNullVariant):
|
|
|
|
v = None
|
|
|
|
except:
|
|
|
|
pass
|
2014-05-08 18:48:25 +02:00
|
|
|
return v
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def setSettingValue(name, value):
|
2013-08-12 20:44:27 +02:00
|
|
|
if name in ProcessingConfig.settings.keys():
|
2015-11-04 12:27:39 +01:00
|
|
|
ProcessingConfig.settings[name].setValue(value)
|
2014-04-23 18:24:23 +02:00
|
|
|
ProcessingConfig.settings[name].save()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-10-09 17:34:39 +03:00
|
|
|
@staticmethod
|
|
|
|
def tr(string, context=''):
|
|
|
|
if context == '':
|
|
|
|
context = 'ProcessingConfig'
|
|
|
|
return QCoreApplication.translate(context, string)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
class Setting:
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
"""A simple config parameter that will appear on the config dialog.
|
|
|
|
"""
|
2014-04-17 01:41:25 +02:00
|
|
|
STRING = 0
|
|
|
|
FILE = 1
|
|
|
|
FOLDER = 2
|
2015-10-24 21:11:46 +02:00
|
|
|
SELECTION = 3
|
2015-11-04 12:27:39 +01:00
|
|
|
FLOAT = 4
|
|
|
|
INT = 5
|
2014-04-19 22:04:24 +02:00
|
|
|
|
2015-11-04 12:27:39 +01:00
|
|
|
def __init__(self, group, name, description, default, hidden=False, valuetype=None,
|
|
|
|
validator=None, options=None):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.group = group
|
2012-09-15 18:25:25 +03:00
|
|
|
self.name = name
|
2014-04-23 18:24:23 +02:00
|
|
|
self.qname = "Processing/Configuration/" + self.name
|
2012-09-15 18:25:25 +03:00
|
|
|
self.description = description
|
|
|
|
self.default = default
|
|
|
|
self.hidden = hidden
|
2015-11-04 12:27:39 +01:00
|
|
|
if valuetype is None:
|
|
|
|
if isinstance(default, (int, long)):
|
|
|
|
valuetype = self.INT
|
|
|
|
elif isinstance(default, float):
|
|
|
|
valuetype = self.FLOAT
|
2014-04-17 01:41:25 +02:00
|
|
|
self.valuetype = valuetype
|
2015-10-24 21:11:46 +02:00
|
|
|
self.options = options
|
2015-11-04 12:27:39 +01:00
|
|
|
if validator is None:
|
|
|
|
if valuetype == self.FLOAT:
|
|
|
|
def checkFloat(v):
|
|
|
|
try:
|
|
|
|
float(v)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(self.tr('Wrong parameter value:\n%s') % unicode(v))
|
|
|
|
validator = checkFloat
|
|
|
|
elif valuetype == self.INT:
|
|
|
|
def checkInt(v):
|
|
|
|
try:
|
|
|
|
int(v)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(self.tr('Wrong parameter value:\n%s') % unicode(v))
|
|
|
|
validator = checkInt
|
|
|
|
elif valuetype in [self.FILE, self.FOLDER]:
|
|
|
|
def checkFileOrFolder(v):
|
|
|
|
if v and not os.path.exists(v):
|
|
|
|
raise ValueError(self.tr('Specified path does not exist:\n%s') % unicode(v))
|
|
|
|
validator = checkFileOrFolder
|
|
|
|
else:
|
|
|
|
validator = lambda x: True
|
|
|
|
self.validator = validator
|
|
|
|
self.value = default
|
|
|
|
|
|
|
|
def setValue(self, value):
|
|
|
|
self.validator(value)
|
2015-11-08 16:28:10 +01:00
|
|
|
self.value = value
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2014-04-23 18:24:23 +02:00
|
|
|
def read(self):
|
2014-05-02 10:32:37 +02:00
|
|
|
qsettings = QSettings()
|
2014-04-23 18:24:23 +02:00
|
|
|
value = qsettings.value(self.qname, None)
|
|
|
|
if value is not None:
|
|
|
|
if isinstance(self.value, bool):
|
2015-08-16 20:57:24 +02:00
|
|
|
value = unicode(value).lower() == unicode(True).lower()
|
2014-04-23 18:24:23 +02:00
|
|
|
self.value = value
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2014-04-23 18:24:23 +02:00
|
|
|
def save(self):
|
|
|
|
QSettings().setValue(self.qname, self.value)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-08-16 20:57:24 +02:00
|
|
|
return self.name + '=' + unicode(self.value)
|
2015-11-04 12:27:39 +01:00
|
|
|
|
|
|
|
def tr(self, string, context=''):
|
|
|
|
if context == '':
|
|
|
|
context = 'ProcessingConfig'
|
|
|
|
return QCoreApplication.translate(context, string)
|