00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "qgsdbtablemodel.h"
00019 #include "qgsapplication.h"
00020 #include "qgisapp.h"
00021
00022 QgsDbTableModel::QgsDbTableModel(): QStandardItemModel(), mTableCount( 0 )
00023 {
00024 QStringList headerLabels;
00025 headerLabels << tr( "Schema" );
00026 headerLabels << tr( "Table" );
00027 headerLabels << tr( "Type" );
00028 headerLabels << tr( "Geometry column" );
00029 headerLabels << tr( "Sql" );
00030 setHorizontalHeaderLabels( headerLabels );
00031 }
00032
00033 QgsDbTableModel::~QgsDbTableModel()
00034 {
00035
00036 }
00037
00038 void QgsDbTableModel::addTableEntry( QString type, QString schemaName, QString tableName, QString geometryColName, QString sql )
00039 {
00040
00041 QStandardItem* schemaItem;
00042 QList<QStandardItem*> schemaItems = findItems( schemaName, Qt::MatchExactly, 0 );
00043
00044
00045 if ( schemaItems.size() > 0 )
00046 {
00047 schemaItem = schemaItems.at( 0 );
00048 }
00049 else
00050 {
00051 schemaItem = new QStandardItem( schemaName );
00052 schemaItem->setFlags( Qt::ItemIsEnabled );
00053 invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), schemaItem );
00054 }
00055
00056
00057 QString typeName;
00058
00059 QGis::WkbType wkbType = qgisTypeFromDbType( type );
00060 QIcon iconFile = iconForType( wkbType );
00061
00062 QList<QStandardItem*> childItemList;
00063 QStandardItem* schemaNameItem = new QStandardItem( schemaName );
00064 schemaNameItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
00065 QStandardItem* typeItem = new QStandardItem( QIcon( iconFile ), type );
00066 typeItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
00067 QStandardItem* tableItem = new QStandardItem( tableName );
00068 tableItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
00069 QStandardItem* geomItem = new QStandardItem( geometryColName );
00070 geomItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
00071 QStandardItem* sqlItem = new QStandardItem( sql );
00072 sqlItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
00073
00074
00075 childItemList.push_back( schemaNameItem );
00076 childItemList.push_back( tableItem );
00077 childItemList.push_back( typeItem );
00078 childItemList.push_back( geomItem );
00079 childItemList.push_back( sqlItem );
00080
00081 schemaItem->appendRow( childItemList );
00082 ++mTableCount;
00083 }
00084
00085 void QgsDbTableModel::setSql( const QModelIndex& index, const QString& sql )
00086 {
00087 if ( !index.isValid() || !index.parent().isValid() )
00088 {
00089 return;
00090 }
00091
00092
00093 QModelIndex schemaSibling = index.sibling( index.row(), 0 );
00094 QModelIndex tableSibling = index.sibling( index.row(), 1 );
00095 QModelIndex geomSibling = index.sibling( index.row(), 3 );
00096
00097 if ( !schemaSibling.isValid() || !tableSibling.isValid() || !geomSibling.isValid() )
00098 {
00099 return;
00100 }
00101
00102 QString schemaName = itemFromIndex( schemaSibling )->text();
00103 QString tableName = itemFromIndex( tableSibling )->text();
00104 QString geomName = itemFromIndex( geomSibling )->text();
00105
00106 QList<QStandardItem*> schemaItems = findItems( schemaName, Qt::MatchExactly, 0 );
00107 if ( schemaItems.size() < 1 )
00108 {
00109 return;
00110 }
00111
00112 QStandardItem* schemaItem = schemaItems.at( 0 );
00113 int numChildren = schemaItem->rowCount();
00114
00115 QModelIndex currentChildIndex;
00116 QModelIndex currentTableIndex;
00117 QModelIndex currentGeomIndex;
00118
00119 for ( int i = 0; i < numChildren; ++i )
00120 {
00121 currentChildIndex = indexFromItem( schemaItem->child( i, 0 ) );
00122 if ( !currentChildIndex.isValid() )
00123 {
00124 continue;
00125 }
00126 currentTableIndex = currentChildIndex.sibling( i, 1 );
00127 if ( !currentTableIndex.isValid() )
00128 {
00129 continue;
00130 }
00131
00132 currentGeomIndex = currentChildIndex.sibling( i, 3 );
00133 if ( !currentGeomIndex.isValid() )
00134 {
00135 continue;
00136 }
00137
00138 if ( itemFromIndex( currentTableIndex )->text() == tableName &&
00139 itemFromIndex( currentGeomIndex )->text() == geomName )
00140 {
00141 QModelIndex sqlIndex = currentChildIndex.sibling( i, 4 );
00142 if ( sqlIndex.isValid() )
00143 {
00144 itemFromIndex( sqlIndex )->setText( sql );
00145 break;
00146 }
00147 }
00148 }
00149 }
00150
00151 void QgsDbTableModel::setGeometryTypesForTable( const QString& schema, const QString& table, const QString& attribute, const QString& type )
00152 {
00153 bool typeIsEmpty = type.isEmpty();
00154 QStringList typeList = type.split( "," );
00155
00156
00157 QStandardItem* schemaItem;
00158 QList<QStandardItem*> schemaItems = findItems( schema, Qt::MatchExactly, 0 );
00159
00160 if ( schemaItems.size() < 1 )
00161 {
00162 return;
00163 }
00164 schemaItem = schemaItems.at( 0 );
00165 int numChildren = schemaItem->rowCount();
00166
00167 QModelIndex currentChildIndex;
00168 QModelIndex currentTableIndex;
00169 QModelIndex currentTypeIndex;
00170 QModelIndex currentGeomColumnIndex;
00171
00172 for ( int i = 0; i < numChildren; ++i )
00173 {
00174 currentChildIndex = indexFromItem( schemaItem->child( i, 0 ) );
00175 if ( !currentChildIndex.isValid() )
00176 {
00177 continue;
00178 }
00179 currentTableIndex = currentChildIndex.sibling( i, 1 );
00180 currentTypeIndex = currentChildIndex.sibling( i, 2 );
00181 currentGeomColumnIndex = currentChildIndex.sibling( i, 3 );
00182 QString geomColText = itemFromIndex( currentGeomColumnIndex )->text();
00183
00184 if ( !currentTypeIndex.isValid() || !currentTableIndex.isValid() || !currentGeomColumnIndex.isValid() )
00185 {
00186 continue;
00187 }
00188
00189 if ( itemFromIndex( currentTableIndex )->text() == table &&
00190 ( geomColText == attribute || geomColText.startsWith( attribute + " AS " ) ) )
00191 {
00192 if ( typeIsEmpty )
00193 {
00194 removeRow( i, indexFromItem( schemaItem ) );
00195 return;
00196 }
00197
00198 QGis::WkbType wkbType = qgisTypeFromDbType( typeList.at( 0 ) );
00199 QIcon myIcon = iconForType( wkbType );
00200 itemFromIndex( currentTypeIndex )->setText( typeList.at( 0 ) );
00201 itemFromIndex( currentTypeIndex )->setIcon( myIcon );
00202 if ( !geomColText.contains( " AS " ) )
00203 {
00204 itemFromIndex( currentGeomColumnIndex )->setText( geomColText + " AS " + typeList.at( 0 ) );
00205 }
00206
00207 for ( int j = 1; j < typeList.size(); ++j )
00208 {
00209
00210 addTableEntry( typeList.at( j ), schema, table, geomColText + " AS " + typeList.at( j ), "" );
00211 }
00212 }
00213 }
00214 }
00215
00216 QIcon QgsDbTableModel::iconForType( QGis::WkbType type ) const
00217 {
00218 if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D || type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
00219 {
00220 return QgisApp::getThemeIcon( "/mIconPointLayer.png" );
00221 }
00222 else if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D || type == QGis::WKBMultiLineString || type == QGis::WKBMultiLineString25D )
00223 {
00224 return QgisApp::getThemeIcon( "/mIconLineLayer.png" );
00225 }
00226 else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D || type == QGis::WKBMultiPolygon || type == QGis::WKBMultiPolygon25D )
00227 {
00228 return QgisApp::getThemeIcon( "/mIconPolygonLayer.png" );
00229 }
00230 else return QIcon();
00231 }
00232
00233 QString QgsDbTableModel::displayStringForType( QGis::WkbType type ) const
00234 {
00235 if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D )
00236 {
00237 return tr( "Point" );
00238 }
00239 else if ( type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
00240 {
00241 return tr( "Multipoint" );
00242 }
00243 else if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D )
00244 {
00245 return tr( "Line" );
00246 }
00247 else if ( type == QGis::WKBMultiLineString || type == QGis::WKBMultiLineString25D )
00248 {
00249 return tr( "Multiline" );
00250 }
00251 else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D )
00252 {
00253 return tr( "Polygon" );
00254 }
00255 else if ( type == QGis::WKBMultiPolygon || type == QGis::WKBMultiPolygon25D )
00256 {
00257 return tr( "Multipolygon" );
00258 }
00259 return "Unknown";
00260 }
00261
00262 QGis::WkbType QgsDbTableModel::qgisTypeFromDbType( const QString& dbType ) const
00263 {
00264 if ( dbType == "POINT" )
00265 {
00266 return QGis::WKBPoint;
00267 }
00268 else if ( dbType == "MULTIPOINT" )
00269 {
00270 return QGis::WKBMultiPoint;
00271 }
00272 else if ( dbType == "LINESTRING" )
00273 {
00274 return QGis::WKBLineString;
00275 }
00276 else if ( dbType == "MULTILINESTRING" )
00277 {
00278 return QGis::WKBMultiLineString;
00279 }
00280 else if ( dbType == "POLYGON" )
00281 {
00282 return QGis::WKBPolygon;
00283 }
00284 else if ( dbType == "MULTIPOLYGON" )
00285 {
00286 return QGis::WKBMultiPolygon;
00287 }
00288 return QGis::WKBUnknown;
00289 }