Skip to content
Permalink
Browse files

import and export of styles done

  • Loading branch information
tecoholic committed Jul 28, 2012
1 parent 8be4178 commit adddc022b8ec45e2182004799664f6344024748c
@@ -40,6 +40,7 @@ QgsStyleV2* QgsStyleV2::mDefaultStyle = NULL;

QgsStyleV2::QgsStyleV2()
{
mCurrentDB = NULL;
}

QgsStyleV2::~QgsStyleV2()
@@ -75,8 +76,8 @@ void QgsStyleV2::clear()

mSymbols.clear();
mColorRamps.clear();

sqlite3_close( mCurrentDB );
if ( mCurrentDB )
sqlite3_close( mCurrentDB );
}

bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
@@ -88,7 +89,6 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
delete mSymbols.value( name );

mSymbols.insert( name, symbol );
saveSymbol( name, symbol, 0, QStringList() );

return true;
}
@@ -182,9 +182,19 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
if ( mColorRamps.contains( name ) )
delete mColorRamps.value( name );

mColorRamps.insert( name, colorRamp );
return true;
}

bool QgsStyleV2::saveColorRamp( QString name, QgsVectorColorRampV2* ramp, int groupid, QStringList tags )
{
// TODO add support for groups and tags
Q_UNUSED( groupid );
Q_UNUSED( tags );

// insert it into the database
QDomDocument doc( "dummy" );
QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp( name, colorRamp, doc );
QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp( name, ramp, doc );
if ( rampEl.isNull() )
{
QgsDebugMsg( "Couldnot convert color ramp to valid XML!" );
@@ -204,7 +214,6 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
return false;
}

mColorRamps.insert( name, colorRamp );
return true;
}

@@ -271,7 +280,11 @@ bool QgsStyleV2::load( QString filename )

// Open the sqlite database
if ( !openDB( filename ) )
{
mErrorString = "Unable to open database file specified";
QgsDebugMsg( mErrorString );
return false;
}
// First create all the Main symbols
sqlite3_stmt *ppStmt;
const char *query = "SELECT * FROM symbol;";
@@ -1223,3 +1236,128 @@ QString QgsStyleV2::smartgroupOperator( int id )

return op;
}

bool QgsStyleV2::exportXML( QString filename )
{
if ( filename.isEmpty() )
{
QgsDebugMsg( "Invalid filename for style export." );
return false;
}

QDomDocument doc( "qgis_style" );
QDomElement root = doc.createElement( "qgis_style" );
root.setAttribute( "version", STYLE_CURRENT_VERSION );
doc.appendChild( root );

// TODO work on the groups and tags

QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( mSymbols, "symbols", doc );

QDomElement rampsElem = doc.createElement( "colorramps" );

// save color ramps
for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps.begin(); itr != mColorRamps.end(); ++itr )
{
QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp( itr.key(), itr.value(), doc );
rampsElem.appendChild( rampEl );
}

root.appendChild( symbolsElem );
root.appendChild( rampsElem );

// save
QFile f( filename );
if ( !f.open( QFile::WriteOnly ) )
{
mErrorString = "Couldn't open file for writing: " + filename;
return false;
}
QTextStream ts( &f );
doc.save( ts, 2 );
f.close();

mFileName = filename;
return true;

}

