Skip to content

Commit ab74ecc

Browse files
committedSep 18, 2017
File browser: use GeoPackage items for gpkg files
1 parent 9f1f3ea commit ab74ecc

File tree

3 files changed

+117
-27
lines changed

3 files changed

+117
-27
lines changed
 

‎src/providers/ogr/qgsgeopackagedataitems.cpp

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void QgsGeoPackageRootItem::createDatabase()
120120
#endif
121121

122122

123-
QgsGeoPackageConnectionItem::QgsGeoPackageConnectionItem( QgsDataItem *parent, const QString &name, const QString &path )
123+
QgsGeoPackageCollectionItem::QgsGeoPackageCollectionItem( QgsDataItem *parent, const QString &name, const QString &path )
124124
: QgsDataCollectionItem( parent, name, path )
125125
, mPath( path )
126126
{
@@ -129,7 +129,7 @@ QgsGeoPackageConnectionItem::QgsGeoPackageConnectionItem( QgsDataItem *parent, c
129129

130130

131131

132-
QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
132+
QVector<QgsDataItem *> QgsGeoPackageCollectionItem::createChildren()
133133
{
134134
QVector<QgsDataItem *> children;
135135
const auto layers = QgsOgrLayerItem::subLayers( mPath, QStringLiteral( "GPKG" ) );
@@ -148,30 +148,31 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
148148
return children;
149149
}
150150

151-
bool QgsGeoPackageConnectionItem::equal( const QgsDataItem *other )
151+
bool QgsGeoPackageCollectionItem::equal( const QgsDataItem *other )
152152
{
153153
if ( type() != other->type() )
154154
{
155155
return false;
156156
}
157-
const QgsGeoPackageConnectionItem *o = dynamic_cast<const QgsGeoPackageConnectionItem *>( other );
157+
const QgsGeoPackageCollectionItem *o = dynamic_cast<const QgsGeoPackageCollectionItem *>( other );
158158
return o && mPath == o->mPath && mName == o->mName;
159159

160160
}
161161

162162
#ifdef HAVE_GUI
163163

164-
QList<QAction *> QgsGeoPackageConnectionItem::actions()
164+
QList<QAction *> QgsGeoPackageCollectionItem::actions()
165165
{
166166
QList<QAction *> lst;
167167

168-
QAction *actionDeleteConnection = new QAction( tr( "Remove connection" ), this );
169-
connect( actionDeleteConnection, &QAction::triggered, this, &QgsGeoPackageConnectionItem::deleteConnection );
170-
lst.append( actionDeleteConnection );
168+
// Add to stored connections
169+
QAction *actionAddConnection = new QAction( tr( "Add connection" ), this );
170+
connect( actionAddConnection, &QAction::triggered, this, &QgsGeoPackageCollectionItem::addConnection );
171+
lst.append( actionAddConnection );
171172

172173
// Add table to existing DB
173174
QAction *actionAddTable = new QAction( tr( "Create a new layer or table..." ), this );
174-
connect( actionAddTable, &QAction::triggered, this, &QgsGeoPackageConnectionItem::addTable );
175+
connect( actionAddTable, &QAction::triggered, this, &QgsGeoPackageCollectionItem::addTable );
175176
lst.append( actionAddTable );
176177

177178
return lst;
@@ -180,7 +181,7 @@ QList<QAction *> QgsGeoPackageConnectionItem::actions()
180181

181182

182183

183-
bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAction )
184+
bool QgsGeoPackageCollectionItem::handleDrop( const QMimeData *data, Qt::DropAction )
184185
{
185186

186187
if ( !QgsMimeDataUtils::isUriList( data ) )
@@ -327,7 +328,7 @@ bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAct
327328
}
328329

329330

330-
bool QgsGeoPackageConnectionItem::deleteGeoPackageRasterLayer( const QString &uri, QString &errCause )
331+
bool QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( const QString &uri, QString &errCause )
331332
{
332333
bool result = false;
333334
// Better safe than sorry
@@ -444,15 +445,49 @@ bool QgsGeoPackageConnectionItem::deleteGeoPackageRasterLayer( const QString &ur
444445
return result;
445446
}
446447

448+
QgsGeoPackageConnectionItem::QgsGeoPackageConnectionItem( QgsDataItem *parent, const QString &name, const QString &path )
449+
: QgsGeoPackageCollectionItem( parent, name, path )
450+
{
451+
452+
}
453+
454+
bool QgsGeoPackageConnectionItem::equal( const QgsDataItem *other )
455+
{
456+
if ( type() != other->type() )
457+
{
458+
return false;
459+
}
460+
const QgsGeoPackageConnectionItem *o = dynamic_cast<const QgsGeoPackageConnectionItem *>( other );
461+
return o && mPath == o->mPath && mName == o->mName;
462+
463+
}
464+
465+
#ifdef HAVE_GUI
466+
QList<QAction *> QgsGeoPackageConnectionItem::actions()
467+
{
468+
QList<QAction *> lst;
469+
470+
QAction *actionDeleteConnection = new QAction( tr( "Remove connection" ), this );
471+
connect( actionDeleteConnection, &QAction::triggered, this, &QgsGeoPackageConnectionItem::deleteConnection );
472+
lst.append( actionDeleteConnection );
473+
474+
// Add table to existing DB
475+
QAction *actionAddTable = new QAction( tr( "Create a new layer or table..." ), this );
476+
connect( actionAddTable, &QAction::triggered, this, &QgsGeoPackageConnectionItem::addTable );
477+
lst.append( actionAddTable );
478+
479+
480+
return lst;
481+
}
447482

448483
void QgsGeoPackageConnectionItem::deleteConnection()
449484
{
450485
QgsOgrDbConnection::deleteConnection( name(), QStringLiteral( "GPKG" ) );
451486
mParent->refreshConnections();
452487
}
453488

454-
#ifdef HAVE_GUI
455-
void QgsGeoPackageConnectionItem::addTable()
489+
490+
void QgsGeoPackageCollectionItem::addTable()
456491
{
457492
QgsNewGeoPackageLayerDialog dialog( nullptr );
458493
QFileInfo fileInfo( mPath );
@@ -472,6 +507,14 @@ void QgsGeoPackageConnectionItem::addTable()
472507
QgsDebugMsg( QStringLiteral( "Cannot add Table: connection %1 does not exists or the path is empy!" ).arg( connName ) );
473508
}
474509
}
510+
511+
void QgsGeoPackageCollectionItem::addConnection()
512+
{
513+
QgsOgrDbConnection connection( mName, QStringLiteral( "GeoPackage" ) );
514+
connection.save();
515+
emit connectionsChanged();
516+
}
517+
475518
#endif
476519

477520
#ifdef HAVE_GUI
@@ -554,7 +597,7 @@ QgsGeoPackageRasterLayerItem::QgsGeoPackageRasterLayerItem( QgsDataItem *parent,
554597

555598
bool QgsGeoPackageRasterLayerItem::executeDeleteLayer( QString &errCause )
556599
{
557-
return QgsGeoPackageConnectionItem::deleteGeoPackageRasterLayer( mUri, errCause );
600+
return QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( mUri, errCause );
558601
}
559602

560603

‎src/providers/ogr/qgsgeopackagedataitems.h

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ class QgsGeoPackageVectorLayerItem : public QgsGeoPackageAbstractLayerItem
6464
};
6565

6666

67-
class QgsGeoPackageConnectionItem : public QgsDataCollectionItem
67+
/**
68+
* \brief The QgsGeoPackageCollectionItem class is the base class for
69+
* GeoPackage container
70+
*/
71+
class QgsGeoPackageCollectionItem : public QgsDataCollectionItem
6872
{
6973
Q_OBJECT
7074

7175
public:
72-
QgsGeoPackageConnectionItem( QgsDataItem *parent, const QString &name, const QString &path );
76+
QgsGeoPackageCollectionItem( QgsDataItem *parent, const QString &name, const QString &path );
7377
QVector<QgsDataItem *> createChildren() override;
7478
virtual bool equal( const QgsDataItem *other ) override;
7579

@@ -87,16 +91,40 @@ class QgsGeoPackageConnectionItem : public QgsDataCollectionItem
8791

8892
public slots:
8993
#ifdef HAVE_GUI
90-
void editConnection();
91-
void deleteConnection();
9294
void addTable();
95+
void addConnection();
9396
#endif
9497

9598
protected:
9699
QString mPath;
97100
};
98101

99102

103+
/**
104+
* \brief The QgsGeoPackageConnectionItem class adds the stored
105+
* connection management to QgsGeoPackageCollectionItem
106+
*/
107+
class QgsGeoPackageConnectionItem : public QgsGeoPackageCollectionItem
108+
{
109+
Q_OBJECT
110+
111+
public:
112+
QgsGeoPackageConnectionItem( QgsDataItem *parent, const QString &name, const QString &path );
113+
virtual bool equal( const QgsDataItem *other ) override;
114+
115+
#ifdef HAVE_GUI
116+
QList<QAction *> actions() override;
117+
#endif
118+
119+
public slots:
120+
#ifdef HAVE_GUI
121+
void editConnection();
122+
void deleteConnection();
123+
#endif
124+
125+
};
126+
127+
100128
class QgsGeoPackageRootItem : public QgsDataCollectionItem
101129
{
102130
Q_OBJECT

‎src/providers/ogr/qgsogrdataitems.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsproject.h"
2323
#include "qgsvectorlayer.h"
2424
#include "qgsrasterlayer.h"
25+
#include "qgsgeopackagedataitems.h"
2526

2627
#include <QFileInfo>
2728
#include <QTextStream>
@@ -567,9 +568,19 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
567568
#endif
568569
}
569570

570-
// return item without testing if:
571-
// scanExtSetting
572-
// or zipfile and scan zip == "Basic scan"
571+
// Filters out the OGR/GDAL supported formats that can contain multiple layers
572+
// and should be treated like a DB: GeoPackage and SQLite
573+
// NOTE: this formats are scanned for rasters too and they must
574+
// be skipped by "gdal" provider or the rasters will be listed
575+
// twice. ogrSupportedDbLayersExtensions must be kept in sync
576+
// with the companion variable (same name) in the gdal provider
577+
// class
578+
// TODO: add more OGR supported multiple layers formats here!
579+
QStringList ogrSupportedDbLayersExtensions;
580+
ogrSupportedDbLayersExtensions << QLatin1String( "gpkg" ) << QLatin1String( "sqlite" ) << QLatin1String( "db" );
581+
582+
// Fast track: return item without testing if:
583+
// scanExtSetting or zipfile and scan zip == "Basic scan"
573584
if ( scanExtSetting ||
574585
( ( is_vsizip || is_vsitar ) && scanZipSetting == QLatin1String( "basic" ) ) )
575586
{
@@ -596,13 +607,18 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
596607
// Handle collections
597608
// Check if the layer has sublayers by comparing the extension
598609
QgsDataItem *item;
599-
QStringList multipleLayersExtensions;
600-
// TODO: add more OGR supported multiple layers formats here!
601-
multipleLayersExtensions << QLatin1String( "gpkg" ) << QLatin1String( "sqlite" ) << QLatin1String( "db" );
602-
if ( ! multipleLayersExtensions.contains( suffix ) )
610+
if ( ! ogrSupportedDbLayersExtensions.contains( suffix ) )
611+
{
603612
item = new QgsOgrLayerItem( parentItem, name, path, path, QgsLayerItem::Vector );
613+
}
614+
else if ( suffix.compare( QLatin1String( "gpkg" ), Qt::CaseInsensitive ) == 0 )
615+
{
616+
item = new QgsGeoPackageCollectionItem( parentItem, name, path );
617+
}
604618
else
619+
{
605620
item = new QgsOgrDataCollectionItem( parentItem, name, path );
621+
}
606622

607623
if ( item )
608624
return item;
@@ -625,10 +641,9 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
625641

626642
QgsDebugMsgLevel( QString( "GDAL Driver : %1" ).arg( GDALGetDriverShortName( hDriver ) ), 2 );
627643

628-
int numLayers = GDALDatasetGetLayerCount( hDataSource );
629-
630644
QgsDataItem *item = nullptr;
631645

646+
int numLayers = OGR_DS_GetLayerCount( hDataSource );
632647
if ( numLayers == 1 )
633648
{
634649
QgsDebugMsgLevel( QString( "using name = %1" ).arg( name ), 2 );
@@ -639,6 +654,10 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
639654
QgsDebugMsgLevel( QString( "using name = %1" ).arg( name ), 2 );
640655
item = new QgsOgrDataCollectionItem( parentItem, name, path );
641656
}
657+
else if ( suffix.compare( QLatin1String( "gpkg" ), Qt::CaseInsensitive ) == 0 )
658+
{
659+
item = new QgsGeoPackageCollectionItem( parentItem, name, path );
660+
}
642661

643662
GDALClose( hDataSource );
644663
return item;

0 commit comments

Comments
 (0)
Please sign in to comment.