29
29
30
30
import os
31
31
32
- from qgis .core import QgsSettings
32
+ from qgis .core import (QgsSettings ,
33
+ QgsProcessing ,
34
+ QgsVectorFileWriter ,
35
+ QgsProviderRegistry )
33
36
from qgis .PyQt import uic
34
37
from qgis .PyQt .QtCore import Qt
35
38
from qgis .PyQt .QtCore import QByteArray
36
- from qgis .PyQt .QtWidgets import QDialog , QAbstractItemView , QPushButton , QDialogButtonBox
39
+ from qgis .PyQt .QtWidgets import QDialog , QAbstractItemView , QPushButton , QDialogButtonBox , QFileDialog
37
40
from qgis .PyQt .QtGui import QStandardItemModel , QStandardItem
41
+ from processing .tools import dataobjects
38
42
39
43
pluginPath = os .path .split (os .path .dirname (__file__ ))[0 ]
40
44
WIDGET , BASE = uic .loadUiType (
43
47
44
48
class MultipleInputDialog (BASE , WIDGET ):
45
49
46
- def __init__ (self , options , selectedoptions = None ):
50
+ def __init__ (self , options , selectedoptions = None , datatype = None ):
47
51
super (MultipleInputDialog , self ).__init__ (None )
48
52
self .setupUi (self )
53
+ self .datatype = datatype
54
+ self .model = None
49
55
50
56
self .lstLayers .setSelectionMode (QAbstractItemView .NoSelection )
51
57
@@ -68,6 +74,11 @@ def __init__(self, options, selectedoptions=None):
68
74
self .btnToggleSelection = QPushButton (self .tr ('Toggle selection' ))
69
75
self .buttonBox .addButton (self .btnToggleSelection ,
70
76
QDialogButtonBox .ActionRole )
77
+ if self .datatype is not None :
78
+ btnAddFile = QPushButton (self .tr ('Add file(s)…' ))
79
+ btnAddFile .clicked .connect (self .addFiles )
80
+ self .buttonBox .addButton (btnAddFile ,
81
+ QDialogButtonBox .ActionRole )
71
82
72
83
self .btnSelectAll .clicked .connect (lambda : self .selectAll (True ))
73
84
self .btnClearSelection .clicked .connect (lambda : self .selectAll (False ))
@@ -83,15 +94,23 @@ def saveWindowGeometry(self):
83
94
self .settings .setValue ("/Processing/multipleInputDialogGeometry" , self .saveGeometry ())
84
95
85
96
def populateList (self ):
86
- model = QStandardItemModel ()
97
+ self . model = QStandardItemModel ()
87
98
for value , text in self .options :
88
99
item = QStandardItem (text )
89
100
item .setData (value , Qt .UserRole )
90
101
item .setCheckState (Qt .Checked if value in self .selectedoptions else Qt .Unchecked )
91
102
item .setCheckable (True )
92
- model .appendRow (item )
103
+ self . model .appendRow (item )
93
104
94
- self .lstLayers .setModel (model )
105
+ # add extra options (e.g. manually added layers)
106
+ for t in [o for o in self .selectedoptions if not isinstance (o , int )]:
107
+ item = QStandardItem (t )
108
+ item .setData (t , Qt .UserRole )
109
+ item .setCheckState (Qt .Checked )
110
+ item .setCheckable (True )
111
+ self .model .appendRow (item )
112
+
113
+ self .lstLayers .setModel (self .model )
95
114
96
115
def accept (self ):
97
116
self .selectedoptions = []
@@ -118,3 +137,38 @@ def toggleSelection(self):
118
137
item = model .item (i )
119
138
checked = item .checkState () == Qt .Checked
120
139
item .setCheckState (Qt .Unchecked if checked else Qt .Checked )
140
+
141
+ def getFileFilter (self , datatype ):
142
+ """
143
+ Returns a suitable file filter pattern for the specified parameter definition
144
+ :param param:
145
+ :return:
146
+ """
147
+ if datatype == QgsProcessing .TypeRaster :
148
+ return QgsProviderRegistry .instance ().fileRasterFilters ()
149
+ elif datatype == QgsProcessing .TypeFile :
150
+ return self .tr ('All files (*.*)' )
151
+ else :
152
+ exts = QgsVectorFileWriter .supportedFormatExtensions ()
153
+ for i in range (len (exts )):
154
+ exts [i ] = self .tr ('{0} files (*.{1})' ).format (exts [i ].upper (), exts [i ].lower ())
155
+ return self .tr ('All files (*.*)' ) + ';;' + ';;' .join (exts )
156
+
157
+ def addFiles (self ):
158
+ filter = self .getFileFilter (self .datatype )
159
+
160
+ settings = QgsSettings ()
161
+ path = str (settings .value ('/Processing/LastInputPath' ))
162
+
163
+ ret , selected_filter = QFileDialog .getOpenFileNames (self , self .tr ('Select file(s)' ),
164
+ path , filter )
165
+ if ret :
166
+ files = list (ret )
167
+ settings .setValue ('/Processing/LastInputPath' ,
168
+ os .path .dirname (str (files [0 ])))
169
+ for filename in files :
170
+ item = QStandardItem (filename )
171
+ item .setData (filename , Qt .UserRole )
172
+ item .setCheckState (Qt .Checked )
173
+ item .setCheckable (True )
174
+ self .model .appendRow (item )
0 commit comments