Skip to content

Commit

Permalink
supporting the SpatiaLite Data Provider
Browse files Browse the repository at this point in the history
2009-04-24 Sandro Furieri <a.furieri@lqt.it>


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10414 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
esseffe committed Mar 24, 2009
1 parent 2108aa6 commit 75811a3
Show file tree
Hide file tree
Showing 18 changed files with 3,523 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Expand Up @@ -50,6 +50,12 @@ IF (WITH_POSTGRESQL)
SET (POSTGRESQL_PREFIX "" CACHE PATH "Path to POSTGRESQL base directory")
ENDIF (WITH_POSTGRESQL)

# try to configure and build SPATIALITE support
SET (WITH_SPATIALITE TRUE CACHE BOOL "Determines whether SPATIALITE support should be built")
IF (WITH_SPATIALITE)
SET (SPATIALITE_PREFIX "" CACHE PATH "Path to SPATIALITE base directory")
ENDIF (WITH_SPATIALITE)

# try to configure and build python bindings by default
SET (WITH_BINDINGS TRUE CACHE BOOL "Determines whether python bindings should be built")
IF (WITH_BINDINGS)
Expand Down Expand Up @@ -118,6 +124,9 @@ FIND_PACKAGE(GSL) # Georeferencer
IF (WITH_GRASS)
FIND_PACKAGE(GRASS) # GRASS plugin
ENDIF (WITH_GRASS)
IF (WITH_SPATIALITE)
FIND_PACKAGE(SPATIALITE) # SPATIALITE provider
ENDIF (WITH_SPATIALITE)

IF (WITH_BINDINGS)
# python support:
Expand All @@ -136,6 +145,10 @@ IF (POSTGRES_FOUND)
SET (HAVE_POSTGRESQL TRUE)
ENDIF (POSTGRES_FOUND)

IF (SPATIALITE_FOUND)
# following variable is used in qgsconfig.h
SET (HAVE_SPATIALITE TRUE)
ENDIF (SPATIALITE_FOUND)

#############################################################
# search for Qt4
Expand Down
50 changes: 50 additions & 0 deletions cmake/FindSPATIALITE.cmake
@@ -0,0 +1,50 @@

# CMake module to search for SpatiaLite library
#
# If it's found it sets SPATIALITE_FOUND to TRUE
# and following variables are set:
# SPATIALITE_INCLUDE_DIR
# SPATIALITE_LIBRARY


# FIND_PATH and FIND_LIBRARY normally search standard locations
# before the specified paths. To search non-standard paths first,
# FIND_* is invoked first with specified paths and NO_DEFAULT_PATH
# and then again with no specified paths to search the default
# locations. When an earlier FIND_* succeeds, subsequent FIND_*s
# searching for the same item do nothing.
FIND_PATH(SPATIALITE_INCLUDE_DIR spatialite.h
"$ENV{LIB_DIR}/include"
"$ENV{LIB_DIR}/include/spatialite"
#mingw
c:/msys/local/include
NO_DEFAULT_PATH
)
FIND_PATH(SPATIALITE_INCLUDE_DIR spatialite.h)

FIND_LIBRARY(SPATIALITE_LIBRARY NAMES spatialite PATHS
"$ENV{LIB_DIR}/lib"
#mingw
c:/msys/local/lib
NO_DEFAULT_PATH
)
FIND_LIBRARY(SPATIALITE_LIBRARY NAMES spatialite)

IF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)
SET(SPATIALITE_FOUND TRUE)
ENDIF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)


IF (SPATIALITE_FOUND)

IF (NOT SPATIALITE_FIND_QUIETLY)
MESSAGE(STATUS "Found SpatiaLite: ${SPATIALITE_LIBRARY}")
ENDIF (NOT SPATIALITE_FIND_QUIETLY)

ELSE (SPATIALITE_FOUND)

