Skip to content

Commit

Permalink
add QgsDataItem to the postgres provider
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed Jul 12, 2011
1 parent 7647d82 commit bd3f77a
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 72 deletions.
10 changes: 0 additions & 10 deletions src/app/CMakeLists.txt
Expand Up @@ -152,7 +152,6 @@ SET (QGIS_APP_MOC_HDRS
qgscontinuouscolordialog.h
qgscustomization.h
qgscustomprojectiondialog.h
qgsdbtablemodel.h
qgsdelattrdialog.h
qgsdisplayangle.h
qgsembedlayerdialog.h
Expand Down Expand Up @@ -277,15 +276,6 @@ IF (POSTGRES_FOUND)
IF(HAVE_PGCONFIG)
ADD_DEFINITIONS(-DHAVE_PGCONFIG=1)
ENDIF(HAVE_PGCONFIG)

SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
postgres/qgspgsourceselect.cpp
postgres/qgspgnewconnection.cpp
)
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
postgres/qgspgsourceselect.h
postgres/qgspgnewconnection.h
)
ENDIF (POSTGRES_FOUND)

IF (HAVE_SPATIALITE)
Expand Down
102 changes: 58 additions & 44 deletions src/app/qgisapp.cpp
Expand Up @@ -225,7 +225,7 @@
// Conditional Includes
//
#ifdef HAVE_POSTGRESQL
#include "postgres/qgspgsourceselect.h"
#include "../providers/postgres/qgspgsourceselect.h"
#ifdef HAVE_PGCONFIG
#include <pg_config.h>
#else
Expand Down Expand Up @@ -2322,58 +2322,75 @@ void QgisApp::addDatabaseLayer()
{
return;
}
// Fudge for now
QgsDebugMsg( "about to addRasterLayer" );

// only supports postgis layers at present
// show the postgis dialog

QgsPgSourceSelect *dbs = new QgsPgSourceSelect( this );
// TODO: QDialog for now, switch to QWidget in future
QDialog *pgs = dynamic_cast<QDialog*>( QgsProviderRegistry::instance()->selectWidget( QString( "postgres" ), this ) );
if ( !pgs )
{
QMessageBox::warning( this, tr( "PostgreSQL" ), tr( "Cannot get PostgreSQL select dialog from provider." ) );
return;
}
connect( pgs , SIGNAL( addDatabaseLayers( QStringList const &, QString const & ) ),
this , SLOT( addDatabaseLayers( QStringList const &, QString const & ) ) );
pgs->exec();
delete pgs;
} // QgisApp::addDatabaseLayer()
#endif

mMapCanvas->freeze();
void QgisApp::addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey )
{
if ( mMapCanvas && mMapCanvas->isDrawing() )
{
return;
}

if ( dbs->exec() )
if ( layerPathList.empty() )
{
// Let render() do its own cursor management
// QApplication::setOverrideCursor(Qt::WaitCursor);
// no layers to add so bail out, but
// allow mMapCanvas to handle events
// first
mMapCanvas->freeze( false );
return;
}

mMapCanvas->freeze( true );

// repaint the canvas if it was covered by the dialog
QApplication::setOverrideCursor(Qt::WaitCursor);

// add files to the map canvas
QStringList tables = dbs->selectedTables();
foreach( QString layerPath, layerPathList )
{
// create the layer
QgsDataSourceURI uri( layerPath );

QApplication::setOverrideCursor( Qt::WaitCursor );
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri(), uri.table(), providerKey );
Q_CHECK_PTR( layer );

// for each selected table, connect to the database, parse the Wkt geometry,
// and build a canvasitem for it
// readWKB(connectionInfo,tables);
QStringList::Iterator it = tables.begin();
while ( it != tables.end() )
if ( ! layer )
{
// create the layer
//qWarning("creating layer");
QgsDataSourceURI uri( *it );
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri(), uri.table(), "postgres" );
if ( layer->isValid() )
{
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
}
else
{
QgsDebugMsg(( *it ) + " is an invalid layer - not loaded" );
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( *it ) );
delete layer;
}
//qWarning("incrementing iterator");
++it;
}
mMapCanvas->freeze( false );
QApplication::restoreOverrideCursor();

QApplication::restoreOverrideCursor();
// XXX insert meaningful whine to the user here
return;
}

statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
if ( layer->isValid() )
{
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
}
else
{
QgsDebugMsg(( layerPath ) + " is an invalid layer - not loaded" );
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( layerPath ) );
delete layer;
}
//qWarning("incrementing iterator");
}

delete dbs;
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );

// update UI
qApp->processEvents();
Expand All @@ -2382,11 +2399,8 @@ void QgisApp::addDatabaseLayer()
mMapCanvas->freeze( false );
mMapCanvas->refresh();

// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();

} // QgisApp::addDatabaseLayer()
#endif
QApplication::restoreOverrideCursor();
}


