Skip to content

Commit

Permalink
Move getFileFilter from params to gui wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 379d060 commit 7683b25
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 34 deletions.
29 changes: 0 additions & 29 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -428,17 +428,6 @@ def getAsString(self, value):
return str(layer.source()) return str(layer.source())
return s return s


def getFileFilter(self):
if self.datatype == dataobjects.TYPE_RASTER:
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
elif self.datatype == dataobjects.TYPE_FILE:
return self.tr('All files (*.*)', 'ParameterMultipleInput')
else:
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterMultipleInput').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)

def dataType(self): def dataType(self):
if self.datatype == dataobjects.TYPE_VECTOR_POINT: if self.datatype == dataobjects.TYPE_VECTOR_POINT:
return 'points' return 'points'
Expand Down Expand Up @@ -627,12 +616,6 @@ def getSafeExportedLayer(self):
self.exported = self.value self.exported = self.value
return self.exported return self.exported


def getFileFilter(self):
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterRaster').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)

def getAsScriptCode(self): def getAsScriptCode(self):
param_type = '' param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional: if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
Expand Down Expand Up @@ -821,12 +804,6 @@ def getSafeExportedTable(self):
self.exported = self.value self.exported = self.value
return self.exported return self.exported


def getFileFilter(self):
exts = ['csv', 'dbf']
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterTable').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)

def getAsScriptCode(self): def getAsScriptCode(self):
param_type = '' param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional: if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
Expand Down Expand Up @@ -951,12 +928,6 @@ def getSafeExportedLayer(self):
self.exported = self.value self.exported = self.value
return self.exported return self.exported


def getFileFilter(self):
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)

def dataType(self): def dataType(self):
return dataobjects.vectorDataType(self) return dataobjects.vectorDataType(self)


Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/BatchInputSelectionPanel.py
Expand Up @@ -44,7 +44,7 @@
from processing.core.parameters import ParameterRaster from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterVector from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTable from processing.core.parameters import ParameterTable

from processing.gui.ParameterGuiUtils import getFileFilter
from processing.tools import dataobjects from processing.tools import dataobjects




Expand Down Expand Up @@ -145,7 +145,7 @@ def showFileSelectionDialog(self):
path = '' path = ''


ret, selected_filter = QFileDialog.getOpenFileNames(self, self.tr('Open file'), path, ret, selected_filter = QFileDialog.getOpenFileNames(self, self.tr('Open file'), path,
self.tr('All files (*.*);;') + self.param.getFileFilter()) self.tr('All files (*.*);;') + getFileFilter(self.param))
if ret: if ret:
files = list(ret) files = list(ret)
settings.setValue('/Processing/LastInputPath', settings.setValue('/Processing/LastInputPath',
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/gui/DestinationSelectionPanel.py
Expand Up @@ -42,6 +42,7 @@
from processing.core.outputs import OutputVector from processing.core.outputs import OutputVector
from processing.core.outputs import OutputDirectory from processing.core.outputs import OutputDirectory
from processing.gui.PostgisTableSelector import PostgisTableSelector from processing.gui.PostgisTableSelector import PostgisTableSelector
from processing.gui.ParameterGuiUtils import getFileFilter


pluginPath = os.path.split(os.path.dirname(__file__))[0] pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType( WIDGET, BASE = uic.loadUiType(
Expand Down Expand Up @@ -187,7 +188,7 @@ def saveToSpatialite(self):
self.leText.setText("spatialite:" + uri.uri()) self.leText.setText("spatialite:" + uri.uri())


def selectFile(self): def selectFile(self):
fileFilter = self.parameter.getFileFilter(self.alg) fileFilter = getFileFilter(self.parameter)


settings = QgsSettings() settings = QgsSettings()
if settings.contains('/Processing/LastOutputPath'): if settings.contains('/Processing/LastOutputPath'):
Expand Down
74 changes: 74 additions & 0 deletions python/plugins/processing/gui/ParameterGuiUtils.py
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
ParameterGuiUtils.py
---------------------
Date : June 2017
Copyright : (C) 2017 by Nyall Dawson
Email : nyall dot dawson 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__ = 'Nyall Dawson'
__date__ = 'June 2017'
__copyright__ = '(C) 2017, Nyall Dawson'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.core import (
QgsProcessingParameterDefinition,
QgsProcessingFeatureSourceDefinition,
QgsVectorFileWriter)
from qgis.PyQt.QtCore import QCoreApplication
from processing.tools import dataobjects


def tr(string, context=''):
if context == '':
context = 'Processing'
return QCoreApplication.translate(context, string)


def getFileFilter(param):
"""
Returns a suitable file filter pattern for the specified parameter definition
:param param:
:return:
"""
if param.type() == 'multilayer':
if param.layerType() == QgsProcessingParameterDefinition.TypeRaster:
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
elif param.layerType() == QgsProcessingParameterDefinition.TypeFile:
return tr('All files (*.*)', 'QgsProcessingParameterMultipleLayers')
else:
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterMultipleLayers').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
elif param.type() == 'raster':
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'ParameterRaster').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
elif param.type() == 'table':
exts = ['csv', 'dbf']
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'ParameterTable').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
elif param.type() == 'sink':
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
return ''
38 changes: 36 additions & 2 deletions python/plugins/processing/gui/wrappers.py
Expand Up @@ -111,7 +111,7 @@
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from processing.gui.FixedTablePanel import FixedTablePanel from processing.gui.FixedTablePanel import FixedTablePanel
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel

from processing.gui.ParameterGuiUtils import getFileFilter
DIALOG_STANDARD = 'standard' DIALOG_STANDARD = 'standard'
DIALOG_BATCH = 'batch' DIALOG_BATCH = 'batch'
DIALOG_MODELER = 'modeler' DIALOG_MODELER = 'modeler'
Expand Down Expand Up @@ -207,7 +207,7 @@ def getFileName(self, initial_value=''):


filename, selected_filter = QFileDialog.getOpenFileName(self.widget, self.tr('Select file'), filename, selected_filter = QFileDialog.getOpenFileName(self.widget, self.tr('Select file'),
path, self.tr( path, self.tr(
'All files (*.*);;') + self.param.getFileFilter()) 'All files (*.*);;') + getFileFilter(self.param))
if filename: if filename:
settings.setValue('/Processing/LastInputPath', settings.setValue('/Processing/LastInputPath',
os.path.dirname(str(filename))) os.path.dirname(str(filename)))
Expand Down Expand Up @@ -1217,6 +1217,40 @@ def validator(v):
return self.comboValue(validator) return self.comboValue(validator)




def getFileFilter(param):
"""
Returns a suitable file filter pattern for the specified parameter definition
:param param:
:return:
"""
if param.type() == 'multilayer':
if param.layerType() == QgsProcessingParameterDefinition.TypeRaster:
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
elif param.layerType() == QgsProcessingParameterDefinition.TypeFile:
return self.tr('All files (*.*)', 'QgsProcessingParameterMultipleLayers')
else:
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'QgsProcessingParameterMultipleLayers').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
elif param.type() == 'raster':
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterRaster').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
elif param.type() == 'table':
exts = ['csv', 'dbf']
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterTable').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
elif param.type() == 'sink':
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
return ''


class WidgetWrapperFactory: class WidgetWrapperFactory:


""" """
Expand Down

0 comments on commit 7683b25

Please sign in to comment.