IF (SPATIALITE_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find SpatiaLite")
ENDIF (SPATIALITE_FIND_REQUIRED)

ENDIF (SPATIALITE_FOUND)
2 changes: 2 additions & 0 deletions cmake_templates/qgsconfig.h.in
Expand Up @@ -25,6 +25,8 @@

#cmakedefine HAVE_POSTGRESQL

#cmakedefine HAVE_SPATIALITE

#cmakedefine HAVE_PYTHON

#endif
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -15,6 +15,8 @@ SET(QGIS_APP_SRCS
qgscustomprojectiondialog.cpp
qgsdbfilterproxymodel.cpp
qgsdbtablemodel.cpp
qgsspatialitefilterproxymodel.cpp
qgsspatialitetablemodel.cpp
qgsdelattrdialog.cpp
qgsgeomtypedialog.cpp
qgsgraduatedsymboldialog.cpp
Expand Down Expand Up @@ -135,6 +137,7 @@ SET (QGIS_APP_MOC_HDRS
qgsuniquevaluedialog.h
qgsvectorlayerproperties.h
qgsdbtablemodel.h
qgsspatialitetablemodel.h

composer/qgscomposer.h
composer/qgscomposeritemwidget.h
Expand Down Expand Up @@ -175,6 +178,15 @@ IF (POSTGRES_FOUND)
)
ENDIF (POSTGRES_FOUND)

IF (SPATIALITE_FOUND)
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
qgsspatialitesourceselect.cpp
)
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
qgsspatialitesourceselect.h
)
ENDIF (SPATIALITE_FOUND)


QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})

Expand Down Expand Up @@ -227,6 +239,10 @@ IF (POSTGRES_FOUND)
INCLUDE_DIRECTORIES(${POSTGRES_INCLUDE_DIR})
ENDIF (POSTGRES_FOUND)

IF (SPATIALITE_FOUND)
INCLUDE_DIRECTORIES(${SPATIALITE_INCLUDE_DIR})
ENDIF (SPATIALITE_FOUND)

#############

IF (WIN32)
Expand Down Expand Up @@ -269,6 +285,10 @@ IF (POSTGRES_FOUND)
TARGET_LINK_LIBRARIES (qgis ${POSTGRES_LIBRARY})
ENDIF (POSTGRES_FOUND)

IF (SPATIALITE_FOUND)
TARGET_LINK_LIBRARIES (qgis ${SPATIALITE_LIBRARY})
ENDIF (SPATIALITE_FOUND)

