@@ -1,5 +1,5 @@
/* **************************************************************************
qgsdbtablemodel .cpp - description
qgspgtablemodel .cpp - description
-------------------
begin : Dec 2007
copyright : (C) 2007 by Marco Hugentobler
Expand All
@@ -15,90 +15,119 @@
* *
***************************************************************************/
#include " qgsdbtablemodel .h"
#include " qgspgtablemodel .h"
#include " qgsdataitem.h"
#include " qgslogger.h"
QgsDbTableModel::QgsDbTableModel (): QStandardItemModel(), mTableCount( 0 )
QgsPgTableModel::QgsPgTableModel ()
: QStandardItemModel()
, mTableCount( 0 )
{
QStringList headerLabels;
headerLabels << tr ( " Schema" );
headerLabels << tr ( " Table" );
headerLabels << tr ( " Type" );
headerLabels << tr ( " Geometry column" );
headerLabels << tr ( " SRID" );
headerLabels << tr ( " Primary key column" );
headerLabels << tr ( " Select at id" );
headerLabels << tr ( " Sql" );
setHorizontalHeaderLabels ( headerLabels );
}
QgsDbTableModel ::~QgsDbTableModel ()
QgsPgTableModel ::~QgsPgTableModel ()
{
}
void QgsDbTableModel ::addTableEntry ( QString type, QString schemaName, QString tableName, QString geometryColName, const QStringList &pkCols, QString sql )
void QgsPgTableModel ::addTableEntry ( QgsPostgresLayerProperty layerProperty )
{
// is there already a root item with the given scheme Name?
QgsDebugMsg ( QString ( " %1.%2.%3 type=%4 srid=%5 pk=%6 sql=%7" )
.arg ( layerProperty.schemaName )
.arg ( layerProperty.tableName )
.arg ( layerProperty.geometryColName )
.arg ( layerProperty.type )
.arg ( layerProperty.srid )
.arg ( layerProperty.pkCols .join ( " ," ) )
.arg ( layerProperty.sql ) );
// is there already a root item with the given scheme Name?
QStandardItem *schemaItem;
QList<QStandardItem*> schemaItems = findItems ( schemaName, Qt::MatchExactly, dbtmSchema );
QList<QStandardItem*> schemaItems = findItems ( layerProperty. schemaName , Qt::MatchExactly, dbtmSchema );
// there is already an item for this schema
// there is already an item for this schema
if ( schemaItems.size () > 0 )
{
schemaItem = schemaItems.at ( dbtmSchema );
}
else // create a new toplevel item for this schema
else
{
schemaItem = new QStandardItem ( schemaName );
// create a new toplevel item for this schema
schemaItem = new QStandardItem ( layerProperty.schemaName );
schemaItem->setFlags ( Qt::ItemIsEnabled );
invisibleRootItem ()->setChild ( invisibleRootItem ()->rowCount (), schemaItem );
}
// path to icon for specified type
QString typeName;
QGis::WkbType wkbType = qgisTypeFromDbType ( type ) ;
QIcon iconFile = iconForType ( wkbType );
QGis::WkbType wkbType = qgisTypeFromDbType ( layerProperty. type );
if ( wkbType == QGis::WKBUnknown && layerProperty. geometryColName . isEmpty () )
{
wkbType = QGis::WKBNoGeometry ;
}
QList<QStandardItem*> childItemList;
QStandardItem* schemaNameItem = new QStandardItem ( schemaName );
QStandardItem * schemaNameItem = new QStandardItem ( layerProperty. schemaName );
schemaNameItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QStandardItem* typeItem = new QStandardItem ( QIcon ( iconFile ), type );
typeItem->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* tableItem = new QStandardItem ( tableName );
QStandardItem * tableItem = new QStandardItem ( layerProperty. tableName );
tableItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QStandardItem* geomItem = new QStandardItem ( geometryColName );
QStandardItem * geomItem = new QStandardItem ( layerProperty. geometryColName );
geomItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QStandardItem* pkItem = new QStandardItem ( " " );
pkItem->setData ( pkCols );
QStandardItem *sridItem = new QStandardItem ( QString::number ( layerProperty.srid ) );
sridItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QString pkText, pkCol = " " ;
switch ( layerProperty.pkCols .size () )
{
case 0 : pkText = " " ; break ;
case 1 : pkText = layerProperty.pkCols [0 ]; pkCol = pkText; break ;
default : pkText = tr ( " Select..." ); break ;
}
QStandardItem *pkItem = new QStandardItem ( pkText );
pkItem->setData ( layerProperty.pkCols , Qt::UserRole + 1 );
pkItem->setData ( pkCol, Qt::UserRole + 2 );
pkItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
QStandardItem* selItem = new QStandardItem ( " " );
QStandardItem * selItem = new QStandardItem ( " " );
selItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
selItem->setCheckState ( Qt::Checked );
selItem->setToolTip ( tr ( " Disable 'Fast Access to Features at ID' capability to force keeping the attribute table in memory (e.g. in case of expensive views)." ) );
QStandardItem* sqlItem = new QStandardItem ( sql );
QStandardItem* sqlItem = new QStandardItem ( layerProperty. sql );
sqlItem->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
childItemList.push_back ( schemaNameItem );
childItemList.push_back ( tableItem );
childItemList.push_back ( typeItem );
childItemList.push_back ( geomItem );
childItemList.push_back ( pkItem );
childItemList.push_back ( selItem );
childItemList.push_back ( sqlItem );
childItemList << schemaNameItem;
childItemList << tableItem;
childItemList << typeItem;
childItemList << geomItem;
childItemList << sridItem;
childItemList << pkItem;
childItemList << selItem;
childItemList << sqlItem;
schemaItem->appendRow ( childItemList );
++mTableCount ;
}
void QgsDbTableModel ::setSql ( const QModelIndex &index, const QString &sql )
void QgsPgTableModel ::setSql ( const QModelIndex &index, const QString &sql )
{
if ( !index .isValid () || !index .parent ().isValid () )
{
Expand Down
Expand Up
@@ -126,26 +155,22 @@ void QgsDbTableModel::setSql( const QModelIndex &index, const QString &sql )
}
QStandardItem* schemaItem = schemaItems.at ( dbtmSchema );
int numChildren = schemaItem->rowCount ();
QModelIndex currentChildIndex;
QModelIndex currentTableIndex;
QModelIndex currentGeomIndex;
for ( int i = 0 ; i < numChildren; ++i )
int n = schemaItem->rowCount ();
for ( int i = 0 ; i < n; i++ )
{
currentChildIndex = indexFromItem ( schemaItem->child ( i, dbtmSchema ) );
QModelIndex currentChildIndex = indexFromItem ( schemaItem->child ( i, dbtmSchema ) );
if ( !currentChildIndex.isValid () )
{
continue ;
}
currentTableIndex = currentChildIndex.sibling ( i, dbtmTable );
QModelIndex currentTableIndex = currentChildIndex.sibling ( i, dbtmTable );
if ( !currentTableIndex.isValid () )
{
continue ;
}
currentGeomIndex = currentChildIndex.sibling ( i, dbtmGeomCol );
QModelIndex currentGeomIndex = currentChildIndex.sibling ( i, dbtmGeomCol );
if ( !currentGeomIndex.isValid () )
{
continue ;
Expand All
@@ -164,75 +189,82 @@ void QgsDbTableModel::setSql( const QModelIndex &index, const QString &sql )
}
}
void QgsDbTableModel ::setGeometryTypesForTable ( const QString& schema, const QString& table, const QString& attribute, const QString& type )
void QgsPgTableModel ::setGeometryTypesForTable ( QgsPostgresLayerProperty layerProperty )
{
bool typeIsEmpty = type.isEmpty (); // true means the table has no valid geometry entry and the item for this table should be removed
QStringList typeList = type.split ( " ," );
QStringList typeList = layerProperty.type .split ( " ," , QString::SkipEmptyParts );
// find schema item and table item
QStandardItem* schemaItem;
QList<QStandardItem*> schemaItems = findItems ( schema , Qt::MatchExactly, dbtmSchema );
QList<QStandardItem*> schemaItems = findItems ( layerProperty. schemaName , Qt::MatchExactly, dbtmSchema );
if ( schemaItems.size () < 1 )
{
return ;
}
schemaItem = schemaItems.at ( 0 );
int numChildren = schemaItem->rowCount ();
QModelIndex currentChildIndex;
QModelIndex currentTableIndex;
QModelIndex currentTypeIndex;
QModelIndex currentGeomColumnIndex;
QModelIndex currentPkColumnIndex;
schemaItem = schemaItems.at ( 0 );
for ( int i = 0 ; i < numChildren; ++i )
int n = schemaItem->rowCount ();
for ( int i = 0 ; i < n; i++ )
{
currentChildIndex = indexFromItem ( schemaItem->child ( i, dbtmSchema ) );
QModelIndex currentChildIndex = indexFromItem ( schemaItem->child ( i, dbtmSchema ) );
if ( !currentChildIndex.isValid () )
{
continue ;
}
currentTableIndex = currentChildIndex.sibling ( i, dbtmTable );
currentTypeIndex = currentChildIndex.sibling ( i, dbtmType );
currentGeomColumnIndex = currentChildIndex.sibling ( i, dbtmGeomCol );
currentPkColumnIndex = currentChildIndex.sibling ( i, dbtmPkCol );
QString geomColText = itemFromIndex ( currentGeomColumnIndex )->text ();
QStringList pkCols = itemFromIndex ( currentPkColumnIndex )->data ().toStringList ();
if ( !currentTypeIndex.isValid () || !currentTableIndex.isValid () || !currentGeomColumnIndex.isValid () )
QModelIndex currentTypeIndex = currentChildIndex.sibling ( i, dbtmType );
QModelIndex currentTableIndex = currentChildIndex.sibling ( i, dbtmTable );
QModelIndex currentGeomColumnIndex = currentChildIndex.sibling ( i, dbtmGeomCol );
QModelIndex currentPkColumnIndex = currentChildIndex.sibling ( i, dbtmPkCol );
QModelIndex currentSridIndex = currentChildIndex.sibling ( i, dbtmSrid );
if ( !currentTypeIndex.isValid ()
|| !currentTableIndex.isValid ()
|| !currentGeomColumnIndex.isValid ()
|| !currentPkColumnIndex.isValid ()
|| !currentSridIndex.isValid ()
)
{
continue ;
}
if ( itemFromIndex ( currentTableIndex )->text () == table &&
( geomColText == attribute || geomColText.startsWith ( attribute + " AS " ) ) )
QString tableText = itemFromIndex ( currentTableIndex )->text ();
QString geomColText = itemFromIndex ( currentGeomColumnIndex )->text ();
QStandardItem *typeItem = itemFromIndex ( currentTypeIndex );
QStandardItem *sridItem = itemFromIndex ( currentSridIndex );
if ( tableText == layerProperty.tableName && geomColText == layerProperty.geometryColName )
{
if ( typeIsEmpty )
{
removeRow ( i, indexFromItem ( schemaItem ) );
return ;
}
sridItem->setText ( QString::number ( layerProperty.srid ) );
QGis::WkbType wkbType = qgisTypeFromDbType ( typeList.at ( 0 ) );
QIcon myIcon = iconForType ( wkbType );
itemFromIndex ( currentTypeIndex )->setText ( typeList.at ( 0 ) ); // todo: add other rows
itemFromIndex ( currentTypeIndex )->setIcon ( myIcon );
if ( !geomColText.contains ( " AS " ) )
if ( typeList.isEmpty () )
{
itemFromIndex ( currentGeomColumnIndex ) ->setText ( geomColText + " AS " + typeList. at ( 0 ) );
typeItem ->setText ( tr ( " Select... " ) );
}
for ( int j = 1 ; j < typeList.size (); ++j )
else
{
// todo: add correct type
addTableEntry ( typeList.at ( j ), schema, table, geomColText + " AS " + typeList.at ( j ), pkCols, " " );
// update existing row
QGis::WkbType wkbType = qgisTypeFromDbType ( typeList.at ( 0 ) );
typeItem->setIcon ( iconForType ( wkbType ) );
typeItem->setText ( displayStringForType ( wkbType ) );
typeItem->setData ( false , Qt::UserRole + 1 );
typeItem->setData ( wkbType, Qt::UserRole + 2 );
for ( int j = 1 ; j < typeList.size (); j++ )
{
layerProperty.type = typeList[j];
addTableEntry ( layerProperty );
}
}
typeItem->setFlags ( typeItem->flags () | Qt::ItemIsEnabled );
}
}
}
QIcon QgsDbTableModel ::iconForType ( QGis::WkbType type ) const
QIcon QgsPgTableModel ::iconForType ( QGis::WkbType type )
{
if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D || type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
{
Expand All
@@ -246,10 +278,17 @@ QIcon QgsDbTableModel::iconForType( QGis::WkbType type ) const
{
return QIcon ( QgsDataItem::getThemePixmap ( " /mIconPolygonLayer.png" ) );
}
else return QIcon ();
else if ( type == QGis::WKBNoGeometry )
{
return QIcon ( QgsDataItem::getThemePixmap ( " /mIconTableLayer.png" ) );
}
else
{
return QIcon ( QgsDataItem::getThemePixmap ( " /mIconLayer.png" ) );
}
}
QString QgsDbTableModel ::displayStringForType ( QGis::WkbType type ) const
QString QgsPgTableModel ::displayStringForType ( QGis::WkbType type )
{
if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D )
{
Expand All
@@ -275,11 +314,20 @@ QString QgsDbTableModel::displayStringForType( QGis::WkbType type ) const
{
return tr ( " Multipolygon" );
}
return " Unknown" ;
else if ( type == QGis::WKBNoGeometry )
{
return tr ( " No Geometry" );
}
else
{
return tr ( " Unknown" );
}
}
QGis::WkbType QgsDbTableModel ::qgisTypeFromDbType ( const QString& dbType ) const
QGis::WkbType QgsPgTableModel ::qgisTypeFromDbType ( QString dbType )
{
dbType = dbType.toUpper ();
if ( dbType == " POINT" )
{
return QGis::WKBPoint;
Expand All
@@ -304,5 +352,8 @@ QGis::WkbType QgsDbTableModel::qgisTypeFromDbType( const QString& dbType ) const
{
return QGis::WKBMultiPolygon;
}
return QGis::WKBUnknown;
else
{
return QGis::WKBUnknown;
}
}