@@ -40,6 +40,7 @@ QgsStyleV2* QgsStyleV2::mDefaultStyle = NULL;
4040
4141QgsStyleV2::QgsStyleV2 ()
4242{
43+ mCurrentDB = NULL ;
4344}
4445
4546QgsStyleV2::~QgsStyleV2 ()
@@ -75,8 +76,8 @@ void QgsStyleV2::clear()
7576
7677 mSymbols .clear ();
7778 mColorRamps .clear ();
78-
79- sqlite3_close ( mCurrentDB );
79+ if ( mCurrentDB )
80+ sqlite3_close ( mCurrentDB );
8081}
8182
8283bool QgsStyleV2::addSymbol ( QString name, QgsSymbolV2* symbol )
@@ -88,7 +89,6 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
8889 delete mSymbols .value ( name );
8990
9091 mSymbols .insert ( name, symbol );
91- saveSymbol ( name, symbol, 0 , QStringList () );
9292
9393 return true ;
9494}
@@ -182,9 +182,19 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
182182 if ( mColorRamps .contains ( name ) )
183183 delete mColorRamps .value ( name );
184184
185+ mColorRamps .insert ( name, colorRamp );
186+ return true ;
187+ }
188+
189+ bool QgsStyleV2::saveColorRamp ( QString name, QgsVectorColorRampV2* ramp, int groupid, QStringList tags )
190+ {
191+ // TODO add support for groups and tags
192+ Q_UNUSED ( groupid );
193+ Q_UNUSED ( tags );
194+
185195 // insert it into the database
186196 QDomDocument doc ( " dummy" );
187- QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp ( name, colorRamp , doc );
197+ QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp ( name, ramp , doc );
188198 if ( rampEl.isNull () )
189199 {
190200 QgsDebugMsg ( " Couldnot convert color ramp to valid XML!" );
@@ -204,7 +214,6 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
204214 return false ;
205215 }
206216
207- mColorRamps .insert ( name, colorRamp );
208217 return true ;
209218}
210219
@@ -271,7 +280,11 @@ bool QgsStyleV2::load( QString filename )
271280
272281 // Open the sqlite database
273282 if ( !openDB ( filename ) )
283+ {
284+ mErrorString = " Unable to open database file specified" ;
285+ QgsDebugMsg ( mErrorString );
274286 return false ;
287+ }
275288 // First create all the Main symbols
276289 sqlite3_stmt *ppStmt;
277290 const char *query = " SELECT * FROM symbol;" ;
@@ -1223,3 +1236,128 @@ QString QgsStyleV2::smartgroupOperator( int id )
12231236
12241237 return op;
12251238}
1239+
1240+ bool QgsStyleV2::exportXML ( QString filename )
1241+ {
1242+ if ( filename.isEmpty () )
1243+ {
1244+ QgsDebugMsg ( " Invalid filename for style export." );
1245+ return false ;
1246+ }
1247+
1248+ QDomDocument doc ( " qgis_style" );
1249+ QDomElement root = doc.createElement ( " qgis_style" );
1250+ root.setAttribute ( " version" , STYLE_CURRENT_VERSION );
1251+ doc.appendChild ( root );
1252+
1253+ // TODO work on the groups and tags
1254+
1255+ QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols ( mSymbols , " symbols" , doc );
1256+
1257+ QDomElement rampsElem = doc.createElement ( " colorramps" );
1258+
1259+ // save color ramps
1260+ for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps .begin (); itr != mColorRamps .end (); ++itr )
1261+ {
1262+ QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp ( itr.key (), itr.value (), doc );
1263+ rampsElem.appendChild ( rampEl );
1264+ }
1265+
1266+ root.appendChild ( symbolsElem );
1267+ root.appendChild ( rampsElem );
1268+
1269+ // save
1270+ QFile f ( filename );
1271+ if ( !f.open ( QFile::WriteOnly ) )
1272+ {
1273+ mErrorString = " Couldn't open file for writing: " + filename;
1274+ return false ;
1275+ }
1276+ QTextStream ts ( &f );
1277+ doc.save ( ts, 2 );
1278+ f.close ();
1279+
1280+ mFileName = filename;
1281+ return true ;
1282+
1283+ }
1284+
1285+ bool QgsStyleV2::importXML ( QString filename )
1286+ {
1287+ mErrorString = QString ();
1288+ QDomDocument doc ( " style" );
1289+ QFile f ( filename );
1290+ if ( !f.open ( QFile::ReadOnly ) )
1291+ {
1292+ mErrorString = " Unable to open the specified file" ;
1293+ QgsDebugMsg ( " Error opening the style XML file." );
1294+ return false ;
1295+ }
1296+
1297+ if ( !doc.setContent ( &f ) )
1298+ {
1299+ mErrorString = " Unbale to understand the style file." ;
1300+ QgsDebugMsg ( " XML Parsing error" );
1301+ f.close ();
1302+ return false ;
1303+ }
1304+ f.close ();
1305+
1306+ QDomElement docEl = doc.documentElement ();
1307+ if ( docEl.tagName () != " qgis_style" )
1308+ {
1309+ mErrorString = " Incoerrect root tag in style: " + docEl.tagName ();
1310+ return false ;
1311+ }
1312+
1313+ QString version = docEl.attribute ( " version" );
1314+ if ( version != STYLE_CURRENT_VERSION )
1315+ {
1316+ mErrorString = " Unknown style file version: " + version;
1317+ return false ;
1318+ }
1319+
1320+ QgsStyleV2* defStyle = QgsStyleV2::defaultStyle ();
1321+
1322+ // load symbols
1323+ QDomElement symbolsElement = docEl.firstChildElement ( " symbols" );
1324+ QDomElement e = symbolsElement.firstChildElement ();
1325+ while ( !e.isNull () )
1326+ {
1327+ if ( e.tagName () == " symbol" )
1328+ {
1329+ QgsSymbolV2* symbol = QgsSymbolLayerV2Utils::loadSymbol ( e );
1330+ if ( symbol != NULL )
1331+ {
1332+ addSymbol ( e.attribute ( " name" ), symbol );
1333+ }
1334+ }
1335+ else
1336+ {
1337+ QgsDebugMsg ( " unknown tag: " + e.tagName () );
1338+ }
1339+ e = e.nextSiblingElement ();
1340+ }
1341+
1342+ // load color ramps
1343+ QDomElement rampsElement = docEl.firstChildElement ( " colorramps" );
1344+ e = rampsElement.firstChildElement ();
1345+ while ( !e.isNull () )
1346+ {
1347+ if ( e.tagName () == " colorramp" )
1348+ {
1349+ QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp ( e );
1350+ if ( ramp != NULL )
1351+ {
1352+ addColorRamp ( e.attribute ( " name" ), ramp );
1353+ }
1354+ }
1355+ else
1356+ {
1357+ QgsDebugMsg ( " unknown tag: " + e.tagName () );
1358+ }
1359+ e = e.nextSiblingElement ();
1360+ }
1361+ mFileName = filename;
1362+ return true ;
1363+ }
0 commit comments