Skip to content

Commit bd3f77a

Browse files
committedJul 12, 2011
add QgsDataItem to the postgres provider
1 parent 7647d82 commit bd3f77a

16 files changed

+290
-72
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ SET (QGIS_APP_MOC_HDRS
152152
qgscontinuouscolordialog.h
153153
qgscustomization.h
154154
qgscustomprojectiondialog.h
155-
qgsdbtablemodel.h
156155
qgsdelattrdialog.h
157156
qgsdisplayangle.h
158157
qgsembedlayerdialog.h
@@ -277,15 +276,6 @@ IF (POSTGRES_FOUND)
277276
IF(HAVE_PGCONFIG)
278277
ADD_DEFINITIONS(-DHAVE_PGCONFIG=1)
279278
ENDIF(HAVE_PGCONFIG)
280-
281-
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
282-
postgres/qgspgsourceselect.cpp
283-
postgres/qgspgnewconnection.cpp
284-
)
285-
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
286-
postgres/qgspgsourceselect.h
287-
postgres/qgspgnewconnection.h
288-
)
289279
ENDIF (POSTGRES_FOUND)
290280

291281
IF (HAVE_SPATIALITE)

‎src/app/qgisapp.cpp

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
// Conditional Includes
226226
//
227227
#ifdef HAVE_POSTGRESQL
228-
#include "postgres/qgspgsourceselect.h"
228+
#include "../providers/postgres/qgspgsourceselect.h"
229229
#ifdef HAVE_PGCONFIG
230230
#include <pg_config.h>
231231
#else
@@ -2322,58 +2322,75 @@ void QgisApp::addDatabaseLayer()
23222322
{
23232323
return;
23242324
}
2325+
// Fudge for now
2326+
QgsDebugMsg( "about to addRasterLayer" );
23252327

2326-
// only supports postgis layers at present
2327-
// show the postgis dialog
2328-
2329-
QgsPgSourceSelect *dbs = new QgsPgSourceSelect( this );
2328+
// TODO: QDialog for now, switch to QWidget in future
2329+
QDialog *pgs = dynamic_cast<QDialog*>( QgsProviderRegistry::instance()->selectWidget( QString( "postgres" ), this ) );
2330+
if ( !pgs )
2331+
{
2332+
QMessageBox::warning( this, tr( "PostgreSQL" ), tr( "Cannot get PostgreSQL select dialog from provider." ) );
2333+
return;
2334+
}
2335+
connect( pgs , SIGNAL( addDatabaseLayers( QStringList const &, QString const & ) ),
2336+
this , SLOT( addDatabaseLayers( QStringList const &, QString const & ) ) );
2337+
pgs->exec();
2338+
delete pgs;
2339+
} // QgisApp::addDatabaseLayer()
2340+
#endif
23302341

2331-
mMapCanvas->freeze();
2342+
void QgisApp::addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey )
2343+
{
2344+
if ( mMapCanvas && mMapCanvas->isDrawing() )
2345+
{
2346+
return;
2347+
}
23322348

2333-
if ( dbs->exec() )
2349+
if ( layerPathList.empty() )
23342350
{
2335-
// Let render() do its own cursor management
2336-
// QApplication::setOverrideCursor(Qt::WaitCursor);
2351+
// no layers to add so bail out, but
2352+
// allow mMapCanvas to handle events
2353+
// first
2354+
mMapCanvas->freeze( false );
2355+
return;
2356+
}
23372357

2358+
mMapCanvas->freeze( true );
23382359

2339-
// repaint the canvas if it was covered by the dialog
2360+
QApplication::setOverrideCursor(Qt::WaitCursor);
23402361

2341-
// add files to the map canvas
2342-
QStringList tables = dbs->selectedTables();
2362+
foreach( QString layerPath, layerPathList )
2363+
{
2364+
// create the layer
2365+
QgsDataSourceURI uri( layerPath );
23432366

2344-
QApplication::setOverrideCursor( Qt::WaitCursor );
2367+
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri(), uri.table(), providerKey );
2368+
Q_CHECK_PTR( layer );
23452369

2346-
// for each selected table, connect to the database, parse the Wkt geometry,
2347-
// and build a canvasitem for it
2348-
// readWKB(connectionInfo,tables);
2349-
QStringList::Iterator it = tables.begin();
2350-
while ( it != tables.end() )
2370+
if ( ! layer )
23512371
{
2352-
// create the layer
2353-
//qWarning("creating layer");
2354-
QgsDataSourceURI uri( *it );
2355-
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri(), uri.table(), "postgres" );
2356-
if ( layer->isValid() )
2357-
{
2358-
// register this layer with the central layers registry
2359-
QgsMapLayerRegistry::instance()->addMapLayer( layer );
2360-
}
2361-
else
2362-
{
2363-
QgsDebugMsg(( *it ) + " is an invalid layer - not loaded" );
2364-
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( *it ) );
2365-
delete layer;
2366-
}
2367-
//qWarning("incrementing iterator");
2368-
++it;
2369-
}
2372+
mMapCanvas->freeze( false );
2373+
QApplication::restoreOverrideCursor();
23702374

