Skip to content

Commit 49e4e99

Browse files
committed
[FEATURE] PostGIS connections and layer now available from browser
Merge of one part of Giuseppe's Summer of Code work
2 parents cfedf09 + f932140 commit 49e4e99

18 files changed

+991
-506
lines changed

src/app/CMakeLists.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ SET(QGIS_APP_SRCS
1919
qgsconfigureshortcutsdialog.cpp
2020
qgscustomization.cpp
2121
qgscustomprojectiondialog.cpp
22-
qgsdbfilterproxymodel.cpp
23-
qgsdbtablemodel.cpp
2422
qgsdecorationcopyright.cpp
2523
qgsdecorationcopyrightdialog.cpp
2624
qgsdecorationnortharrow.cpp
@@ -158,7 +156,6 @@ SET (QGIS_APP_MOC_HDRS
158156
qgscontinuouscolordialog.h
159157
qgscustomization.h
160158
qgscustomprojectiondialog.h
161-
qgsdbtablemodel.h
162159
qgsdecorationcopyright.h
163160
qgsdecorationcopyrightdialog.h
164161
qgsdecorationnortharrow.h
@@ -322,15 +319,6 @@ IF (POSTGRES_FOUND)
322319
IF(HAVE_PGCONFIG)
323320
ADD_DEFINITIONS(-DHAVE_PGCONFIG=1)
324321
ENDIF(HAVE_PGCONFIG)
325-
326-
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
327-
postgres/qgspgsourceselect.cpp
328-
postgres/qgspgnewconnection.cpp
329-
)
330-
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
331-
postgres/qgspgsourceselect.h
332-
postgres/qgspgnewconnection.h
333-
)
334322
ENDIF (POSTGRES_FOUND)
335323

336324
IF (HAVE_SPATIALITE)

src/app/qgisapp.cpp

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
// Conditional Includes
229229
//
230230
#ifdef HAVE_POSTGRESQL
231-
#include "postgres/qgspgsourceselect.h"
231+
#include "../providers/postgres/qgspgsourceselect.h"
232232
#ifdef HAVE_PGCONFIG
233233
#include <pg_config.h>
234234
#else
@@ -2363,58 +2363,75 @@ void QgisApp::addDatabaseLayer()
23632363
{
23642364
return;
23652365
}
2366+
// Fudge for now
2367+
QgsDebugMsg( "about to addRasterLayer" );
23662368

2367-
// only supports postgis layers at present
2368-
// show the postgis dialog
2369-
2370-
QgsPgSourceSelect *dbs = new QgsPgSourceSelect( this );
2369+
// TODO: QDialog for now, switch to QWidget in future
2370+
QDialog *pgs = dynamic_cast<QDialog*>( QgsProviderRegistry::instance()->selectWidget( QString( "postgres" ), this ) );
2371+
if ( !pgs )
2372+
{
2373+
QMessageBox::warning( this, tr( "PostgreSQL" ), tr( "Cannot get PostgreSQL select dialog from provider." ) );
2374+
return;
2375+
}
2376+
connect( pgs , SIGNAL( addDatabaseLayers( QStringList const &, QString const & ) ),
2377+
this , SLOT( addDatabaseLayers( QStringList const &, QString const & ) ) );
2378+
pgs->exec();
2379+
delete pgs;
2380+
} // QgisApp::addDatabaseLayer()
2381+
#endif
23712382

2372-
mMapCanvas->freeze();
2383+
void QgisApp::addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey )
2384+
{
2385+
if ( mMapCanvas && mMapCanvas->isDrawing() )
2386+
{
2387+
return;
2388+
}
23732389

2374-
if ( dbs->exec() )
2390+
if ( layerPathList.empty() )
23752391
{
2376-
// Let render() do its own cursor management
2377-
// QApplication::setOverrideCursor(Qt::WaitCursor);
2392+
// no layers to add so bail out, but
2393+
// allow mMapCanvas to handle events
2394+
// first
2395+
mMapCanvas->freeze( false );
2396+
return;
2397+
}
23782398

2399+
mMapCanvas->freeze( true );
23792400

2380-
// repaint the canvas if it was covered by the dialog
2401+
QApplication::setOverrideCursor(Qt::WaitCursor);
23812402

2382-
// add files to the map canvas
2383-
QStringList tables = dbs->selectedTables();
2403+
foreach( QString layerPath, layerPathList )
2404+
{
2405+
// create the layer
2406+
QgsDataSourceURI uri( layerPath );
23842407

2385-
QApplication::setOverrideCursor( Qt::WaitCursor );
2408+
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri(), uri.table(), providerKey );
2409+
Q_CHECK_PTR( layer );
23862410

2387-
// for each selected table, connect to the database, parse the Wkt geometry,
2388-
// and build a canvasitem for it
2389-
// readWKB(connectionInfo,tables);
2390-
QStringList::Iterator it = tables.begin();
2391-
while ( it != tables.end() )
2411+
if ( ! layer )
23922412
{
2393-
// create the layer
2394-
//qWarning("creating layer");
2395-
QgsDataSourceURI uri( *it );
2396-
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri(), uri.table(), "postgres" );
2397-
if ( layer->isValid() )
2398-
{
2399-
// register this layer with the central layers registry
2400-
QgsMapLayerRegistry::instance()->addMapLayer( layer );
2401-
}
2402-
else
2403-
{
2404-
QgsDebugMsg(( *it ) + " is an invalid layer - not loaded" );
2405-
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( *it ) );
2406-
delete layer;
2407-
}
2408-
//qWarning("incrementing iterator");
2409-
++it;
2410-
}
2413+
mMapCanvas->freeze( false );
2414+
QApplication::restoreOverrideCursor();
24112415

