Skip to content

Commit

Permalink
introduce separate qml databases for fileless datasources (fixes #1127)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8777 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jul 14, 2008
1 parent 588c21f commit f13b48a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
88 changes: 55 additions & 33 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -38,6 +38,7 @@
#include "qgsmaplayer.h"
#include "qgsspatialrefsys.h"
#include "qgsapplication.h"
#include "qgsproject.h"

QgsMapLayer::QgsMapLayer(int type,
QString lyrname,
Expand Down Expand Up @@ -420,11 +421,50 @@ QString QgsMapLayer::loadDefaultStyle ( bool & theResultFlag )
return loadNamedStyle ( key, theResultFlag );
}

QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFlag )
bool QgsMapLayer::loadNamedStyleFromDb (const QString db, const QString theURI, QString &qml)
{
bool theResultFlag = false;

// read from database
sqlite3 *myDatabase;
sqlite3_stmt *myPreparedStatement;
const char *myTail;
int myResult;

QgsDebugMsg( QString("Trying to load style for \"%1\" from \"%2\"").arg(theURI).arg(db) );

myResult = sqlite3_open(db.toUtf8().data(), &myDatabase);
if (!myResult)
{
return false;
}

QString mySql = "select qml from tbl_styles where style=?";
myResult = sqlite3_prepare(myDatabase, mySql.toUtf8().data(), mySql.length(), &myPreparedStatement, &myTail);
if (myResult==SQLITE_OK)
{
QByteArray param = theURI.toUtf8();

if( sqlite3_bind_text(myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC)==SQLITE_OK &&
sqlite3_step(myPreparedStatement)==SQLITE_ROW )
{
qml = QString::fromUtf8( (char *)sqlite3_column_text(myPreparedStatement, 0) );
theResultFlag = true;
}

sqlite3_finalize(myPreparedStatement);
}

sqlite3_close(myDatabase);

return theResultFlag;
}

QString QgsMapLayer::loadNamedStyle ( const QString theURI, bool &theResultFlag)
{
theResultFlag = false;

QDomDocument myDocument ( "qgis" );
QDomDocument myDocument( "qgis" );

// location of problem associated with errorMsg
int line, column;
Expand All @@ -440,50 +480,32 @@ QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFla
myFile.close();
}
else
{
// read from database
sqlite3 *myDatabase;
sqlite3_stmt *myPreparedStatement;
const char *myTail;
int myResult;

myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase);
if (myResult)
{
return tr("could not open user database");
}
{
QFileInfo project( QgsProject::instance()->filename() );
QgsDebugMsg( QString("project filename: %1").arg( project.absoluteFilePath() ) );

QString mySql = "select qml from tbl_styles where style=?";
myResult = sqlite3_prepare(myDatabase, mySql.toUtf8().data(), mySql.length(), &myPreparedStatement, &myTail);
if (myResult==SQLITE_OK)
QString qml;
if( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) ||
( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml) ) ||
loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml) )
{
QByteArray param = theURI.toUtf8();

if( sqlite3_bind_text(myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC)==SQLITE_OK &&
sqlite3_step(myPreparedStatement)==SQLITE_ROW )
theResultFlag = myDocument.setContent ( qml, &myErrorMessage, &line, &column );
if(!theResultFlag)
{
QString qml = QString::fromUtf8( (char *)sqlite3_column_text(myPreparedStatement, 0) );
theResultFlag = myDocument.setContent ( qml, &myErrorMessage, &line, &column );
if(!theResultFlag)
{
myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column);
}
myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column);
}
}
else
{
theResultFlag = false;
myErrorMessage = tr("style %1 not found in database").arg(theURI);
myErrorMessage = tr("style not found in database");
}

sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
}

if(!theResultFlag)
{
return myErrorMessage;
}

// now get the layer node out and pass it over to the layer
// to deserialise...
QDomElement myRoot = myDocument.firstChildElement("qgis");
Expand Down Expand Up @@ -587,7 +609,7 @@ QString QgsMapLayer::saveNamedStyle ( const QString theURI, bool & theResultFlag
const char *myTail;
int myResult;

myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase);
myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb").toUtf8().data(), &myDatabase);
if (myResult)
{
return tr("User database could not be opened.");
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsmaplayer.h
Expand Up @@ -201,7 +201,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
* @see also loadNamedStyle ();
*/
virtual QString loadDefaultStyle ( bool & theResultFlag );

/** Retrieve a named style for this layer if one
* exists (either as a .qml file on disk or as a
* record in the users style table in their personal qgis.db)
Expand All @@ -217,6 +217,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
virtual QString loadNamedStyle ( const QString theURI , bool & theResultFlag );

virtual bool loadNamedStyleFromDb ( const QString db, const QString theURI , QString &qml );

/** Save the properties of this layer as the default style
* (either as a .qml file on disk or as a
* record in the users style table in their personal qgis.db)
Expand Down

0 comments on commit f13b48a

Please sign in to comment.