2727#include < QDomElement>
2828#include < QFile>
2929#include < QTextStream>
30+ #include < QByteArray>
31+
32+ #include < sqlite3.h>
3033
3134#define STYLE_CURRENT_VERSION " 0"
3235
@@ -167,69 +170,71 @@ QStringList QgsStyleV2::colorRampNames()
167170 return mColorRamps .keys ();
168171}
169172
170-
171- bool QgsStyleV2::load ( QString filename )
173+ sqlite3* QgsStyleV2::openDB ( QString filename )
172174{
173- mErrorString = QString ();
175+ sqlite3 *db;
176+ int rc;
174177
175- // import xml file
176- QDomDocument doc ( " style" );
177- QFile f ( filename );
178- if ( !f.open ( QFile::ReadOnly ) )
178+ QByteArray namearray = filename.toUtf8 ();
179+ rc = sqlite3_open ( namearray.constData (), &db );
180+ if ( rc )
179181 {
180- mErrorString = " Couldn't open the style file: " + filename;
181- return false ;
182+ mErrorString = " Couldn't open the style DB: " + QString ( sqlite3_errmsg ( db ) );
183+ sqlite3_close ( db );
184+ return NULL ;
182185 }
183186
184- // parse the document
185- if ( !doc.setContent ( &f ) )
186- {
187- mErrorString = " Couldn't parse the style file: " + filename;
188- f.close ();
189- return false ;
190- }
191- f.close ();
187+ return db;
188+ }
192189
193- QDomElement docElem = doc.documentElement ();
194- if ( docElem.tagName () != " qgis_style" )
195- {
196- mErrorString = " Incorrect root tag in style: " + docElem.tagName ();
197- return false ;
198- }
190+ bool QgsStyleV2::load ( QString filename )
191+ {
192+ mErrorString = QString ();
199193
200- // check for style version
201- QString version = docElem.attribute ( " version" );
202- if ( version != STYLE_CURRENT_VERSION )
203- {
204- mErrorString = " Unknown style file version: " + version;
194+ // Open the sqlite DB
195+ sqlite3* db = openDB ( filename );
196+ if ( db == NULL )
205197 return false ;
206- }
207-
208- // load symbols
209- QDomElement symbolsElement = docElem. firstChildElement ( " symbols " );
210- if ( !symbolsElement. isNull () )
198+ // First create all the Main symbols
199+ sqlite3_stmt *ppStmt;
200+ const char *query = " SELECT * FROM symbol; " ;
201+ int nError = sqlite3_prepare_v2 ( db, query, - 1 , &ppStmt, NULL );
202+ while ( nError == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
211203 {
212- mSymbols = QgsSymbolLayerV2Utils::loadSymbols ( symbolsElement );
204+ QDomDocument doc;
205+ QString symbol_name = QString ( reinterpret_cast <const char *>( sqlite3_column_text ( ppStmt, SymbolName ) ) );
206+ QString xmlstring = QString ( reinterpret_cast <const char *>( sqlite3_column_text ( ppStmt, SymbolXML ) ) );
207+ if ( !doc.setContent ( xmlstring ) )
208+ {
209+ QgsDebugMsg ( " Cannot open symbol" + symbol_name );
210+ continue ;
211+ }
212+ QDomElement symElement = doc.documentElement ();
213+ QgsSymbolV2 *symbol = QgsSymbolLayerV2Utils::loadSymbol ( symElement );
214+ if ( symbol != NULL )
215+ mSymbols .insert ( symbol_name, symbol );
213216 }
217+ sqlite3_finalize ( ppStmt );
214218
215- // load color ramps
216- QDomElement rampsElement = docElem.firstChildElement ( " colorramps" );
217- QDomElement e = rampsElement.firstChildElement ();
218- while ( !e.isNull () )
219+ const char *rquery = " SELECT * FROM colorramp;" ;
220+ nError = sqlite3_prepare_v2 ( db, rquery, -1 , &ppStmt, NULL );
221+ while ( nError == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
219222 {
220- if ( e.tagName () == " colorramp" )
223+ QDomDocument doc;
224+ QString ramp_name = QString ( reinterpret_cast <const char *>( sqlite3_column_text ( ppStmt, ColorrampName ) ) );
225+ QString xmlstring = QString ( reinterpret_cast <const char *>( sqlite3_column_text ( ppStmt, ColorrampXML ) ) );
226+ if ( !doc.setContent ( xmlstring ) )
221227 {
222- QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp ( e );
223- if ( ramp != NULL )
224- addColorRamp ( e.attribute ( " name" ), ramp );
228+ QgsDebugMsg ( " Cannot open symbol" + ramp_name );
229+ continue ;
225230 }
226- else
227- {
228- QgsDebugMsg ( " unknown tag: " + e.tagName () );
229- }
230- e = e.nextSiblingElement ();
231+ QDomElement rampElement = doc.documentElement ();
232+ QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp ( rampElement );
233+ if ( ramp != NULL )
234+ addColorRamp ( ramp_name, ramp );
231235 }
232236
237+ sqlite3_close ( db );
233238 mFileName = filename;
234239 return true ;
235240}
@@ -242,6 +247,9 @@ bool QgsStyleV2::save( QString filename )
242247 if ( filename.isEmpty () )
243248 filename = mFileName ;
244249
250+ // TODO evaluate the requirement of this function and change implementation accordingly
251+
252+ /*
245253 QDomDocument doc( "qgis_style" );
246254 QDomElement root = doc.createElement( "qgis_style" );
247255 root.setAttribute( "version", STYLE_CURRENT_VERSION );
@@ -271,6 +279,7 @@ bool QgsStyleV2::save( QString filename )
271279 QTextStream ts( &f );
272280 doc.save( ts, 2 );
273281 f.close();
282+ */
274283
275284 mFileName = filename;
276285 return true ;
@@ -293,3 +302,68 @@ bool QgsStyleV2::renameColorRamp( QString oldName, QString newName )
293302 mColorRamps .insert ( newName, mColorRamps .take ( oldName ) );
294303 return true ;
295304}
305+
306+ QgsSymbolGroupMap QgsStyleV2::groupNames ( QString parent )
307+ {
308+ QgsDebugMsg ( " Request for groupNames for parent " + parent );
309+ // get the name list from the sqlite db and return as a QStringList
310+ sqlite3* db = openDB ( mFileName );
311+ if ( db == NULL )
312+ {
313+ QgsDebugMsg ( " Cannot open database for listing groups" );
314+ return QgsSymbolGroupMap ();
315+ }
316+ sqlite3_stmt *ppStmt;
317+ int nError;
318+ char *query;
319+
320+ if ( parent == " " || parent == QString () )
321+ {
322+ query = sqlite3_mprintf ( " SELECT * FROM symgroup WHERE parent IS NULL;" );
323+ }
324+ else
325+ {
326+ QByteArray parentArray = parent.toUtf8 ();
327+ char *subquery = sqlite3_mprintf ( " SELECT * FROM symgroup WHERE name='%q';" , parentArray.constData () );
328+ nError = sqlite3_prepare_v2 ( db, subquery, -1 , &ppStmt, NULL );
329+ if ( nError == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
330+ {
331+ query = sqlite3_mprintf ( " SELECT * FROM symgroup WHERE parent=%d;" , sqlite3_column_int ( ppStmt, SymgroupId ) );
332+ }
333+ sqlite3_finalize ( ppStmt );
334+ }
335+
336+ QgsSymbolGroupMap groupNames;
337+
338+ nError = sqlite3_prepare_v2 ( db, query, -1 , &ppStmt, NULL );
339+ while ( nError == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
340+ {
341+ QString group = QString ( reinterpret_cast <const char *>( sqlite3_column_text ( ppStmt, SymgroupName ) ) );
342+ groupNames.insert ( sqlite3_column_int ( ppStmt, SymgroupId ), group );
343+ }
344+ sqlite3_finalize ( ppStmt );
345+ sqlite3_close ( db );
346+ return groupNames;
347+ }
348+
349+ QStringList QgsStyleV2::symbolsOfGroup ( int groupid )
350+ {
351+ sqlite3 *db = openDB ( mFileName );
352+ if ( db == NULL )
353+ {
354+ QgsDebugMsg ( " Cannot Open Db for getting group symbols of groupid: " + groupid );
355+ return QStringList ();
356+ }
357+
358+ QStringList symbols;
359+ sqlite3_stmt *ppStmt;
360+ char *query = sqlite3_mprintf ( " SELECT name FROM symbol WHERE groupid=%d;" , groupid );
361+ int nErr = sqlite3_prepare_v2 ( db, query, -1 , &ppStmt, NULL );
362+ while ( nErr == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
363+ {
364+ QString symbol = QString ( reinterpret_cast <const char *>( sqlite3_column_text ( ppStmt, 0 ) ) );
365+ symbols.append ( symbol );
366+ }
367+
368+ return symbols;
369+ }
0 commit comments