2371-
QApplication::restoreOverrideCursor();
2375+
// XXX insert meaningful whine to the user here
2376+
return;
2377+
}
23722378

2373-
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
2379+
if ( layer->isValid() )
2380+
{
2381+
// register this layer with the central layers registry
2382+
QgsMapLayerRegistry::instance()->addMapLayer( layer );
2383+
}
2384+
else
2385+
{
2386+
QgsDebugMsg(( layerPath ) + " is an invalid layer - not loaded" );
2387+
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( layerPath ) );
2388+
delete layer;
2389+
}
2390+
//qWarning("incrementing iterator");
23742391
}
23752392

2376-
delete dbs;
2393+
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
23772394

23782395
// update UI
23792396
qApp->processEvents();
@@ -2382,11 +2399,8 @@ void QgisApp::addDatabaseLayer()
23822399
mMapCanvas->freeze( false );
23832400
mMapCanvas->refresh();
23842401

2385-
// Let render() do its own cursor management
2386-
// QApplication::restoreOverrideCursor();
2387-
2388-
} // QgisApp::addDatabaseLayer()
2389-
#endif
2402+
QApplication::restoreOverrideCursor();
2403+
}
23902404

23912405

23922406
#ifndef HAVE_SPATIALITE

‎src/app/qgisapp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
460460
//! Add a databaselayer to the map
461461
void addDatabaseLayer();
462462
//#endif
463+
//! Add a list of database layers to the map
464+
void addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey );
463465
//#ifdef HAVE_SPATIALITE
464466
//! Add a SpatiaLite layer to the map
465467
void addSpatiaLiteLayer();

