@@ -42,104 +42,109 @@ QgsPgTableModel::~QgsPgTableModel()
42
42
43
43
void QgsPgTableModel::addTableEntry ( QgsPostgresLayerProperty layerProperty )
44
44
{
45
- QgsDebugMsg ( QString ( " %1.%2.%3 type=%4 geomType=%5 srid=%6 pk=%7 sql=%8" )
46
- .arg ( layerProperty.schemaName )
47
- .arg ( layerProperty.tableName )
48
- .arg ( layerProperty.geometryColName )
49
- .arg ( layerProperty.type )
50
- .arg ( layerProperty.geometryColType )
51
- .arg ( layerProperty.srid )
52
- .arg ( layerProperty.pkCols .join ( " ," ) )
53
- .arg ( layerProperty.sql ) );
45
+ QgsDebugMsg ( layerProperty.toString () );
54
46
55
47
// is there already a root item with the given scheme Name?
56
- QStandardItem *schemaItem;
57
- QList<QStandardItem*> schemaItems = findItems ( layerProperty.schemaName , Qt::MatchExactly, dbtmSchema );
48
+ QStandardItem *schemaItem = 0 ;
58
49
59
- // there is already an item for this schema
60
- if ( schemaItems.size () > 0 )
50
+ for ( int i = 0 ; i < layerProperty.size (); i++ )
61
51
{
62
- schemaItem = schemaItems.at ( dbtmSchema );
63
- }
64
- else
65
- {
66
- // create a new toplevel item for this schema
67
- schemaItem = new QStandardItem ( layerProperty.schemaName );
68
- schemaItem->setFlags ( Qt::ItemIsEnabled );
69
- invisibleRootItem ()->setChild ( invisibleRootItem ()->rowCount (), schemaItem );
70
- }
71
-
72
- QGis::WkbType wkbType = QgsPostgresConn::wkbTypeFromPostgis ( layerProperty.type );
73
- if ( wkbType == QGis::WKBUnknown && layerProperty.geometryColName .isEmpty () )
74
- {
75
- wkbType = QGis::WKBNoGeometry;
76
- }
77
-
78
- QList<QStandardItem*> childItemList;
52
+ QGis::WkbType wkbType = layerProperty.types [ i ];
53
+ int srid = layerProperty.srids [ i ];
54
+ if ( wkbType == QGis::WKBUnknown && layerProperty.geometryColName .isEmpty () )
55
+ {
56
+ wkbType = QGis::WKBNoGeometry;
57
+ }
79
58
80
- QStandardItem *schemaNameItem = new QStandardItem ( layerProperty.schemaName );
81
- schemaNameItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
59
+ bool selectable = wkbType == QGis::WKBNoGeometry || ( wkbType != QGis::WKBUnknown && srid != 0 );
82
60
83
- QStandardItem *typeItem = new QStandardItem ( iconForWkbType ( wkbType ),
84
- wkbType == QGis::WKBUnknown
85
- ? tr ( " Detecting... " )
86
- : QgsPostgresConn::displayStringForWkbType ( wkbType ) );
87
- typeItem-> setData ( wkbType == QGis::WKBUnknown, Qt::UserRole + 1 );
88
- typeItem->setData ( wkbType, Qt::UserRole + 2 );
61
+ QStandardItem *schemaNameItem = new QStandardItem ( layerProperty. schemaName );
62
+ QStandardItem *typeItem = new QStandardItem ( iconForWkbType ( wkbType ), wkbType == QGis::WKBUnknown ? tr ( " Select... " ) : QgsPostgresConn::displayStringForWkbType ( wkbType ) );
63
+ typeItem-> setData ( wkbType == QGis::WKBUnknown, Qt::UserRole + 1 );
64
+ typeItem-> setData ( wkbType, Qt::UserRole + 2 );
65
+ if ( wkbType == QGis::WKBUnknown )
66
+ typeItem->setFlags ( typeItem-> flags () | Qt::ItemIsEditable );
89
67
90
- QStandardItem *geomTypeItem = new QStandardItem ( QgsPostgresConn::displayStringForGeomType ( layerProperty.geometryColType ) );
68
+ QStandardItem *geomTypeItem = new QStandardItem ( QgsPostgresConn::displayStringForGeomType ( layerProperty.geometryColType ) );
91
69
92
- QStandardItem *tableItem = new QStandardItem ( layerProperty.tableName );
93
- QStandardItem *geomItem = new QStandardItem ( layerProperty.geometryColName );
94
- QStandardItem *sridItem = new QStandardItem ( layerProperty.srid );
95
- sridItem->setEditable ( false );
70
+ QStandardItem *tableItem = new QStandardItem ( layerProperty.tableName );
71
+ QStandardItem *geomItem = new QStandardItem ( layerProperty.geometryColName );
72
+ QStandardItem *sridItem = new QStandardItem ( wkbType != QGis::WKBNoGeometry ? QString::number ( srid ) : " " );
73
+ sridItem->setEditable ( wkbType != QGis::WKBNoGeometry && srid == 0 );
74
+ if ( sridItem->isEditable () )
75
+ {
76
+ sridItem->setText ( tr ( " Enter..." ) );
77
+ sridItem->setFlags ( sridItem->flags () | Qt::ItemIsEditable );
78
+ }
96
79
97
- QString pkCol = " " ;
98
- if ( layerProperty.pkCols .size () > 0 )
99
- {
100
- pkCol = layerProperty.pkCols [0 ];
101
- }
80
+ QString pkCol = " " ;
81
+ if ( layerProperty.pkCols .size () > 0 )
82
+ {
83
+ pkCol = layerProperty.pkCols [0 ];
84
+ }
102
85
103
- QStandardItem *pkItem = new QStandardItem ( pkCol );
104
- if ( layerProperty.pkCols .size () > 1 )
105
- pkItem->setFlags ( pkItem->flags () | Qt::ItemIsEditable );
106
- else
107
- pkItem->setFlags ( pkItem->flags () & ~Qt::ItemIsEditable );
86
+ QStandardItem *pkItem = new QStandardItem ( pkCol );
87
+ if ( layerProperty.pkCols .size () > 1 )
88
+ pkItem->setFlags ( pkItem->flags () | Qt::ItemIsEditable );
89
+ else
90
+ pkItem->setFlags ( pkItem->flags () & ~Qt::ItemIsEditable );
108
91
109
- pkItem->setData ( layerProperty.pkCols , Qt::UserRole + 1 );
110
- pkItem->setData ( pkCol, Qt::UserRole + 2 );
92
+ pkItem->setData ( layerProperty.pkCols , Qt::UserRole + 1 );
93
+ pkItem->setData ( pkCol, Qt::UserRole + 2 );
111
94
112
- QStandardItem *selItem = new QStandardItem ( " " );
113
- selItem->setFlags ( selItem->flags () | Qt::ItemIsUserCheckable );
114
- selItem->setCheckState ( Qt::Checked );
115
- selItem->setToolTip ( tr ( " Disable 'Fast Access to Features at ID' capability to force keeping the attribute table in memory (e.g. in case of expensive views)." ) );
95
+ QStandardItem *selItem = new QStandardItem ( " " );
96
+ selItem->setFlags ( selItem->flags () | Qt::ItemIsUserCheckable );
97
+ selItem->setCheckState ( Qt::Checked );
98
+ selItem->setToolTip ( tr ( " Disable 'Fast Access to Features at ID' capability to force keeping the attribute table in memory (e.g. in case of expensive views)." ) );
116
99
117
- QStandardItem* sqlItem = new QStandardItem ( layerProperty.sql );
100
+ QStandardItem* sqlItem = new QStandardItem ( layerProperty.sql );
118
101
119
- childItemList << schemaNameItem;
120
- childItemList << tableItem;
121
- childItemList << geomItem;
122
- childItemList << geomTypeItem;
123
- childItemList << typeItem;
124
- childItemList << sridItem;
125
- childItemList << pkItem;
126
- childItemList << selItem;
127
- childItemList << sqlItem;
102
+ QList<QStandardItem*> childItemList;
128
103
129
- bool detailsFromThread = wkbType == QGis::WKBUnknown ||
130
- ( wkbType != QGis::WKBNoGeometry && layerProperty.srid .isEmpty () );
104
+ childItemList << schemaNameItem;
105
+ childItemList << tableItem;
106
+ childItemList << geomItem;
107
+ childItemList << geomTypeItem;
108
+ childItemList << typeItem;
109
+ childItemList << sridItem;
110
+ childItemList << pkItem;
111
+ childItemList << selItem;
112
+ childItemList << sqlItem;
131
113
132
- if ( detailsFromThread )
133
- {
134
114
foreach ( QStandardItem *item, childItemList )
135
115
{
136
- item->setFlags ( item->flags () & ~( Qt::ItemIsSelectable | Qt::ItemIsEnabled ) );
116
+ if ( selectable )
117
+ {
118
+ item->setFlags ( item->flags () | Qt::ItemIsSelectable );
119
+ }
120
+ else
121
+ {
122
+ item->setFlags ( item->flags () & ~Qt::ItemIsSelectable );
123
+ }
124
+ }
125
+
126
+ if ( !schemaItem )
127
+ {
128
+ QList<QStandardItem*> schemaItems = findItems ( layerProperty.schemaName , Qt::MatchExactly, dbtmSchema );
129
+
130
+ // there is already an item for this schema
131
+ if ( schemaItems.size () > 0 )
132
+ {
133
+ schemaItem = schemaItems.at ( dbtmSchema );
134
+ }
135
+ else
136
+ {
137
+ // create a new toplevel item for this schema
138
+ schemaItem = new QStandardItem ( layerProperty.schemaName );
139
+ schemaItem->setFlags ( Qt::ItemIsEnabled );
140
+ invisibleRootItem ()->setChild ( invisibleRootItem ()->rowCount (), schemaItem );
141
+ }
137
142
}
138
- }
139
143
140
- schemaItem->appendRow ( childItemList );
144
+ schemaItem->appendRow ( childItemList );
141
145
142
- ++mTableCount ;
146
+ ++mTableCount ;
147
+ }
143
148
}
144
149
145
150
void QgsPgTableModel::setSql ( const QModelIndex &index, const QString &sql )
@@ -204,84 +209,6 @@ void QgsPgTableModel::setSql( const QModelIndex &index, const QString &sql )
204
209
}
205
210
}
206
211
207
- void QgsPgTableModel::setGeometryTypesForTable ( QgsPostgresLayerProperty layerProperty )
208
- {
209
- QStringList typeList = layerProperty.type .split ( " ," , QString::SkipEmptyParts );
210
- QStringList sridList = layerProperty.srid .split ( " ," , QString::SkipEmptyParts );
211
- Q_ASSERT ( typeList.size () == sridList.size () );
212
-
213
- // find schema item and table item
214
- QStandardItem* schemaItem;
215
- QList<QStandardItem*> schemaItems = findItems ( layerProperty.schemaName , Qt::MatchExactly, dbtmSchema );
216
-
217
- if ( schemaItems.size () < 1 )
218
- {
219
- return ;
220
- }
221
-
222
- schemaItem = schemaItems.at ( 0 );
223
-
224
- int n = schemaItem->rowCount ();
225
- for ( int i = 0 ; i < n; i++ )
226
- {
227
- QModelIndex currentChildIndex = indexFromItem ( schemaItem->child ( i, dbtmSchema ) );
228
- if ( !currentChildIndex.isValid () )
229
- {
230
- continue ;
231
- }
232
-
233
- QList<QStandardItem *> row;
234
-
235
- for ( int j = 0 ; j < dbtmColumns; j++ )
236
- {
237
- row << itemFromIndex ( currentChildIndex.sibling ( i, j ) );
238
- }
239
-
240
- if ( row[ dbtmTable ]->text () == layerProperty.tableName && row[ dbtmGeomCol ]->text () == layerProperty.geometryColName )
241
- {
242
- row[ dbtmSrid ]->setText ( layerProperty.srid );
243
-
244
- if ( typeList.isEmpty () )
245
- {
246
- row[ dbtmType ]->setText ( tr ( " Select..." ) );
247
- row[ dbtmType ]->setFlags ( row[ dbtmType ]->flags () | Qt::ItemIsEditable );
248
-
249
- row[ dbtmSrid ]->setText ( tr ( " Enter..." ) );
250
- row[ dbtmSrid ]->setFlags ( row[ dbtmSrid ]->flags () | Qt::ItemIsEditable );
251
-
252
- foreach ( QStandardItem *item, row )
253
- {
254
- item->setFlags ( item->flags () | Qt::ItemIsEnabled );
255
- }
256
- }
257
- else
258
- {
259
- // update existing row
260
- QGis::WkbType wkbType = QgsPostgresConn::wkbTypeFromPostgis ( typeList.at ( 0 ) );
261
-
262
- row[ dbtmType ]->setIcon ( iconForWkbType ( wkbType ) );
263
- row[ dbtmType ]->setText ( QgsPostgresConn::displayStringForWkbType ( wkbType ) );
264
- row[ dbtmType ]->setData ( false , Qt::UserRole + 1 );
265
- row[ dbtmType ]->setData ( wkbType, Qt::UserRole + 2 );
266
-
267
- row[ dbtmSrid ]->setText ( sridList.at ( 0 ) );
268
-
269
- foreach ( QStandardItem *item, row )
270
- {
271
- item->setFlags ( item->flags () | Qt::ItemIsSelectable | Qt::ItemIsEnabled );
272
- }
273
-
274
- for ( int j = 1 ; j < typeList.size (); j++ )
275
- {
276
- layerProperty.type = typeList[j];
277
- layerProperty.srid = sridList[j];
278
- addTableEntry ( layerProperty );
279
- }
280
- }
281
- }
282
- }
283
- }
284
-
285
212
QIcon QgsPgTableModel::iconForWkbType ( QGis::WkbType type )
286
213
{
287
214
switch ( type )
@@ -321,7 +248,10 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
321
248
bool ok = geomType != QGis::WKBUnknown;
322
249
323
250
if ( ok && geomType != QGis::WKBNoGeometry )
324
- idx.sibling ( idx.row (), dbtmSrid ).data ().toInt ( &ok );
251
+ {
252
+ int srid = idx.sibling ( idx.row (), dbtmSrid ).data ().toInt ( &ok );
253
+ ok &= srid != 0 ;
254
+ }
325
255
326
256
QStringList pkCols = idx.sibling ( idx.row (), dbtmPkCol ).data ( Qt::UserRole + 1 ).toStringList ();
327
257
if ( ok && pkCols.size () > 0 )
@@ -343,20 +273,29 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
343
273
QString QgsPgTableModel::layerURI ( const QModelIndex &index, QString connInfo, bool useEstimatedMetadata )
344
274
{
345
275
if ( !index .isValid () )
276
+ {
277
+ QgsDebugMsg ( " invalid index" );
346
278
return QString::null;
279
+ }
347
280
348
281
QGis::WkbType wkbType = ( QGis::WkbType ) itemFromIndex ( index .sibling ( index .row (), dbtmType ) )->data ( Qt::UserRole + 2 ).toInt ();
349
282
if ( wkbType == QGis::WKBUnknown )
283
+ {
284
+ QgsDebugMsg ( " unknown geometry type" );
350
285
// no geometry type selected
351
286
return QString::null;
287
+ }
352
288
353
289
QStandardItem *pkItem = itemFromIndex ( index .sibling ( index .row (), dbtmPkCol ) );
354
290
QString pkColumnName = pkItem->data ( Qt::UserRole + 2 ).toString ();
355
291
356
292
if ( pkItem->data ( Qt::UserRole + 1 ).toStringList ().size () > 0 &&
357
293
!pkItem->data ( Qt::UserRole + 1 ).toStringList ().contains ( pkColumnName ) )
294
+ {
358
295
// no valid primary candidate selected
296
+ QgsDebugMsg ( " no pk candidate selected" );
359
297
return QString::null;
298
+ }
360
299
361
300
QString schemaName = index .sibling ( index .row (), dbtmSchema ).data ( Qt::DisplayRole ).toString ();
362
301
QString tableName = index .sibling ( index .row (), dbtmTable ).data ( Qt::DisplayRole ).toString ();
@@ -371,7 +310,10 @@ QString QgsPgTableModel::layerURI( const QModelIndex &index, QString connInfo, b
371
310
bool ok;
372
311
srid.toInt ( &ok );
373
312
if ( !ok )
313
+ {
314
+ QgsDebugMsg ( " srid not numeric" );
374
315
return QString::null;
316
+ }
375
317
}
376
318
377
319
bool selectAtId = itemFromIndex ( index .sibling ( index .row (), dbtmSelectAtId ) )->checkState () == Qt::Checked;
@@ -384,5 +326,6 @@ QString QgsPgTableModel::layerURI( const QModelIndex &index, QString connInfo, b
384
326
uri.setSrid ( srid );
385
327
uri.disableSelectAtId ( !selectAtId );
386
328
329
+ QgsDebugMsg ( QString ( " returning uri %1" ).arg ( uri.uri () ) );
387
330
return uri.uri ();
388
331
}
0 commit comments