Skip to content

Commit dbe6497

Browse files
committedJan 22, 2012
move layerURI from QgsPgSourceSelect to QgsPgTableModel
1 parent 60b6ad9 commit dbe6497

File tree

4 files changed

+55
-59
lines changed

4 files changed

+55
-59
lines changed
 

‎src/providers/postgres/qgspgsourceselect.cpp

100755100644
Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -412,74 +412,30 @@ void QgsPgSourceSelect::populateConnectionList()
412412
cmbConnections->setDisabled( cmbConnections->count() == 0 );
413413
}
414414

415-
QString QgsPgSourceSelect::layerURI( const QModelIndex &index )
416-
{
417-
QGis::GeometryType geomType = ( QGis::GeometryType ) mTableModel.itemFromIndex( index.sibling( index.row(), QgsPgTableModel::dbtmType ) )->data( Qt::UserRole + 2 ).toInt();
418-
if ( geomType == QGis::UnknownGeometry )
419-
// no geometry type selected
420-
return QString::null;
421-
422-
QStandardItem *pkItem = mTableModel.itemFromIndex( index.sibling( index.row(), QgsPgTableModel::dbtmPkCol ) );
423-
QString pkColumnName = pkItem->data( Qt::UserRole + 2 ).toString();
424-
425-
if ( pkItem->data( Qt::UserRole + 1 ).toStringList().size() > 0 && pkColumnName.isEmpty() )
426-
// no primary key for view selected
427-
return QString::null;
428-
429-
QString schemaName = index.sibling( index.row(), QgsPgTableModel::dbtmSchema ).data( Qt::DisplayRole ).toString();
430-
QString tableName = index.sibling( index.row(), QgsPgTableModel::dbtmTable ).data( Qt::DisplayRole ).toString();
431-
QString geomColumnName = index.sibling( index.row(), QgsPgTableModel::dbtmGeomCol ).data( Qt::DisplayRole ).toString();
432-
433-
QString srid = index.sibling( index.row(), QgsPgTableModel::dbtmSrid ).data( Qt::DisplayRole ).toString();
434-
bool ok;
435-
srid.toInt( &ok );
436-
if ( !ok )
437-
return QString::null;
438-
439-
bool selectAtId = mTableModel.itemFromIndex( index.sibling( index.row(), QgsPgTableModel::dbtmSelectAtId ) )->checkState() == Qt::Checked;
440-
QString sql = index.sibling( index.row(), QgsPgTableModel::dbtmSql ).data( Qt::DisplayRole ).toString();
441-
442-
QgsDataSourceURI uri( m_connInfo );
443-
uri.setDataSource( schemaName, tableName, geomType != QGis::NoGeometry ? geomColumnName : QString::null, sql, pkColumnName );
444-
uri.setUseEstimatedMetadata( mUseEstimatedMetadata );
445-
uri.setGeometryType( geomType );
446-
uri.setSrid( srid );
447-
uri.disableSelectAtId( !selectAtId );
448-
449-
return uri.uri();
450-
}
451-
452415
// Slot for performing action when the Add button is clicked
453416
void QgsPgSourceSelect::addTables()
454417
{
455-
m_selectedTables.clear();
418+
mSelectedTables.clear();
456419

457-
QItemSelection selection = mTablesTreeView->selectionModel()->selection();
458-
QModelIndexList selectedIndices = selection.indexes();
459-
QModelIndexList::const_iterator selected_it = selectedIndices.constBegin();
460-
for ( ; selected_it != selectedIndices.constEnd(); ++selected_it )
420+
foreach( QModelIndex idx, mTablesTreeView->selectionModel()->selection().indexes() )
461421
{
462-
if ( !selected_it->parent().isValid() || selected_it->column() > 0 )
463-
{
464-
//top level items only contain the schema names
422+
if ( idx.column() != QgsPgTableModel::dbtmTable )
465423
continue;
466-
}
467424

468-
QModelIndex index = mProxyModel.mapToSource( *selected_it );
469-
QString uri = layerURI( index );
425+
QString uri = mTableModel.layerURI( mProxyModel.mapToSource( idx ), mConnInfo, mUseEstimatedMetadata );
470426
if ( uri.isNull() )
471427
continue;
472428

473-
m_selectedTables << uri;
429+
mSelectedTables << uri;
474430
}
475431

476-
if ( m_selectedTables.empty() )
432+
if ( mSelectedTables.empty() )
477433
{
478434
QMessageBox::information( this, tr( "Select Table" ), tr( "You must select a table in order to add a layer." ) );
479435
}
480436
else
481437
{
482-
emit addDatabaseLayers( m_selectedTables, "postgres" );
438+
emit addDatabaseLayers( mSelectedTables, "postgres" );
483439
accept();
484440
}
485441
}
@@ -502,7 +458,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
502458

503459
QgsDebugMsg( "Connection info: " + uri.connectionInfo() );
504460

505-
m_connInfo = uri.connectionInfo();
461+
mConnInfo = uri.connectionInfo();
506462
mUseEstimatedMetadata = uri.useEstimatedMetadata();
507463

508464
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo(), true );
@@ -596,12 +552,12 @@ void QgsPgSourceSelect::columnThreadFinished()
596552

