Navigation Menu

Skip to content

Commit

Permalink
import and export of styles done
Browse files Browse the repository at this point in the history
  • Loading branch information
Arunmozhi committed Jul 28, 2012
1 parent 8be4178 commit adddc02
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 15 deletions.
148 changes: 143 additions & 5 deletions src/core/symbology-ng/qgsstylev2.cpp
Expand Up @@ -40,6 +40,7 @@ QgsStyleV2* QgsStyleV2::mDefaultStyle = NULL;

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

QgsStyleV2::~QgsStyleV2()
Expand Down Expand Up @@ -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 )
Expand All @@ -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;
}
Expand Down Expand Up @@ -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!" );
Expand All @@ -204,7 +214,6 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
return false;
}

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

Expand Down Expand Up @@ -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;";
Expand Down Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions src/core/symbology-ng/qgsstylev2.h
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 23 additions & 10 deletions src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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" )
Expand All @@ -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
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 )
{
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 )
{
Expand All @@ -273,6 +284,8 @@ void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsSt
else
{
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, 0, QStringList() );
}
}
}
Expand Down

0 comments on commit adddc02

Please sign in to comment.