Skip to content

Commit 7e0c149

Browse files
authored
Merge pull request #5818 from nirvn/filewidget_fest
Use QgsFileWidget in several dialogs
2 parents bac80aa + de0ac14 commit 7e0c149

16 files changed

+240
-244
lines changed

python/gui/qgsfilewidget.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsFileWidget : QWidget
3131
GetFile,
3232
GetDirectory,
3333
GetMultipleFiles,
34+
SaveFile,
3435
};
3536

3637
enum RelativeStorage
@@ -97,6 +98,19 @@ returns the filters used for QDialog.getOpenFileName
9798
\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 ';;',
9899
%End
99100

101+
void setConfirmOverwrite( bool confirmOverwrite );
102+
%Docstring
103+
Sets whether a confirmation to overwrite an existing file will appear.
104+
By default, a confirmation will appear.
105+
\param confirmOverwrite If set to true, an overwrite confirmation will be shown
106+
%End
107+
108+
bool confirmOverwrite() const;
109+
%Docstring
110+
Returns whether a confirmation will be shown when overwriting an existing file
111+
:rtype: bool
112+
%End
113+
100114
bool fileWidgetButtonVisible() const;
101115
%Docstring
102116
determines if the tool button is shown

src/gui/ogr/qgsvectorlayersaveasdialog.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ void QgsVectorLayerSaveAsDialog::setup()
7373
{
7474
setupUi( this );
7575
connect( mFormatComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsVectorLayerSaveAsDialog::mFormatComboBox_currentIndexChanged );
76-
connect( leFilename, &QLineEdit::textChanged, this, &QgsVectorLayerSaveAsDialog::leFilename_textChanged );
77-
connect( browseFilename, &QPushButton::clicked, this, &QgsVectorLayerSaveAsDialog::browseFilename_clicked );
7876
connect( mCrsSelector, &QgsProjectionSelectionWidget::crsChanged, this, &QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged );
7977
connect( mSymbologyExportComboBox, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::currentIndexChanged ), this, &QgsVectorLayerSaveAsDialog::mSymbologyExportComboBox_currentIndexChanged );
8078
connect( mGeometryTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsVectorLayerSaveAsDialog::mGeometryTypeComboBox_currentIndexChanged );
@@ -139,6 +137,24 @@ void QgsVectorLayerSaveAsDialog::setup()
139137
mExtentGroupBox->setCheckable( true );
140138
mExtentGroupBox->setChecked( false );
141139
mExtentGroupBox->setCollapsed( true );
140+
141+
mFilename->setStorageMode( QgsFileWidget::SaveFile );
142+
mFilename->setDialogTitle( tr( "Select layer as..." ) );
143+
mFilename->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
144+
mFilename->setConfirmOverwrite( false );
145+
connect( mFilename, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
146+
{
147+
QgsSettings settings;
148+
QFileInfo tmplFileInfo( filePath );
149+
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
150+
if ( !filePath.isEmpty() && leLayername->isEnabled() )
151+
{
152+
QFileInfo fileInfo( filePath );
153+
leLayername->setText( fileInfo.baseName() );
154+
}
155+
buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
156+
!filePath.isEmpty() && QFileInfo( filePath ).absoluteDir().exists() );
157+
} );
142158
}
143159

144160
QList<QPair<QLabel *, QWidget *> > QgsVectorLayerSaveAsDialog::createControls( const QMap<QString, QgsVectorFileWriter::Option *> &options )
@@ -352,8 +368,9 @@ void QgsVectorLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( int idx )
352368
{
353369
Q_UNUSED( idx );
354370

355-
browseFilename->setEnabled( true );
356-
leFilename->setEnabled( true );
371+
mFilename->setEnabled( true );
372+
mFilename->setFilter( QgsVectorFileWriter::filterForDriver( format() ) );
373+
357374
bool selectAllFields = true;
358375
bool fieldsAsDisplayedValues = false;
359376

@@ -379,9 +396,9 @@ void QgsVectorLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( int idx )
379396
if ( !leLayername->isEnabled() )
380397
leLayername->setText( QString() );
381398
else if ( leLayername->text().isEmpty() &&
382-
!leFilename->text().isEmpty() )
399+
!mFilename->filePath().isEmpty() )
383400
{
384-
QString layerName = QFileInfo( leFilename->text() ).baseName();
401+
QString layerName = QFileInfo( mFilename->filePath() ).baseName();
385402
leLayername->setText( layerName );
386403
}
387404

@@ -626,30 +643,6 @@ void QgsVectorLayerSaveAsDialog::mAttributeTable_itemChanged( QTableWidgetItem *
626643
mReplaceRawFieldValuesStateChangedSlotEnabled = true;
627644
}
628645

