diff --git a/python/gui/qgsfilewidget.sip b/python/gui/qgsfilewidget.sip index 32da03f48f2..3ebfffe7db5 100644 --- a/python/gui/qgsfilewidget.sip +++ b/python/gui/qgsfilewidget.sip @@ -96,6 +96,16 @@ returns the filters used for QDialog.getOpenFileName setFilter sets the filter used by the model to filters. The filter is used to specify the kind of files that should be shown. :param filter: Only files that match the given filter are shown, it may be an empty string. If you want multiple filters, separate them with ';;', +%End + + void setSelectedFilter( const QString selectedFilter ); +%Docstring +Sets the selected filter when the file dialog opens. +%End + + QString selectedFilter() const; +%Docstring +Returns the selected filter from the last opened file dialog. %End void setConfirmOverwrite( bool confirmOverwrite ); diff --git a/src/gui/qgsfilewidget.cpp b/src/gui/qgsfilewidget.cpp index bf7ff03e444..82d998c093c 100644 --- a/src/gui/qgsfilewidget.cpp +++ b/src/gui/qgsfilewidget.cpp @@ -253,11 +253,11 @@ void QgsFileWidget::openFileDialog() { case GetFile: title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a file" ); - fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter ); + fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter ); break; case GetMultipleFiles: title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select one or more files" ); - fileNames = QFileDialog::getOpenFileNames( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter ); + fileNames = QFileDialog::getOpenFileNames( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter ); break; case GetDirectory: title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" ); @@ -265,20 +265,19 @@ void QgsFileWidget::openFileDialog() break; case SaveFile: { - QString filter; title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Create or select a file" ); if ( !confirmOverwrite() ) { - fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &filter, QFileDialog::DontConfirmOverwrite ); + fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, QFileDialog::DontConfirmOverwrite ); } else { - fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &filter ); + fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter ); } // make sure filename ends with filter. This isn't automatically done by // getSaveFileName on some platforms (e.g. gnome) - fileName = QgsFileUtils::addExtensionFromFilter( fileName, filter ); + fileName = QgsFileUtils::addExtensionFromFilter( fileName, mSelectedFilter ); } break; } diff --git a/src/gui/qgsfilewidget.h b/src/gui/qgsfilewidget.h index 0bca051034c..1d74ea81ed8 100644 --- a/src/gui/qgsfilewidget.h +++ b/src/gui/qgsfilewidget.h @@ -121,6 +121,16 @@ class GUI_EXPORT QgsFileWidget : public QWidget */ void setFilter( const QString &filter ); + /** + * Sets the selected filter when the file dialog opens. + */ + void setSelectedFilter( const QString selectedFilter ) { mSelectedFilter = selectedFilter; } + + /** + * Returns the selected filter from the last opened file dialog. + */ + QString selectedFilter() const { return mSelectedFilter; } + /** * Sets whether a confirmation to overwrite an existing file will appear. * By default, a confirmation will appear. @@ -185,6 +195,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget bool mFullUrl = false; QString mDialogTitle; QString mFilter; + QString mSelectedFilter; QString mDefaultRoot; bool mConfirmOverwrite = true; StorageMode mStorageMode = GetFile; diff --git a/src/providers/delimitedtext/qgsdelimitedtextsourceselect.cpp b/src/providers/delimitedtext/qgsdelimitedtextsourceselect.cpp index bbd45fd4d80..f48abcb8a15 100644 --- a/src/providers/delimitedtext/qgsdelimitedtextsourceselect.cpp +++ b/src/providers/delimitedtext/qgsdelimitedtextsourceselect.cpp @@ -40,7 +40,6 @@ QgsDelimitedTextSourceSelect::QgsDelimitedTextSourceSelect( QWidget *parent, Qt: { setupUi( this ); - connect( btnBrowseForFile, &QPushButton::clicked, this, &QgsDelimitedTextSourceSelect::btnBrowseForFile_clicked ); setupButtons( buttonBox ); connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsDelimitedTextSourceSelect::showHelp ); @@ -67,7 +66,6 @@ QgsDelimitedTextSourceSelect::QgsDelimitedTextSourceSelect( QWidget *parent, Qt: loadSettings(); updateFieldsAndEnable(); - connect( txtFilePath, &QLineEdit::textChanged, this, &QgsDelimitedTextSourceSelect::updateFileName ); connect( txtLayerName, &QLineEdit::textChanged, this, &QgsDelimitedTextSourceSelect::enableAccept ); connect( cmbEncoding, static_cast( &QComboBox::currentIndexChanged ), this, &QgsDelimitedTextSourceSelect::updateFieldsAndEnable ); @@ -93,6 +91,11 @@ QgsDelimitedTextSourceSelect::QgsDelimitedTextSourceSelect( QWidget *parent, Qt: connect( cbxPointIsComma, &QAbstractButton::toggled, this, &QgsDelimitedTextSourceSelect::updateFieldsAndEnable ); connect( cbxXyDms, &QAbstractButton::toggled, this, &QgsDelimitedTextSourceSelect::updateFieldsAndEnable ); + + mFileWidget->setDialogTitle( tr( "Choose a Delimited Text File to Ppen" ) ); + mFileWidget->setFilter( tr( "Text files" ) + " (*.txt *.csv *.dat *.wkt);;" + tr( "All files" ) + " (* *.*)" ); + mFileWidget->setSelectedFilter( settings.value( mPluginKey + "/file_filter", "" ).toString() ); + connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]() { updateFileName(); } ); } QgsDelimitedTextSourceSelect::~QgsDelimitedTextSourceSelect() @@ -102,11 +105,6 @@ QgsDelimitedTextSourceSelect::~QgsDelimitedTextSourceSelect() delete mFile; } -void QgsDelimitedTextSourceSelect::btnBrowseForFile_clicked() -{ - getOpenFileName(); -} - void QgsDelimitedTextSourceSelect::addButtonClicked() { // The following conditions should not be hit! OK will not be enabled... @@ -187,7 +185,7 @@ void QgsDelimitedTextSourceSelect::addButtonClicked() // store the settings saveSettings(); - saveSettingsForFile( txtFilePath->text() ); + saveSettingsForFile( mFileWidget->filePath() ); // add the layer to the map @@ -343,7 +341,7 @@ void QgsDelimitedTextSourceSelect::saveSettingsForFile( const QString &filename bool QgsDelimitedTextSourceSelect::loadDelimitedFileDefinition() { - mFile->setFileName( txtFilePath->text() ); + mFile->setFileName( mFileWidget->filePath() ); mFile->setEncoding( cmbEncoding->currentText() ); if ( delimiterChars->isChecked() ) { @@ -610,37 +608,20 @@ bool QgsDelimitedTextSourceSelect::trySetXYField( QStringList &fields, QList= 0; } -void QgsDelimitedTextSourceSelect::getOpenFileName() -{ - // Get a file to process, starting at the current directory - // Set initial dir to last used - QgsSettings settings; - QString selectedFilter = settings.value( mPluginKey + "/file_filter", "" ).toString(); - - QString s = QFileDialog::getOpenFileName( - this, - tr( "Choose a delimited text file to open" ), - settings.value( mPluginKey + "/text_path", QDir::homePath() ).toString(), - tr( "Text files" ) + " (*.txt *.csv *.dat *.wkt);;" - + tr( "All files" ) + " (* *.*)", - &selectedFilter - ); - // set path - if ( s.isNull() ) return; - settings.setValue( mPluginKey + "/file_filter", selectedFilter ); - txtFilePath->setText( s ); -} - void QgsDelimitedTextSourceSelect::updateFileName() { + QgsSettings settings; + settings.setValue( mPluginKey + "/file_filter", mFileWidget->selectedFilter() ); + // put a default layer name in the text entry - QString filename = txtFilePath->text(); + QString filename = mFileWidget->filePath(); QFileInfo finfo( filename ); if ( finfo.exists() ) { QgsSettings settings; settings.setValue( mPluginKey + "/text_path", finfo.path() ); } + txtLayerName->setText( finfo.completeBaseName() ); loadSettingsForFile( filename ); updateFieldsAndEnable(); @@ -659,13 +640,13 @@ bool QgsDelimitedTextSourceSelect::validate() QString message( QLatin1String( "" ) ); bool enabled = false; - if ( txtFilePath->text().trimmed().isEmpty() ) + if ( mFileWidget->filePath().trimmed().isEmpty() ) { message = tr( "Please select an input file" ); } - else if ( ! QFileInfo::exists( txtFilePath->text() ) ) + else if ( ! QFileInfo::exists( mFileWidget->filePath() ) ) { - message = tr( "File %1 does not exist" ).arg( txtFilePath->text() ); + message = tr( "File %1 does not exist" ).arg( mFileWidget->filePath() ); } else if ( txtLayerName->text().isEmpty() ) { diff --git a/src/providers/delimitedtext/qgsdelimitedtextsourceselect.h b/src/providers/delimitedtext/qgsdelimitedtextsourceselect.h index de8bb0ad2e4..f1007fc0164 100644 --- a/src/providers/delimitedtext/qgsdelimitedtextsourceselect.h +++ b/src/providers/delimitedtext/qgsdelimitedtextsourceselect.h @@ -41,7 +41,6 @@ class QgsDelimitedTextSourceSelect : public QgsAbstractDataSourceWidget, private private: bool loadDelimitedFileDefinition(); void updateFieldLists(); - void getOpenFileName(); QString selectedChars(); void setSelectedChars( const QString &delimiters ); void loadSettings( const QString &subkey = QString(), bool loadGeomSettings = true ); @@ -60,9 +59,6 @@ class QgsDelimitedTextSourceSelect : public QgsAbstractDataSourceWidget, private QButtonGroup *bgGeomType = nullptr; void showHelp(); - private slots: - void btnBrowseForFile_clicked(); - public slots: void addButtonClicked() override; void updateFileName(); diff --git a/src/ui/qgsdelimitedtextsourceselectbase.ui b/src/ui/qgsdelimitedtextsourceselectbase.ui index 81123b49058..d914f5d7468 100644 --- a/src/ui/qgsdelimitedtextsourceselectbase.ui +++ b/src/ui/qgsdelimitedtextsourceselectbase.ui @@ -79,42 +79,7 @@ - - - Full path to the delimited text file - - - Full path to the delimited text file. In order to properly parse the fields in the file, the delimiter must be defined prior to entering the file name. Use the Browse button to the right of this field to choose the input file. - - - false - - - - - - - true - - - - 16777215 - 16777215 - - - - Browse to find the delimited text file to be processed - - - Use this button to browse to the location of the delimited text file. This button will not be enabled until a delimiter has been entered in the <i>Delimiter</i> box. Once a file is chosen, the X and Y field drop-down boxes will be populated with the fields from the delimited text file. - - - Browse... - - - false - - + @@ -132,7 +97,7 @@ - Layer name + Layer Name @@ -1354,6 +1319,11 @@
qgscollapsiblegroupbox.h
1 + + QgsFileWidget + QWidget +
qgsfilewidget.h
+
txtFilePath