Skip to content

Commit 75811a3

Browse files
author
esseffe
committed
supporting the SpatiaLite Data Provider
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
1 parent 2108aa6 commit 75811a3

18 files changed

+3523
-0
lines changed

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ IF (WITH_POSTGRESQL)
5050
SET (POSTGRESQL_PREFIX "" CACHE PATH "Path to POSTGRESQL base directory")
5151
ENDIF (WITH_POSTGRESQL)
5252

53+
# try to configure and build SPATIALITE support
54+
SET (WITH_SPATIALITE TRUE CACHE BOOL "Determines whether SPATIALITE support should be built")
55+
IF (WITH_SPATIALITE)
56+
SET (SPATIALITE_PREFIX "" CACHE PATH "Path to SPATIALITE base directory")
57+
ENDIF (WITH_SPATIALITE)
58+
5359
# try to configure and build python bindings by default
5460
SET (WITH_BINDINGS TRUE CACHE BOOL "Determines whether python bindings should be built")
5561
IF (WITH_BINDINGS)
@@ -118,6 +124,9 @@ FIND_PACKAGE(GSL) # Georeferencer
118124
IF (WITH_GRASS)
119125
FIND_PACKAGE(GRASS) # GRASS plugin
120126
ENDIF (WITH_GRASS)
127+
IF (WITH_SPATIALITE)
128+
FIND_PACKAGE(SPATIALITE) # SPATIALITE provider
129+
ENDIF (WITH_SPATIALITE)
121130

122131
IF (WITH_BINDINGS)
123132
# python support:
@@ -136,6 +145,10 @@ IF (POSTGRES_FOUND)
136145
SET (HAVE_POSTGRESQL TRUE)
137146
ENDIF (POSTGRES_FOUND)
138147

148+
IF (SPATIALITE_FOUND)
149+
# following variable is used in qgsconfig.h
150+
SET (HAVE_SPATIALITE TRUE)
151+
ENDIF (SPATIALITE_FOUND)
139152

140153
#############################################################
141154
# search for Qt4

cmake/FindSPATIALITE.cmake

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
# CMake module to search for SpatiaLite library
3+
#
4+
# If it's found it sets SPATIALITE_FOUND to TRUE
5+
# and following variables are set:
6+
# SPATIALITE_INCLUDE_DIR
7+
# SPATIALITE_LIBRARY
8+
9+
10+
# FIND_PATH and FIND_LIBRARY normally search standard locations
11+
# before the specified paths. To search non-standard paths first,
12+
# FIND_* is invoked first with specified paths and NO_DEFAULT_PATH
13+
# and then again with no specified paths to search the default
14+
# locations. When an earlier FIND_* succeeds, subsequent FIND_*s
15+
# searching for the same item do nothing.
16+
FIND_PATH(SPATIALITE_INCLUDE_DIR spatialite.h
17+
"$ENV{LIB_DIR}/include"
18+
"$ENV{LIB_DIR}/include/spatialite"
19+
#mingw
20+
c:/msys/local/include
21+
NO_DEFAULT_PATH
22+
)
23+
FIND_PATH(SPATIALITE_INCLUDE_DIR spatialite.h)
24+
25+
FIND_LIBRARY(SPATIALITE_LIBRARY NAMES spatialite PATHS
26+
"$ENV{LIB_DIR}/lib"
27+
#mingw
28+
c:/msys/local/lib
29+
NO_DEFAULT_PATH
30+
)
31+
FIND_LIBRARY(SPATIALITE_LIBRARY NAMES spatialite)
32+
33+
IF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)
34+
SET(SPATIALITE_FOUND TRUE)
35+
ENDIF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)
36+
37+
38+
IF (SPATIALITE_FOUND)
39+
40+
IF (NOT SPATIALITE_FIND_QUIETLY)
41+
MESSAGE(STATUS "Found SpatiaLite: ${SPATIALITE_LIBRARY}")
42+
ENDIF (NOT SPATIALITE_FIND_QUIETLY)
43+
44+
ELSE (SPATIALITE_FOUND)
45+
46+
IF (SPATIALITE_FIND_REQUIRED)
47+
MESSAGE(FATAL_ERROR "Could not find SpatiaLite")
48+
ENDIF (SPATIALITE_FIND_REQUIRED)
49+
50+
ENDIF (SPATIALITE_FOUND)

cmake_templates/qgsconfig.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#cmakedefine HAVE_POSTGRESQL
2727

28+
#cmakedefine HAVE_SPATIALITE
29+
2830
#cmakedefine HAVE_PYTHON
2931

