Skip to content
Permalink
Browse files

postgres provider:

- base geometrytype filtering on GeometryType not WkbType
- use tooltips on dataitems
- fix #4783
- adapt #4767
  • Loading branch information
jef-n committed Jan 11, 2012
1 parent 5e59deb commit 2b739d945d90d31b11561f0ca3938706b4162ebf
@@ -63,6 +63,7 @@ class CORE_EXPORT QGis
WKBMultiLineString25D,
WKBMultiPolygon25D,
};

enum GeometryType
{
Point,
@@ -27,7 +27,7 @@ QgsDataSourceURI::QgsDataSourceURI()
, mKeyColumn( "" )
, mUseEstimatedMetadata( false )
, mSelectAtIdDisabled( false )
, mGeometryType( QGis::WKBUnknown )
, mGeometryType( QGis::UnknownGeometry )
{
// do nothing
}
@@ -37,7 +37,7 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri )
, mKeyColumn( "" )
, mUseEstimatedMetadata( false )
, mSelectAtIdDisabled( false )
, mGeometryType( QGis::WKBUnknown )
, mGeometryType( QGis::UnknownGeometry )
{
int i = 0;
while ( i < uri.length() )
@@ -134,31 +134,19 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri )
QString geomTypeUpper = pval.toUpper();
if ( geomTypeUpper == "POINT" )
{
mGeometryType = QGis::WKBPoint;
}
else if ( geomTypeUpper == "MULTIPOINT" )
{
mGeometryType = QGis::WKBMultiPoint;
mGeometryType = QGis::Point;
}
else if ( geomTypeUpper == "LINESTRING" )
{
mGeometryType = QGis::WKBLineString;
}
else if ( geomTypeUpper == "MULTILINESTRING" )
{
mGeometryType = QGis::WKBMultiLineString;
mGeometryType = QGis::Line;
}
else if ( geomTypeUpper == "POLYGON" )
{
mGeometryType = QGis::WKBPolygon;
}
else if ( geomTypeUpper == "MULTIPOLYGON" )
{
mGeometryType = QGis::WKBMultiPolygon;
mGeometryType = QGis::Polygon;
}
else
{
mGeometryType = QGis::WKBUnknown;
mGeometryType = QGis::UnknownGeometry;
}
}
else if ( pname == "selectatid" )
@@ -526,9 +514,9 @@ QString QgsDataSourceURI::uri() const
theUri += QString( " srid=%1" ).arg( mSrid );
}

if ( mGeometryType != QGis::WKBUnknown && mGeometryType != QGis::WKBNoGeometry )
if ( mGeometryType != QGis::UnknownGeometry && mGeometryType != QGis::NoGeometry )
{
theUri += QString( " type=%1" ).arg( QGis::qgisFeatureTypes[mGeometryType] + 3 );
theUri += QString( " type=%1" ).arg( QGis::qgisVectorGeometryType[mGeometryType] );
}

if ( mSelectAtIdDisabled )
@@ -601,12 +589,12 @@ void QgsDataSourceURI::setDatabase( const QString &database )
mDatabase = database;
}

QGis::WkbType QgsDataSourceURI::geometryType() const
QGis::GeometryType QgsDataSourceURI::geometryType() const
{
return mGeometryType;
}

void QgsDataSourceURI::setGeometryType( QGis::WkbType geometryType )
void QgsDataSourceURI::setGeometryType( QGis::GeometryType geometryType )
{
mGeometryType = geometryType;
}
@@ -118,8 +118,9 @@ class CORE_EXPORT QgsDataSourceURI
void setKeyColumn( QString column );

// added in 1.9
QGis::WkbType geometryType() const;
void setGeometryType( QGis::WkbType type );
QGis::GeometryType geometryType() const;
void setGeometryType( QGis::GeometryType type );

QString srid() const;
void setSrid( QString srid );

@@ -159,7 +160,7 @@ class CORE_EXPORT QgsDataSourceURI
//! Disable SelectAtId capability (eg. to trigger the attribute table memory model for expensive views)
bool mSelectAtIdDisabled;
//! geometry type (or QGis::WKBUnknown if not specified)
QGis::WkbType mGeometryType;
QGis::GeometryType mGeometryType;
//! SRID or a null string if not specified
QString mSrid;
};
@@ -29,7 +29,6 @@ class QgsGeomColumnTypeThread : public QThread
Q_OBJECT
public:
QgsGeomColumnTypeThread( QgsPostgresConn *conn, bool useEstimatedMetaData );
void addGeometryColumn( QgsPostgresLayerProperty layerProperty );