bool QgsStyleV2::importXML( QString filename )
{
mErrorString = QString();
QDomDocument doc( "style" );
QFile f( filename );
if ( !f.open( QFile::ReadOnly ) )
{
mErrorString = "Unable to open the specified file";
QgsDebugMsg( "Error opening the style XML file.");
return false;
}

if( !doc.setContent( &f ) )
{
mErrorString = "Unbale to understand the style file.";
QgsDebugMsg( "XML Parsing error" );
f.close();
return false;
}
f.close();

QDomElement docEl = doc.documentElement();
if ( docEl.tagName() != "qgis_style" )
{
mErrorString = "Incoerrect root tag in style: " + docEl.tagName();
return false;
}

QString version = docEl.attribute( "version" );
if ( version != STYLE_CURRENT_VERSION )
{
mErrorString = "Unknown style file version: " + version;
return false;
}

QgsStyleV2* defStyle = QgsStyleV2::defaultStyle();

// load symbols
QDomElement symbolsElement = docEl.firstChildElement( "symbols" );
QDomElement e = symbolsElement.firstChildElement();
while ( !e.isNull() )
{
if ( e.tagName() == "symbol" )
{
QgsSymbolV2* symbol = QgsSymbolLayerV2Utils::loadSymbol( e );
if ( symbol != NULL )
{
addSymbol( e.attribute( "name" ), symbol );
}
}
else
{
QgsDebugMsg( "unknown tag: " + e.tagName() );
}
e = e.nextSiblingElement();
}

// load color ramps
QDomElement rampsElement = docEl.firstChildElement( "colorramps" );
e = rampsElement.firstChildElement();
while ( !e.isNull() )
{
if ( e.tagName() == "colorramp" )
{
QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp( e );
if ( ramp != NULL )
{
addColorRamp( e.attribute( "name" ), ramp );
}
}
else
{
QgsDebugMsg( "unknown tag: " + e.tagName() );
}
e = e.nextSiblingElement();
}
mFileName = filename;
return true;
}
@@ -115,6 +115,8 @@ class CORE_EXPORT QgsStyleV2

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

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

//! Exports the style as a XML file
bool exportXML( QString filename );

//! Imports the symbols and colorramps into the default style database from the given XML file
bool importXML( QString filename );

protected:

QgsSymbolV2Map mSymbols;
@@ -13,21 +13,20 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsstylev2exportimportdialog.h"

#include "qgsstylev2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgslogger.h"

#include <QCloseEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QStandardItemModel>

#include "qgsstylev2exportimportdialog.h"

#include "qgsstylev2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"

QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent, Mode mode, QString fileName )
: QDialog( parent )
, mFileName( fileName )
@@ -102,7 +101,7 @@ void QgsStyleV2ExportImportDialog::doExportImport()
mFileName = fileName;

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

// clear model
@@ -130,10 +130,11 @@ bool QgsStyleV2ExportImportDialog::populateStyles( QgsStyleV2* style )
// load symbols and color ramps from file
if ( mDialogMode == Import )
{
if ( !mTempStyle->load( mFileName ) )
// NOTE mTempStyle is style here
if ( !style->importXML( mFileName ) )
{
QMessageBox::warning( this, tr( "Import error" ),
tr( "An error occured during import:\n%1" ).arg( mTempStyle->errorString() ) );
tr( "An error occured during import:\n%1" ).arg( style->errorString() ) );
return false;
}
}
@@ -210,6 +211,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
continue;
case QMessageBox::Yes:
dst->addSymbol( symbolName, symbol );
if ( mDialogMode == Import )
dst->saveSymbol( symbolName, symbol, 0, QStringList() );
continue;
case QMessageBox::YesToAll:
prompt = false;
@@ -225,6 +228,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
if ( dst->symbolNames().contains( symbolName ) && overwrite )
{
dst->addSymbol( symbolName, symbol );
if ( mDialogMode == Import )
dst->saveSymbol( symbolName, symbol, 0, QStringList() );
}
else if ( dst->symbolNames().contains( symbolName ) && !overwrite )
{
@@ -233,6 +238,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
else
{
dst->addSymbol( symbolName, symbol );
if ( mDialogMode == Import )
dst->saveSymbol( symbolName, symbol, 0, QStringList() );
}
}
else
@@ -251,6 +258,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
continue;
case QMessageBox::Yes:
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
continue;
case QMessageBox::YesToAll:
prompt = false;
@@ -265,6 +274,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
if ( dst->colorRampNames().contains( symbolName ) && overwrite )
{
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
}
else if ( dst->colorRampNames().contains( symbolName ) && !overwrite )
{
@@ -273,6 +284,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
else
{
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
}
}
}

0 comments on commit adddc02

Please sign in to comment.
You can’t perform that action at this time.