Skip to content
Permalink
Browse files

[Processing] add default extensions for outputs as setting

Based on an idea and code by Evgeniy Nikulin

It includes some improvements to the settings sysmte
  • Loading branch information
volaya committed Oct 24, 2015
1 parent cbead0b commit 4f8a27dd64fbfa527984515d31f477202856eb87
@@ -30,7 +30,7 @@
from PyQt4.QtCore import QPyNullVariant, QCoreApplication, QSettings
from PyQt4.QtGui import QIcon
from processing.tools.system import tempFolder

import processing.tools.dataobjects

class ProcessingConfig:

@@ -49,6 +49,8 @@ class ProcessingConfig:
POST_EXECUTION_SCRIPT = 'POST_EXECUTION_SCRIPT'
SHOW_CRS_DEF = 'SHOW_CRS_DEF'
WARN_UNMATCHING_CRS = 'WARN_UNMATCHING_CRS'
DEFAULT_OUTPUT_RASTER_LAYER_EXT = 'DEFAULT_OUTPUT_RASTER_LAYER_EXT'
DEFAULT_OUTPUT_VECTOR_LAYER_EXT = 'DEFAULT_OUTPUT_VECTOR_LAYER_EXT'

settings = {}
settingIcons = {}
@@ -93,35 +95,49 @@ def initialize():
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.RASTER_STYLE,
ProcessingConfig.tr('Style for raster layers'), ''))
ProcessingConfig.tr('Style for raster layers'), '',
valuetype=Setting.FILE))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.VECTOR_POINT_STYLE,
ProcessingConfig.tr('Style for point layers'), ''))
ProcessingConfig.tr('Style for point layers'), '',
valuetype=Setting.FILE))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.VECTOR_LINE_STYLE,
ProcessingConfig.tr('Style for line layers'), ''))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.VECTOR_POLYGON_STYLE,
ProcessingConfig.tr('Style for polygon layers'), ''))
ProcessingConfig.tr('Style for line layers'), '',
valuetype=Setting.FILE))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.VECTOR_POLYGON_STYLE,
ProcessingConfig.tr('Style for polygon layers'), ''))
ProcessingConfig.tr('Style for polygon layers'), '',
valuetype=Setting.FILE))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.PRE_EXECUTION_SCRIPT,
ProcessingConfig.tr('Pre-execution script'), ''))
ProcessingConfig.tr('Pre-execution script'), '',
valuetype=Setting.FILE))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.POST_EXECUTION_SCRIPT,
ProcessingConfig.tr('Post-execution script'), ''))
ProcessingConfig.tr('Post-execution script'), '',
valuetype=Setting.FILE))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.RECENT_ALGORITHMS,
ProcessingConfig.tr('Recent algs'), '', hidden=True))
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],
valuetype=Setting.SELECTION, options=extensions))
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],
valuetype=Setting.SELECTION, options=extensions))

@staticmethod
def setGroupIcon(group, icon):
@@ -192,8 +208,9 @@ class Setting:
STRING = 0
FILE = 1
FOLDER = 2
SELECTION = 3

def __init__(self, group, name, description, default, hidden=False, valuetype=None):
def __init__(self, group, name, description, default, hidden=False, valuetype=None, options=None):
self.group = group
self.name = name
self.qname = "Processing/Configuration/" + self.name
@@ -202,6 +219,7 @@ def __init__(self, group, name, description, default, hidden=False, valuetype=No
self.value = default
self.hidden = hidden
self.valuetype = valuetype
self.options = options

def read(self):
qsettings = QSettings()
@@ -15,6 +15,7 @@
***************************************************************************
"""


__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
@@ -28,7 +29,7 @@
from processing.tools.system import isWindows, getTempFilenameInTempFolder
from processing.tools.vector import VectorWriter, TableWriter
from processing.tools import dataobjects

from processing.core.ProcessingConfig import ProcessingConfig

def getOutputFromString(s):
tokens = s.split("|")
@@ -159,7 +160,10 @@ def getFileFilter(self, alg):
return ';;'.join(exts)

def getDefaultFileExtension(self, alg):
return alg.provider.getSupportedOutputRasterLayerExtensions()[0]
supported = alg.provider.getSupportedOutputRasterLayerExtensions()
default = ProcessingConfig.getSetting(ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT)
ext = default if default in supported else supported[0]
return ext

def getCompatibleFileName(self, alg):
"""
@@ -253,7 +257,10 @@ def getFileFilter(self, alg):
return ';;'.join(exts)

def getDefaultFileExtension(self, alg):
return alg.provider.getSupportedOutputVectorLayerExtensions()[0]
supported = alg.provider.getSupportedOutputVectorLayerExtensions()
default = ProcessingConfig.getSetting(ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT)
ext = default if default in supported else supported[0]
return ext

def getCompatibleFileName(self, alg):
"""Returns a filename that is compatible with the algorithm
@@ -32,7 +32,8 @@
from PyQt4.QtCore import Qt, QEvent, QPyNullVariant
from PyQt4.QtGui import (QFileDialog, QDialog, QIcon, QStyle,
QStandardItemModel, QStandardItem, QMessageBox, QStyledItemDelegate,
QLineEdit, QSpinBox, QDoubleSpinBox, QWidget, QToolButton, QHBoxLayout)
QLineEdit, QSpinBox, QDoubleSpinBox, QWidget, QToolButton, QHBoxLayout,
QComboBox)

from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.core.Processing import Processing
@@ -166,7 +167,7 @@ class SettingItem(QStandardItem):
def __init__(self, setting):
QStandardItem.__init__(self)
self.setting = setting
self.setData(setting.valuetype, Qt.UserRole)
self.setData(setting, Qt.UserRole)
if isinstance(setting.value, bool):
self.setCheckable(True)
self.setEditable(False)
@@ -189,40 +190,55 @@ def createEditor(
options,
index,
):
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)
spnBox.setRange(-999999999.999999, 999999999.999999)
spnBox.setDecimals(6)
return spnBox
elif isinstance(value, (str, unicode)):
valuetype = self.convertValue(index.model().data(index, Qt.UserRole))
if valuetype == Setting.FOLDER:
return FileDirectorySelector(parent)
else:
return FileDirectorySelector(parent, True)
setting = index.model().data(index, Qt.UserRole)
if setting.valuetype == Setting.FOLDER:
return FileDirectorySelector(parent)
elif setting.valuetype == Setting.FILE:
return FileDirectorySelector(parent, True)
elif setting.valuetype == Setting.SELECTION:
combo = QComboBox(parent)
combo.addItems(setting.options)
return combo
else:
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)
spnBox.setRange(-999999999.999999, 999999999.999999)
spnBox.setDecimals(6)
return spnBox
elif isinstance(value, (str, unicode)):
return QLineEdit(parent)


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)
setting = index.model().data(index, Qt.UserRole)
if setting.valuetype == Setting.SELECTION:
editor.setCurrentIndex(editor.findText(value))
else:
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)
setting = index.model().data(index, Qt.UserRole)
if setting.valuetype == Setting.SELECTION:
model.setData(index, editor.currentText(), Qt.EditRole)
else:
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()
@@ -238,10 +254,10 @@ def convertValue(self, value):
return ""
try:
return int(value)
except ValueError:
except:
try:
return float(value)
except ValueError:
except:
return unicode(value)


0 comments on commit 4f8a27d

Please sign in to comment.
You can’t perform that action at this time.