‎src/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ SET(QGIS_CORE_SRCS
4949
qgscoordinatetransform.cpp
5050
qgsdatasourceuri.cpp
5151
qgsdataitem.cpp
52+
qgsdbfilterproxymodel.cpp
53+
qgsdbtablemodel.cpp
5254
qgsdiagram.cpp
5355
qgsdiagramrendererv2.cpp
5456
qgsdistancearea.cpp
@@ -225,6 +227,7 @@ SET(QGIS_CORE_MOC_HDRS
225227
qgscontexthelp.h
226228
qgscoordinatetransform.h
227229
qgsdataitem.h
230+
qgsdbtablemodel.h
228231
qgsdataprovider.h
229232
qgshttptransaction.h
230233
qgsmaplayer.h
File renamed without changes.

‎src/app/qgsdbtablemodel.cpp renamed to ‎src/core/qgsdbtablemodel.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgsdbtablemodel.h"
19-
#include "qgsapplication.h"
20-
#include "qgisapp.h"
19+
#include "qgsdataitem.h"
2120

2221
QgsDbTableModel::QgsDbTableModel(): QStandardItemModel(), mTableCount( 0 )
2322
{
@@ -224,15 +223,15 @@ QIcon QgsDbTableModel::iconForType( QGis::WkbType type ) const
224223
{
225224
if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D || type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
226225
{
227-
return QgisApp::getThemeIcon( "/mIconPointLayer.png" );
226+
return QIcon( QgsDataItem::getThemePixmap( "/mIconPointLayer.png" ) );
228227
}
229228
else if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D || type == QGis::WKBMultiLineString || type == QGis::WKBMultiLineString25D )
230229
{
231-
return QgisApp::getThemeIcon( "/mIconLineLayer.png" );
230+
return QIcon( QgsDataItem::getThemePixmap( "/mIconLineLayer.png" ) );
232231
}
233232
else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D || type == QGis::WKBMultiPolygon || type == QGis::WKBMultiPolygon25D )
234233
{
235-
return QgisApp::getThemeIcon( "/mIconPolygonLayer.png" );
234+
return QIcon( QgsDataItem::getThemePixmap( "/mIconPolygonLayer.png" ) );
236235
}
237236
else return QIcon();
238237
}
File renamed without changes.

‎src/plugins/spit/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
SET (SPIT_SRCS
66
qgsspit.cpp
7-
../../app/postgres/qgspgnewconnection.cpp
7+
../../providers/postgres/qgspgnewconnection.cpp
88
qgspgutil.cpp
99
qgsshapefile.cpp
1010
)
@@ -29,7 +29,7 @@ SET (SPIT_UIS
2929
SET (SPIT_EXE_MOC_HDRS
3030
qgsspit.h
3131
qgsshapefile.h
32-
../../app/postgres/qgspgnewconnection.h
32+
../../providers/postgres/qgspgnewconnection.h
3333
)
3434

3535
SET (SPIT_PLUGIN_MOC_HDRS
@@ -57,7 +57,7 @@ INCLUDE_DIRECTORIES(
5757
../../gui
5858
../../ui
5959
../../app
60-
../../app/postgres
60+
../../providers/postgres
6161
..
6262
${POSTGRES_INCLUDE_DIR}
6363
${GDAL_INCLUDE_DIR}

‎src/providers/postgres/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
SET(PG_SRCS
66
qgspostgresprovider.cpp
77
qgspostgresconnection.cpp
8-
../../app/postgres/qgspgsourceselect.cpp
8+
qgspgsourceselect.cpp
9+
qgspgnewconnection.cpp
910
)
1011
SET(PG_MOC_HDRS
1112
qgspostgresprovider.h
1213
qgspostgresconnection.h
13-
../../app/postgres/qgspgsourceselect.h
14+
qgspgsourceselect.h
15+
qgspgnewconnection.h
1416
)
1517

1618

@@ -26,7 +28,6 @@ INCLUDE_DIRECTORIES(
2628
../../core
2729
../../gui
2830
../../app
29-
../../app/postgres
3031
${CMAKE_CURRENT_BINARY_DIR}/../../ui
3132
)
3233

‎src/app/postgres/qgspgsourceselect.h renamed to ‎src/providers/postgres/qgspgsourceselect.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
116116
//! Connection info (database, host, user, password)
117117
QString connectionInfo();
118118

119+
signals:
120+
void addDatabaseLayers( QStringList const & layerPathList,
121+
QString const & providerKey );
122+
void connectionsChanged();
123+
119124
public slots:
120125
//! Determines the tables the user selected and closes the dialog
121126
void addTables();
@@ -155,12 +160,6 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
155160
typedef QPair<QString, QString> geomPair;
156161
typedef QList<geomPair> geomCol;
157162

158-
/**Inserts information about the spatial tables into mTableModel*/
159-
bool getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables );
160-
161-
/** get primary key candidates (all int4 columns) */
162-
QStringList pkCandidates( PGconn *pg, QString schemaName, QString viewName );
163-
164163
// queue another query for the thread
165164
void addSearchGeometryColumn( const QString &schema, const QString &table, const QString &column );
166165

@@ -180,7 +179,6 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
180179
bool mUseEstimatedMetadata;
181180
// Storage for the range of layer type icons
182181
QMap<QString, QPair<QString, QIcon> > mLayerIcons;
183-
PGconn *pd;
184182

185183
//! Model that acts as datasource for mTableTreeWidget
186184
QgsDbTableModel mTableModel;

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,3 +3959,161 @@ QGISEXTERN int dataCapabilities()
39593959
{
39603960
return QgsDataProvider::Database;
39613961
}
3962+
// ---------------------------------------------------------------------------
3963+
QgsPGConnectionItem::QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path )
3964+
: QgsDataCollectionItem( parent, name, path )
3965+
{
3966+
}
3967+
3968+
QgsPGConnectionItem::~QgsPGConnectionItem()
3969+
{
3970+
}
3971+
3972+
QVector<QgsDataItem*> QgsPGConnectionItem::createChildren()
3973+
{
3974+
QgsDebugMsg( "Entered" );
3975+
QVector<QgsDataItem*> children;
3976+
QgsPostgresConnection connection( mName );
3977+
QgsPostgresProvider *pgProvider = connection.provider( );
3978+
if ( !pgProvider )
3979+
return children;
3980+
3981+
QString mConnInfo = connection.connectionInfo();
3982+
QgsDebugMsg( "mConnInfo = " + mConnInfo );
3983+
3984+
if ( !pgProvider->supportedLayers( mLayerProperties, true, false, false ) )
3985+
return children;
3986+
3987+
QMap<QString, QVector<QgsPostgresLayerProperty> > schemasMap;
3988+
foreach( QgsPostgresLayerProperty layerProperty, mLayerProperties )
3989+
{
3990+
schemasMap[ layerProperty.schemaName ].push_back( layerProperty );
3991+
}
3992+
3993+
QMap<QString, QVector<QgsPostgresLayerProperty> >::const_iterator it = schemasMap.constBegin();
3994+
for( ; it != schemasMap.constEnd(); it++ )
3995+
{
3996+
QgsDebugMsg( "schema: " + it.key() );
3997+
QgsPGSchemaItem * schema = new QgsPGSchemaItem( this, it.key(), mPath + "/" + it.key(), mConnInfo, it.value() );
3998+
3999+
children.append( schema );
4000+
}
4001+
return children;
4002+
}
4003+
4004+
bool QgsPGConnectionItem::equal( const QgsDataItem *other )
4005+
{
4006+
if ( type() != other->type() )
4007+
{
4008+
return false;
4009+
}
4010+
const QgsPGConnectionItem *o = dynamic_cast<const QgsPGConnectionItem *>( other );
4011+
return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo );
4012+
}
4013+
// ---------------------------------------------------------------------------
4014+
QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty )
4015+
: QgsLayerItem( parent, name, path, QString(), layerType, "postgres" ),
4016+
mConnInfo( connInfo ),
4017+
mLayerProperty( layerProperty )
4018+
{
4019+
mUri = createUri();
4020+
mPopulated = true;
4021+
}
4022+
4023+
QgsPGLayerItem::~QgsPGLayerItem()
4024+
{
4025+
}
4026+
4027+
QString QgsPGLayerItem::createUri()
4028+
{
4029+
QString pkColName = mLayerProperty.pkCols.size() > 0 ? mLayerProperty.pkCols.at(0) : QString::null;
4030+
QgsDataSourceURI uri( mConnInfo );
4031+
uri.setDataSource( mLayerProperty.schemaName, mLayerProperty.tableName, mLayerProperty.geometryColName, mLayerProperty.sql, pkColName);
4032+
return uri.uri();
4033+
}
4034+
4035+
// ---------------------------------------------------------------------------
4036+
QgsPGSchemaItem::QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties )
4037+
: QgsDataCollectionItem( parent, name, path )
4038+
{
4039+
mIcon = QIcon( getThemePixmap( "mIconNamespace.png" ) );
4040+
4041+
// Populate everything, it costs nothing, all info about layers is collected
4042+
foreach( QgsPostgresLayerProperty layerProperty, layerProperties )
4043+
{
4044+
QgsDebugMsg( "table: " + layerProperty.schemaName + "." + layerProperty.tableName );
4045+
4046+
QgsLayerItem::LayerType layerType = QgsLayerItem::NoType;
4047+
if ( layerProperty.type.contains( "POINT" ) )
4048+
{
4049+
layerType = QgsLayerItem::Point;
4050+
}
4051+
else if ( layerProperty.type.contains( "LINE" ) )
4052+
{
4053+
layerType = QgsLayerItem::Line;
4054+
}
4055+
else if ( layerProperty.type.contains( "POLYGON" ) )
4056+
{
4057+
layerType = QgsLayerItem::Polygon;
4058+
}
4059+
else if ( layerProperty.type == QString::null )
4060+
{
4061+
if ( layerProperty.geometryColName == QString::null )
4062+
{
4063+
layerType = QgsLayerItem::TableLayer;
4064+
}
4065+
}
4066+
4067+
QgsPGLayerItem * layer = new QgsPGLayerItem( this, layerProperty.tableName, mPath + "/" + layerProperty.tableName, connInfo, layerType, layerProperty );
4068+
mChildren.append( layer );
4069+
}
4070+
4071+
mPopulated = true;
4072+
}
4073+
4074+
QgsPGSchemaItem::~QgsPGSchemaItem()
4075+
{
4076+
}
4077+
4078+
// ---------------------------------------------------------------------------
4079+
QgsPGRootItem::QgsPGRootItem( QgsDataItem* parent, QString name, QString path )
4080+
: QgsDataCollectionItem( parent, name, path )
4081+
{
4082+
//mIcon = QIcon( getThemePixmap( "mIconPg.png" ) );
4083+
populate();
4084+
}
4085+
4086+
QgsPGRootItem::~QgsPGRootItem()
4087+
{
4088+
}
4089+
4090+
QVector<QgsDataItem*>QgsPGRootItem::createChildren()
4091+
{
4092+
QVector<QgsDataItem*> connections;
4093+
foreach( QString connName, QgsPostgresConnection::connectionList() )
4094+
{
4095+
QgsDataItem * conn = new QgsPGConnectionItem( this, connName, mPath + "/" + connName );
4096+
connections.push_back( conn );
4097+
}
4098+
return connections;
4099+
}
4100+
4101+
QWidget * QgsPGRootItem::paramWidget()
4102+
{
4103+
QgsPgSourceSelect *select = new QgsPgSourceSelect( 0, 0 );
4104+
//connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) );
4105+
return select;
4106+
}
4107+
void QgsPGRootItem::connectionsChanged()
4108+
{
4109+
refresh();
4110+
}
4111+
4112+
// ---------------------------------------------------------------------------
4113+
4114+
QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
4115+
{
4116+
QgsPGRootItem * root = new QgsPGRootItem( parentItem, "Postgres/PostGIS", "pg:" );
4117+
4118+
return root;
4119+
}

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,4 +747,57 @@ class QgsPostgresProvider : public QgsVectorDataProvider
747747
QString mPrimaryKeyDefault;
748748
};
749749