3032
#endif
1.53 KB
Loading

src/app/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ SET(QGIS_APP_SRCS
1515
qgscustomprojectiondialog.cpp
1616
qgsdbfilterproxymodel.cpp
1717
qgsdbtablemodel.cpp
18+
qgsspatialitefilterproxymodel.cpp
19+
qgsspatialitetablemodel.cpp
1820
qgsdelattrdialog.cpp
1921
qgsgeomtypedialog.cpp
2022
qgsgraduatedsymboldialog.cpp
@@ -135,6 +137,7 @@ SET (QGIS_APP_MOC_HDRS
135137
qgsuniquevaluedialog.h
136138
qgsvectorlayerproperties.h
137139
qgsdbtablemodel.h
140+
qgsspatialitetablemodel.h
138141

139142
composer/qgscomposer.h
140143
composer/qgscomposeritemwidget.h
@@ -175,6 +178,15 @@ IF (POSTGRES_FOUND)
175178
)
176179
ENDIF (POSTGRES_FOUND)
177180

181+
IF (SPATIALITE_FOUND)
182+
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
183+
qgsspatialitesourceselect.cpp
184+
)
185+
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
186+
qgsspatialitesourceselect.h
187+
)
188+
ENDIF (SPATIALITE_FOUND)
189+
178190

179191
QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})
180192

@@ -227,6 +239,10 @@ IF (POSTGRES_FOUND)
227239
INCLUDE_DIRECTORIES(${POSTGRES_INCLUDE_DIR})
228240
ENDIF (POSTGRES_FOUND)
229241

242+
IF (SPATIALITE_FOUND)
243+
INCLUDE_DIRECTORIES(${SPATIALITE_INCLUDE_DIR})
244+
ENDIF (SPATIALITE_FOUND)
245+
230246
#############
231247

232248
IF (WIN32)
@@ -269,6 +285,10 @@ IF (POSTGRES_FOUND)
269285
TARGET_LINK_LIBRARIES (qgis ${POSTGRES_LIBRARY})
270286
ENDIF (POSTGRES_FOUND)
271287

