Skip to content

Commit e9d91b3

Browse files
author
jef
committed
introduce separate qml databases for fileless datasources (fixes #1127)
git-svn-id: http://svn.osgeo.org/qgis/trunk@8777 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent bf23700 commit e9d91b3

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

src/core/qgsmaplayer.cpp

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "qgsmaplayer.h"
3939
#include "qgsspatialrefsys.h"
4040
#include "qgsapplication.h"
41+
#include "qgsproject.h"
4142

4243
QgsMapLayer::QgsMapLayer(int type,
4344
QString lyrname,
@@ -420,11 +421,50 @@ QString QgsMapLayer::loadDefaultStyle ( bool & theResultFlag )
420421
return loadNamedStyle ( key, theResultFlag );
421422
}
422423

423-
QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFlag )
424+
bool QgsMapLayer::loadNamedStyleFromDb (const QString db, const QString theURI, QString &qml)
425+
{
426+
bool theResultFlag = false;
427+
428+
// read from database
429+
sqlite3 *myDatabase;
430+
sqlite3_stmt *myPreparedStatement;
431+
const char *myTail;
432+
int myResult;
433+
434+
QgsDebugMsg( QString("Trying to load style for \"%1\" from \"%2\"").arg(theURI).arg(db) );
435+
436+
myResult = sqlite3_open(db.toUtf8().data(), &myDatabase);
437+
if (!myResult)
438+
{
439+
return false;
440+
}
441+
442+
QString mySql = "select qml from tbl_styles where style=?";
443+
myResult = sqlite3_prepare(myDatabase, mySql.toUtf8().data(), mySql.length(), &myPreparedStatement, &myTail);
444+
if (myResult==SQLITE_OK)
445+
{
446+
QByteArray param = theURI.toUtf8();
447+
448+
if( sqlite3_bind_text(myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC)==SQLITE_OK &&
449+
sqlite3_step(myPreparedStatement)==SQLITE_ROW )
450+
{
451+
qml = QString::fromUtf8( (char *)sqlite3_column_text(myPreparedStatement, 0) );
452+
theResultFlag = true;
453+
}
454+
455+
sqlite3_finalize(myPreparedStatement);
456+
}
457+
458+
sqlite3_close(myDatabase);
459+
460+
return theResultFlag;
461+
}
462+
463+
QString QgsMapLayer::loadNamedStyle ( const QString theURI, bool &theResultFlag)
424464
{
425465
theResultFlag = false;
426466

427-
QDomDocument myDocument ( "qgis" );
467+
QDomDocument myDocument( "qgis" );
428468

429469
// location of problem associated with errorMsg
430470
int line, column;
@@ -440,50 +480,32 @@ QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFla
440480
myFile.close();
441481
}
442482
else
443-
{
444-
// read from database
445-
sqlite3 *myDatabase;
446-
sqlite3_stmt *myPreparedStatement;
447-
const char *myTail;
448-
int myResult;
449-
450-
myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase);
451-
if (myResult)
452-
{
453-
return tr("could not open user database");
454-
}
483+
{
484+
QFileInfo project( QgsProject::instance()->filename() );
485+
QgsDebugMsg( QString("project filename: %1").arg( project.absoluteFilePath() ) );
455486

456-
QString mySql = "select qml from tbl_styles where style=?";
457-
myResult = sqlite3_prepare(myDatabase, mySql.toUtf8().data(), mySql.length(), &myPreparedStatement, &myTail);
458-
if (myResult==SQLITE_OK)
487+
QString qml;
488+
if( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) ||
489+
( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml) ) ||
490+
loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml) )
459491
{
460-
QByteArray param = theURI.toUtf8();
461-
462-
if( sqlite3_bind_text(myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC)==SQLITE_OK &&
463-
sqlite3_step(myPreparedStatement)==SQLITE_ROW )
492+
theResultFlag = myDocument.setContent ( qml, &myErrorMessage, &line, &column );
493+
if(!theResultFlag)
464494
{
465-
QString qml = QString::fromUtf8( (char *)sqlite3_column_text(myPreparedStatement, 0) );
466-
theResultFlag = myDocument.setContent ( qml, &myErrorMessage, &line, &column );
467-
if(!theResultFlag)
468-
{
469-
myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column);
470-
}
495+
myErrorMessage = tr("%1 at line %2 column %3").arg( myErrorMessage ).arg( line ).arg(column);
471496
}
472497
}
473498
else
474499
{
475-
theResultFlag = false;
476-
myErrorMessage = tr("style %1 not found in database").arg(theURI);
500+
myErrorMessage = tr("style not found in database");
477501
}
478-
479-
sqlite3_finalize(myPreparedStatement);
480-
sqlite3_close(myDatabase);
481502
}
482503

483504
if(!theResultFlag)
484505
{
485506
return myErrorMessage;
486507
}
508+
487509
// now get the layer node out and pass it over to the layer
488510
// to deserialise...
489511
QDomElement myRoot = myDocument.firstChildElement("qgis");
@@ -587,7 +609,7 @@ QString QgsMapLayer::saveNamedStyle ( const QString theURI, bool & theResultFlag
587609
const char *myTail;
588610
int myResult;
589611

590-
myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase);
612+
myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb").toUtf8().data(), &myDatabase);
591613
if (myResult)
592614
{
593615
return tr("User database could not be opened.");

src/core/qgsmaplayer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
201201
* @see also loadNamedStyle ();
202202
*/
203203
virtual QString loadDefaultStyle ( bool & theResultFlag );
204-
204+
205205
/** Retrieve a named style for this layer if one
206206
* exists (either as a .qml file on disk or as a
207207
* record in the users style table in their personal qgis.db)
@@ -217,6 +217,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
217217
*/
218218
virtual QString loadNamedStyle ( const QString theURI , bool & theResultFlag );
219219

220+
virtual bool loadNamedStyleFromDb ( const QString db, const QString theURI , QString &qml );
221+
220222
/** Save the properties of this layer as the default style
221223
* (either as a .qml file on disk or as a
222224
* record in the users style table in their personal qgis.db)

0 commit comments

Comments
 (0)