597553
QStringList QgsPgSourceSelect::selectedTables()
598554
{
599-
return m_selectedTables;
555+
return mSelectedTables;
600556
}
601557

602558
QString QgsPgSourceSelect::connectionInfo()
603559
{
604-
return m_connInfo;
560+
return mConnInfo;
605561
}
606562

607563
void QgsPgSourceSelect::setSql( const QModelIndex &index )
@@ -615,7 +571,7 @@ void QgsPgSourceSelect::setSql( const QModelIndex &index )
615571
QModelIndex idx = mProxyModel.mapToSource( index );
616572
QString tableName = mTableModel.itemFromIndex( idx.sibling( idx.row(), QgsPgTableModel::dbtmTable ) )->text();
617573

618-
QgsVectorLayer *vlayer = new QgsVectorLayer( layerURI( idx ), tableName, "postgres" );
574+
QgsVectorLayer *vlayer = new QgsVectorLayer( mTableModel.layerURI( idx, mConnInfo, mUseEstimatedMetadata ), tableName, "postgres" );
619575

620576
if ( !vlayer->isValid() )
621577
{
@@ -639,7 +595,7 @@ void QgsPgSourceSelect::addSearchGeometryColumn( QgsPostgresLayerProperty layerP
639595
// store the column details and do the query in a thread
640596
if ( !mColumnTypeThread )
641597
{
642-
QgsPostgresConn *conn = QgsPostgresConn::connectDb( m_connInfo, true /* readonly */ );
598+
QgsPostgresConn *conn = QgsPostgresConn::connectDb( mConnInfo, true /* readonly */ );
643599
if ( conn )
644600
{
645601

‎src/providers/postgres/qgspgsourceselect.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
139139
QStringList mColumnLabels;
140140
// Our thread for doing long running queries
141141
QgsGeomColumnTypeThread* mColumnTypeThread;
142-
QString m_connInfo;
143-
QStringList m_selectedTables;
142+
QString mConnInfo;
143+
QStringList mSelectedTables;
144144
bool mUseEstimatedMetadata;
145145
// Storage for the range of layer type icons
146146
QMap<QString, QPair<QString, QIcon> > mLayerIcons;
@@ -149,7 +149,6 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
149149
QgsPgTableModel mTableModel;
150150
QgsDbFilterProxyModel mProxyModel;
151151

152-
QString layerURI( const QModelIndex &index );
153152
QPushButton *mBuildQueryButton;
154153
QPushButton *mAddButton;
155154
void updateSelectableState( const QModelIndex &index );

‎src/providers/postgres/qgspgtablemodel.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,43 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
314314

315315
return true;
316316
}
317+
318+
QString QgsPgTableModel::layerURI( const QModelIndex &index, QString connInfo, bool useEstimatedMetadata )
319+
{
320+
if ( !index.isValid() )
321+
return QString::null;
322+
323+
QGis::GeometryType geomType = ( QGis::GeometryType ) itemFromIndex( index.sibling( index.row(), dbtmType ) )->data( Qt::UserRole + 2 ).toInt();
324+
if ( geomType == QGis::UnknownGeometry )
325+
// no geometry type selected
326+
return QString::null;
327+
328+
QStandardItem *pkItem = itemFromIndex( index.sibling( index.row(), dbtmPkCol ) );
329+
QString pkColumnName = pkItem->data( Qt::UserRole + 2 ).toString();
330+
331+
if ( pkItem->data( Qt::UserRole + 1 ).toStringList().size() > 0 && pkColumnName.isEmpty() )
332+
// no primary key for view selected
333+
return QString::null;
334+
335+
QString schemaName = index.sibling( index.row(), dbtmSchema ).data( Qt::DisplayRole ).toString();
336+
QString tableName = index.sibling( index.row(), dbtmTable ).data( Qt::DisplayRole ).toString();
337+
QString geomColumnName = index.sibling( index.row(), dbtmGeomCol ).data( Qt::DisplayRole ).toString();
338+
339+
QString srid = index.sibling( index.row(), dbtmSrid ).data( Qt::DisplayRole ).toString();
340+
bool ok;
341+
srid.toInt( &ok );
342+
if ( !ok )
343+
return QString::null;
344+
345+
bool selectAtId = itemFromIndex( index.sibling( index.row(), dbtmSelectAtId ) )->checkState() == Qt::Checked;
346+
QString sql = index.sibling( index.row(), dbtmSql ).data( Qt::DisplayRole ).toString();
347+
348+
QgsDataSourceURI uri( connInfo );
349+
uri.setDataSource( schemaName, tableName, geomType != QGis::NoGeometry ? geomColumnName : QString::null, sql, pkColumnName );
350+
uri.setUseEstimatedMetadata( useEstimatedMetadata );
351+
uri.setGeometryType( geomType );
352+
uri.setSrid( srid );
353+
uri.disableSelectAtId( !selectAtId );
354+
355+
return uri.uri();
356+
}

‎src/providers/postgres/qgspgtablemodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class QgsPgTableModel : public QStandardItemModel
6060

6161
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
6262

63+
QString layerURI( const QModelIndex &index, QString connInfo, bool useEstimatedMetadata );
6364

6465
static QIcon iconForGeomType( QGis::GeometryType type );
6566

0 commit comments

Comments
 (0)
Please sign in to comment.