|
| 1 | +#include "qgspostgresdataitems.h" |
| 2 | + |
| 3 | +#include "qgslogger.h" |
| 4 | + |
| 5 | +#include "qgspostgresconnection.h" |
| 6 | +#include "qgspgsourceselect.h" |
| 7 | +#include "qgspgnewconnection.h" |
| 8 | + |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +QgsPGConnectionItem::QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path ) |
| 11 | + : QgsDataCollectionItem( parent, name, path ) |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +QgsPGConnectionItem::~QgsPGConnectionItem() |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +QVector<QgsDataItem*> QgsPGConnectionItem::createChildren() |
| 20 | +{ |
| 21 | + QgsDebugMsg( "Entered" ); |
| 22 | + QVector<QgsDataItem*> children; |
| 23 | + QgsPostgresConnection connection( mName ); |
| 24 | + QgsPostgresProvider *pgProvider = connection.provider( ); |
| 25 | + if ( !pgProvider ) |
| 26 | + return children; |
| 27 | + |
| 28 | + QString mConnInfo = connection.connectionInfo(); |
| 29 | + QgsDebugMsg( "mConnInfo = " + mConnInfo ); |
| 30 | + |
| 31 | + if ( !pgProvider->supportedLayers( mLayerProperties, true, false, false ) ) |
| 32 | + return children; |
| 33 | + |
| 34 | + QMap<QString, QVector<QgsPostgresLayerProperty> > schemasMap; |
| 35 | + foreach( QgsPostgresLayerProperty layerProperty, mLayerProperties ) |
| 36 | + { |
| 37 | + schemasMap[ layerProperty.schemaName ].push_back( layerProperty ); |
| 38 | + } |
| 39 | + |
| 40 | + QMap<QString, QVector<QgsPostgresLayerProperty> >::const_iterator it = schemasMap.constBegin(); |
| 41 | + for ( ; it != schemasMap.constEnd(); it++ ) |
| 42 | + { |
| 43 | + QgsDebugMsg( "schema: " + it.key() ); |
| 44 | + QgsPGSchemaItem * schema = new QgsPGSchemaItem( this, it.key(), mPath + "/" + it.key(), mConnInfo, it.value() ); |
| 45 | + |
| 46 | + children.append( schema ); |
| 47 | + } |
| 48 | + return children; |
| 49 | +} |
| 50 | + |
| 51 | +bool QgsPGConnectionItem::equal( const QgsDataItem *other ) |
| 52 | +{ |
| 53 | + if ( type() != other->type() ) |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + const QgsPGConnectionItem *o = dynamic_cast<const QgsPGConnectionItem *>( other ); |
| 58 | + return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo ); |
| 59 | +} |
| 60 | + |
| 61 | +QList<QAction*> QgsPGConnectionItem::actions() |
| 62 | +{ |
| 63 | + QList<QAction*> lst; |
| 64 | + |
| 65 | + QAction* actionEdit = new QAction( tr( "Edit..." ), this ); |
| 66 | + connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) ); |
| 67 | + lst.append( actionEdit ); |
| 68 | + |
| 69 | + QAction* actionDelete = new QAction( tr( "Delete" ), this ); |
| 70 | + connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) ); |
| 71 | + lst.append( actionDelete ); |
| 72 | + |
| 73 | + return lst; |
| 74 | +} |
| 75 | + |
| 76 | +void QgsPGConnectionItem::editConnection() |
| 77 | +{ |
| 78 | + QgsPgNewConnection nc( NULL, mName ); |
| 79 | + if ( nc.exec() ) |
| 80 | + { |
| 81 | + // the parent should be updated |
| 82 | + mParent->refresh(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void QgsPGConnectionItem::deleteConnection() |
| 87 | +{ |
| 88 | + QgsPgSourceSelect::deleteConnection( mName ); |
| 89 | + // the parent should be updated |
| 90 | + mParent->refresh(); |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +// --------------------------------------------------------------------------- |
| 95 | +QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty ) |
| 96 | + : QgsLayerItem( parent, name, path, QString(), layerType, "postgres" ), |
| 97 | + mConnInfo( connInfo ), |
| 98 | + mLayerProperty( layerProperty ) |
| 99 | +{ |
| 100 | + mUri = createUri(); |
| 101 | + mPopulated = true; |
| 102 | +} |
| 103 | + |
| 104 | +QgsPGLayerItem::~QgsPGLayerItem() |
| 105 | +{ |
| 106 | +} |
| 107 | + |
| 108 | +QString QgsPGLayerItem::createUri() |
| 109 | +{ |
| 110 | + QString pkColName = mLayerProperty.pkCols.size() > 0 ? mLayerProperty.pkCols.at( 0 ) : QString::null; |
| 111 | + QgsDataSourceURI uri( mConnInfo ); |
| 112 | + uri.setDataSource( mLayerProperty.schemaName, mLayerProperty.tableName, mLayerProperty.geometryColName, mLayerProperty.sql, pkColName ); |
| 113 | + return uri.uri(); |
| 114 | +} |
| 115 | + |
| 116 | +// --------------------------------------------------------------------------- |
| 117 | +QgsPGSchemaItem::QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties ) |
| 118 | + : QgsDataCollectionItem( parent, name, path ) |
| 119 | +{ |
| 120 | + mIcon = QIcon( getThemePixmap( "mIconNamespace.png" ) ); |
| 121 | + |
| 122 | + // Populate everything, it costs nothing, all info about layers is collected |
| 123 | + foreach( QgsPostgresLayerProperty layerProperty, layerProperties ) |
| 124 | + { |
| 125 | + QgsDebugMsg( "table: " + layerProperty.schemaName + "." + layerProperty.tableName ); |
| 126 | + |
| 127 | + QgsLayerItem::LayerType layerType = QgsLayerItem::NoType; |
| 128 | + if ( layerProperty.type.contains( "POINT" ) ) |
| 129 | + { |
| 130 | + layerType = QgsLayerItem::Point; |
| 131 | + } |
| 132 | + else if ( layerProperty.type.contains( "LINE" ) ) |
| 133 | + { |
| 134 | + layerType = QgsLayerItem::Line; |
| 135 | + } |
| 136 | + else if ( layerProperty.type.contains( "POLYGON" ) ) |
| 137 | + { |
| 138 | + layerType = QgsLayerItem::Polygon; |
| 139 | + } |
| 140 | + else if ( layerProperty.type == QString::null ) |
| 141 | + { |
| 142 | + if ( layerProperty.geometryColName == QString::null ) |
| 143 | + { |
| 144 | + layerType = QgsLayerItem::TableLayer; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + QgsPGLayerItem * layer = new QgsPGLayerItem( this, layerProperty.tableName, mPath + "/" + layerProperty.tableName, connInfo, layerType, layerProperty ); |
| 149 | + mChildren.append( layer ); |
| 150 | + } |
| 151 | + |
| 152 | + mPopulated = true; |
| 153 | +} |
| 154 | + |
| 155 | +QgsPGSchemaItem::~QgsPGSchemaItem() |
| 156 | +{ |
| 157 | +} |
| 158 | + |
| 159 | +// --------------------------------------------------------------------------- |
| 160 | +QgsPGRootItem::QgsPGRootItem( QgsDataItem* parent, QString name, QString path ) |
| 161 | + : QgsDataCollectionItem( parent, name, path ) |
| 162 | +{ |
| 163 | + //mIcon = QIcon( getThemePixmap( "mIconPg.png" ) ); |
| 164 | + populate(); |
| 165 | +} |
| 166 | + |
| 167 | +QgsPGRootItem::~QgsPGRootItem() |
| 168 | +{ |
| 169 | +} |
| 170 | + |
| 171 | +QVector<QgsDataItem*>QgsPGRootItem::createChildren() |
| 172 | +{ |
| 173 | + QVector<QgsDataItem*> connections; |
| 174 | + foreach( QString connName, QgsPostgresConnection::connectionList() ) |
| 175 | + { |
| 176 | + QgsDataItem * conn = new QgsPGConnectionItem( this, connName, mPath + "/" + connName ); |
| 177 | + connections.push_back( conn ); |
| 178 | + } |
| 179 | + return connections; |
| 180 | +} |
| 181 | + |
| 182 | +QList<QAction*> QgsPGRootItem::actions() |
| 183 | +{ |
| 184 | + QList<QAction*> lst; |
| 185 | + |
| 186 | + QAction* actionNew = new QAction( tr( "New..." ), this ); |
| 187 | + connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) ); |
| 188 | + lst.append( actionNew ); |
| 189 | + |
| 190 | + return lst; |
| 191 | +} |
| 192 | + |
| 193 | +QWidget * QgsPGRootItem::paramWidget() |
| 194 | +{ |
| 195 | + QgsPgSourceSelect *select = new QgsPgSourceSelect( 0, 0, true, true ); |
| 196 | + connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) ); |
| 197 | + return select; |
| 198 | +} |
| 199 | +void QgsPGRootItem::connectionsChanged() |
| 200 | +{ |
| 201 | + refresh(); |
| 202 | +} |
| 203 | + |
| 204 | +void QgsPGRootItem::newConnection() |
| 205 | +{ |
| 206 | + QgsPgNewConnection nc( NULL ); |
| 207 | + if ( nc.exec() ) |
| 208 | + { |
| 209 | + refresh(); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +// --------------------------------------------------------------------------- |
| 214 | + |
| 215 | +QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem ) |
| 216 | +{ |
| 217 | + Q_UNUSED( thePath ); |
| 218 | + return new QgsPGRootItem( parentItem, "PostGIS", "pg:" ); |
| 219 | +} |
0 commit comments