[QGIS Commit] r8777 - trunk/qgis/src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Jul 14 16:23:43 EDT 2008


Author: jef
Date: 2008-07-14 16:23:43 -0400 (Mon, 14 Jul 2008)
New Revision: 8777

Modified:
   trunk/qgis/src/core/qgsmaplayer.cpp
   trunk/qgis/src/core/qgsmaplayer.h
Log:
introduce separate qml databases for fileless datasources (fixes #1127)

Modified: trunk/qgis/src/core/qgsmaplayer.cpp
===================================================================
--- trunk/qgis/src/core/qgsmaplayer.cpp	2008-07-14 20:23:14 UTC (rev 8776)
+++ trunk/qgis/src/core/qgsmaplayer.cpp	2008-07-14 20:23:43 UTC (rev 8777)
@@ -38,6 +38,7 @@
 #include "qgsmaplayer.h"
 #include "qgsspatialrefsys.h"
 #include "qgsapplication.h"
+#include "qgsproject.h"
 
 QgsMapLayer::QgsMapLayer(int type,
                          QString lyrname,
@@ -420,11 +421,50 @@
   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;
@@ -440,50 +480,32 @@
     myFile.close();
   }
   else
-  {
-    // read from database
-    sqlite3 *myDatabase; 
-    sqlite3_stmt *myPreparedStatement;
-    const char *myTail;
-    int myResult;
+  { 
+    QFileInfo project( QgsProject::instance()->filename() );
+    QgsDebugMsg( QString("project filename: %1").arg( project.absoluteFilePath() ) );
 
-    myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase);
-    if (myResult)
+    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) )
     {
-      return tr("could not open user database");
-    }
-
-    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 ) 
+      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");
@@ -587,7 +609,7 @@
     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.");

Modified: trunk/qgis/src/core/qgsmaplayer.h
===================================================================
--- trunk/qgis/src/core/qgsmaplayer.h	2008-07-14 20:23:14 UTC (rev 8776)
+++ trunk/qgis/src/core/qgsmaplayer.h	2008-07-14 20:23:43 UTC (rev 8777)
@@ -201,7 +201,7 @@
      * @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)
@@ -217,6 +217,8 @@
      */
     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)



More information about the QGIS-commit mailing list