// These functions get the layer types and pass that information out
// by emitting the setLayerType() signal.
@@ -39,6 +38,7 @@ class QgsGeomColumnTypeThread : public QThread
void setLayerType( QgsPostgresLayerProperty layerProperty );

public slots:
void addGeometryColumn( QgsPostgresLayerProperty layerProperty );
void stop();

private:
@@ -51,17 +51,14 @@ QWidget *QgsPgSourceSelectDelegate::createEditor( QWidget *parent, const QStyleO
if ( index.column() == QgsPgTableModel::dbtmType && index.data( Qt::UserRole + 1 ).toBool() )
{
QComboBox *cb = new QComboBox( parent );
foreach( QGis::WkbType type,
QList<QGis::WkbType>()
<< QGis::WKBPoint
<< QGis::WKBLineString
<< QGis::WKBPolygon
<< QGis::WKBMultiPoint
<< QGis::WKBMultiLineString
<< QGis::WKBMultiPolygon
<< QGis::WKBNoGeometry )
foreach( QGis::GeometryType type,
QList<QGis::GeometryType>()
<< QGis::Point
<< QGis::Line
<< QGis::Polygon
<< QGis::NoGeometry )
{
cb->addItem( QgsPgTableModel::iconForType( type ), QgsPgTableModel::displayStringForType( type ).toUpper(), type );
cb->addItem( QgsPgTableModel::iconForGeomType( type ), QgsPostgresConn::displayStringForGeomType( type ), type );
}
cb->setCurrentIndex( cb->findData( index.data( Qt::UserRole + 2 ).toInt() ) );
return cb;
@@ -90,10 +87,10 @@ void QgsPgSourceSelectDelegate::setModelData( QWidget *editor, QAbstractItemMode
{
if ( index.column() == QgsPgTableModel::dbtmType )
{
QGis::WkbType type = ( QGis::WkbType ) cb->itemData( cb->currentIndex() ).toInt();
QGis::GeometryType type = ( QGis::GeometryType ) cb->itemData( cb->currentIndex() ).toInt();

model->setData( index, QgsPgTableModel::iconForType( type ), Qt::DecorationRole );
model->setData( index, type != QGis::WKBUnknown ? QgsPgTableModel::displayStringForType( type ) : tr( "Select..." ) );
model->setData( index, QgsPgTableModel::iconForGeomType( type ), Qt::DecorationRole );
model->setData( index, type != QGis::UnknownGeometry ? QgsPostgresConn::displayStringForGeomType( type ) : tr( "Select..." ) );
model->setData( index, type, Qt::UserRole + 2 );
}
else if ( index.column() == QgsPgTableModel::dbtmPkCol )
@@ -412,8 +409,8 @@ void QgsPgSourceSelect::populateConnectionList()

QString QgsPgSourceSelect::layerURI( const QModelIndex &index )
{
QGis::WkbType geomType = ( QGis::WkbType ) mTableModel.itemFromIndex( index.sibling( index.row(), QgsPgTableModel::dbtmType ) )->data( Qt::UserRole + 2 ).toInt();
if ( geomType == QGis::WKBUnknown )
QGis::GeometryType geomType = ( QGis::GeometryType ) mTableModel.itemFromIndex( index.sibling( index.row(), QgsPgTableModel::dbtmType ) )->data( Qt::UserRole + 2 ).toInt();
if ( geomType == QGis::UnknownGeometry )
// no geometry type selected
return QString::null;

@@ -433,7 +430,7 @@ QString QgsPgSourceSelect::layerURI( const QModelIndex &index )
QString sql = mTableModel.itemFromIndex( index.sibling( index.row(), QgsPgTableModel::dbtmSql ) )->text();

QgsDataSourceURI uri( m_connInfo );
uri.setDataSource( schemaName, tableName, geomType != QGis::WKBNoGeometry ? geomColumnName : QString::null, sql, pkColumnName );
uri.setDataSource( schemaName, tableName, geomType != QGis::NoGeometry ? geomColumnName : QString::null, sql, pkColumnName );
uri.setUseEstimatedMetadata( mUseEstimatedMetadata );
uri.setGeometryType( geomType );
uri.setSrid( srid );
@@ -531,18 +528,9 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
mTableModel.addTableEntry( layer );
}

// Start the thread that gets the geometry type for relations that
// may take a long time to return
if ( mColumnTypeThread )
{
connect( mColumnTypeThread, SIGNAL( setLayerType( QgsPostgresLayerProperty ) ),
this, SLOT( setLayerType( QgsPostgresLayerProperty ) ) );
connect( mColumnTypeThread, SIGNAL( finished() ),
this, SLOT( columnThreadFinished() ) );

btnConnect->setText( tr( "Stop" ) );

// Do it in a thread.
mColumnTypeThread->start();
}
}
@@ -634,10 +622,19 @@ void QgsPgSourceSelect::addSearchGeometryColumn( QgsPostgresLayerProperty layerP
QgsPostgresConn *conn = QgsPostgresConn::connectDb( m_connInfo, true /* readonly */ );
if ( conn )
{

mColumnTypeThread = new QgsGeomColumnTypeThread( conn, mUseEstimatedMetadata );

connect( mColumnTypeThread, SIGNAL( setLayerType( QgsPostgresLayerProperty ) ),
this, SLOT( setLayerType( QgsPostgresLayerProperty ) ) );
connect( this, SIGNAL( addGeometryColumn( QgsPostgresLayerProperty ) ),
mColumnTypeThread, SLOT( addGeometryColumn( QgsPostgresLayerProperty ) ) );
connect( mColumnTypeThread, SIGNAL( finished() ),
this, SLOT( columnThreadFinished() ) );
}
}
mColumnTypeThread->addGeometryColumn( layerProperty );

emit addGeometryColumn( layerProperty );
}

QString QgsPgSourceSelect::fullDescription( QString schema, QString table,
@@ -78,6 +78,7 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
signals:
void addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey );
void connectionsChanged();
void addGeometryColumn( QgsPostgresLayerProperty );

public slots:
//! Determines the tables the user selected and closes the dialog
@@ -67,21 +67,21 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), schemaItem );
}

