|
33 | 33 | from qgis.PyQt.QtCore import QCoreApplication, QDir
|
34 | 34 | from qgis.PyQt.QtWidgets import QDialog, QMenu, QAction, QFileDialog
|
35 | 35 | from qgis.PyQt.QtGui import QCursor
|
36 |
| -from qgis.gui import QgsEncodingFileDialog, QgsExpressionBuilderDialog |
| 36 | +from qgis.gui import QgsEncodingFileDialog, QgsEncodingSelectionDialog |
37 | 37 | from qgis.core import (QgsDataSourceUri,
|
38 | 38 | QgsCredentials,
|
39 | 39 | QgsExpression,
|
40 | 40 | QgsSettings,
|
41 | 41 | QgsProcessingParameterFeatureSink,
|
| 42 | + QgsProcessingParameterRasterDestination, |
42 | 43 | QgsProcessingOutputLayerDefinition,
|
43 | 44 | QgsProcessingParameterDefinition,
|
44 | 45 | QgsProcessingParameterFileDestination,
|
@@ -122,6 +123,11 @@ def selectOutput(self):
|
122 | 123 | actionSaveToFile.triggered.connect(self.selectFile)
|
123 | 124 | popupMenu.addAction(actionSaveToFile)
|
124 | 125 |
|
| 126 | + actionSetEncoding = QAction( |
| 127 | + self.tr('Change file encoding ({})...').format(self.encoding), self.btnSelect) |
| 128 | + actionSetEncoding.triggered.connect(self.selectEncoding) |
| 129 | + popupMenu.addAction(actionSetEncoding) |
| 130 | + |
125 | 131 | if isinstance(self.parameter, QgsProcessingParameterFeatureSink) \
|
126 | 132 | and self.alg.provider().supportsNonFileBasedOutput():
|
127 | 133 | actionSaveToSpatialite = QAction(
|
@@ -208,35 +214,53 @@ def saveToSpatialite(self):
|
208 | 214 | self.leText.setText("spatialite:" + uri.uri())
|
209 | 215 |
|
210 | 216 | def selectFile(self):
|
211 |
| - fileFilter = getFileFilter(self.parameter) |
212 |
| - |
| 217 | + file_filter = getFileFilter(self.parameter) |
213 | 218 | settings = QgsSettings()
|
| 219 | + if isinstance(self.parameter, QgsProcessingParameterFeatureSink): |
| 220 | + last_ext_path = '/Processing/LastVectorOutputExt' |
| 221 | + last_ext = settings.value(last_ext_path, '.gpkg') |
| 222 | + elif isinstance(self.parameter, QgsProcessingParameterRasterDestination): |
| 223 | + last_ext_path = '/Processing/LastRasterOutputExt' |
| 224 | + last_ext = settings.value(last_ext_path, '.tif') |
| 225 | + else: |
| 226 | + last_ext_path = None |
| 227 | + last_ext = None |
| 228 | + |
| 229 | + # get default filter |
| 230 | + filters = file_filter.split(';;') |
| 231 | + try: |
| 232 | + last_filter = [f for f in filters if '*{}'.format(last_ext) in f.lower()][0] |
| 233 | + except: |
| 234 | + last_filter = None |
| 235 | + |
214 | 236 | if settings.contains('/Processing/LastOutputPath'):
|
215 | 237 | path = settings.value('/Processing/LastOutputPath')
|
216 | 238 | else:
|
217 | 239 | path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)
|
218 | 240 |
|
219 |
| - fileDialog = QgsEncodingFileDialog( |
220 |
| - self, self.tr('Save file'), path, fileFilter, self.encoding) |
221 |
| - fileDialog.setFileMode(QFileDialog.AnyFile) |
222 |
| - fileDialog.setAcceptMode(QFileDialog.AcceptSave) |
223 |
| - fileDialog.setOption(QFileDialog.DontConfirmOverwrite, False) |
224 |
| - |
225 |
| - if fileDialog.exec_() == QDialog.Accepted: |
| 241 | + filename, filter = QFileDialog.getSaveFileName(self, self.tr("Save file"), path, |
| 242 | + file_filter, last_filter) |
| 243 | + if filename: |
226 | 244 | self.use_temporary = False
|
227 |
| - files = fileDialog.selectedFiles() |
228 |
| - self.encoding = str(fileDialog.encoding()) |
229 |
| - fileName = str(files[0]) |
230 |
| - selectedFileFilter = str(fileDialog.selectedNameFilter()) |
231 |
| - if not fileName.lower().endswith( |
232 |
| - tuple(re.findall("\\*(\\.[a-z]{1,10})", fileFilter))): |
233 |
| - ext = re.search("\\*(\\.[a-z]{1,10})", selectedFileFilter) |
| 245 | + if not filename.lower().endswith( |
| 246 | + tuple(re.findall("\\*(\\.[a-z]{1,10})", file_filter))): |
| 247 | + ext = re.search("\\*(\\.[a-z]{1,10})", filter) |
234 | 248 | if ext:
|
235 |
| - fileName += ext.group(1) |
236 |
| - self.leText.setText(fileName) |
| 249 | + filename += ext.group(1) |
| 250 | + self.leText.setText(filename) |
237 | 251 | settings.setValue('/Processing/LastOutputPath',
|
238 |
| - os.path.dirname(fileName)) |
| 252 | + os.path.dirname(filename)) |
| 253 | + if not last_ext_path is None: |
| 254 | + settings.setValue(last_ext_path, os.path.splitext(filename)[1].lower()) |
| 255 | + |
| 256 | + def selectEncoding(self): |
| 257 | + dialog = QgsEncodingSelectionDialog( |
| 258 | + self, self.tr('File encoding'), self.encoding) |
| 259 | + if dialog.exec_() == QDialog.Accepted: |
| 260 | + self.encoding = dialog.encoding() |
| 261 | + settings = QgsSettings() |
239 | 262 | settings.setValue('/Processing/encoding', self.encoding)
|
| 263 | + dialog.deleteLater() |
240 | 264 |
|
241 | 265 | def selectDirectory(self):
|
242 | 266 | lastDir = self.leText.text()
|
|
0 commit comments