Skip to content

Commit 17ced91

Browse files
committed
Tweak behavior of new shapefile/gpkg browser actions
1 parent d062274 commit 17ced91

7 files changed

+53
-12
lines changed

python/gui/auto_generated/qgsdataitemguiprovider.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Encapsulates the context in which a QgsDataItem is shown within the application
2828
Constructor for QgsDataItemGuiContext.
2929
%End
3030

31-
QgsMessageBar *messageBar();
31+
QgsMessageBar *messageBar() const;
3232
%Docstring
3333
Returns the associated message bar.
3434

python/gui/auto_generated/qgsnewgeopackagelayerdialog.sip.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ Sets the ``behavior`` to use when a path to an existing geopackage file is used.
6767
The default behavior is to prompt the user for an action to take.
6868

6969
.. versionadded:: 3.0
70+
%End
71+
72+
void setAddToProject( bool addToProject );
73+
%Docstring
74+
Sets whether a newly created layer should automatically be added to the current project.
75+
Defaults to true.
76+
77+
.. versionadded:: 3.6
7078
%End
7179

7280
};

src/app/browser/qgsinbuiltdataitemproviders.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
#include "qgsgui.h"
2323
#include "qgsnative.h"
2424
#include "qgisapp.h"
25+
#include "qgsmessagebar.h"
2526
#include "qgsnewnamedialog.h"
2627
#include "qgsbrowsermodel.h"
2728
#include "qgsbrowserdockwidget_p.h"
2829
#include "qgswindowmanagerinterface.h"
2930
#include "qgsrasterlayer.h"
3031
#include "qgsnewvectorlayerdialog.h"
3132
#include "qgsnewgeopackagelayerdialog.h"
33+
#include "qgsfileutils.h"
3234
#include <QMenu>
3335
#include <QInputDialog>
3436
#include <QMessageBox>
@@ -40,7 +42,7 @@ QString QgsAppDirectoryItemGuiProvider::name()
4042
return QStringLiteral( "directory_items" );
4143
}
4244

43-
void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
45+
void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext context )
4446
{
4547
if ( item->type() != QgsDataItem::Directory )
4648
return;
@@ -84,8 +86,15 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
8486
QDir dir( directoryItem->dirPath() );
8587
dialog.setDatabasePath( dir.filePath( QStringLiteral( "new_geopackage" ) ) );
8688
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
89+
dialog.setAddToProject( false );
8790
if ( dialog.exec() )
91+
{
92+
QString file = dialog.databasePath();
93+
file = QgsFileUtils::ensureFileNameHasExtension( file, QStringList() << QStringLiteral( "gpkg" ) );
94+
context.messageBar()->pushSuccess( tr( "New GeoPackage" ), tr( "Created <a href=\"%1\">%2</a>" ).arg(
95+
QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
8896
item->refresh();
97+
}
8998
} );
9099
newMenu->addAction( createGpkg );
91100

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

src/gui/qgsdataitemguiprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// QgsDataItemGuiContext
2020
//
2121

22-
QgsMessageBar *QgsDataItemGuiContext::messageBar()
22+
QgsMessageBar *QgsDataItemGuiContext::messageBar() const
2323
{
2424
return mMessageBar;
2525
}

src/gui/qgsdataitemguiprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class GUI_EXPORT QgsDataItemGuiContext
4848
*
4949
* \see setMessageBar()
5050
*/
51-
QgsMessageBar *messageBar();
51+
QgsMessageBar *messageBar() const;
5252

5353
/**
5454
* Sets the associated message \a bar.

src/gui/qgsnewgeopackagelayerdialog.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,22 +483,28 @@ bool QgsNewGeoPackageLayerDialog::apply()
483483

484484
QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
485485
QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
486-
QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
486+
std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
487487
if ( layer->isValid() )
488488
{
489-
// register this layer with the central layers registry
490-
QList<QgsMapLayer *> myList;
491-
myList << layer;
492-
//addMapLayers returns a list of all successfully added layers
493-
//so we compare that to our original list.
494-
if ( myList == QgsProject::instance()->addMapLayers( myList ) )
489+
if ( mAddToProject )
490+
{
491+
// register this layer with the central layers registry
492+
QList<QgsMapLayer *> myList;
493+
myList << layer.release();
494+
//addMapLayers returns a list of all successfully added layers
495+
//so we compare that to our original list.
496+
if ( myList == QgsProject::instance()->addMapLayers( myList ) )
497+
return true;
498+
}
499+
else
500+
{
495501
return true;
502+
}
496503
}
497504
else
498505
{
499506
if ( !property( "hideDialogs" ).toBool() )
500507
QMessageBox::critical( this, tr( "New GeoPackage Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
501-
delete layer;
502508
}
503509

504510
return false;
@@ -509,6 +515,11 @@ void QgsNewGeoPackageLayerDialog::setOverwriteBehavior( OverwriteBehavior behavi
509515
mBehavior = behavior;
510516
}
511517

518+
void QgsNewGeoPackageLayerDialog::setAddToProject( bool addToProject )
519+
{
520+
mAddToProject = addToProject;
521+
}
522+
512523
void QgsNewGeoPackageLayerDialog::showHelp()
513524
{
514525
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );

src/gui/qgsnewgeopackagelayerdialog.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
7676
*/
7777
void setOverwriteBehavior( OverwriteBehavior behavior );
7878

79+
/**
80+
* Sets whether a newly created layer should automatically be added to the current project.
81+
* Defaults to true.
82+
*
83+
* \since QGIS 3.6
84+
*/
85+
void setAddToProject( bool addToProject );
86+
7987
private slots:
8088
void mAddAttributeButton_clicked();
8189
void mRemoveAttributeButton_clicked();
@@ -100,6 +108,7 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
100108
bool mTableNameEdited = false;
101109
bool mLayerIdentifierEdited = false;
102110
OverwriteBehavior mBehavior = Prompt;
111+
bool mAddToProject = true;
103112
};
104113

105114
#endif // QGSNEWVECTORLAYERDIALOG_H

0 commit comments

Comments
 (0)