Skip to content

Commit

Permalink
postgres selection:
Browse files Browse the repository at this point in the history
- make geometryless table selectable
- only make srid column editable when necessary
- leave primary key column editable
- require primary key selection for views
  • Loading branch information
jef-n committed Jan 25, 2012
1 parent 318a827 commit 8d59d04
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
27 changes: 17 additions & 10 deletions src/providers/postgres/qgspgsourceselect.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,6 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
}
}

if ( cmbConnections->count() > 0 && !mColumnTypeThread )
mAddButton->setEnabled( true );

//if we have only one schema item, expand it by default
int numTopLevelItems = mTableModel.invisibleRootItem()->rowCount();
if ( numTopLevelItems < 2 || mTableModel.tableCount() < 20 )
Expand All @@ -522,9 +519,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()

if ( !mColumnTypeThread )
{
QApplication::restoreOverrideCursor();
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmTable, Qt::AscendingOrder );
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmSchema, Qt::AscendingOrder );
finishList();
}
}
else
Expand All @@ -536,18 +531,30 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
}
}

void QgsPgSourceSelect::columnThreadFinished()
void QgsPgSourceSelect::finishList()
{
delete mColumnTypeThread;
mColumnTypeThread = 0;
btnConnect->setText( tr( "Connect" ) );
QApplication::restoreOverrideCursor();

if ( cmbConnections->count() > 0 )
mAddButton->setEnabled( true );

#if 0
for ( int i = 0; i < QgsPgTableModel::dbtmColumns; i++ )
mTablesTreeView->resizeColumnToContents( i );
#endif

mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmTable, Qt::AscendingOrder );
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmSchema, Qt::AscendingOrder );

}

void QgsPgSourceSelect::columnThreadFinished()
{
delete mColumnTypeThread;
mColumnTypeThread = 0;
btnConnect->setText( tr( "Connect" ) );

finishList();
}

QStringList QgsPgSourceSelect::selectedTables()
Expand Down
3 changes: 2 additions & 1 deletion src/providers/postgres/qgspgsourceselect.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase

QPushButton *mBuildQueryButton;
QPushButton *mAddButton;
void updateSelectableState( const QModelIndex &index );

void finishList();
};

#endif // QGSPGSOURCESELECT_H
32 changes: 23 additions & 9 deletions src/providers/postgres/qgspgtablemodel.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
QStandardItem *tableItem = new QStandardItem( layerProperty.tableName );
QStandardItem *geomItem = new QStandardItem( layerProperty.geometryColName );
QStandardItem *sridItem = new QStandardItem( layerProperty.srid );
sridItem->setEditable( false );

QString pkText, pkCol = "";
switch ( layerProperty.pkCols.size() )
Expand Down Expand Up @@ -120,11 +121,13 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
childItemList << selItem;
childItemList << sqlItem;

if ( geomType == QGis::UnknownGeometry || layerProperty.srid.isEmpty() )
if ( geomType == QGis::UnknownGeometry ||
( geomType != QGis::NoGeometry && layerProperty.srid.isEmpty() ) ||
pkText == tr( "Select..." ) )
{
foreach( QStandardItem *item, childItemList )
{
item->setFlags( item->flags() & ~( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
item->setFlags( item->flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
}
}

Expand Down Expand Up @@ -257,9 +260,13 @@ void QgsPgTableModel::setGeometryTypesForTable( QgsPostgresLayerProperty layerPr

row[ dbtmSrid ]->setText( sridList.at( 0 ) );

Qt::ItemFlags flags = Qt::ItemIsEnabled;
if ( layerProperty.pkCols.size() < 2 )
flags |= Qt::ItemIsSelectable;

foreach( QStandardItem *item, row )
{
item->setFlags( item->flags() | Qt::ItemIsSelectable | Qt::ItemIsEnabled );
item->setFlags( item->flags() | flags );
}

for ( int j = 1; j < typeList.size(); j++ )
Expand Down Expand Up @@ -295,12 +302,18 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
if ( !QStandardItemModel::setData( idx, value, role ) )
return false;

if ( idx.column() == dbtmType || idx.column() == dbtmSrid )
if ( idx.column() == dbtmType || idx.column() == dbtmSrid || idx.column() == dbtmPkCol )
{
bool ok;
idx.sibling( idx.row(), dbtmSrid ).data().toInt( &ok );
QGis::GeometryType geomType = ( QGis::GeometryType ) idx.sibling( idx.row(), dbtmType ).data( Qt::UserRole + 2 ).toInt();

bool ok = geomType != QGis::UnknownGeometry;

if ( geomType != QGis::NoGeometry )
idx.sibling( idx.row(), dbtmSrid ).data().toInt( &ok );

ok &= idx.sibling( idx.row(), dbtmType ).data( Qt::UserRole + 2 ).toInt() != QGis::UnknownGeometry;
QStringList pkCols = idx.sibling( idx.row(), dbtmPkCol ).data( Qt::UserRole + 1 ).toStringList();
if ( pkCols.size() > 0 )
ok = pkCols.contains( idx.sibling( idx.row(), dbtmPkCol ).data().toString() );

for ( int i = 0; i < dbtmColumns; i++ )
{
Expand Down Expand Up @@ -328,8 +341,9 @@ QString QgsPgTableModel::layerURI( const QModelIndex &index, QString connInfo, b
QStandardItem *pkItem = itemFromIndex( index.sibling( index.row(), dbtmPkCol ) );
QString pkColumnName = pkItem->data( Qt::UserRole + 2 ).toString();

if ( pkItem->data( Qt::UserRole + 1 ).toStringList().size() > 0 && pkColumnName.isEmpty() )
// no primary key for view selected
if ( pkItem->data( Qt::UserRole + 1 ).toStringList().size() > 0 &&
!pkItem->data( Qt::UserRole + 1 ).toStringList().contains( pkColumnName ) )
// no valid primary candidate selected
return QString::null;

QString schemaName = index.sibling( index.row(), dbtmSchema ).data( Qt::DisplayRole ).toString();
Expand Down

0 comments on commit 8d59d04

Please sign in to comment.