288+
IF (SPATIALITE_FOUND)
289+
TARGET_LINK_LIBRARIES (qgis ${SPATIALITE_LIBRARY})
290+
ENDIF (SPATIALITE_FOUND)
291+
272292
IF (APPLE)
273293
# For Mac OS X, the executable must be at the root of the bundle's executable folder
274294
INSTALL(TARGETS qgis RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

src/app/qgisapp.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@
175175
#ifdef HAVE_POSTGRESQL
176176
#include "qgsdbsourceselect.h"
177177
#endif
178+
#ifdef HAVE_SPATIALITE
179+
#include "qgsspatialitesourceselect.h"
180+
#endif
178181

179182
#include "qgspythondialog.h"
180183
#include "qgspythonutils.h"
@@ -776,6 +779,18 @@ void QgisApp::createActions()
776779
//#endif
777780
connect( mActionAddPgLayer, SIGNAL( triggered() ), this, SLOT( addDatabaseLayer() ) );
778781

782+
mActionAddSpatiaLiteLayer = new QAction( getThemeIcon( "mActionAddSpatiaLiteLayer.png" ), tr( "Add SpatiaLite Layer..." ), this );
783+
mActionAddSpatiaLiteLayer->setShortcut( tr( "L", "Add a SpatiaLite Layer" ) );
784+
mActionAddSpatiaLiteLayer->setStatusTip( tr( "Add a SpatiaLite Layer" ) );
785+
connect( mActionAddSpatiaLiteLayer, SIGNAL( triggered() ), this, SLOT( addSpatiaLiteLayer() ) );
786+
//#ifdef HAVE_SPATIALITE
787+
// QgsDebugMsg("HAVE_SPATIALITE is defined");
788+
// assert(0);
789+
//#else
790+
// QgsDebugMsg("HAVE_SPATIALITE not defined");
791+
// assert(0);
792+
//#endif
793+
779794
mActionAddWmsLayer = new QAction( getThemeIcon( "mActionAddWmsLayer.png" ), tr( "Add WMS Layer..." ), this );
780795
mActionAddWmsLayer->setShortcut( tr( "W", "Add a Web Mapping Server Layer" ) );
781796
mActionAddWmsLayer->setStatusTip( tr( "Add a Web Mapping Server Layer" ) );
@@ -1109,6 +1124,9 @@ void QgisApp::createMenus()
11091124
mLayerMenu->addAction( mActionAddRasterLayer );
11101125
#ifdef HAVE_POSTGRESQL
11111126
mLayerMenu->addAction( mActionAddPgLayer );
1127+
#endif
1128+
#ifdef HAVE_SPATIALITE
1129+
mLayerMenu->addAction( mActionAddSpatiaLiteLayer );
11121130
#endif
11131131
mLayerMenu->addAction( mActionAddWmsLayer );
11141132
mActionLayerSeparator1 = mLayerMenu->addSeparator();
@@ -1207,6 +1225,9 @@ void QgisApp::createToolBars()
12071225
mFileToolBar->addAction( mActionAddRasterLayer );
12081226
#ifdef HAVE_POSTGRESQL
12091227
mFileToolBar->addAction( mActionAddPgLayer );
1228+
#endif
1229+
#ifdef HAVE_SPATIALITE
1230+
mFileToolBar->addAction( mActionAddSpatiaLiteLayer );
12101231
#endif
12111232
mFileToolBar->addAction( mActionAddWmsLayer );
12121233
mToolbarMenu->addAction( mFileToolBar->toggleViewAction() );
@@ -1432,6 +1453,7 @@ void QgisApp::setTheme( QString theThemeName )
14321453
mActionAddOgrLayer->setIcon( getThemeIcon( "/mActionAddOgrLayer.png" ) );
14331454
mActionAddRasterLayer->setIcon( getThemeIcon( "/mActionAddRasterLayer.png" ) );
14341455
mActionAddPgLayer->setIcon( getThemeIcon( "/mActionAddLayer.png" ) );
1456+
mActionAddSpatiaLiteLayer->setIcon( getThemeIcon( "/mActionAddSpatiaLiteLayer.png" ) );
14351457
mActionRemoveLayer->setIcon( getThemeIcon( "/mActionRemoveLayer.png" ) );
14361458
mActionNewVectorLayer->setIcon( getThemeIcon( "/mActionNewVectorLayer.png" ) );
14371459
mActionAddAllToOverview->setIcon( getThemeIcon( "/mActionAddAllToOverview.png" ) );
@@ -1794,6 +1816,13 @@ void QgisApp::about()
17941816
#else
17951817

17961818
versionString += tr( " This copy of QGIS has been built without PostgreSQL support." );
1819+
#endif
1820+
#ifdef HAVE_SPATIALITE
1821+
1822+
versionString += tr( "\nThis copy of QGIS has been built with SpatiaLite support." );
1823+
#else
1824+
1825+
versionString += tr( "\nThis copy of QGIS has been built without SpatiaLite support." );
17971826
#endif
17981827
versionString += tr( "\nThis binary was compiled against Qt %1,"
17991828
"and is currently running against Qt %2" )
@@ -2384,6 +2413,87 @@ void QgisApp::addDatabaseLayer()
23842413
#endif
23852414

23862415

2416+
#ifndef HAVE_SPATIALITE
2417+
void QgisApp::addSpatiaLiteLayer() {}
2418+
#else
2419+
void QgisApp::addSpatiaLiteLayer()
2420+
{
2421+
if(mMapCanvas && mMapCanvas->isDrawing())
2422+
{
2423+
return;
2424+
}
2425+
2426+
// show the SpatiaLite dialog
2427+
2428+
QgsSpatiaLiteSourceSelect *dbs = new QgsSpatiaLiteSourceSelect( this );
2429+
2430+
mMapCanvas->freeze();
2431+
2432+
if (dbs->exec())
2433+
{
2434+
// Let render() do its own cursor management
2435+
// QApplication::setOverrideCursor(Qt::WaitCursor);
2436+
2437+
2438+
// repaint the canvas if it was covered by the dialog
2439+
2440+
// add files to the map canvas
2441+
QStringList tables = dbs->selectedTables();
2442+
2443+
QApplication::setOverrideCursor(Qt::WaitCursor);
2444+
2445+
QString connectionInfo = dbs->connectionInfo();
2446+
// for each selected table, connect to the database and build a canvasitem for it
2447+
QStringList::Iterator it = tables.begin();
2448+
while (it != tables.end())
2449+
{
2450+
2451+
// normalizing the layer name
2452+
QString layername = *it;
2453+
layername = layername.mid(1);
2454+
int idx = layername.indexOf( "\" (" );
2455+
if (idx > 0)
2456+
layername.truncate(idx);
2457+
2458+
// create the layer
2459+
//qWarning("creating layer");
2460+
QgsVectorLayer *layer = new QgsVectorLayer( "dbname='" + connectionInfo + "' table=" + *it + ")", layername, "spatialite" );
2461+
if ( layer->isValid() )
2462+
{
2463+
// register this layer with the central layers registry
2464+
QgsMapLayerRegistry::instance()->addMapLayer( layer );
2465+
// notify the project we've made a change
2466+
QgsProject::instance()->dirty( true );
2467+
}
2468+
else
2469+
{
2470+
QgsDebugMsg( (*it) + " is an invalid layer - not loaded" );
2471+
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( *it ) );
2472+
delete layer;
2473+
}
2474+
//qWarning("incrementing iterator");
2475+
++it;
2476+
}
2477+
2478+
QApplication::restoreOverrideCursor();
2479+
2480+
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
2481+
}
2482+
delete dbs;
2483+
2484+
// update UI
2485+
qApp->processEvents();
2486+
2487+
// draw the map
2488+
mMapCanvas->freeze( false );
2489+
mMapCanvas->refresh();
2490+
2491+
// Let render() do its own cursor management
2492+
// QApplication::restoreOverrideCursor();
2493+
2494+
} // QgisApp::addSpatiaLiteLayer()
2495+
#endif
2496+
23872497
void QgisApp::addWmsLayer()
23882498
{
23892499
if ( mMapCanvas && mMapCanvas->isDrawing() )

src/app/qgisapp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ class QgisApp : public QMainWindow
246246
QAction *actionAddOgrLayer() { return mActionAddOgrLayer; }
247247
QAction *actionAddRasterLayer() { return mActionAddRasterLayer; }
248248
QAction *actionAddPgLayer() { return mActionAddPgLayer; }
249+
QAction *actionAddSpatiaLiteLayer() { return mActionAddSpatiaLiteLayer; };
249250
QAction *actionAddWmsLayer() { return mActionAddWmsLayer; }
250251
QAction *actionLayerSeparator1() { return mActionLayerSeparator1; }
251252
QAction *actionOpenTable() { return mActionOpenTable; }
@@ -369,6 +370,10 @@ class QgisApp : public QMainWindow
369370
//#ifdef HAVE_POSTGRESQL
370371
//! Add a databaselayer to the map
371372
void addDatabaseLayer();
373+
//#endif
374+
//#ifdef HAVE_SPATIALITE
375+
//! Add a SpatiaLite layer to the map
376+
void addSpatiaLiteLayer();
372377
//#endif
373378
/** toggles whether the current selected layer is in overview or not */
374379
void isInOverview();
@@ -717,6 +722,7 @@ class QgisApp : public QMainWindow
717722
QAction *mActionAddOgrLayer;
718723
QAction *mActionAddRasterLayer;
719724
QAction *mActionAddPgLayer;
725+
QAction *mActionAddSpatiaLiteLayer;
720726
QAction *mActionAddWmsLayer;
721727
QAction *mActionLayerSeparator1;
722728
QAction *mActionOpenTable;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/***************************************************************************
2+
qgsspatialitefilterproxymodel.cpp - description
3+
-------------------------
4+
begin : Dec 2008
5+
copyright : (C) 2008 by Sandro Furieri
6+
email : a.furieri@lqt.it
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsspatialitefilterproxymodel.h"
19+
20+
QgsSpatiaLiteFilterProxyModel::QgsSpatiaLiteFilterProxyModel(QObject * parent):QSortFilterProxyModel(parent)
21+
{
22+
23+
}
24+
25+
QgsSpatiaLiteFilterProxyModel::~QgsSpatiaLiteFilterProxyModel()
26+
{
27+
28+
}
29+
30+
bool QgsSpatiaLiteFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & source_parent) const
31+
{
32+
//if parent is valid, we have a toplevel item that should be always shown
33+
if (!source_parent.isValid())
34+
{
35+
return true;
36+
}
37+
//else we have a row that describes a table and that
38+
//should be tested using the given wildcard/regexp
39+
return QSortFilterProxyModel::filterAcceptsRow(row, source_parent);
40+
}
41+
42+
void QgsSpatiaLiteFilterProxyModel::_setFilterWildcard(const QString & pattern)
43+
{
44+
QSortFilterProxyModel::setFilterWildcard(pattern);
45+
emit layoutChanged();
46+
}
47+
48+
void QgsSpatiaLiteFilterProxyModel::_setFilterRegExp(const QString & pattern)
49+
{
50+
QSortFilterProxyModel::setFilterRegExp(pattern);
51+
emit layoutChanged();
52+
}

0 commit comments

Comments
 (0)