2412-
QApplication::restoreOverrideCursor();
2416+
// XXX insert meaningful whine to the user here
2417+
return;
2418+
}
24132419

2414-
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
2420+
if ( layer->isValid() )
2421+
{
2422+
// register this layer with the central layers registry
2423+
QgsMapLayerRegistry::instance()->addMapLayer( layer );
2424+
}
2425+
else
2426+
{
2427+
QgsDebugMsg(( layerPath ) + " is an invalid layer - not loaded" );
2428+
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( layerPath ) );
2429+
delete layer;
2430+
}
2431+
//qWarning("incrementing iterator");
24152432
}
24162433

2417-
delete dbs;
2434+
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
24182435

24192436
// update UI
24202437
qApp->processEvents();
@@ -2423,11 +2440,8 @@ void QgisApp::addDatabaseLayer()
24232440
mMapCanvas->freeze( false );
24242441
mMapCanvas->refresh();
24252442

2426-
// Let render() do its own cursor management
2427-
// QApplication::restoreOverrideCursor();
2428-
2429-
} // QgisApp::addDatabaseLayer()
2430-
#endif
2443+
QApplication::restoreOverrideCursor();
2444+
}
24312445

24322446

24332447
#ifndef HAVE_SPATIALITE

src/app/qgisapp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
466466
//! Add a databaselayer to the map
467467
void addDatabaseLayer();
468468
//#endif
469+
//! Add a list of database layers to the map
470+
void addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey );
469471
//#ifdef HAVE_SPATIALITE
470472
//! Add a SpatiaLite layer to the map
471473
void addSpatiaLiteLayer();

src/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ SET(QGIS_CORE_SRCS
5151
qgscrscache.cpp
5252
qgsdatasourceuri.cpp
5353
qgsdataitem.cpp
54+
qgsdbfilterproxymodel.cpp
55+
qgsdbtablemodel.cpp
5456
qgsdiagram.cpp
5557
qgsdiagramrendererv2.cpp
5658
qgsdistancearea.cpp
@@ -228,6 +230,7 @@ SET(QGIS_CORE_MOC_HDRS
228230
qgscontexthelp.h
229231
qgscoordinatetransform.h
230232
qgsdataitem.h
233+
qgsdbtablemodel.h
231234
qgsdataprovider.h
232235
qgshttptransaction.h
233236
qgsmaplayer.h
File renamed without changes.
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: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
22
########################################################
33
# Files
44

5-
SET(PG_SRCS qgspostgresprovider.cpp)
6-
SET(PG_MOC_HDRS qgspostgresprovider.h)
5+
SET(PG_SRCS
6+
qgspostgresprovider.cpp
7+
qgspostgresconnection.cpp
8+
qgspgsourceselect.cpp
9+
qgspgnewconnection.cpp
10+
)
11+
SET(PG_MOC_HDRS
12+
qgspostgresprovider.h
13+
qgspostgresconnection.h
14+
qgspgsourceselect.h
15+
qgspgnewconnection.h
16+
)
717

818

919
########################################################
1020
# Build
1121

1222
QT4_WRAP_CPP(PG_MOC_SRCS ${PG_MOC_HDRS})
1323

14-
INCLUDE_DIRECTORIES (
24+
INCLUDE_DIRECTORIES(
1525
../../core
1626
${POSTGRES_INCLUDE_DIR}
1727
${GEOS_INCLUDE_DIR}
28+
../../core
29+
../../gui
30+
../../app
31+
${CMAKE_CURRENT_BINARY_DIR}/../../ui
1832
)
1933

2034
ADD_LIBRARY (postgresprovider MODULE ${PG_SRCS} ${PG_MOC_SRCS})

0 commit comments

Comments
 (0)