Skip to content

Commit

Permalink
Tweak behavior of new shapefile/gpkg browser actions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 12, 2018
1 parent d062274 commit 17ced91
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion python/gui/auto_generated/qgsdataitemguiprovider.sip.in
Expand Up @@ -28,7 +28,7 @@ Encapsulates the context in which a QgsDataItem is shown within the application
Constructor for QgsDataItemGuiContext.
%End

QgsMessageBar *messageBar();
QgsMessageBar *messageBar() const;
%Docstring
Returns the associated message bar.

Expand Down
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsnewgeopackagelayerdialog.sip.in
Expand Up @@ -67,6 +67,14 @@ 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

void setAddToProject( bool addToProject );
%Docstring
Sets whether a newly created layer should automatically be added to the current project.
Defaults to true.

.. versionadded:: 3.6
%End

};
Expand Down
15 changes: 14 additions & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -22,13 +22,15 @@
#include "qgsgui.h"
#include "qgsnative.h"
#include "qgisapp.h"
#include "qgsmessagebar.h"
#include "qgsnewnamedialog.h"
#include "qgsbrowsermodel.h"
#include "qgsbrowserdockwidget_p.h"
#include "qgswindowmanagerinterface.h"
#include "qgsrasterlayer.h"
#include "qgsnewvectorlayerdialog.h"
#include "qgsnewgeopackagelayerdialog.h"
#include "qgsfileutils.h"
#include <QMenu>
#include <QInputDialog>
#include <QMessageBox>
Expand All @@ -40,7 +42,7 @@ QString QgsAppDirectoryItemGuiProvider::name()
return QStringLiteral( "directory_items" );
}

void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext context )
{
if ( item->type() != QgsDataItem::Directory )
return;
Expand Down Expand Up @@ -84,8 +86,15 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
QDir dir( directoryItem->dirPath() );
dialog.setDatabasePath( dir.filePath( QStringLiteral( "new_geopackage" ) ) );
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
dialog.setAddToProject( false );
if ( dialog.exec() )
{
QString file = dialog.databasePath();
file = QgsFileUtils::ensureFileNameHasExtension( file, QStringList() << QStringLiteral( "gpkg" ) );
context.messageBar()->pushSuccess( tr( "New GeoPackage" ), tr( "Created <a href=\"%1\">%2</a>" ).arg(
QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
item->refresh();
}
} );
newMenu->addAction( createGpkg );

Expand All @@ -97,7 +106,11 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
QDir dir( directoryItem->dirPath() );
const QString newFile = QgsNewVectorLayerDialog::runAndCreateLayer( QgisApp::instance(), &enc, QgsProject::instance()->defaultCrsForNewLayers(), dir.filePath( QStringLiteral( "new_layer.shp" ) ) );
if ( !newFile.isEmpty() )
{
context.messageBar()->pushSuccess( tr( "New ShapeFile" ), tr( "Created <a href=\"%1\">%2</a>" ).arg(
QUrl::fromLocalFile( newFile ).toString(), QDir::toNativeSeparators( newFile ) ) );
item->refresh();
}
} );
newMenu->addAction( createShp );

Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsdataitemguiprovider.cpp
Expand Up @@ -19,7 +19,7 @@
// QgsDataItemGuiContext
//

QgsMessageBar *QgsDataItemGuiContext::messageBar()
QgsMessageBar *QgsDataItemGuiContext::messageBar() const
{
return mMessageBar;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsdataitemguiprovider.h
Expand Up @@ -48,7 +48,7 @@ class GUI_EXPORT QgsDataItemGuiContext
*
* \see setMessageBar()
*/
QgsMessageBar *messageBar();
QgsMessageBar *messageBar() const;

/**
* Sets the associated message \a bar.
Expand Down
27 changes: 19 additions & 8 deletions src/gui/qgsnewgeopackagelayerdialog.cpp
Expand Up @@ -483,22 +483,28 @@ bool QgsNewGeoPackageLayerDialog::apply()

QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
if ( layer->isValid() )
{
// register this layer with the central layers registry
QList<QgsMapLayer *> myList;
myList << layer;
//addMapLayers returns a list of all successfully added layers
//so we compare that to our original list.
if ( myList == QgsProject::instance()->addMapLayers( myList ) )
if ( mAddToProject )
{
// register this layer with the central layers registry
QList<QgsMapLayer *> myList;
myList << layer.release();
//addMapLayers returns a list of all successfully added layers
//so we compare that to our original list.
if ( myList == QgsProject::instance()->addMapLayers( myList ) )
return true;
}
else
{
return true;
}
}
else
{
if ( !property( "hideDialogs" ).toBool() )
QMessageBox::critical( this, tr( "New GeoPackage Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
delete layer;
}

return false;
Expand All @@ -509,6 +515,11 @@ void QgsNewGeoPackageLayerDialog::setOverwriteBehavior( OverwriteBehavior behavi
mBehavior = behavior;
}

void QgsNewGeoPackageLayerDialog::setAddToProject( bool addToProject )
{
mAddToProject = addToProject;
}

void QgsNewGeoPackageLayerDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgsnewgeopackagelayerdialog.h
Expand Up @@ -76,6 +76,14 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
*/
void setOverwriteBehavior( OverwriteBehavior behavior );

/**
* Sets whether a newly created layer should automatically be added to the current project.
* Defaults to true.
*
* \since QGIS 3.6
*/
void setAddToProject( bool addToProject );

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

#endif // QGSNEWVECTORLAYERDIALOG_H

0 comments on commit 17ced91

Please sign in to comment.