IF (APPLE)
# For Mac OS X, the executable must be at the root of the bundle's executable folder
INSTALL(TARGETS qgis RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
Expand Down
110 changes: 110 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -175,6 +175,9 @@
#ifdef HAVE_POSTGRESQL
#include "qgsdbsourceselect.h"
#endif
#ifdef HAVE_SPATIALITE
#include "qgsspatialitesourceselect.h"
#endif

#include "qgspythondialog.h"
#include "qgspythonutils.h"
Expand Down Expand Up @@ -776,6 +779,18 @@ void QgisApp::createActions()
//#endif
connect( mActionAddPgLayer, SIGNAL( triggered() ), this, SLOT( addDatabaseLayer() ) );

mActionAddSpatiaLiteLayer = new QAction( getThemeIcon( "mActionAddSpatiaLiteLayer.png" ), tr( "Add SpatiaLite Layer..." ), this );
mActionAddSpatiaLiteLayer->setShortcut( tr( "L", "Add a SpatiaLite Layer" ) );
mActionAddSpatiaLiteLayer->setStatusTip( tr( "Add a SpatiaLite Layer" ) );
connect( mActionAddSpatiaLiteLayer, SIGNAL( triggered() ), this, SLOT( addSpatiaLiteLayer() ) );
//#ifdef HAVE_SPATIALITE
// QgsDebugMsg("HAVE_SPATIALITE is defined");
// assert(0);
//#else
// QgsDebugMsg("HAVE_SPATIALITE not defined");
// assert(0);
//#endif

mActionAddWmsLayer = new QAction( getThemeIcon( "mActionAddWmsLayer.png" ), tr( "Add WMS Layer..." ), this );
mActionAddWmsLayer->setShortcut( tr( "W", "Add a Web Mapping Server Layer" ) );
mActionAddWmsLayer->setStatusTip( tr( "Add a Web Mapping Server Layer" ) );
Expand Down Expand Up @@ -1109,6 +1124,9 @@ void QgisApp::createMenus()
mLayerMenu->addAction( mActionAddRasterLayer );
#ifdef HAVE_POSTGRESQL
mLayerMenu->addAction( mActionAddPgLayer );
#endif
#ifdef HAVE_SPATIALITE
mLayerMenu->addAction( mActionAddSpatiaLiteLayer );
#endif
mLayerMenu->addAction( mActionAddWmsLayer );
mActionLayerSeparator1 = mLayerMenu->addSeparator();
Expand Down Expand Up @@ -1207,6 +1225,9 @@ void QgisApp::createToolBars()
mFileToolBar->addAction( mActionAddRasterLayer );
#ifdef HAVE_POSTGRESQL
mFileToolBar->addAction( mActionAddPgLayer );
#endif
#ifdef HAVE_SPATIALITE
mFileToolBar->addAction( mActionAddSpatiaLiteLayer );
#endif
mFileToolBar->addAction( mActionAddWmsLayer );
mToolbarMenu->addAction( mFileToolBar->toggleViewAction() );
Expand Down Expand Up @@ -1432,6 +1453,7 @@ void QgisApp::setTheme( QString theThemeName )
mActionAddOgrLayer->setIcon( getThemeIcon( "/mActionAddOgrLayer.png" ) );
mActionAddRasterLayer->setIcon( getThemeIcon( "/mActionAddRasterLayer.png" ) );
mActionAddPgLayer->setIcon( getThemeIcon( "/mActionAddLayer.png" ) );
mActionAddSpatiaLiteLayer->setIcon( getThemeIcon( "/mActionAddSpatiaLiteLayer.png" ) );
mActionRemoveLayer->setIcon( getThemeIcon( "/mActionRemoveLayer.png" ) );
mActionNewVectorLayer->setIcon( getThemeIcon( "/mActionNewVectorLayer.png" ) );
mActionAddAllToOverview->setIcon( getThemeIcon( "/mActionAddAllToOverview.png" ) );
Expand Down Expand Up @@ -1794,6 +1816,13 @@ void QgisApp::about()
#else

versionString += tr( " This copy of QGIS has been built without PostgreSQL support." );
#endif
#ifdef HAVE_SPATIALITE

versionString += tr( "\nThis copy of QGIS has been built with SpatiaLite support." );
#else

versionString += tr( "\nThis copy of QGIS has been built without SpatiaLite support." );
#endif
versionString += tr( "\nThis binary was compiled against Qt %1,"
"and is currently running against Qt %2" )
Expand Down Expand Up @@ -2384,6 +2413,87 @@ void QgisApp::addDatabaseLayer()
#endif


#ifndef HAVE_SPATIALITE
void QgisApp::addSpatiaLiteLayer() {}
#else
void QgisApp::addSpatiaLiteLayer()
{
if(mMapCanvas && mMapCanvas->isDrawing())
{
return;
}

// show the SpatiaLite dialog

QgsSpatiaLiteSourceSelect *dbs = new QgsSpatiaLiteSourceSelect( this );

mMapCanvas->freeze();

if (dbs->exec())
{
// Let render() do its own cursor management
// QApplication::setOverrideCursor(Qt::WaitCursor);


// repaint the canvas if it was covered by the dialog

// add files to the map canvas
QStringList tables = dbs->selectedTables();

QApplication::setOverrideCursor(Qt::WaitCursor);

QString connectionInfo = dbs->connectionInfo();
// for each selected table, connect to the database and build a canvasitem for it
QStringList::Iterator it = tables.begin();
while (it != tables.end())
{

// normalizing the layer name
QString layername = *it;
layername = layername.mid(1);
int idx = layername.indexOf( "\" (" );
if (idx > 0)
layername.truncate(idx);

// create the layer
//qWarning("creating layer");
QgsVectorLayer *layer = new QgsVectorLayer( "dbname='" + connectionInfo + "' table=" + *it + ")", layername, "spatialite" );
if ( layer->isValid() )
{
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
// notify the project we've made a change
QgsProject::instance()->dirty( true );
}
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;
}

QApplication::restoreOverrideCursor();

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

// update UI
qApp->processEvents();

// draw the map
mMapCanvas->freeze( false );
mMapCanvas->refresh();

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

} // QgisApp::addSpatiaLiteLayer()
#endif

void QgisApp::addWmsLayer()
{
if ( mMapCanvas && mMapCanvas->isDrawing() )
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -246,6 +246,7 @@ class QgisApp : public QMainWindow
QAction *actionAddOgrLayer() { return mActionAddOgrLayer; }
QAction *actionAddRasterLayer() { return mActionAddRasterLayer; }
QAction *actionAddPgLayer() { return mActionAddPgLayer; }
QAction *actionAddSpatiaLiteLayer() { return mActionAddSpatiaLiteLayer; };
QAction *actionAddWmsLayer() { return mActionAddWmsLayer; }
QAction *actionLayerSeparator1() { return mActionLayerSeparator1; }
QAction *actionOpenTable() { return mActionOpenTable; }
Expand Down Expand Up @@ -369,6 +370,10 @@ class QgisApp : public QMainWindow
//#ifdef HAVE_POSTGRESQL
//! Add a databaselayer to the map
void addDatabaseLayer();
//#endif
//#ifdef HAVE_SPATIALITE
//! Add a SpatiaLite layer to the map
void addSpatiaLiteLayer();
//#endif
/** toggles whether the current selected layer is in overview or not */
void isInOverview();
Expand Down Expand Up @@ -717,6 +722,7 @@ class QgisApp : public QMainWindow
QAction *mActionAddOgrLayer;
QAction *mActionAddRasterLayer;
QAction *mActionAddPgLayer;
QAction *mActionAddSpatiaLiteLayer;
QAction *mActionAddWmsLayer;
QAction *mActionLayerSeparator1;
QAction *mActionOpenTable;
Expand Down
52 changes: 52 additions & 0 deletions src/app/qgsspatialitefilterproxymodel.cpp
@@ -0,0 +1,52 @@
/***************************************************************************
qgsspatialitefilterproxymodel.cpp - description
-------------------------
begin : Dec 2008
copyright : (C) 2008 by Sandro Furieri
email : a.furieri@lqt.it
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsspatialitefilterproxymodel.h"

QgsSpatiaLiteFilterProxyModel::QgsSpatiaLiteFilterProxyModel(QObject * parent):QSortFilterProxyModel(parent)
{

}

QgsSpatiaLiteFilterProxyModel::~QgsSpatiaLiteFilterProxyModel()
{

}

bool QgsSpatiaLiteFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & source_parent) const
{
//if parent is valid, we have a toplevel item that should be always shown
if (!source_parent.isValid())
{
return true;
}
//else we have a row that describes a table and that
//should be tested using the given wildcard/regexp
return QSortFilterProxyModel::filterAcceptsRow(row, source_parent);
}

void QgsSpatiaLiteFilterProxyModel::_setFilterWildcard(const QString & pattern)
{
QSortFilterProxyModel::setFilterWildcard(pattern);
emit layoutChanged();
}

void QgsSpatiaLiteFilterProxyModel::_setFilterRegExp(const QString & pattern)
{
QSortFilterProxyModel::setFilterRegExp(pattern);
emit layoutChanged();
}

0 comments on commit 75811a3

Please sign in to comment.