Skip to content

Commit 135b5c6

Browse files
committedNov 15, 2017
Nicer UX when adding a layer to an existing gpkg through the browser
1 parent ef7cc49 commit 135b5c6

File tree

4 files changed

+107
-26
lines changed

4 files changed

+107
-26
lines changed
 

‎python/gui/qgsnewgeopackagelayerdialog.sip

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class QgsNewGeoPackageLayerDialog: QDialog
1919
#include "qgsnewgeopackagelayerdialog.h"
2020
%End
2121
public:
22+
23+
enum OverwriteBehavior
24+
{
25+
Prompt,
26+
Overwrite,
27+
AddNewLayer,
28+
};
29+
2230
QgsNewGeoPackageLayerDialog( QWidget *parent /TransferThis/ = 0, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
2331
%Docstring
2432
Constructor
@@ -40,7 +48,22 @@ Constructor
4048

4149
void setDatabasePath( const QString &path );
4250
%Docstring
43-
Sets the the database ``path``
51+
Sets the initial database ``path``
52+
.. versionadded:: 3.0
53+
%End
54+
55+
void lockDatabasePath();
56+
%Docstring
57+
Sets the database path widgets to a locked and read-only mode.
58+
.. versionadded:: 3.0
59+
%End
60+
61+
void setOverwriteBehavior( OverwriteBehavior behavior );
62+
%Docstring
63+
Sets the ``behavior`` to use when a path to an existing geopackage file is used.
64+
65+
The default behavior is to prompt the user for an action to take.
66+
4467
.. versionadded:: 3.0
4568
%End
4669

‎src/gui/qgsnewgeopackagelayerdialog.cpp

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ void QgsNewGeoPackageLayerDialog::setCrs( const QgsCoordinateReferenceSystem &cr
119119
mCrsSelector->setCrs( crs );
120120
}
121121

122+
void QgsNewGeoPackageLayerDialog::lockDatabasePath()
123+
{
124+
mDatabaseEdit->setReadOnly( true );
125+
mSelectDatabaseButton->hide();
126+
mSelectDatabaseButton->deleteLater();
127+
mSelectDatabaseButton = nullptr;
128+
}
129+
122130
void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged( int )
123131
{
124132
QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
@@ -248,36 +256,54 @@ bool QgsNewGeoPackageLayerDialog::apply()
248256
{
249257
QString fileName( mDatabaseEdit->text() );
250258
bool createNewDb = false;
259+
251260
if ( QFile( fileName ).exists( fileName ) )
252261
{
253-
QMessageBox msgBox;
254-
msgBox.setIcon( QMessageBox::Question );
255-
msgBox.setWindowTitle( tr( "The File Already Exists." ) );
256-
msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
257-
QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
258-
QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
259-
msgBox.setStandardButtons( QMessageBox::Cancel );
260-
msgBox.setDefaultButton( addNewLayerButton );
261262
bool overwrite = false;
262-
bool cancel = false;
263-
if ( property( "hideDialogs" ).toBool() )
264-
{
265-
overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
266-
if ( !overwrite )
267-
cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
268-
}
269-
else
263+
264+
switch ( mBehavior )
270265
{
271-
int ret = msgBox.exec();
272-
if ( ret == QMessageBox::Cancel )
273-
cancel = true;
274-
if ( msgBox.clickedButton() == overwriteButton )
266+
case Prompt:
267+
{
268+
QMessageBox msgBox;
269+
msgBox.setIcon( QMessageBox::Question );
270+
msgBox.setWindowTitle( tr( "The File Already Exists." ) );
271+
msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
272+
QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
273+
QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
274+
msgBox.setStandardButtons( QMessageBox::Cancel );
275+
msgBox.setDefaultButton( addNewLayerButton );
276+
bool cancel = false;
277+
if ( property( "hideDialogs" ).toBool() )
278+
{
279+
overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
280+
if ( !overwrite )
281+
cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
282+
}
283+
else
284+
{
285+
int ret = msgBox.exec();
286+
if ( ret == QMessageBox::Cancel )
287+
cancel = true;
288+
if ( msgBox.clickedButton() == overwriteButton )
289+
overwrite = true;
290+
}
291+
if ( cancel )
292+
{
293+
return false;
294+
}
295+
break;
296+
}
297+
298+
case Overwrite:
275299
overwrite = true;
300+
break;
301+
302+
case AddNewLayer:
303+
overwrite = false;
304+
break;
276305
}
277-
if ( cancel )
278-
{
279-
return false;
280-
}
306+
281307
if ( overwrite )
282308
{
283309
QFile( fileName ).remove();
@@ -483,6 +509,11 @@ bool QgsNewGeoPackageLayerDialog::apply()
483509
return false;
484510
}
485511

512+
void QgsNewGeoPackageLayerDialog::setOverwriteBehavior( OverwriteBehavior behavior )
513+
{
514+
mBehavior = behavior;
515+
}
516+
486517
void QgsNewGeoPackageLayerDialog::showHelp()
487518
{
488519
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );

‎src/gui/qgsnewgeopackagelayerdialog.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
3131
Q_OBJECT
3232

3333
public:
34+
35+
//! Behavior to use when an existing geopackage already exists
36+
enum OverwriteBehavior
37+
{
38+
Prompt, //!< Prompt user for action
39+
Overwrite, //!< Overwrite whole geopackage
40+
AddNewLayer, //!< Keep existing contents and add new layer
41+
};
42+
3443
//! Constructor
3544
QgsNewGeoPackageLayerDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
3645
~QgsNewGeoPackageLayerDialog();
@@ -48,11 +57,26 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
4857
QString databasePath() const { return mDatabaseEdit->text(); }
4958

5059
/**
51-
* Sets the the database \a path
60+
* Sets the initial database \a path
5261
* \since QGIS 3.0
5362
*/
5463
void setDatabasePath( const QString &path ) { mDatabaseEdit->setText( path ); }
5564

65+
/**
66+
* Sets the database path widgets to a locked and read-only mode.
67+
* \since QGIS 3.0
68+
*/
69+
void lockDatabasePath();
70+
71+
/**
72+
* Sets the \a behavior to use when a path to an existing geopackage file is used.
73+
*
74+
* The default behavior is to prompt the user for an action to take.
75+
*
76+
* \since QGIS 3.0
77+
*/
78+
void setOverwriteBehavior( OverwriteBehavior behavior );
79+
5680
private slots:
5781
void mAddAttributeButton_clicked();
5882
void mRemoveAttributeButton_clicked();
@@ -78,6 +102,7 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
78102
QString mCrsId;
79103
bool mTableNameEdited = false;
80104
bool mLayerIdentifierEdited = false;
105+
OverwriteBehavior mBehavior = Prompt;
81106
};
82107

83108
#endif // QGSNEWVECTORLAYERDIALOG_H

‎src/providers/ogr/qgsgeopackagedataitems.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ void QgsGeoPackageCollectionItem::addTable()
507507
QgsNewGeoPackageLayerDialog dialog( nullptr );
508508
dialog.setDatabasePath( mPath );
509509
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
510+
dialog.setOverwriteBehavior( QgsNewGeoPackageLayerDialog::AddNewLayer );
511+
dialog.lockDatabasePath();
510512
if ( dialog.exec() == QDialog::Accepted )
511513
{
512514
refreshConnections();

0 commit comments

Comments
 (0)
Please sign in to comment.