Skip to content

Commit

Permalink
Use qgsfilewidget for the delimited text prodivder source select (#5920)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 20, 2017
1 parent 8a5d533 commit bf85709
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 80 deletions.
10 changes: 10 additions & 0 deletions python/gui/qgsfilewidget.sip
Expand Up @@ -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 );
Expand Down
11 changes: 5 additions & 6 deletions src/gui/qgsfilewidget.cpp
Expand Up @@ -253,32 +253,31 @@ 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" );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
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;
}
Expand Down
11 changes: 11 additions & 0 deletions src/gui/qgsfilewidget.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
47 changes: 14 additions & 33 deletions src/providers/delimitedtext/qgsdelimitedtextsourceselect.cpp
Expand Up @@ -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 );

Expand All @@ -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<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsDelimitedTextSourceSelect::updateFieldsAndEnable );

Expand All @@ -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()
Expand All @@ -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...
Expand Down Expand Up @@ -187,7 +185,7 @@ void QgsDelimitedTextSourceSelect::addButtonClicked()

// store the settings
saveSettings();
saveSettingsForFile( txtFilePath->text() );
saveSettingsForFile( mFileWidget->filePath() );


// add the layer to the map
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -610,37 +608,20 @@ bool QgsDelimitedTextSourceSelect::trySetXYField( QStringList &fields, QList<boo
return indexY >= 0;
}

void QgsDelimitedTextSourceSelect::getOpenFileName()
void QgsDelimitedTextSourceSelect::updateFileName()
{
// 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 );
}
settings.setValue( mPluginKey + "/file_filter", mFileWidget->selectedFilter() );

void QgsDelimitedTextSourceSelect::updateFileName()
{
// 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();
Expand All @@ -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() )
{
Expand Down
4 changes: 0 additions & 4 deletions src/providers/delimitedtext/qgsdelimitedtextsourceselect.h
Expand Up @@ -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 );
Expand All @@ -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();
Expand Down
44 changes: 7 additions & 37 deletions src/ui/qgsdelimitedtextsourceselectbase.ui
Expand Up @@ -79,42 +79,7 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="txtFilePath">
<property name="toolTip">
<string>Full path to the delimited text file</string>
</property>
<property name="whatsThis">
<string>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.</string>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBrowseForFile">
<property name="enabled">
<bool>true</bool>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Browse to find the delimited text file to be processed</string>
</property>
<property name="whatsThis">
<string>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 &lt;i&gt;Delimiter&lt;/i&gt; 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.</string>
</property>
<property name="text">
<string>Browse...</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
<widget class="QgsFileWidget" name="mFileWidget" native="true"/>
</item>
</layout>
</item>
Expand All @@ -132,7 +97,7 @@
</sizepolicy>
</property>
<property name="text">
<string>Layer name</string>
<string>Layer Name</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -1354,6 +1319,11 @@
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>txtFilePath</tabstop>
Expand Down

0 comments on commit bf85709

Please sign in to comment.