Skip to content
Permalink
Browse files

[processing] Move destination encoding option to separate menu action

...instead of embedding in output destination file selector. Otherwise
Qt downgrades the file picker to the Qt basic file picker, instead of
the native OS file picker. Which is a huge downgrade and barely
functional for users.

Also remember last selected file format
  • Loading branch information
nyalldawson committed Nov 21, 2017
1 parent f660d78 commit 31086d3ac587aef637f085da738aee95e6957b6f
Showing with 45 additions and 20 deletions.
  1. +44 −20 python/plugins/processing/gui/DestinationSelectionPanel.py
  2. +1 −0 src/gui/qgsencodingfiledialog.cpp
@@ -33,12 +33,13 @@
from qgis.PyQt.QtCore import QCoreApplication, QDir
from qgis.PyQt.QtWidgets import QDialog, QMenu, QAction, QFileDialog
from qgis.PyQt.QtGui import QCursor
from qgis.gui import QgsEncodingFileDialog, QgsExpressionBuilderDialog
from qgis.gui import QgsEncodingFileDialog, QgsEncodingSelectionDialog
from qgis.core import (QgsDataSourceUri,
QgsCredentials,
QgsExpression,
QgsSettings,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterRasterDestination,
QgsProcessingOutputLayerDefinition,
QgsProcessingParameterDefinition,
QgsProcessingParameterFileDestination,
@@ -122,6 +123,11 @@ def selectOutput(self):
actionSaveToFile.triggered.connect(self.selectFile)
popupMenu.addAction(actionSaveToFile)

actionSetEncoding = QAction(
self.tr('Change file encoding ({})...').format(self.encoding), self.btnSelect)
actionSetEncoding.triggered.connect(self.selectEncoding)
popupMenu.addAction(actionSetEncoding)

if isinstance(self.parameter, QgsProcessingParameterFeatureSink) \
and self.alg.provider().supportsNonFileBasedOutput():
actionSaveToSpatialite = QAction(
@@ -208,35 +214,53 @@ def saveToSpatialite(self):
self.leText.setText("spatialite:" + uri.uri())

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

file_filter = getFileFilter(self.parameter)
settings = QgsSettings()
if isinstance(self.parameter, QgsProcessingParameterFeatureSink):
last_ext_path = '/Processing/LastVectorOutputExt'
last_ext = settings.value(last_ext_path, '.gpkg')
elif isinstance(self.parameter, QgsProcessingParameterRasterDestination):
last_ext_path = '/Processing/LastRasterOutputExt'
last_ext = settings.value(last_ext_path, '.tif')
else:
last_ext_path = None
last_ext = None

# get default filter
filters = file_filter.split(';;')
try:
last_filter = [f for f in filters if '*{}'.format(last_ext) in f.lower()][0]
except:
last_filter = None

if settings.contains('/Processing/LastOutputPath'):
path = settings.value('/Processing/LastOutputPath')
else:
path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)

fileDialog = QgsEncodingFileDialog(
self, self.tr('Save file'), path, fileFilter, self.encoding)
fileDialog.setFileMode(QFileDialog.AnyFile)
fileDialog.setAcceptMode(QFileDialog.AcceptSave)
fileDialog.setOption(QFileDialog.DontConfirmOverwrite, False)

if fileDialog.exec_() == QDialog.Accepted:
filename, filter = QFileDialog.getSaveFileName(self, self.tr("Save file"), path,
file_filter, last_filter)
if filename:
self.use_temporary = False
files = fileDialog.selectedFiles()
self.encoding = str(fileDialog.encoding())
fileName = str(files[0])
selectedFileFilter = str(fileDialog.selectedNameFilter())
if not fileName.lower().endswith(
tuple(re.findall("\\*(\\.[a-z]{1,10})", fileFilter))):
ext = re.search("\\*(\\.[a-z]{1,10})", selectedFileFilter)
if not filename.lower().endswith(
tuple(re.findall("\\*(\\.[a-z]{1,10})", file_filter))):
ext = re.search("\\*(\\.[a-z]{1,10})", filter)
if ext:
fileName += ext.group(1)
self.leText.setText(fileName)
filename += ext.group(1)
self.leText.setText(filename)
settings.setValue('/Processing/LastOutputPath',
os.path.dirname(fileName))
os.path.dirname(filename))
if not last_ext_path is None:
settings.setValue(last_ext_path, os.path.splitext(filename)[1].lower())

def selectEncoding(self):
dialog = QgsEncodingSelectionDialog(
self, self.tr('File encoding'), self.encoding)
if dialog.exec_() == QDialog.Accepted:
self.encoding = dialog.encoding()
settings = QgsSettings()
settings.setValue('/Processing/encoding', self.encoding)
dialog.deleteLater()

def selectDirectory(self):
lastDir = self.leText.text()
@@ -132,6 +132,7 @@ QgsEncodingSelectionDialog::QgsEncodingSelectionDialog( QWidget *parent, const Q
layout->addWidget( buttonBox );
setLayout( layout );

mEncodingComboBox->addItem( tr( "System" ) );
mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );

// Use default encoding if none supplied

0 comments on commit 31086d3

Please sign in to comment.
You can’t perform that action at this time.