Skip to content
Permalink
Browse files

Nicer UX when adding a layer to an existing gpkg through the browser

  • Loading branch information
nyalldawson committed Nov 15, 2017
1 parent ef7cc49 commit 135b5c6c258d5403bf5833f84ab3f12083b34e14
@@ -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
@@ -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

@@ -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();
@@ -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();
@@ -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" ) );
@@ -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();
@@ -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();
@@ -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
@@ -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();

0 comments on commit 135b5c6

Please sign in to comment.
You can’t perform that action at this time.