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
|
|
|
|
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
|
2020-01-09 15:52:05 +02:00
|
|
|
import tempfile
|
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
|
|
|
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication, QObject, pyqtSignal
|
2017-04-03 15:41:32 +10:00
|
|
|
from qgis.core import (NULL,
|
|
|
|
QgsApplication,
|
|
|
|
QgsSettings,
|
2017-12-07 18:24:20 +02:00
|
|
|
QgsVectorFileWriter,
|
2019-11-26 13:16:17 +01:00
|
|
|
QgsRasterFileWriter,
|
|
|
|
QgsProcessingUtils)
|
2015-11-05 09:16:00 +01:00
|
|
|
from processing.tools.system import defaultOutputFolder
|
2017-01-12 14:49:26 +02:00
|
|
|
import processing.tools.dataobjects
|
2019-08-04 18:21:49 -07:00
|
|
|
from multiprocessing import cpu_count
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-04-27 15:40:56 +03:00
|
|
|
|
2016-04-27 13:36:28 +02:00
|
|
|
class SettingsWatcher(QObject):
|
|
|
|
settingsChanged = pyqtSignal()
|
|
|
|
|
2017-03-03 20:44:03 +01:00
|
|
|
|
2016-04-27 13:36:28 +02:00
|
|
|
settingsWatcher = SettingsWatcher()
|
|
|
|
|
2015-10-30 23:30:16 +01:00
|
|
|
|
2017-11-27 13:23:49 +02: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'
|
2016-12-15 09:43:31 +01:00
|
|
|
FILTER_INVALID_GEOMETRIES = 'FILTER_INVALID_GEOMETRIES'
|
2019-11-08 14:49:19 +10:00
|
|
|
PREFER_FILENAME_AS_LAYER_NAME = 'PREFER_FILENAME_AS_LAYER_NAME'
|
2013-10-01 20:52:22 +03:00
|
|
|
KEEP_DIALOG_OPEN = 'KEEP_DIALOG_OPEN'
|
|
|
|
PRE_EXECUTION_SCRIPT = 'PRE_EXECUTION_SCRIPT'
|
|
|
|
POST_EXECUTION_SCRIPT = 'POST_EXECUTION_SCRIPT'
|
|
|
|
SHOW_CRS_DEF = 'SHOW_CRS_DEF'
|
|
|
|
WARN_UNMATCHING_CRS = 'WARN_UNMATCHING_CRS'
|
2017-01-10 14:17:25 +02:00
|
|
|
SHOW_PROVIDERS_TOOLTIP = 'SHOW_PROVIDERS_TOOLTIP'
|
2019-03-05 17:04:00 +10:00
|
|
|
SHOW_ALGORITHMS_KNOWN_ISSUES = 'SHOW_ALGORITHMS_KNOWN_ISSUES'
|
2019-08-04 18:21:49 -07:00
|
|
|
MAX_THREADS = 'MAX_THREADS'
|
2019-09-24 10:18:18 +10:00
|
|
|
DEFAULT_OUTPUT_RASTER_LAYER_EXT = 'DefaultOutputRasterLayerExt'
|
|
|
|
DEFAULT_OUTPUT_VECTOR_LAYER_EXT = 'DefaultOutputVectorLayerExt'
|
2020-01-10 09:44:45 +10:00
|
|
|
TEMP_PATH = 'TEMP_PATH2'
|
2020-07-20 12:25:06 -05:00
|
|
|
RESULTS_GROUP_NAME = 'RESULTS_GROUP_NAME'
|
2022-08-19 12:10:10 +02:00
|
|
|
VECTOR_FEATURE_COUNT = 'VECTOR_FEATURE_COUNT'
|
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():
|
2017-01-09 12:34:46 +10:00
|
|
|
icon = QgsApplication.getThemeIcon("/processingAlgorithm.svg")
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingConfig.settingIcons['General'] = icon
|
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.KEEP_DIALOG_OPEN,
|
2018-01-11 11:54:13 +02:00
|
|
|
ProcessingConfig.tr('Keep dialog open after running an algorithm'), 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'),
|
2019-11-08 14:49:19 +10:00
|
|
|
ProcessingConfig.PREFER_FILENAME_AS_LAYER_NAME,
|
|
|
|
ProcessingConfig.tr('Prefer output filename for layer names'), 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,
|
2018-05-01 15:33:26 +10:00
|
|
|
ProcessingConfig.tr("Warn before executing if parameter CRS's do not match"), True))
|
2019-03-05 17:04:00 +10:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.SHOW_ALGORITHMS_KNOWN_ISSUES,
|
|
|
|
ProcessingConfig.tr("Show algorithms with known issues"), False))
|
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.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))
|
2017-01-12 12:51:08 +02:00
|
|
|
|
2017-01-25 02:19:33 +01:00
|
|
|
invalidFeaturesOptions = [ProcessingConfig.tr('Do not filter (better performance)'),
|
2019-02-04 09:21:17 +10:00
|
|
|
ProcessingConfig.tr('Skip (ignore) features with invalid geometries'),
|
2017-01-12 12:51:08 +02:00
|
|
|
ProcessingConfig.tr('Stop algorithm execution when a geometry is invalid')]
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.FILTER_INVALID_GEOMETRIES,
|
|
|
|
ProcessingConfig.tr('Invalid features filtering'),
|
|
|
|
invalidFeaturesOptions[2],
|
|
|
|
valuetype=Setting.SELECTION,
|
|
|
|
options=invalidFeaturesOptions))
|
|
|
|
|
2020-05-11 08:10:20 +10:00
|
|
|
threads = QgsApplication.maxThreads() # if user specified limit for rendering, lets keep that as default here, otherwise max
|
|
|
|
threads = cpu_count() if threads == -1 else threads # if unset, maxThreads() returns -1
|
2019-08-04 18:21:49 -07:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.MAX_THREADS,
|
|
|
|
ProcessingConfig.tr('Max Threads'), threads,
|
|
|
|
valuetype=Setting.INT))
|
|
|
|
|
2019-09-24 10:18:18 +10:00
|
|
|
extensions = QgsVectorFileWriter.supportedFormatExtensions()
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
|
|
|
|
ProcessingConfig.tr('Default output vector layer extension'),
|
2019-11-08 09:54:52 +01:00
|
|
|
QgsVectorFileWriter.supportedFormatExtensions()[0],
|
2019-09-24 10:18:18 +10:00
|
|
|
valuetype=Setting.SELECTION,
|
|
|
|
options=extensions))
|
|
|
|
|
|
|
|
extensions = QgsRasterFileWriter.supportedFormatExtensions()
|
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.DEFAULT_OUTPUT_RASTER_LAYER_EXT,
|
|
|
|
ProcessingConfig.tr('Default output raster layer extension'),
|
|
|
|
'tif',
|
|
|
|
valuetype=Setting.SELECTION,
|
|
|
|
options=extensions))
|
|
|
|
|
2019-11-26 13:16:17 +01:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.TEMP_PATH,
|
2020-07-20 14:54:50 -05:00
|
|
|
ProcessingConfig.tr('Override temporary output folder path'), None,
|
|
|
|
valuetype=Setting.FOLDER,
|
|
|
|
placeholder=ProcessingConfig.tr('Leave blank for default')))
|
2019-11-26 13:16:17 +01:00
|
|
|
|
2020-07-20 12:25:06 -05:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.RESULTS_GROUP_NAME,
|
|
|
|
ProcessingConfig.tr("Results group name"),
|
|
|
|
"",
|
2020-07-20 14:54:50 -05:00
|
|
|
valuetype=Setting.STRING,
|
|
|
|
placeholder=ProcessingConfig.tr("Leave blank to avoid loading results in a predetermined group")
|
|
|
|
))
|
2020-07-20 12:25:06 -05:00
|
|
|
|
2022-08-19 12:10:10 +02:00
|
|
|
ProcessingConfig.addSetting(Setting(
|
|
|
|
ProcessingConfig.tr('General'),
|
|
|
|
ProcessingConfig.VECTOR_FEATURE_COUNT,
|
|
|
|
ProcessingConfig.tr('Show feature count for output vector layers'), False))
|
|
|
|
|
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'):
|
2017-01-09 12:34:46 +10:00
|
|
|
return QgsApplication.getThemeIcon("/processingAlgorithm.svg")
|
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:
|
2017-01-09 12:34:46 +10:00
|
|
|
return QgsApplication.getThemeIcon("/processingAlgorithm.svg")
|
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 = {}
|
2016-09-21 18:24:26 +02:00
|
|
|
for setting in list(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():
|
2016-09-21 18:24:26 +02:00
|
|
|
for setting in list(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
|
2017-01-18 08:46:56 +02:00
|
|
|
def getSetting(name, readable=False):
|
2021-02-11 20:00:06 -08:00
|
|
|
if name not in list(ProcessingConfig.settings.keys()):
|
2012-09-15 18:25:25 +03:00
|
|
|
return None
|
2021-02-11 20:00:06 -08:00
|
|
|
v = ProcessingConfig.settings[name].value
|
|
|
|
try:
|
|
|
|
if v == NULL:
|
|
|
|
v = None
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if ProcessingConfig.settings[name].valuetype != Setting.SELECTION:
|
|
|
|
return v
|
|
|
|
if readable:
|
|
|
|
return v
|
|
|
|
return ProcessingConfig.settings[name].options.index(v)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def setSettingValue(name, value):
|
2016-09-21 18:24:26 +02:00
|
|
|
if name in list(ProcessingConfig.settings.keys()):
|
2017-01-12 12:51:08 +02:00
|
|
|
if ProcessingConfig.settings[name].valuetype == Setting.SELECTION:
|
|
|
|
ProcessingConfig.settings[name].setValue(ProcessingConfig.settings[name].options[value])
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
2017-11-27 13:23:49 +02:00
|
|
|
class Setting:
|
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
|
2016-05-24 21:56:17 +03:00
|
|
|
MULTIPLE_FOLDERS = 6
|
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,
|
2020-07-20 14:54:50 -05:00
|
|
|
validator=None, options=None, placeholder=""):
|
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
|
2014-04-17 01:41:25 +02:00
|
|
|
self.valuetype = valuetype
|
2015-10-24 21:11:46 +02:00
|
|
|
self.options = options
|
2020-07-20 14:54:50 -05:00
|
|
|
self.placeholder = placeholder
|
2017-01-12 12:51:08 +02:00
|
|
|
|
|
|
|
if self.valuetype is None:
|
|
|
|
if isinstance(default, int):
|
|
|
|
self.valuetype = self.INT
|
|
|
|
elif isinstance(default, float):
|
|
|
|
self.valuetype = self.FLOAT
|
|
|
|
|
2015-11-04 12:27:39 +01:00
|
|
|
if validator is None:
|
2017-01-12 12:51:08 +02:00
|
|
|
if self.valuetype == self.FLOAT:
|
2015-11-04 12:27:39 +01:00
|
|
|
def checkFloat(v):
|
|
|
|
try:
|
|
|
|
float(v)
|
|
|
|
except ValueError:
|
2017-03-04 16:23:36 +01:00
|
|
|
raise ValueError(self.tr('Wrong parameter value:\n{0}').format(v))
|
2020-05-11 08:10:20 +10:00
|
|
|
|
2015-11-04 12:27:39 +01:00
|
|
|
validator = checkFloat
|
2017-01-12 12:51:08 +02:00
|
|
|
elif self.valuetype == self.INT:
|
2015-11-04 12:27:39 +01:00
|
|
|
def checkInt(v):
|
|
|
|
try:
|
|
|
|
int(v)
|
|
|
|
except ValueError:
|
2017-03-04 16:23:36 +01:00
|
|
|
raise ValueError(self.tr('Wrong parameter value:\n{0}').format(v))
|
2020-05-11 08:10:20 +10:00
|
|
|
|
2015-11-04 12:27:39 +01:00
|
|
|
validator = checkInt
|
2017-01-12 12:51:08 +02:00
|
|
|
elif self.valuetype in [self.FILE, self.FOLDER]:
|
2015-11-04 12:27:39 +01:00
|
|
|
def checkFileOrFolder(v):
|
|
|
|
if v and not os.path.exists(v):
|
2017-03-04 16:23:36 +01:00
|
|
|
raise ValueError(self.tr('Specified path does not exist:\n{0}').format(v))
|
2020-05-11 08:10:20 +10:00
|
|
|
|
2015-11-04 12:27:39 +01:00
|
|
|
validator = checkFileOrFolder
|
2017-01-12 12:51:08 +02:00
|
|
|
elif self.valuetype == self.MULTIPLE_FOLDERS:
|
2016-05-24 21:56:17 +03:00
|
|
|
def checkMultipleFolders(v):
|
|
|
|
folders = v.split(';')
|
|
|
|
for f in folders:
|
|
|
|
if f and not os.path.exists(f):
|
2017-03-04 16:23:36 +01:00
|
|
|
raise ValueError(self.tr('Specified path does not exist:\n{0}').format(f))
|
2020-05-11 08:10:20 +10:00
|
|
|
|
2016-05-24 21:56:17 +03:00
|
|
|
validator = checkMultipleFolders
|
2015-11-04 12:27:39 +01:00
|
|
|
else:
|
2016-03-21 04:58:12 +01:00
|
|
|
def validator(x):
|
|
|
|
return True
|
2015-11-04 12:27:39 +01:00
|
|
|
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
|
|
|
|
2018-10-18 17:43:00 +02:00
|
|
|
def read(self, qsettings=None):
|
|
|
|
if not qsettings:
|
|
|
|
qsettings = QgsSettings()
|
2014-04-23 18:24:23 +02:00
|
|
|
value = qsettings.value(self.qname, None)
|
|
|
|
if value is not None:
|
|
|
|
if isinstance(self.value, bool):
|
2016-09-21 18:24:26 +02:00
|
|
|
value = str(value).lower() == str(True).lower()
|
2017-01-12 12:51:08 +02:00
|
|
|
|
|
|
|
if self.valuetype == self.SELECTION:
|
2017-01-13 16:43:16 +02:00
|
|
|
try:
|
|
|
|
self.value = self.options[int(value)]
|
|
|
|
except:
|
|
|
|
self.value = self.options[0]
|
2017-01-12 12:51:08 +02:00
|
|
|
else:
|
|
|
|
self.value = value
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2018-10-18 17:43:00 +02:00
|
|
|
def save(self, qsettings=None):
|
|
|
|
if not qsettings:
|
|
|
|
qsettings = QgsSettings()
|
2017-01-12 12:51:08 +02:00
|
|
|
if self.valuetype == self.SELECTION:
|
|
|
|
qsettings.setValue(self.qname, self.options.index(self.value))
|
|
|
|
else:
|
|
|
|
qsettings.setValue(self.qname, self.value)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-09-21 18:24:26 +02:00
|
|
|
return self.name + '=' + str(self.value)
|
2015-11-04 12:27:39 +01:00
|
|
|
|
|
|
|
def tr(self, string, context=''):
|
|
|
|
if context == '':
|
|
|
|
context = 'ProcessingConfig'
|
|
|
|
return QCoreApplication.translate(context, string)
|