QGis::WkbType wkbType = qgisTypeFromDbType( layerProperty.type );
if ( wkbType == QGis::WKBUnknown && layerProperty.geometryColName.isEmpty() )
QGis::GeometryType geomType = QgsPostgresConn::geomTypeFromPostgis( layerProperty.type );
if ( geomType == QGis::UnknownGeometry && layerProperty.geometryColName.isEmpty() )
{
wkbType = QGis::WKBNoGeometry;
geomType = QGis::NoGeometry;
}

QList<QStandardItem*> childItemList;

QStandardItem *schemaNameItem = new QStandardItem( layerProperty.schemaName );
schemaNameItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );

QStandardItem *typeItem = new QStandardItem( iconForType( wkbType ), wkbType == QGis::WKBUnknown ? tr( "Waiting..." ) : displayStringForType( wkbType ) );
typeItem->setData( wkbType == QGis::WKBUnknown, Qt::UserRole + 1 );
typeItem->setData( wkbType, Qt::UserRole + 2 );
typeItem->setFlags(( wkbType != QGis::WKBUnknown ? Qt::ItemIsEnabled : Qt::NoItemFlags ) | Qt::ItemIsSelectable | Qt::ItemIsEditable );
QStandardItem *typeItem = new QStandardItem( iconForGeomType( geomType ), geomType == QGis::UnknownGeometry ? tr( "Waiting..." ) : QgsPostgresConn::displayStringForGeomType( geomType ) );
typeItem->setData( geomType == QGis::UnknownGeometry, Qt::UserRole + 1 );
typeItem->setData( geomType, Qt::UserRole + 2 );
typeItem->setFlags(( geomType != QGis::UnknownGeometry ? Qt::ItemIsEnabled : Qt::NoItemFlags ) | Qt::ItemIsSelectable | Qt::ItemIsEditable );

