Skip to content

Commit a1d8d9a

Browse files
committed
Fix invalid data source message is always shown after dragging and dropping
vector layers to QGIS window
1 parent 9b6367f commit a1d8d9a

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

src/app/qgisapp.cpp

+32-22
Original file line numberDiff line numberDiff line change
@@ -4626,7 +4626,8 @@ static void setupVectorLayer( const QString &vectorLayerPath,
46264626
bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
46274627
{
46284628
bool wasfrozen = mMapCanvas->isFrozen();
4629-
QList<QgsMapLayer *> myList;
4629+
QList<QgsMapLayer *> layersToAdd;
4630+
QList<QgsMapLayer *> addedLayers;
46304631
QgsSettings settings;
46314632

46324633
Q_FOREACH ( QString src, layerQStringList )
@@ -4713,7 +4714,7 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
47134714
// sublayers selection dialog so the user can select the sublayers to actually load.
47144715
if ( sublayers.count() > 1 )
47154716
{
4716-
askUserForOGRSublayers( layer );
4717+
addedLayers.append( askUserForOGRSublayers( layer ) );
47174718

47184719
// The first layer loaded is not useful in that case. The user can select it in
47194720
// the list if he wants to load it.
@@ -4725,7 +4726,7 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
47254726
setupVectorLayer( src, sublayers, layer,
47264727
QStringLiteral( "ogr" ), options );
47274728

4728-
myList << layer;
4729+
layersToAdd << layer;
47294730
}
47304731
else
47314732
{
@@ -4754,14 +4755,14 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
47544755
}
47554756

47564757
// make sure at least one layer was successfully added
4757-
if ( myList.isEmpty() )
4758+
if ( layersToAdd.isEmpty() )
47584759
{
4759-
return false;
4760+
return !addedLayers.isEmpty();
47604761
}
47614762

47624763
// Register this layer with the layers registry
4763-
QgsProject::instance()->addMapLayers( myList );
4764-
Q_FOREACH ( QgsMapLayer *l, myList )
4764+
QgsProject::instance()->addMapLayers( layersToAdd );
4765+
for ( QgsMapLayer *l : qgis::as_const( layersToAdd ) )
47654766
{
47664767
bool ok;
47674768
l->loadDefaultStyle( ok );
@@ -4782,10 +4783,15 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
47824783
// statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
47834784

47844785
return true;
4785-
} // QgisApp::addVectorLayer()
4786+
}
47864787

47874788

47884789
QgsMeshLayer *QgisApp::addMeshLayer( const QString &url, const QString &baseName, const QString &providerKey )
4790+
{
4791+
return addMeshLayerPrivate( url, baseName, providerKey );
4792+
}
4793+
4794+
QgsMeshLayer *QgisApp::addMeshLayerPrivate( const QString &url, const QString &baseName, const QString &providerKey, const bool guiWarning )
47894795
{
47904796
bool wasfrozen = mMapCanvas->isFrozen();
47914797
QgsSettings settings;
@@ -4805,8 +4811,11 @@ QgsMeshLayer *QgisApp::addMeshLayer( const QString &url, const QString &baseName
48054811

48064812
if ( ! layer || !layer->isValid() )
48074813
{
4808-
QString msg = tr( "%1 is not a valid or recognized data source." ).arg( url );
4809-
visibleMessageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );
4814+
if ( guiWarning )
4815+
{
4816+
QString msg = tr( "%1 is not a valid or recognized data source." ).arg( url );
4817+
visibleMessageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );
4818+
}
48104819

48114820
// since the layer is bad, stomp on it
48124821
return nullptr;
@@ -4830,7 +4839,7 @@ QgsMeshLayer *QgisApp::addMeshLayer( const QString &url, const QString &baseName
48304839
refreshMapCanvas();
48314840
}
48324841
return layer.release();
4833-
} // QgisApp::addMeshLayer()
4842+
}
48344843

48354844
// present a dialog to choose zipitem layers
48364845
bool QgisApp::askUserForZipItemLayers( const QString &path )
@@ -5119,13 +5128,14 @@ void QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
51195128

