Skip to content

Commit cdadc70

Browse files
committedJan 31, 2014
add ParameterMultipleExternalInput parameter type
1 parent e846521 commit cdadc70

File tree

6 files changed

+326
-1
lines changed

6 files changed

+326
-1
lines changed
 

‎python/plugins/processing/gui/AlgorithmExecutionDialog.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
---------------------
77
Date : August 2012
88
Copyright : (C) 2012 by Victor Olaya
9+
(C) 2013 by CS Systemes d'information (CS SI)
910
Email : volayaf at gmail dot com
11+
otb at c-s dot fr (CS SI)
12+
Contributors : Victor Olaya
13+
Alexia Mondot (CS SI) - managing the new parameter ParameterMultipleExternalInput
1014
***************************************************************************
1115
* *
1216
* This program is free software; you can redistribute it and/or modify *
@@ -50,6 +54,7 @@
5054
from processing.parameters.ParameterCrs import ParameterCrs
5155
from processing.parameters.ParameterExtent import ParameterExtent
5256
from processing.parameters.ParameterString import ParameterString
57+
from processing.parameters.ParameterMultipleExternalInput import ParameterMultipleExternalInput
5358
from processing.outputs.OutputRaster import OutputRaster
5459
from processing.outputs.OutputVector import OutputVector
5560
from processing.outputs.OutputTable import OutputTable
@@ -162,6 +167,13 @@ def setParamValues(self):
162167
return True
163168