750+
class QgsPGConnectionItem : public QgsDataCollectionItem
751+
{
752+
public:
753+
QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path );
754+
~QgsPGConnectionItem();
755+
756+
QVector<QgsDataItem*> createChildren();
757+
virtual bool equal( const QgsDataItem *other );
758+
759+
QString mConnInfo;
760+
QVector<QgsPostgresLayerProperty> mLayerProperties;
761+
};
762+
763+
// WMS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem
764+
// We have to use QgsDataCollectionItem and support layer methods if necessary
765+
class QgsPGLayerItem : public QgsLayerItem
766+
{
767+
Q_OBJECT
768+
public:
769+
QgsPGLayerItem( QgsDataItem* parent, QString name, QString path,
770+
QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperties );
771+
~QgsPGLayerItem();
772+
773+
QString createUri();
774+
775+
QString mConnInfo;
776+
QgsPostgresLayerProperty mLayerProperty;
777+
};
778+
779+
class QgsPGSchemaItem : public QgsDataCollectionItem
780+
{
781+
Q_OBJECT
782+
public:
783+
QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path,
784+
QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties );
785+
~QgsPGSchemaItem();
786+
};
787+
788+
class QgsPGRootItem : public QgsDataCollectionItem
789+
{
790+
Q_OBJECT
791+
public:
792+
QgsPGRootItem( QgsDataItem* parent, QString name, QString path );
793+
~QgsPGRootItem();
794+
795+
QVector<QgsDataItem*> createChildren();
796+
797+
virtual QWidget * paramWidget();
798+
799+
public slots:
800+
void connectionsChanged();
801+
};
802+
750803
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.