Skip to content

Commit adddc02

Browse files
author
Arunmozhi
committed
import and export of styles done
1 parent 8be4178 commit adddc02

File tree

3 files changed

+174
-15
lines changed

3 files changed

+174
-15
lines changed

src/core/symbology-ng/qgsstylev2.cpp

Lines changed: 143 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ QgsStyleV2* QgsStyleV2::mDefaultStyle = NULL;
4040

4141
QgsStyleV2::QgsStyleV2()
4242
{
43+
mCurrentDB = NULL;
4344
}
4445

4546
QgsStyleV2::~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

8283
bool 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+
}

src/core/symbology-ng/qgsstylev2.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class CORE_EXPORT QgsStyleV2
115115

116116
//! add the symbol to the DB with the tags
117117
bool saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QStringList tags );
118+
//! add the colorramp to the DB
119+
bool saveColorRamp( QString name, QgsVectorColorRampV2* ramp, int groupid, QStringList tags );
118120

119121
//! add color ramp to style. takes ramp's ownership
120122
bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );
@@ -182,6 +184,12 @@ class CORE_EXPORT QgsStyleV2
182184
//! returns the symbols for the smartgroup
183185
QStringList symbolsOfSmartgroup( StyleEntity type, int id );
184186

187+
//! Exports the style as a XML file
188+
bool exportXML( QString filename );
189+
190+
//! Imports the symbols and colorramps into the default style database from the given XML file
191+
bool importXML( QString filename );
192+
185193
protected:
186194

187195
QgsSymbolV2Map mSymbols;

src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@
1313
* (at your option) any later version. *
1414
* *
1515
***************************************************************************/
16+
#include "qgsstylev2exportimportdialog.h"
1617

18+
#include "qgsstylev2.h"
19+
#include "qgssymbolv2.h"
20+
#include "qgssymbollayerv2utils.h"
21+
#include "qgsvectorcolorrampv2.h"
22+
#include "qgslogger.h"
1723

1824
#include <QCloseEvent>
1925
#include <QFileDialog>
2026
#include <QMessageBox>
2127
#include <QPushButton>
2228
#include <QStandardItemModel>
2329

24-
#include "qgsstylev2exportimportdialog.h"
25-
26-
#include "qgsstylev2.h"
27-
#include "qgssymbolv2.h"
28-
#include "qgssymbollayerv2utils.h"
29-
#include "qgsvectorcolorrampv2.h"
30-
3130
QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent, Mode mode, QString fileName )
3231
: QDialog( parent )
3332
, mFileName( fileName )
@@ -102,7 +101,7 @@ void QgsStyleV2ExportImportDialog::doExportImport()
102101
mFileName = fileName;
103102

104103
moveStyles( &selection, mQgisStyle, mTempStyle );
105-
if ( !mTempStyle->save( mFileName ) )
104+
if ( !mTempStyle->exportXML( mFileName ) )
106105
{
107106
QMessageBox::warning( this, tr( "Export/import error" ),
108107
tr( "Error when saving selected symbols to file:\n%1" )
@@ -113,6 +112,7 @@ void QgsStyleV2ExportImportDialog::doExportImport()
113112
else // import
114113
{
115114
moveStyles( &selection, mTempStyle, mQgisStyle );
115+
// TODO save in the move function itself using saveSymbol and saveColorRamp
116116
mQgisStyle->save();
117117

118118
// clear model
@@ -130,10 +130,11 @@ bool QgsStyleV2ExportImportDialog::populateStyles( QgsStyleV2* style )
130130
// load symbols and color ramps from file
131131
if ( mDialogMode == Import )
132132
{
133-
if ( !mTempStyle->load( mFileName ) )
133+
// NOTE mTempStyle is style here
134+
if ( !style->importXML( mFileName ) )
134135
{
135136
QMessageBox::warning( this, tr( "Import error" ),
136-
tr( "An error occured during import:\n%1" ).arg( mTempStyle->errorString() ) );
137+
tr( "An error occured during import:\n%1" ).arg( style->errorString() ) );
137138
return false;
138139
}
139140
}
@@ -210,6 +211,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
210211
continue;
211212
case QMessageBox::Yes:
212213
dst->addSymbol( symbolName, symbol );
214+
if ( mDialogMode == Import )
215+
dst->saveSymbol( symbolName, symbol, 0, QStringList() );
213216
continue;
214217
case QMessageBox::YesToAll:
215218
prompt = false;
@@ -225,6 +228,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
225228
if ( dst->symbolNames().contains( symbolName ) && overwrite )
226229
{
227230
dst->addSymbol( symbolName, symbol );
231+
if ( mDialogMode == Import )
232+
dst->saveSymbol( symbolName, symbol, 0, QStringList() );
228233
}
229234
else if ( dst->symbolNames().contains( symbolName ) && !overwrite )
230235
{
@@ -233,6 +238,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
233238
else
234239
{
235240
dst->addSymbol( symbolName, symbol );
241+
if ( mDialogMode == Import )
242+
dst->saveSymbol( symbolName, symbol, 0, QStringList() );
236243
}
237244
}
238245
else
@@ -251,6 +258,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
251258
continue;
252259
case QMessageBox::Yes:
253260
dst->addColorRamp( symbolName, ramp );
261+
if ( mDialogMode == Import )
262+
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
254263
continue;
255264
case QMessageBox::YesToAll:
256265
prompt = false;
@@ -265,6 +274,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
265274
if ( dst->colorRampNames().contains( symbolName ) && overwrite )
266275
{
267276
dst->addColorRamp( symbolName, ramp );
277+
if ( mDialogMode == Import )
278+
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
268279
}
269280
else if ( dst->colorRampNames().contains( symbolName ) && !overwrite )
270281
{
@@ -273,6 +284,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
273284
else
274285
{
275286
dst->addColorRamp( symbolName, ramp );
287+
if ( mDialogMode == Import )
288+
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
276289
}
277290
}
278291
}

0 commit comments

Comments
 (0)