164169
def setParamValue(self, param, widget):
170+
"""
171+
set the .value of the parameter according to the given widget
172+
the way to get the value is different for each value,
173+
so there is a code for each kind of parameter
174+
175+
param : -il <ParameterMultipleInput> or -method <ParameterSelection> ...
176+
"""
165177
if isinstance(param, ParameterRaster):
166178
return param.setValue(widget.getValue())
167179
elif isinstance(param, (ParameterVector, ParameterTable)):
@@ -188,6 +200,9 @@ def setParamValue(self, param, widget):
188200
for index in widget.selectedoptions:
189201
value.append(options[index])
190202
return param.setValue(value)
203+
elif isinstance(param, ParameterMultipleExternalInput):
204+
value = widget.selectedoptions
205+
return param.setValue(value)
191206
elif isinstance(param, (ParameterNumber, ParameterFile, ParameterCrs,
192207
ParameterExtent)):
193208
return param.setValue(widget.getValue())
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
MultipleExternalInputDialog.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
(C) 2013 by CS Systemes d'information (CS SI)
10+
Email : volayaf at gmail dot com
11+
otb at c-s dot fr (CS SI)
12+
Contributors : Victor Olaya - basis from MultipleInputDialog
13+
Alexia Mondot (CS SI) - new parameter
14+
***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************
22+
"""
23+
24+
__author__ = 'Victor Olaya'
25+
__date__ = 'August 2012'
26+
__copyright__ = '(C) 2012, Victor Olaya'
27+
# This will get replaced with a git SHA1 when you do a git archive
28+
__revision__ = '$Format:%H$'
29+
30+
from PyQt4 import QtCore, QtGui
31+
import os
32+
33+
class MultipleExternalInputDialog(QtGui.QDialog):
34+
def __init__(self, selectedoptions):
35+
self.selectedoptions = selectedoptions
36+
self.options=selectedoptions
37+
QtGui.QDialog.__init__(self)
38+
self.setModal(True)
39+
self.setupUi()
40+
self.selectedoptions = None
41+
42+
def setupUi(self):
43+
self.resize(381, 320)
44+
self.setWindowTitle("Multiple selection")
45+
self.horizontalLayout = QtGui.QHBoxLayout(self)
46+
self.horizontalLayout.setSpacing(2)
47+
self.horizontalLayout.setMargin(0)
48+
self.buttonBox = QtGui.QDialogButtonBox()
49+
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
50+
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
51+
self.buttonPlus = QtGui.QPushButton("+")
52+
self.buttonBox.addButton(self.buttonPlus, QtGui.QDialogButtonBox.ActionRole)
53+
self.buttonMoins = QtGui.QPushButton("-")
54+
self.buttonBox.addButton(self.buttonMoins, QtGui.QDialogButtonBox.ActionRole)
55+
self.table = QtGui.QTableWidget()
56+
self.table.setColumnCount(1)
57+
self.table.setColumnWidth(0,270)
58+
self.table.verticalHeader().setVisible(False)
59+
self.table.horizontalHeader().setVisible(False)
60+
self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
61+
self.horizontalLayout.addWidget(self.table)
62+
self.horizontalLayout.addWidget(self.buttonBox)
63+
self.setLayout(self.horizontalLayout)
64+
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.okPressed)
65+
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.cancelPressed)
66+
QtCore.QObject.connect(self.buttonPlus, QtCore.SIGNAL("clicked()"), self.addFile)
67+
QtCore.QObject.connect(self.buttonMoins, QtCore.SIGNAL("clicked()"), self.removeFile)
68+
QtCore.QMetaObject.connectSlotsByName(self)
69+
70+
def setTableContent(self):
71+
# "self.options :", ['/temp/confmat1-1.csv', '/temp/confmat2-1.csv']
72+
self.table.setRowCount(len(self.options))
73+
for i in range(len(self.options)):
74+
item = QtGui.QLabel()
75+
item.setText(self.options[i])
76+
self.table.setCellWidget(i,0, item)
77+
78+
def okPressed(self):
79+
self.selectedoptions = []
80+
self.selectedoptions = self.options
81+
# "self.selectedoptions :", ['/temp/confmat1-1.csv', '/temp/confmat2-1.csv']
82+
self.close()
83+
84+
def cancelPressed(self):
85+
self.selectedoptions = None
86+
self.close()
87+
88+
def addFile(self):
89+
settings = QtCore.QSettings()
90+
lastfolder = settings.value("processingFilesLastFolder")
91+
if lastfolder :
92+
path = lastfolder
93+
else :
94+
path = QtCore.QDir.currentPath()
95+
96+
filesOpened = QtGui.QFileDialog.getOpenFileNames( None, "Select the file(s) to use", path, "All files (*.*)" )
97+
98+
lastfile = ""
99+
for item in filesOpened:
100+
self.options.append( str(item) )
101+
lastfile=item
102+
103+
self.setTableContent()
104+
folder = os.path.dirname( str( lastfile ) )
105+
settings.setValue("processingFilesLastFolder", folder)
106+
107+
def removeFile(self):
108+
indexRow = self.table.currentRow()
109+
itemToRemove = self.options[indexRow]
110+
self.options.remove(itemToRemove)
111+
self.setTableContent()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
MultipleExternalInputPanel.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
(C) 2013 by CS Systemes d'information (CS SI)
10+
Email : volayaf at gmail dot com
11+
otb at c-s dot fr (CS SI)
12+
Contributors : Victor Olaya - basis from MultipleInputPanel
13+
Alexia Mondot (CS SI) - adapt for a new parameter
14+
***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************
22+
"""
23+
24+
__author__ = 'Victor Olaya'
25+
__date__ = 'August 2012'
26+
__copyright__ = '(C) 2012, Victor Olaya'
27+
# This will get replaced with a git SHA1 when you do a git archive
28+
__revision__ = '$Format:%H$'
29+
30+
from PyQt4 import QtCore, QtGui
31+
from processing.gui.MultipleExternalInputDialog import MultipleExternalInputDialog
32+
33+
class MultipleExternalInputPanel(QtGui.QWidget):
34+
35+
def __init__(self, parent = None):
36+
super(MultipleExternalInputPanel, self).__init__(parent)
37+
self.selectedoptions = []
38+
self.horizontalLayout = QtGui.QHBoxLayout(self)
39+
self.horizontalLayout.setSpacing(2)
40+
self.horizontalLayout.setMargin(0)
41+
self.label = QtGui.QLabel()
42+
self.label.setText("0 elements selected")
43+
self.label.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
44+
self.horizontalLayout.addWidget(self.label)
45+
self.pushButton = QtGui.QPushButton()
46+
self.pushButton.setText("...")
47+
self.pushButton.clicked.connect(self.showSelectionDialog)
48+
self.horizontalLayout.addWidget(self.pushButton)
49+
self.setLayout(self.horizontalLayout)
50+
51+
def setSelectedItems(self, selected):
52+
#no checking is performed!
53+
self.selectedoptions = selected
54+
self.label.setText(str(len(self.selectedoptions)) + " elements selected")
55+
56+
def showSelectionDialog(self):
57+
#=======================================================================
58+
# #If there is a datatype, we use it to create the list of options
59+
# if self.datatype is not None:
60+
# if self.datatype == ParameterMultipleInput.TYPE_RASTER:
61+
# options = QGisLayers.getRasterLayers()
62+
# elif self.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
63+
# options = QGisLayers.getVectorLayers()
64+
# else:
65+
# options = QGisLayers.getVectorLayers(self.datatype)
66+
# opts = []
67+
# for opt in options:
68+
# opts.append(opt.name())
69+
# self.options = opts
70+
#=======================================================================
71+
dlg = MultipleExternalInputDialog(self.selectedoptions)
72+
dlg.exec_()
73+
if dlg.selectedoptions != None:
74+
self.selectedoptions = dlg.selectedoptions
75+
self.label.setText(str(len(self.selectedoptions)) + " elements selected")

