38
38
#include " qgsmaplayer.h"
39
39
#include " qgsspatialrefsys.h"
40
40
#include " qgsapplication.h"
41
+ #include " qgsproject.h"
41
42
42
43
QgsMapLayer::QgsMapLayer (int type,
43
44
QString lyrname,
@@ -420,11 +421,50 @@ QString QgsMapLayer::loadDefaultStyle ( bool & theResultFlag )
420
421
return loadNamedStyle ( key, theResultFlag );
421
422
}
422
423
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)
424
464
{
425
465
theResultFlag = false ;
426
466
427
- QDomDocument myDocument ( " qgis" );
467
+ QDomDocument myDocument ( " qgis" );
428
468
429
469
// location of problem associated with errorMsg
430
470
int line, column;
@@ -440,50 +480,32 @@ QString QgsMapLayer::loadNamedStyle ( const QString theURI , bool & theResultFla
440
480
myFile.close ();
441
481
}
442
482
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 () ) );
455
486
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) )
459
491
{
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)
464
494
{
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);
471
496
}
472
497
}
473
498
else
474
499
{
475
- theResultFlag = false ;
476
- myErrorMessage = tr (" style %1 not found in database" ).arg (theURI);
500
+ myErrorMessage = tr (" style not found in database" );
477
501
}
478
-
479
- sqlite3_finalize (myPreparedStatement);
480
- sqlite3_close (myDatabase);
481
502
}
482
503
483
504
if (!theResultFlag)
484
505
{
485
506
return myErrorMessage;
486
507
}
508
+
487
509
// now get the layer node out and pass it over to the layer
488
510
// to deserialise...
489
511
QDomElement myRoot = myDocument.firstChildElement (" qgis" );
@@ -587,7 +609,7 @@ QString QgsMapLayer::saveNamedStyle ( const QString theURI, bool & theResultFlag
587
609
const char *myTail;
588
610
int myResult;
589
611
590
- myResult = sqlite3_open (QgsApplication::qgisUserDbFilePath ( ).toUtf8 ().data (), &myDatabase);
612
+ myResult = sqlite3_open ( QDir ( QgsApplication::qgisSettingsDirPath () ). absoluteFilePath ( " qgis.qmldb " ).toUtf8 ().data (), &myDatabase);
591
613
if (myResult)
592
614
{
593
615
return tr (" User database could not be opened." );
0 commit comments