629-
void QgsVectorLayerSaveAsDialog::leFilename_textChanged( const QString &text )
630-
{
631-
buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
632-
!text.isEmpty() && QFileInfo( text ).absoluteDir().exists() );
633-
634-
if ( leLayername->isEnabled() )
635-
{
636-
QString layerName = QFileInfo( text ).baseName();
637-
leLayername->setText( layerName );
638-
}
639-
}
640-
641-
void QgsVectorLayerSaveAsDialog::browseFilename_clicked()
642-
{
643-
QgsSettings settings;
644-
QString dirName = leFilename->text().isEmpty() ? settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() : leFilename->text();
645-
QString filterString = QgsVectorFileWriter::filterForDriver( format() );
646-
QString outputFile = QFileDialog::getSaveFileName( nullptr, tr( "Save layer as..." ), dirName, filterString, nullptr, QFileDialog::DontConfirmOverwrite );
647-
if ( !outputFile.isNull() )
648-
{
649-
leFilename->setText( outputFile );
650-
}
651-
}
652-
653646
void QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged( const QgsCoordinateReferenceSystem &crs )
654647
{
655648
mCRS = crs.srsid();
@@ -658,7 +651,7 @@ void QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged( const QgsCoordinateRef
658651

659652
QString QgsVectorLayerSaveAsDialog::filename() const
660653
{
661-
return leFilename->text();
654+
return mFilename->filePath();
662655
}
663656

664657
QString QgsVectorLayerSaveAsDialog::layername() const

src/gui/ogr/qgsvectorlayersaveasdialog.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ class GUI_EXPORT QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVec
126126
private slots:
127127

128128
void mFormatComboBox_currentIndexChanged( int idx );
129-
void leFilename_textChanged( const QString &text );
130-
void browseFilename_clicked();
131129
void mCrsSelector_crsChanged( const QgsCoordinateReferenceSystem &crs );
132130
void showHelp();
133131
void mSymbologyExportComboBox_currentIndexChanged( const QString &text );

src/gui/qgsfilewidget.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ void QgsFileWidget::openFileDialog()
262262
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" );
263263
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
264264
break;
265+
case SaveFile:
266+
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Create or select a file" );
267+
if ( !confirmOverwrite() )
268+
{
269+
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, nullptr, QFileDialog::DontConfirmOverwrite );
270+
}
271+
else
272+
{
273+
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter );
274+
}
275+
break;
265276
}
266277

267278
if ( fileName.isEmpty() && fileNames.isEmpty( ) )
@@ -283,6 +294,7 @@ void QgsFileWidget::openFileDialog()
283294
switch ( mStorageMode )
284295
{
285296
case GetFile:
297+
case SaveFile:
286298
settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), QFileInfo( fileName ).absolutePath() );
287299
break;
288300
case GetDirectory:

src/gui/qgsfilewidget.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
6565
GetFile, //! Select a single file
6666
GetDirectory, //! Select a directory
6767
GetMultipleFiles, //! Select multiple files
68+
SaveFile, //! Select a single new or pre-existing file
6869
};
6970

7071
/**
@@ -120,6 +121,18 @@ class GUI_EXPORT QgsFileWidget : public QWidget
120121
*/
121122
void setFilter( const QString &filter );
122123

124+
/**
125+
* Sets whether a confirmation to overwrite an existing file will appear.
126+
* By default, a confirmation will appear.
127+
* \param confirmOverwrite If set to true, an overwrite confirmation will be shown
128+
*/
129+
void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }
130+
131+
/**
132+
* Returns whether a confirmation will be shown when overwriting an existing file
133+
*/
134+
bool confirmOverwrite() const { return mConfirmOverwrite; }
135+
123136
//! determines if the tool button is shown
124137
bool fileWidgetButtonVisible() const;
125138
//! determines if the tool button is shown
@@ -173,6 +186,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
173186
QString mDialogTitle;
174187
QString mFilter;
175188
QString mDefaultRoot;
189+
bool mConfirmOverwrite = true;
176190
StorageMode mStorageMode = GetFile;
177191
RelativeStorage mRelativeStorage = Absolute;
178192

src/gui/qgsnewgeopackagelayerdialog.cpp

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
5151
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked );
5252
connect( mFieldTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged );
5353
connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged );
54-
connect( mSelectDatabaseButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mSelectDatabaseButton_clicked );
55-
connect( mDatabaseEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::mDatabaseEdit_textChanged );
5654
connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged );
5755
connect( mTableNameEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited );
5856
connect( mLayerIdentifierEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited );
@@ -102,12 +100,29 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
102100
connect( mFieldNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::fieldNameChanged );
103101
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewGeoPackageLayerDialog::selectionChanged );
104102
connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::checkOk );
105-
connect( mDatabaseEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::checkOk );
106103

107104
mAddAttributeButton->setEnabled( false );
108105
mRemoveAttributeButton->setEnabled( false );
109106