‎python/plugins/processing/gui/ParametersPanel.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
---------------------
77
Date : August 2012
88
Copyright : (C) 2012 by Victor Olaya
9+
(C) 2013 by CS Systemes d'information (CS SI)
910
Email : volayaf at gmail dot com
11+
otb at c-s dot fr (CS SI)
12+
Contributors : Victor Olaya
13+
Alexia Mondot (CS SI) - managing the new parameter ParameterMultipleExternalInput
1014
***************************************************************************
1115
* *
1216
* This program is free software; you can redistribute it and/or modify *
@@ -42,6 +46,7 @@
4246
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
4347
from processing.gui.FileSelectionPanel import FileSelectionPanel
4448
from processing.gui.CrsSelectionPanel import CrsSelectionPanel
49+
from processing.gui.MultipleExternalInputPanel import MultipleExternalInputPanel
4550

4651
from processing.parameters.ParameterRaster import ParameterRaster
4752
from processing.parameters.ParameterVector import ParameterVector
@@ -57,6 +62,7 @@
5762
from processing.parameters.ParameterFile import ParameterFile
5863
from processing.parameters.ParameterCrs import ParameterCrs
5964
from processing.parameters.ParameterString import ParameterString
65+
from processing.parameters.ParameterMultipleExternalInput import ParameterMultipleExternalInput
6066

6167
from processing.outputs.OutputRaster import OutputRaster
6268
from processing.outputs.OutputTable import OutputTable
@@ -276,6 +282,8 @@ def getWidgetFromParameter(self, param):
276282
for opt in options:
277283
opts.append(self.getExtendedLayerName(opt))
278284
item = MultipleInputPanel(opts)
285+
elif isinstance(param, ParameterMultipleExternalInput):
286+
item = MultipleExternalInputPanel()
279287
elif isinstance(param, ParameterNumber):
280288
item = NumberInputPanel(param.default, param.min, param.max,
281289
param.isInteger)

‎python/plugins/processing/parameters/ParameterFactory.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
---------------------
77
Date : August 2012
88
Copyright : (C) 2012 by Victor Olaya
9+
(C) 2013 by CS Systemes d'information (CS SI)
910
Email : volayaf at gmail dot com
11+
otb at c-s dot fr (CS SI)
12+
Contributors : Victor Olaya
13+
Alexia Mondot (CS SI) - managing the new parameter ParameterMultipleExternalInput
1014
***************************************************************************
1115
* *
1216
* This program is free software; you can redistribute it and/or modify *
@@ -39,12 +43,17 @@
3943
from processing.parameters.ParameterExtent import ParameterExtent
4044
from processing.parameters.ParameterFile import ParameterFile
4145
from processing.parameters.ParameterCrs import ParameterCrs
46+
from processing.parameters.ParameterMultipleExternalInput import ParameterMultipleExternalInput
4247

4348

4449
class ParameterFactory:
4550

