@@ -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
453416void 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
597553QStringList QgsPgSourceSelect::selectedTables ()
598554{
599- return m_selectedTables ;
555+ return mSelectedTables ;
600556}
601557
602558QString QgsPgSourceSelect::connectionInfo ()
603559{
604- return m_connInfo ;
560+ return mConnInfo ;
605561}
606562
607563void 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
0 commit comments