17
17
#include " qgsgeopackageconnection.h"
18
18
#include " qgslogger.h"
19
19
#include " qgssettings.h"
20
+ #include " qgsproject.h"
20
21
#include " qgsvectorlayer.h"
22
+ #include " qgsrasterlayer.h"
23
+ #include " qgsnewgeopackagelayerdialog.h"
21
24
22
25
#include < QAction>
23
26
#include < QMessageBox>
@@ -117,7 +120,9 @@ void QgsGeoPackageRootItem::newConnection()
117
120
118
121
void QgsGeoPackageRootItem::createDatabase ()
119
122
{
120
- // TODO
123
+ QgsNewGeoPackageLayerDialog dialog ( nullptr );
124
+ dialog.setCrs ( QgsProject::instance ()->defaultCrsForNewLayers () );
125
+ dialog.exec ();
121
126
}
122
127
123
128
@@ -132,10 +137,11 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
132
137
{
133
138
QVector<QgsDataItem *> children;
134
139
140
+ // Vector layers
135
141
QgsVectorLayer layer ( mPath , QStringLiteral ( " ogr_tmp" ), QStringLiteral ( " ogr" ) );
136
142
if ( ! layer.isValid ( ) )
137
143
{
138
- children. append ( new QgsErrorItem ( this , tr ( " Layer is not a valid GeoPackage layer" ), mPath + " /error " ) );
144
+ QgsDebugMsgLevel ( tr ( " Layer is not a valid GeoPackage Vector layer %1 " ). arg ( mPath ), 3 );
139
145
}
140
146
else
141
147
{
@@ -146,23 +152,34 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
146
152
QString name = pieces[1 ];
147
153
QString featuresCount = pieces[2 ];
148
154
QString geometryType = pieces[3 ];
149
- QgsGeoPackageLayerItem ::LayerType layerType;
155
+ QgsLayerItem ::LayerType layerType;
150
156
layerType = layerTypeFromDb ( geometryType );
151
- if ( layerType != QgsGeoPackageLayerItem ::LayerType::NoType )
157
+ if ( layerType != QgsLayerItem ::LayerType::NoType )
152
158
{
153
- // URI: '/path/gdal_sample_v1.2_no_extensions.gpkg|layerid=7|geometrytype=Point '
154
- QString uri = QStringLiteral ( " %1|layerid=%2|geometrytype=%3 " ).arg ( mPath , layerId, QgsGeoPackageLayerItem::layerTypeAsString ( layerTypeFromDb ( geometryType ) ) );
155
- QgsGeoPackageLayerItem *item = new QgsGeoPackageLayerItem ( this , name, mPath , uri, layerType );
156
- QgsDebugMsg ( QStringLiteral ( " Adding GPKG item %1 %2 %3" ).arg ( name, uri, geometryType ) );
159
+ // example URI: '/path/gdal_sample_v1.2_no_extensions.gpkg|layerid=7'
160
+ QString uri = QStringLiteral ( " %1|layerid=%2" ).arg ( mPath , layerId );
161
+ QgsGeoPackageVectorLayerItem *item = new QgsGeoPackageVectorLayerItem ( this , name, mPath , uri, layerType );
162
+ QgsDebugMsg ( QStringLiteral ( " Adding GPKG Vector item %1 %2 %3" ).arg ( name, uri, geometryType ) );
157
163
children.append ( item );
158
164
}
159
165
else
160
166
{
161
- children. append ( new QgsErrorItem ( this , tr ( " Layer is not a supported GeoPackage layer geometry type: %1" ).arg ( geometryType ), mPath + " /error " ) );
167
+ QgsDebugMsgLevel ( QStringLiteral ( " Layer type is not a supported GeoPackage Vector layer %1" ).arg ( mPath ), 3 );
162
168
}
163
169
164
170
}
165
171
}
172
+ // Raster layers
173
+ QgsRasterLayer rlayer ( mPath , QStringLiteral ( " gdal_tmp" ), QStringLiteral ( " gdal" ), false );
174
+ Q_FOREACH ( const QString &uri, rlayer.dataProvider ()->subLayers ( ) )
175
+ {
176
+ QStringList pieces = uri.split ( ' :' );
177
+ QString name = pieces.value ( pieces.length () - 1 );
178
+ QgsDebugMsg ( QStringLiteral ( " Adding GPKG Raster item %1 %2 %3" ).arg ( name, uri ) );
179
+ QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem ( this , name, mPath , uri );
180
+ children.append ( item );
181
+
182
+ }
166
183
return children;
167
184
168
185
}
@@ -184,38 +201,47 @@ QList<QAction *> QgsGeoPackageConnectionItem::actions()
184
201
{
185
202
QList<QAction *> lst;
186
203
187
- QAction *actionRemoveConnection = new QAction ( tr ( " Remove connection" ), this );
188
204
// TODO: implement layer deletion
205
+
206
+ // QAction *actionRemoveConnection = new QAction( tr( "Remove connection" ), this );
189
207
// connect( actionDeleteLayer, &QAction::triggered, this, &QgsGeoPackageLayerItem::deleteLayer );
190
- lst.append ( actionRemoveConnection );
208
+ // lst.append( actionRemoveConnection );
191
209
return lst;
192
210
}
193
211
#endif
194
212
195
213
QgsLayerItem::LayerType QgsGeoPackageConnectionItem::layerTypeFromDb ( const QString &geometryType )
196
214
{
197
- if ( QString::compare ( geometryType, QStringLiteral ( " Point" ), Qt::CaseInsensitive ) == 0 || QString::compare ( geometryType, QStringLiteral ( " MultiPoint " ), Qt::CaseInsensitive ) == 0 )
215
+ if ( geometryType. contains ( QStringLiteral ( " Point" ), Qt::CaseInsensitive ) )
198
216
{
199
217
return QgsLayerItem::LayerType::Point;
200
218
}
201
- else if ( QString::compare ( geometryType, QStringLiteral ( " Polygon" ), Qt::CaseInsensitive ) == 0 || QString::compare ( geometryType, QStringLiteral ( " MultiPolygon " ), Qt::CaseInsensitive ) == 0 )
219
+ else if ( geometryType. contains ( QStringLiteral ( " Polygon" ), Qt::CaseInsensitive ) )
202
220
{
203
221
return QgsLayerItem::LayerType::Polygon;
204
222
}
205
- else if ( QString::compare ( geometryType, QStringLiteral ( " LineString" ), Qt::CaseInsensitive ) == 0 || QString::compare ( geometryType, QStringLiteral ( " MultiLineString " ), Qt::CaseInsensitive ) == 0 )
223
+ else if ( geometryType. contains ( QStringLiteral ( " LineString" ), Qt::CaseInsensitive ) )
206
224
{
207
225
return QgsLayerItem::LayerType::Line;
208
226
}
227
+ else if ( geometryType.contains ( QStringLiteral ( " Collection" ), Qt::CaseInsensitive ) )
228
+ {
229
+ return QgsLayerItem::LayerType::Vector;
230
+ }
231
+ else if ( geometryType.contains ( QStringLiteral ( " Table" ), Qt::CaseInsensitive ) )
232
+ {
233
+ return QgsLayerItem::LayerType::Table;
234
+ }
209
235
// To be moved in a parent class that would also work for gdal and rasters
210
- else if ( QString::compare ( geometryType, QStringLiteral ( " Raster" ), Qt::CaseInsensitive ) == 0 )
236
+ else if ( geometryType. contains ( QStringLiteral ( " Raster" ), Qt::CaseInsensitive ) )
211
237
{
212
238
return QgsLayerItem::LayerType::Raster;
213
239
}
214
240
return QgsLayerItem::LayerType::NoType;
215
241
}
216
242
217
243
#ifdef HAVE_GUI
218
- QList<QAction *> QgsGeoPackageLayerItem ::actions ()
244
+ QList<QAction *> QgsGeoPackageAbstractLayerItem ::actions ()
219
245
{
220
246
QList<QAction *> lst;
221
247
@@ -227,8 +253,21 @@ QList<QAction *> QgsGeoPackageLayerItem::actions()
227
253
}
228
254
#endif
229
255
230
- QgsGeoPackageLayerItem::QgsGeoPackageLayerItem ( QgsDataItem *parent, QString name, QString path, QString uri, QgsLayerItem::LayerType layerType )
231
- : QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( " ogr " ) )
256
+ QgsGeoPackageAbstractLayerItem::QgsGeoPackageAbstractLayerItem ( QgsDataItem *parent, QString name, QString path, QString uri, QgsLayerItem::LayerType layerType, QString providerKey )
257
+ : QgsLayerItem( parent, name, path, uri, layerType, providerKey )
232
258
{
233
259
setState ( Populated ); // no children are expected
234
260
}
261
+
262
+
263
+ QgsGeoPackageVectorLayerItem::QgsGeoPackageVectorLayerItem ( QgsDataItem *parent, QString name, QString path, QString uri, LayerType layerType )
264
+ : QgsGeoPackageAbstractLayerItem( parent, name, path, uri, layerType, QStringLiteral( " ogr" ) )
265
+ {
266
+
267
+ }
268
+
269
+ QgsGeoPackageRasterLayerItem::QgsGeoPackageRasterLayerItem ( QgsDataItem *parent, QString name, QString path, QString uri )
270
+ : QgsGeoPackageAbstractLayerItem( parent, name, path, uri, QgsLayerItem::LayerType::Raster, QStringLiteral( " gdal" ) )
271
+ {
272
+
273
+ }
0 commit comments