Skip to content
Permalink
Browse files

postgres selection:

- 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 8d59d0430233bf5308b6fb1ceb21b1c2ea7cb011
@@ -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 )
@@ -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
@@ -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()
@@ -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
@@ -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() )
@@ -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 ) );
}
}

@@ -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++ )
@@ -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++ )
{
@@ -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();

0 comments on commit 8d59d04

Please sign in to comment.
You can’t perform that action at this time.