51205129
// This method is the method that does the real job. If the layer given in
51215130
// parameter is nullptr, then the method tries to act on the activeLayer.
5122-
void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
5131+
QList<QgsMapLayer *> QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
51235132
{
5133+
QList<QgsMapLayer *> result;
51245134
if ( !layer )
51255135
{
51265136
layer = qobject_cast<QgsVectorLayer *>( activeLayer() );
51275137
if ( !layer || layer->dataProvider()->name() != QLatin1String( "ogr" ) )
5128-
return;
5138+
return result;
51295139
}
51305140

51315141
QStringList sublayers = layer->dataProvider()->subLayers();
@@ -5170,7 +5180,7 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
51705180
chooseSublayersDialog.populateLayerTable( list );
51715181

51725182
if ( !chooseSublayersDialog.exec() )
5173-
return;
5183+
return result;
51745184

51755185
QString name = layer->name();
51765186

@@ -5181,7 +5191,6 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
51815191
// The uri must contain the actual uri of the vectorLayer from which we are
51825192
// going to load the sublayers.
51835193
QString fileName = QFileInfo( uri ).baseName();
5184-
QList<QgsMapLayer *> myList;
51855194
Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
51865195
{
51875196
QString layerGeometryType = def.type;
@@ -5208,7 +5217,7 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
52085217
QgsVectorLayer *layer = new QgsVectorLayer( composedURI, name, QStringLiteral( "ogr" ), options );
52095218
if ( layer && layer->isValid() )
52105219
{
5211-
myList << layer;
5220+
result << layer;
52125221
}
52135222
else
52145223
{
@@ -5218,7 +5227,7 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
52185227
}
52195228
}
52205229

5221-
if ( ! myList.isEmpty() )
5230+
if ( !result.isEmpty() )
52225231
{
52235232
QgsSettings settings;
52245233
bool addToGroup = settings.value( QStringLiteral( "/qgis/openSublayersInGroup" ), true ).toBool();
@@ -5227,8 +5236,8 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
52275236
if ( addToGroup )
52285237
group = QgsProject::instance()->layerTreeRoot()->insertGroup( 0, name );
52295238

5230-
QgsProject::instance()->addMapLayers( myList, ! addToGroup );
5231-
Q_FOREACH ( QgsMapLayer *l, myList )
5239+
QgsProject::instance()->addMapLayers( result, ! addToGroup );
5240+
for ( QgsMapLayer *l : qgis::as_const( result ) )
52325241
{
52335242
bool ok;
52345243
l->loadDefaultStyle( ok );
@@ -5241,6 +5250,7 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
52415250
if ( addToGroup && ! newLayersVisible )
52425251
group->setItemVisibilityCheckedRecursive( newLayersVisible );
52435252
}
5253+
return result;
52445254
}
52455255

52465256
void QgisApp::addDatabaseLayers( QStringList const &layerPathList, QString const &providerKey )
@@ -6478,7 +6488,7 @@ bool QgisApp::openLayer( const QString &fileName, bool allowInteractive )
64786488
// Try to load as mesh layer after raster & vector
64796489
if ( !ok )
64806490
{
6481-
ok = addMeshLayer( fileName, fileInfo.completeBaseName(), "mdal" );
6491+
ok = static_cast< bool >( addMeshLayerPrivate( fileName, fileInfo.completeBaseName(), QStringLiteral( "mdal" ), false ) );
64826492
}
64836493

64846494
if ( !ok )
@@ -11055,12 +11065,12 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
1105511065
! vectorLayerPath.contains( QStringLiteral( "layerid=" ) ) &&
1105611066
! vectorLayerPath.contains( QStringLiteral( "layername=" ) ) )
1105711067
{
11058-
askUserForOGRSublayers( layer );
11068+
QList< QgsMapLayer * > addedLayers = askUserForOGRSublayers( layer );
1105911069

1106011070
// The first layer loaded is not useful in that case. The user can select it in
1106111071
// the list if he wants to load it.
1106211072
delete layer;
11063-
layer = nullptr;
11073+
layer = addedLayers.isEmpty() ? nullptr : qobject_cast< QgsVectorLayer * >( addedLayers.at( 0 ) );
1106411074
}
1106511075
else
1106611076
{

src/app/qgisapp.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1786,9 +1786,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
17861786
bool shouldAskUserForGDALSublayers( QgsRasterLayer *layer );
17871787

17881788
/**
1789-
* This method will open a dialog so the user can select OGR sublayers to load
1789+
* This method will open a dialog so the user can select OGR sublayers to load,
1790+
* and then returns a list of these layers.
17901791
*/
1791-
void askUserForOGRSublayers( QgsVectorLayer *layer );
1792+
QList< QgsMapLayer * > askUserForOGRSublayers( QgsVectorLayer *layer );
17921793

17931794
/**
17941795
* Add a raster layer to the map (passed in as a ptr).
@@ -1801,6 +1802,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
18011802
const QString &providerKey, bool guiWarning,
18021803
bool guiUpdate );
18031804

1805+
//! Open a mesh layer - this is the generic function which takes all parameters
1806+
QgsMeshLayer *addMeshLayerPrivate( const QString &uri, const QString &baseName,
1807+
const QString &providerKey, bool guiWarning = true );
1808+
18041809
/**
18051810
* Add the current project to the recently opened/saved projects list
18061811
* pass settings by reference since creating more than one

0 commit comments

Comments
 (0)