Skip to content

Commit

Permalink
Nicer UX when adding a layer to an existing gpkg through the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 15, 2017
1 parent ef7cc49 commit 135b5c6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
25 changes: 24 additions & 1 deletion python/gui/qgsnewgeopackagelayerdialog.sip
Expand Up @@ -19,6 +19,14 @@ class QgsNewGeoPackageLayerDialog: QDialog
#include "qgsnewgeopackagelayerdialog.h"
%End
public:

enum OverwriteBehavior
{
Prompt,
Overwrite,
AddNewLayer,
};

QgsNewGeoPackageLayerDialog( QWidget *parent /TransferThis/ = 0, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
%Docstring
Constructor
Expand All @@ -40,7 +48,22 @@ Constructor

void setDatabasePath( const QString &path );
%Docstring
Sets the the database ``path``
Sets the initial database ``path``
.. versionadded:: 3.0
%End

void lockDatabasePath();
%Docstring
Sets the database path widgets to a locked and read-only mode.
.. versionadded:: 3.0
%End

void setOverwriteBehavior( OverwriteBehavior behavior );
%Docstring
Sets the ``behavior`` to use when a path to an existing geopackage file is used.

The default behavior is to prompt the user for an action to take.

.. versionadded:: 3.0
%End

Expand Down
79 changes: 55 additions & 24 deletions src/gui/qgsnewgeopackagelayerdialog.cpp
Expand Up @@ -119,6 +119,14 @@ void QgsNewGeoPackageLayerDialog::setCrs( const QgsCoordinateReferenceSystem &cr
mCrsSelector->setCrs( crs );
}

void QgsNewGeoPackageLayerDialog::lockDatabasePath()
{
mDatabaseEdit->setReadOnly( true );
mSelectDatabaseButton->hide();
mSelectDatabaseButton->deleteLater();
mSelectDatabaseButton = nullptr;
}

void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged( int )
{
QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
Expand Down Expand Up @@ -248,36 +256,54 @@ bool QgsNewGeoPackageLayerDialog::apply()
{
QString fileName( mDatabaseEdit->text() );
bool createNewDb = false;

if ( QFile( fileName ).exists( fileName ) )
{
QMessageBox msgBox;
msgBox.setIcon( QMessageBox::Question );
msgBox.setWindowTitle( tr( "The File Already Exists." ) );
msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
msgBox.setStandardButtons( QMessageBox::Cancel );
msgBox.setDefaultButton( addNewLayerButton );
bool overwrite = false;
bool cancel = false;
if ( property( "hideDialogs" ).toBool() )
{
overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
if ( !overwrite )
cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
}
else

switch ( mBehavior )
{
int ret = msgBox.exec();
if ( ret == QMessageBox::Cancel )
cancel = true;
if ( msgBox.clickedButton() == overwriteButton )
case Prompt:
{
QMessageBox msgBox;
msgBox.setIcon( QMessageBox::Question );
msgBox.setWindowTitle( tr( "The File Already Exists." ) );
msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
msgBox.setStandardButtons( QMessageBox::Cancel );
msgBox.setDefaultButton( addNewLayerButton );
bool cancel = false;
if ( property( "hideDialogs" ).toBool() )
{
overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
if ( !overwrite )
cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
}
else
{
int ret = msgBox.exec();
if ( ret == QMessageBox::Cancel )
cancel = true;
if ( msgBox.clickedButton() == overwriteButton )
overwrite = true;
}
if ( cancel )
{
return false;
}
break;
}

case Overwrite:
overwrite = true;
break;

case AddNewLayer:
overwrite = false;
break;
}
if ( cancel )
{
return false;
}

if ( overwrite )
{
QFile( fileName ).remove();
Expand Down Expand Up @@ -483,6 +509,11 @@ bool QgsNewGeoPackageLayerDialog::apply()
return false;
}

void QgsNewGeoPackageLayerDialog::setOverwriteBehavior( OverwriteBehavior behavior )
{
mBehavior = behavior;
}

void QgsNewGeoPackageLayerDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );
Expand Down
27 changes: 26 additions & 1 deletion src/gui/qgsnewgeopackagelayerdialog.h
Expand Up @@ -31,6 +31,15 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
Q_OBJECT

public:

//! Behavior to use when an existing geopackage already exists
enum OverwriteBehavior
{
Prompt, //!< Prompt user for action
Overwrite, //!< Overwrite whole geopackage
AddNewLayer, //!< Keep existing contents and add new layer
};

//! Constructor
QgsNewGeoPackageLayerDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
~QgsNewGeoPackageLayerDialog();
Expand All @@ -48,11 +57,26 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
QString databasePath() const { return mDatabaseEdit->text(); }

/**
* Sets the the database \a path
* Sets the initial database \a path
* \since QGIS 3.0
*/
void setDatabasePath( const QString &path ) { mDatabaseEdit->setText( path ); }

/**
* Sets the database path widgets to a locked and read-only mode.
* \since QGIS 3.0
*/
void lockDatabasePath();

/**
* Sets the \a behavior to use when a path to an existing geopackage file is used.
*
* The default behavior is to prompt the user for an action to take.
*
* \since QGIS 3.0
*/
void setOverwriteBehavior( OverwriteBehavior behavior );

private slots:
void mAddAttributeButton_clicked();
void mRemoveAttributeButton_clicked();
Expand All @@ -78,6 +102,7 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
QString mCrsId;
bool mTableNameEdited = false;
bool mLayerIdentifierEdited = false;
OverwriteBehavior mBehavior = Prompt;
};

#endif // QGSNEWVECTORLAYERDIALOG_H
2 changes: 2 additions & 0 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -507,6 +507,8 @@ void QgsGeoPackageCollectionItem::addTable()
QgsNewGeoPackageLayerDialog dialog( nullptr );
dialog.setDatabasePath( mPath );
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
dialog.setOverwriteBehavior( QgsNewGeoPackageLayerDialog::AddNewLayer );
dialog.lockDatabasePath();
if ( dialog.exec() == QDialog::Accepted )
{
refreshConnections();
Expand Down

0 comments on commit 135b5c6

Please sign in to comment.