QStandardItem *tableItem = new QStandardItem( layerProperty.tableName );
tableItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
@@ -245,12 +245,12 @@ void QgsPgTableModel::setGeometryTypesForTable( QgsPostgresLayerProperty layerPr
else
{
// update existing row
QGis::WkbType wkbType = qgisTypeFromDbType( typeList.at( 0 ) );
QGis::GeometryType geomType = QgsPostgresConn::geomTypeFromPostgis( typeList.at( 0 ) );

typeItem->setIcon( iconForType( wkbType ) );
typeItem->setText( displayStringForType( wkbType ) );
typeItem->setIcon( iconForGeomType( geomType ) );
typeItem->setText( QgsPostgresConn::displayStringForGeomType( geomType ) );
typeItem->setData( false, Qt::UserRole + 1 );
typeItem->setData( wkbType, Qt::UserRole + 2 );
typeItem->setData( geomType, Qt::UserRole + 2 );

for ( int j = 1; j < typeList.size(); j++ )
{
@@ -264,96 +264,19 @@ void QgsPgTableModel::setGeometryTypesForTable( QgsPostgresLayerProperty layerPr
}
}

QIcon QgsPgTableModel::iconForType( QGis::WkbType type )
QIcon QgsPgTableModel::iconForGeomType( QGis::GeometryType type )
{
if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D || type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
switch ( type )
{
return QIcon( QgsDataItem::getThemePixmap( "/mIconPointLayer.png" ) );
}
else if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D || type == QGis::WKBMultiLineString || type == QGis::WKBMultiLineString25D )
{
return QIcon( QgsDataItem::getThemePixmap( "/mIconLineLayer.png" ) );
}
else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D || type == QGis::WKBMultiPolygon || type == QGis::WKBMultiPolygon25D )
{
return QIcon( QgsDataItem::getThemePixmap( "/mIconPolygonLayer.png" ) );
}
else if ( type == QGis::WKBNoGeometry )
{
return QIcon( QgsDataItem::getThemePixmap( "/mIconTableLayer.png" ) );
}
else
{
return QIcon( QgsDataItem::getThemePixmap( "/mIconLayer.png" ) );
}
}

QString QgsPgTableModel::displayStringForType( QGis::WkbType type )
{
if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D )
{
return tr( "Point" );
}
else if ( type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
{
return tr( "Multipoint" );
}
else if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D )
{
return tr( "Line" );
}
else if ( type == QGis::WKBMultiLineString || type == QGis::WKBMultiLineString25D )
{
return tr( "Multiline" );
}
else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D )
{
return tr( "Polygon" );
}
else if ( type == QGis::WKBMultiPolygon || type == QGis::WKBMultiPolygon25D )
{
return tr( "Multipolygon" );
}
else if ( type == QGis::WKBNoGeometry )
{
return tr( "No Geometry" );
}
else
{
return tr( "Unknown" );
}
}

QGis::WkbType QgsPgTableModel::qgisTypeFromDbType( QString dbType )
{
dbType = dbType.toUpper();

if ( dbType == "POINT" )
{
return QGis::WKBPoint;
}
else if ( dbType == "MULTIPOINT" )
{
return QGis::WKBMultiPoint;
}
else if ( dbType == "LINESTRING" )
{
return QGis::WKBLineString;
}
else if ( dbType == "MULTILINESTRING" )
{
return QGis::WKBMultiLineString;
}
else if ( dbType == "POLYGON" )
{
return QGis::WKBPolygon;
}
else if ( dbType == "MULTIPOLYGON" )
{
return QGis::WKBMultiPolygon;
}
else
{
return QGis::WKBUnknown;
case QGis::Point:
return QIcon( QgsDataItem::getThemePixmap( "/mIconPointLayer.png" ) );
case QGis::Line:
return QIcon( QgsDataItem::getThemePixmap( "/mIconLineLayer.png" ) );
case QGis::Polygon:
return QIcon( QgsDataItem::getThemePixmap( "/mIconPolygonLayer.png" ) );
case QGis::NoGeometry:
return QIcon( QgsDataItem::getThemePixmap( "/mIconTableLayer.png" ) );
default:
return QIcon( QgsDataItem::getThemePixmap( "/mIconLayer.png" ) );
}
}
@@ -58,9 +58,7 @@ class QgsPgTableModel : public QStandardItemModel
dbtmColumns
};

static QIcon iconForType( QGis::WkbType type );
static QString displayStringForType( QGis::WkbType type );
static QGis::WkbType qgisTypeFromDbType( QString dbType );
static QIcon iconForGeomType( QGis::GeometryType type );

private:
/**Number of tables in the model*/

0 comments on commit 2b739d9

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