4651
@staticmethod
4752
def getFromString(s):
53+
"""
54+
In : ParameterNumber|-nodatalabel|Label for the NoData class|None|None|0
55+
Out : returns the object from class pointed by s with information extracted from s
56+
"""
4857
classes = [
4958
ParameterBoolean,
5059
ParameterMultipleInput,
@@ -60,7 +69,8 @@ def getFromString(s):
6069
ParameterExtent,
6170
ParameterFile,
6271
ParameterCrs,
72+
ParameterMultipleExternalInput
6373
]
6474
for clazz in classes:
6575
if s.startswith(clazz().parameterName()):
66-
return clazz().deserialize(s)
76+
return clazz().deserialize(s)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ParameterMultipleExternalInput.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
(C) 2013 by CS Systemes d'information (CS SI)
10+
Email : volayaf at gmail dot com
11+
otb at c-s dot fr (CS SI)
12+
Contributors : Victor Olaya - basis from ParameterMultipleInput
13+
Alexia Mondot (CS SI) - managing the new parameter ParameterMultipleExternalInput
14+
***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************
22+
"""
23+
24+
__author__ = 'Victor Olaya'
25+
__date__ = 'August 2012'
26+
__copyright__ = '(C) 2012, Victor Olaya'
27+
# This will get replaced with a git SHA1 when you do a git archive
28+
__revision__ = '$Format:%H$'
29+
30+
from processing.parameters.ParameterDataObject import ParameterDataObject
31+
from processing.core.QGisLayers import QGisLayers
32+
from qgis.core import *
33+
from processing.core.LayerExporter import LayerExporter
34+
35+
class ParameterMultipleExternalInput(ParameterDataObject):
36+
'''A parameter representing several data objects.
37+
Its value is a string with substrings separated by semicolons, each of which
38+
represents the data source location of each element'''
39+
40+
exported = None
41+
42+
def __init__(self, name="", description="", optional = False):
43+
ParameterDataObject.__init__(self, name, description)
44+
self.datatype = None # to delete
45+
self.optional = optional
46+
self.value = None
47+
self.exported = None
48+
self.default=""
49+
50+
def setValue(self, obj):
51+
if isinstance(obj, list):
52+
if len(obj) == 0:
53+
if self.optional:
54+
return True
55+
else:
56+
return False
57+
s = ""
58+
idx = 0
59+
for filename in obj:
60+
s += filename
61+
if idx < len(obj) - 1:
62+
s+=" "
63+
idx=idx+1;
64+
self.value = s;
65+
return True
66+
67+
68+
def getSafeExportedLayers(self):
69+
'''Returns not the value entered by the user, but a string with semicolon-separated filenames
70+
which contains the data of the selected layers, but saved in a standard format (currently
71+
shapefiles for vector layers and GeoTiff for raster) so that they can be opened by most
72+
external applications.
73+
If there is a selection and QGIS is configured to use just the selection, if exports
74+
the layer even if it is already in a suitable format.
75+
Works only if the layer represented by the parameter value is currently loaded in QGIS.
76+
Otherwise, it will not perform any export and return the current value string.
77+
If the current value represents a layer in a suitable format, it does no export at all
78+
and returns that value.
79+
Currently, it works just for vector layer. In the case of raster layers, it returns the
80+
parameter value.
81+
The layers are exported just the first time the method is called. The method can be called
82+
several times and it will always return the same string, performing the export only the first time.'''
83+
if self.exported:
84+
return self.exported
85+
self.exported = self.value
86+
layers = self.value.split(";")
87+
if layers == None or len(layers) == 0:
88+
return self.value
89+
90+
return layers
91+
92+
93+
94+
def serialize(self):
95+
return self.__module__.split(".")[-1] + "|" + self.name + "|" + self.description + "|" + str(self.optional)
96+
97+
def deserialize(self, s):
98+
"""
99+
in : string : ParameterNumber|-nodatalabel|Label for the NoData class|None|None|0
100+
returns the current class object from extracted information from s
101+
"""
102+
tokens = s.split("|")
103+
return ParameterMultipleExternalInput(tokens[1], tokens[2], tokens[3] == str(True))
104+
105+
def getAsScriptCode(self):
106+
return "##" + self.name

0 commit comments

Comments
 (0)
Please sign in to comment.