File: ModelerParameterDefinitionDialog.py

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (341 lines) | stat: -rw-r--r-- 16,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-

"""
***************************************************************************
    ModelerParameterDefinitionDialog.py
    ---------------------
    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'

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

__revision__ = '$Format:%H$'

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from processing.parameters.Parameter import Parameter
from processing.parameters.ParameterBoolean import ParameterBoolean
from processing.parameters.ParameterRaster import ParameterRaster
from processing.parameters.ParameterTable import ParameterTable
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterString import ParameterString
from processing.parameters.ParameterTableField import ParameterTableField
from processing.parameters.ParameterExtent import ParameterExtent
from processing.parameters.ParameterFile import ParameterFile


class ModelerParameterDefinitionDialog(QtGui.QDialog):

    PARAMETER_NUMBER = 'Number'
    PARAMETER_RASTER = 'Raster Layer'
    PARAMETER_TABLE = 'Table'
    PARAMETER_VECTOR = 'Vector layer'
    PARAMETER_STRING = 'String'
    PARAMETER_BOOLEAN = 'Boolean'
    PARAMETER_TABLE_FIELD = 'Table field'
    PARAMETER_EXTENT = 'Extent'
    PARAMETER_FILE = 'File'

    # To add
    PARAMETER_MULTIPLE = 'Multiple input'
    PARAMETER_FIXED_TABLE = 'Fixed table'

    paramTypes = [
        PARAMETER_BOOLEAN,
        PARAMETER_EXTENT,
        PARAMETER_FILE,
        PARAMETER_NUMBER,
        PARAMETER_RASTER,
        PARAMETER_STRING,
        PARAMETER_TABLE,
        PARAMETER_TABLE_FIELD,
        PARAMETER_VECTOR,
        ]

    def __init__(self, alg, paramType=None, param=None):
        self.alg = alg
        self.paramType = paramType
        self.param = param
        QtGui.QDialog.__init__(self)
        self.setModal(True)
        self.setupUi()

    def setupUi(self):
        self.setWindowTitle('Parameter definition')

        self.verticalLayout = QtGui.QVBoxLayout(self)
        self.verticalLayout.setSpacing(40)
        self.verticalLayout.setMargin(20)

        self.horizontalLayout = QtGui.QHBoxLayout(self)
        self.horizontalLayout.setSpacing(2)
        self.horizontalLayout.setMargin(0)
        self.label = QtGui.QLabel('Parameter name')
        self.horizontalLayout.addWidget(self.label)
        self.nameTextBox = QtGui.QLineEdit()
        self.horizontalLayout.addWidget(self.nameTextBox)
        self.verticalLayout.addLayout(self.horizontalLayout)

        self.horizontalLayout2 = QtGui.QHBoxLayout(self)
        self.horizontalLayout2.setSpacing(2)
        self.horizontalLayout2.setMargin(0)
        self.horizontalLayout3 = QtGui.QHBoxLayout(self)
        self.horizontalLayout3.setSpacing(2)
        self.horizontalLayout3.setMargin(0)

        if isinstance(self.param, Parameter):
            self.nameTextBox.setText(self.param.description)

        if self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_BOOLEAN \
                or isinstance(self.param, ParameterBoolean):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Default value'))
            self.yesNoCombo = QtGui.QComboBox()
            self.yesNoCombo.addItem('Yes')
            self.yesNoCombo.addItem('No')
            if self.param is not None:
                self.yesNoCombo.setCurrentIndex((0 if self.param.value else 1))
            self.horizontalLayout2.addWidget(self.yesNoCombo)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_TABLE_FIELD \
                or isinstance(self.param, ParameterTableField):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Parent layer'))
            self.parentCombo = QtGui.QComboBox()
            idx = 0
            for param in self.alg.parameters:
                if isinstance(param, (ParameterVector, ParameterTable)):
                    self.parentCombo.addItem(param.description, param.name)
                    if self.param is not None:
                        if self.param.parent == param.name:
                            self.parentCombo.setCurrentIndex(idx)
                    idx += 1
            self.horizontalLayout2.addWidget(self.parentCombo)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_RASTER \
                or isinstance(self.param, ParameterRaster):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Required'))
            self.yesNoCombo = QtGui.QComboBox()
            self.yesNoCombo.addItem('Yes')
            self.yesNoCombo.addItem('No')
            if self.param is not None:
                self.yesNoCombo.setCurrentIndex(
                        (1 if self.param.optional else 0))
            self.horizontalLayout2.addWidget(self.yesNoCombo)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_TABLE \
                or isinstance(self.param, ParameterTable):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Required'))
            self.yesNoCombo = QtGui.QComboBox()
            self.yesNoCombo.addItem('Yes')
            self.yesNoCombo.addItem('No')
            if self.param is not None:
                self.yesNoCombo.setCurrentIndex(
                        (1 if self.param.optional else 0))
            self.horizontalLayout2.addWidget(self.yesNoCombo)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_VECTOR \
                or isinstance(self.param, ParameterVector):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Required'))
            self.yesNoCombo = QtGui.QComboBox()
            self.yesNoCombo.addItem('Yes')
            self.yesNoCombo.addItem('No')
            self.horizontalLayout2.addWidget(self.yesNoCombo)
            self.horizontalLayout3.addWidget(QtGui.QLabel('Shape type'))
            self.shapetypeCombo = QtGui.QComboBox()
            self.shapetypeCombo.addItem('Any')
            self.shapetypeCombo.addItem('Point')
            self.shapetypeCombo.addItem('Line')
            self.shapetypeCombo.addItem('Polygon')
            if self.param is not None:
                self.yesNoCombo.setCurrentIndex(
                        (1 if self.param.optional else 0))
                self.shapetypeCombo.setCurrentIndex(self.param.shapetype[0]
                        + 1)
            self.horizontalLayout3.addWidget(self.shapetypeCombo)
            self.verticalLayout.addLayout(self.horizontalLayout3)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE \
                or isinstance(self.param, ParameterMultipleInput):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Mandatory'))
            self.yesNoCombo = QtGui.QComboBox()
            self.yesNoCombo.addItem('Yes')
            self.yesNoCombo.addItem('No')
            self.horizontalLayout2.addWidget(self.yesNoCombo)
            self.horizontalLayout3.addWidget(QtGui.QLabel('Data type'))
            self.datatypeCombo = QtGui.QComboBox()
            self.datatypeCombo.addItem('Vector (any)')
            self.datatypeCombo.addItem('Vector (point)')
            self.datatypeCombo.addItem('Vector (line)')
            self.datatypeCombo.addItem('Vector (polygon)')
            self.datatypeCombo.addItem('Raster')
            self.datatypeCombo.addItem('Table')
            if self.param is not None:
                self.yesNoCombo.setCurrentIndex(
                        (1 if self.param.optional else 0))
                self.datatypeCombo.setCurrentIndex(self.param.datatype + 1)
            self.horizontalLayout3.addWidget(self.datatypeCombo)
            self.verticalLayout.addLayout(self.horizontalLayout3)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_NUMBER \
                or isinstance(self.param, ParameterNumber):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Min/Max values'))
            self.minTextBox = QtGui.QLineEdit()
            self.maxTextBox = QtGui.QLineEdit()
            self.horizontalLayout2.addWidget(self.minTextBox)
            self.horizontalLayout2.addWidget(self.maxTextBox)
            self.verticalLayout.addLayout(self.horizontalLayout2)
            self.horizontalLayout3.addWidget(QtGui.QLabel('Default value'))
            self.defaultTextBox = QtGui.QLineEdit()
            self.defaultTextBox.setText('0')
            if self.param is not None:
                self.defaultTextBox.setText(str(self.param.default))
            self.horizontalLayout3.addWidget(self.defaultTextBox)
            self.verticalLayout.addLayout(self.horizontalLayout3)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_STRING \
                or isinstance(self.param, ParameterString):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Default value'))
            self.defaultTextBox = QtGui.QLineEdit()
            if self.param is not None:
                self.defaultTextBox.setText(self.param.default)
            self.horizontalLayout2.addWidget(self.defaultTextBox)
            self.verticalLayout.addLayout(self.horizontalLayout2)
        elif self.paramType == \
                ModelerParameterDefinitionDialog.PARAMETER_FILE \
                or isinstance(self.param, ParameterFile):
            self.horizontalLayout2.addWidget(QtGui.QLabel('Type'))
            self.fileFolderCombo = QtGui.QComboBox()
            self.fileFolderCombo.addItem('File')
            self.fileFolderCombo.addItem('Folder')
            if self.param is not None:
                self.fileFolderCombo.setCurrentIndex(
                        (1 if self.param.isFolder else 0))
            self.horizontalLayout2.addWidget(self.fileFolderCombo)
            self.verticalLayout.addLayout(self.horizontalLayout2)

        self.buttonBox = QtGui.QDialogButtonBox(self)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel
                | QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName('buttonBox')
        QObject.connect(self.buttonBox, QtCore.SIGNAL('accepted()'),
                        self.okPressed)
        QObject.connect(self.buttonBox, QtCore.SIGNAL('rejected()'),
                        self.cancelPressed)

        self.verticalLayout.addWidget(self.buttonBox)

        self.setLayout(self.verticalLayout)

    def okPressed(self):
        description = unicode(self.nameTextBox.text())
        if description.strip() == '':
            QMessageBox.critical(self, 'Unable to define parameter',
                                 'Invalid parameter name')
            return
        if self.param is None:
            validChars = \
                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
            safeName = ''.join(c for c in description if c in validChars)
            name = self.paramType.upper().replace(' ', '') + '_' \
                + safeName.upper()
        else:
            name = self.param.name
        if self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_BOOLEAN \
                or isinstance(self.param, ParameterBoolean):
            self.param = ParameterBoolean(name, description,
                    self.yesNoCombo.currentIndex() == 0)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_TABLE_FIELD \
                or isinstance(self.param, ParameterTableField):
            if self.parentCombo.currentIndex() < 0:
                QMessageBox.critical(self, 'Unable to define parameter',
                                     'Wrong or missing parameter values')
                return
            parent = self.parentCombo.itemData(self.parentCombo.currentIndex())
            self.param = ParameterTableField(name, description, parent)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_RASTER \
                or isinstance(self.param, ParameterRaster):
            self.param = ParameterRaster(name, description,
                    self.yesNoCombo.currentIndex() == 1)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_TABLE \
                or isinstance(self.param, ParameterTable):
            self.param = ParameterTable(name, description,
                    self.yesNoCombo.currentIndex() == 1)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_VECTOR \
                or isinstance(self.param, ParameterVector):
            self.param = ParameterVector(name, description,
                    [self.shapetypeCombo.currentIndex() - 1],
                    self.yesNoCombo.currentIndex() == 1)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE \
                or isinstance(self.param, ParameterMultipleInput):
            self.param = ParameterMultipleInput(name, description,
                    self.datatypeCombo.currentIndex() - 1,
                    self.yesNoCombo.currentIndex() == 1)
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_NUMBER \
                or isinstance(self.param, ParameterNumber):
            try:
                vmin = str(self.minTextBox.text()).strip()
                if vmin == '':
                    vmin = None
                else:
                    vmin = float(vmin)
                vmax = str(self.maxTextBox.text()).strip()
                if vmax == '':
                    vmax = None
                else:
                    vmax = float(vmax)
                self.param = ParameterNumber(name, description, vmin, vmax,
                        float(str(self.defaultTextBox.text())))
            except:
                QMessageBox.critical(self, 'Unable to define parameter',
                                     'Wrong or missing parameter values')
                return
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_STRING \
                or isinstance(self.param, ParameterString):
            self.param = ParameterString(name, description,
                    unicode(self.defaultTextBox.text()))
        elif self.paramType \
                == ModelerParameterDefinitionDialog.PARAMETER_EXTENT \
                or isinstance(self.param, ParameterExtent):
            self.param = ParameterExtent(name, description)
        elif self.paramType == \
                ModelerParameterDefinitionDialog.PARAMETER_FILE \
                or isinstance(self.param, ParameterFile):
            isFolder = self.fileFolderCombo.currentIndex() == 1
            self.param = ParameterFile(name, description, isFolder=isFolder)
        self.close()

    def cancelPressed(self):
        self.param = None
        self.close()