110107
mCheckBoxCreateSpatialIndex->setChecked( true );
108+
109+
mDatabase->setStorageMode( QgsFileWidget::SaveFile );
110+
mDatabase->setFilter( tr( "GeoPackage" ) + " (*.gpkg)" );
111+
mDatabase->setDialogTitle( tr( "Select Existing or Create a New GeoPackage Database File..." ) );
112+
mDatabase->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
113+
mDatabase->setConfirmOverwrite( false );
114+
connect( mDatabase, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
115+
{
116+
QgsSettings settings;
117+
QFileInfo tmplFileInfo( filePath );
118+
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
119+
if ( !filePath.isEmpty() && !mTableNameEdited )
120+
{
121+
QFileInfo fileInfo( filePath );
122+
mTableNameEdit->setText( fileInfo.baseName() );
123+
}
124+
checkOk();
125+
} );
111126
}
112127

113128
QgsNewGeoPackageLayerDialog::~QgsNewGeoPackageLayerDialog()
@@ -123,10 +138,7 @@ void QgsNewGeoPackageLayerDialog::setCrs( const QgsCoordinateReferenceSystem &cr
123138

124139
void QgsNewGeoPackageLayerDialog::lockDatabasePath()
125140
{
126-
mDatabaseEdit->setReadOnly( true );
127-
mSelectDatabaseButton->hide();
128-
mSelectDatabaseButton->deleteLater();
129-
mSelectDatabaseButton = nullptr;
141+
mDatabase->setReadOnly( true );
130142
}
131143

132144
void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged( int )
@@ -150,34 +162,6 @@ void QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged( int )
150162
mCrsSelector->setEnabled( isSpatial );
151163
}
152164

153-
void QgsNewGeoPackageLayerDialog::mSelectDatabaseButton_clicked()
154-
{
155-
QString fileName = QFileDialog::getSaveFileName( this, tr( "Select existing or create new GeoPackage Database File" ),
156-
QDir::homePath(),
157-
tr( "GeoPackage" ) + " (*.gpkg)",
158-
nullptr,
159-
QFileDialog::DontConfirmOverwrite );
160-
161-
if ( fileName.isEmpty() )
162-
return;
163-
164-
if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
165-
{
166-
fileName += QLatin1String( ".gpkg" );
167-
}
168-
169-
mDatabaseEdit->setText( fileName );
170-
}
171-
172-
void QgsNewGeoPackageLayerDialog::mDatabaseEdit_textChanged( const QString &text )
173-
{
174-
if ( !text.isEmpty() && !mTableNameEdited )
175-
{
176-
QFileInfo fileInfo( text );
177-
mTableNameEdit->setText( fileInfo.baseName() );
178-
}
179-
}
180-
181165
void QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged( const QString &text )
182166
{
183167
mTableNameEdited = !text.isEmpty();
@@ -201,7 +185,7 @@ void QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited( const QString
201185

202186
void QgsNewGeoPackageLayerDialog::checkOk()
203187
{
204-
bool ok = !mDatabaseEdit->text().isEmpty() &&
188+
bool ok = !mDatabase->filePath().isEmpty() &&
205189
!mTableNameEdit->text().isEmpty();
206190
mOkButton->setEnabled( ok );
207191
}
@@ -258,7 +242,10 @@ void QgsNewGeoPackageLayerDialog::buttonBox_rejected()
258242

259243
bool QgsNewGeoPackageLayerDialog::apply()
260244
{
261-
QString fileName( mDatabaseEdit->text() );
245+
QString fileName( mDatabase->filePath() );
246+
if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
247+
fileName += QLatin1String( ".gpkg" );
248+
262249
bool createNewDb = false;
263250

264251
if ( QFile( fileName ).exists( fileName ) )
@@ -490,7 +477,7 @@ bool QgsNewGeoPackageLayerDialog::apply()
490477
}
491478
hDS.reset();
492479

493-
QString uri( QStringLiteral( "%1|layername=%2" ).arg( mDatabaseEdit->text(), tableName ) );
480+
QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
494481
QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
495482
QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
496483
if ( layer->isValid() )

src/gui/qgsnewgeopackagelayerdialog.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
5454
* Returns the database path
5555
* \since QGIS 3.0
5656
*/
57-
QString databasePath() const { return mDatabaseEdit->text(); }
57+
QString databasePath() const { return mDatabase->filePath(); }
5858

5959
/**
6060
* Sets the initial database \a path
6161
* \since QGIS 3.0
6262
*/
63-
void setDatabasePath( const QString &path ) { mDatabaseEdit->setText( path ); }
63+
void setDatabasePath( const QString &path ) { mDatabase->setFilePath( path ); }
6464

6565
/**
6666
* Sets the database path widgets to a locked and read-only mode.
@@ -82,8 +82,6 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
8282
void mRemoveAttributeButton_clicked();
8383
void mFieldTypeBox_currentIndexChanged( int index );
8484
void mGeometryTypeBox_currentIndexChanged( int index );
85-
void mSelectDatabaseButton_clicked();
86-
void mDatabaseEdit_textChanged( const QString &text );
8785
void mTableNameEdit_textChanged( const QString &text );
8886
void mTableNameEdit_textEdited( const QString &text );
8987
void mLayerIdentifierEdit_textEdited( const QString &text );

0 commit comments

Comments
 (0)