Skip to content

Commit 026083f

Browse files
committed
Add QgsGeoPackageLayerInfo and move sublayers logic
sublayers is now a reusable static function
1 parent 16ea693 commit 026083f

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

src/providers/ogr/qgsgeopackagedataitems.cpp

+25-10
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ QgsGeoPackageConnectionItem::QgsGeoPackageConnectionItem( QgsDataItem *parent, Q
154154
mCapabilities |= Collapse;
155155
}
156156

157-
QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
157+
QList<QgsGeoPackageLayerInfo *> QgsGeoPackageConnectionItem::subLayers( const QString &path ) const
158158
{
159-
QVector<QgsDataItem *> children;
159+
160+
QList<QgsGeoPackageLayerInfo *> children;
160161

161162
// Vector layers
162163
QgsVectorLayer layer( mPath, QStringLiteral( "ogr_tmp" ), QStringLiteral( "ogr" ) );
@@ -199,8 +200,7 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
199200
if ( i == 0 && values.size() > 1 )
200201
{
201202
uri = QStringLiteral( "%1|layerid=%2|layername=%3" ).arg( mPath, layerId, name );
202-
QgsGeoPackageVectorLayerItem *item = new QgsGeoPackageVectorLayerItem( this, name, mPath, uri, QgsLayerItem::LayerType::TableLayer );
203-
children.append( item );
203+
children.append( new QgsGeoPackageLayerInfo( mPath, uri, name, QgsLayerItem::LayerType::TableLayer ) );
204204
}
205205
if ( layerType != QgsLayerItem::LayerType::NoType )
206206
{
@@ -218,9 +218,8 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
218218
{
219219
uri = QStringLiteral( "%1|layerid=%2" ).arg( mPath, layerId );
220220
}
221-
QgsGeoPackageVectorLayerItem *item = new QgsGeoPackageVectorLayerItem( this, name, mPath, uri, layerType );
222221
QgsDebugMsgLevel( QStringLiteral( "Adding GeoPackage Vector item %1 %2 %3" ).arg( name, uri, geometryType ), 3 );
223-
children.append( item );
222+
children.append( new QgsGeoPackageLayerInfo( mPath, uri, name, layerType ) );
224223
}
225224
}
226225
else
@@ -240,8 +239,7 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
240239
QStringList pieces = uri.split( ':' );
241240
QString name = pieces.value( pieces.length() - 1 );
242241
QgsDebugMsgLevel( QStringLiteral( "Adding GeoPackage Raster item %1 %2 %3" ).arg( name, uri ), 3 );
243-
QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem( this, name, mPath, uri );
244-
children.append( item );
242+
children.append( new QgsGeoPackageLayerInfo( mPath, uri, name, QgsLayerItem::LayerType::Raster ) );
245243
}
246244
}
247245
else if ( rlayer.isValid( ) )
@@ -275,12 +273,29 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
275273
}
276274

277275
QgsDebugMsgLevel( QStringLiteral( "Adding GeoPackage Raster item %1 %2 %3" ).arg( name, mPath ), 3 );
278-
QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem( this, name, mPath, uri );
279-
children.append( item );
276+
children.append( new QgsGeoPackageLayerInfo( mPath, uri, name, QgsLayerItem::LayerType::Raster ) );
280277
}
281278
}
282279
return children;
280+
}
283281

282+
QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
283+
{
284+
QVector<QgsDataItem *> children;
285+
QList<QgsGeoPackageLayerInfo *> layers = subLayers( mPath );
286+
for ( const QgsGeoPackageLayerInfo *info : qgsAsConst( layers ) )
287+
{
288+
if ( info->type() == QgsLayerItem::LayerType::Raster )
289+
{
290+
children.append( new QgsGeoPackageRasterLayerItem( this, info->name(), info->path(), info->uri() ) );
291+
}
292+
else
293+
{
294+
children.append( new QgsGeoPackageVectorLayerItem( this, info->name(), info->path(), info->uri(), info->type( ) ) );
295+
}
296+
}
297+
qDeleteAll( layers );
298+
return children;
284299
}
285300

286301
bool QgsGeoPackageConnectionItem::equal( const QgsDataItem *other )

src/providers/ogr/qgsgeopackagedataitems.h

+27-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@
1919
#include "qgsdataitemprovider.h"
2020
#include "qgsdataprovider.h"
2121

22+
23+
/**
24+
* Holds the information about a gpkg layer
25+
*/
26+
class QgsGeoPackageLayerInfo
27+
{
28+
public:
29+
QgsGeoPackageLayerInfo( const QString &path, const QString &uri, const QString &name, const QgsLayerItem::LayerType &type )
30+
: mPath( path )
31+
, mUri( uri )
32+
, mName( name )
33+
, mType( type )
34+
{
35+
}
36+
const QString path() const { return mPath; }
37+
const QString uri() const { return mUri; }
38+
const QString name() const { return mName; }
39+
QgsLayerItem::LayerType type() const { return mType; }
40+
private:
41+
QString mPath;
42+
QString mUri;
43+
QString mName;
44+
QgsLayerItem::LayerType mType = QgsLayerItem::LayerType::NoType;
45+
};
46+
2247
/**
2348
* \brief The QgsGeoPackageAbstractLayerItem class is the base class for GeoPackage raster and vector layers
2449
*/
@@ -69,7 +94,8 @@ class QgsGeoPackageConnectionItem : public QgsDataCollectionItem
6994

7095
public:
7196
QgsGeoPackageConnectionItem( QgsDataItem *parent, QString name, QString path );
72-
97+
//! Extract layers information from the geopackage
98+
QList<QgsGeoPackageLayerInfo *> subLayers( const QString &path ) const;
7399
QVector<QgsDataItem *> createChildren() override;
74100
virtual bool equal( const QgsDataItem *other ) override;
75101

0 commit comments

Comments
 (0)