#ifndef HAVE_SPATIALITE
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -460,6 +460,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
//! Add a databaselayer to the map
void addDatabaseLayer();
//#endif
//! Add a list of database layers to the map
void addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey );
//#ifdef HAVE_SPATIALITE
//! Add a SpatiaLite layer to the map
void addSpatiaLiteLayer();
Expand Down
3 changes: 3 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -49,6 +49,8 @@ SET(QGIS_CORE_SRCS
qgscoordinatetransform.cpp
qgsdatasourceuri.cpp
qgsdataitem.cpp
qgsdbfilterproxymodel.cpp
qgsdbtablemodel.cpp
qgsdiagram.cpp
qgsdiagramrendererv2.cpp
qgsdistancearea.cpp
Expand Down Expand Up @@ -225,6 +227,7 @@ SET(QGIS_CORE_MOC_HDRS
qgscontexthelp.h
qgscoordinatetransform.h
qgsdataitem.h
qgsdbtablemodel.h
qgsdataprovider.h
qgshttptransaction.h
qgsmaplayer.h
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 4 additions & 5 deletions src/app/qgsdbtablemodel.cpp → src/core/qgsdbtablemodel.cpp
Expand Up @@ -16,8 +16,7 @@
***************************************************************************/

#include "qgsdbtablemodel.h"
#include "qgsapplication.h"
#include "qgisapp.h"
#include "qgsdataitem.h"

QgsDbTableModel::QgsDbTableModel(): QStandardItemModel(), mTableCount( 0 )
{
Expand Down Expand Up @@ -224,15 +223,15 @@ QIcon QgsDbTableModel::iconForType( QGis::WkbType type ) const
{
if ( type == QGis::WKBPoint || type == QGis::WKBPoint25D || type == QGis::WKBMultiPoint || type == QGis::WKBMultiPoint25D )
{
return QgisApp::getThemeIcon( "/mIconPointLayer.png" );
return QIcon( QgsDataItem::getThemePixmap( "/mIconPointLayer.png" ) );
}
else if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D || type == QGis::WKBMultiLineString || type == QGis::WKBMultiLineString25D )
{
return QgisApp::getThemeIcon( "/mIconLineLayer.png" );
return QIcon( QgsDataItem::getThemePixmap( "/mIconLineLayer.png" ) );
}
else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D || type == QGis::WKBMultiPolygon || type == QGis::WKBMultiPolygon25D )
{
return QgisApp::getThemeIcon( "/mIconPolygonLayer.png" );
return QIcon( QgsDataItem::getThemePixmap( "/mIconPolygonLayer.png" ) );
}
else return QIcon();
}
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/plugins/spit/CMakeLists.txt
Expand Up @@ -4,7 +4,7 @@

SET (SPIT_SRCS
qgsspit.cpp
../../app/postgres/qgspgnewconnection.cpp
../../providers/postgres/qgspgnewconnection.cpp
qgspgutil.cpp
qgsshapefile.cpp
)
Expand All @@ -29,7 +29,7 @@ SET (SPIT_UIS
SET (SPIT_EXE_MOC_HDRS
qgsspit.h
qgsshapefile.h
../../app/postgres/qgspgnewconnection.h
../../providers/postgres/qgspgnewconnection.h
)

SET (SPIT_PLUGIN_MOC_HDRS
Expand Down Expand Up @@ -57,7 +57,7 @@ INCLUDE_DIRECTORIES(
../../gui
../../ui
../../app
../../app/postgres
../../providers/postgres
..
${POSTGRES_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
Expand Down
7 changes: 4 additions & 3 deletions src/providers/postgres/CMakeLists.txt
Expand Up @@ -5,12 +5,14 @@
SET(PG_SRCS
qgspostgresprovider.cpp
qgspostgresconnection.cpp
../../app/postgres/qgspgsourceselect.cpp
qgspgsourceselect.cpp
qgspgnewconnection.cpp
)
SET(PG_MOC_HDRS
qgspostgresprovider.h
qgspostgresconnection.h
../../app/postgres/qgspgsourceselect.h
qgspgsourceselect.h
qgspgnewconnection.h
)


Expand All @@ -26,7 +28,6 @@ INCLUDE_DIRECTORIES(
../../core
../../gui
../../app
../../app/postgres
${CMAKE_CURRENT_BINARY_DIR}/../../ui
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -116,6 +116,11 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
//! Connection info (database, host, user, password)
QString connectionInfo();

signals:
void addDatabaseLayers( QStringList const & layerPathList,
QString const & providerKey );
void connectionsChanged();

public slots:
//! Determines the tables the user selected and closes the dialog
void addTables();
Expand Down Expand Up @@ -155,12 +160,6 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
typedef QPair<QString, QString> geomPair;
typedef QList<geomPair> geomCol;

/**Inserts information about the spatial tables into mTableModel*/
bool getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables );

/** get primary key candidates (all int4 columns) */
QStringList pkCandidates( PGconn *pg, QString schemaName, QString viewName );

// queue another query for the thread
void addSearchGeometryColumn( const QString &schema, const QString &table, const QString &column );

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

//! Model that acts as datasource for mTableTreeWidget
QgsDbTableModel mTableModel;
Expand Down

0 comments on commit bd3f77a

Please sign in to comment.