Added support for creating nested symbol XML and save symbol to DB

This commit is contained in:
Arunmozhi 2012-06-10 16:08:26 +05:30
parent 16a69093b4
commit ca1026d802
5 changed files with 44 additions and 20 deletions

View File

@ -1265,7 +1265,7 @@ class QgsSymbolLayerV2Utils
static QgsSymbolV2* loadSymbol( QDomElement& element ) /Factory/;
static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element ) /Factory/;
static QDomElement saveSymbol( QString name, QgsSymbolV2* symbol, QDomDocument& doc, QgsSymbolV2Map* subSymbols = NULL );
static QDomElement saveSymbol( QString name, QgsSymbolV2* symbol, QDomDocument& doc );
static QgsStringMap parseProperties( QDomElement& element );
static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );

View File

@ -80,7 +80,6 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
if ( !symbol || name.count() == 0 )
return false;
// delete previous symbol (if any)
if ( mSymbols.contains( name ) )
delete mSymbols.value( name );
@ -88,6 +87,35 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
return true;
}
bool QgsStyleV2::saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QStringList tags )
{
// TODO add support for tags and groups
Q_UNUSED( groupid );
Q_UNUSED( tags );
QDomDocument doc( "dummy" );
QDomElement symEl = QgsSymbolLayerV2Utils::saveSymbol( name, symbol, doc );
if ( symEl.isNull() )
{
QgsDebugMsg( "Couldnot convert symbol to valid XML!" );
return false;
}
QByteArray *xmlArray = new QByteArray();
QTextStream stream( xmlArray );
symEl.save( stream, 4 );
QByteArray nameArray = name.toUtf8();
char *query = sqlite3_mprintf( "INSERT INTO symbol VALUES (NULL, '%q', '%q', NULL);",
nameArray.constData(), xmlArray->constData() );
if ( !runEmptyQuery( query ) )
{
QgsDebugMsg( "Couldnot insert symbol into the Database!" );
return false;
}
return true;
}
bool QgsStyleV2::removeSymbol( QString name )
{
if ( !mSymbols.contains( name ) )
@ -543,7 +571,10 @@ bool QgsStyleV2::runEmptyQuery( char *query )
return false;
int nErr = sqlite3_exec( db, query, NULL, NULL, &zErr );
if ( nErr )
{
QgsDebugMsg( zErr );
return false;
}
sqlite3_close( db );
return true;
}

View File

@ -98,6 +98,9 @@ class CORE_EXPORT QgsStyleV2
//! remove the specified entity from the db
void remove( StyleEntity type, int id );
//! add the symbol to the DB with the tags
bool saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QStringList tags );
//! add color ramp to style. takes ramp's ownership
bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );

View File

@ -717,16 +717,16 @@ static QString _nameForSymbolType( QgsSymbolV2::SymbolType type )
}
}
QDomElement QgsSymbolLayerV2Utils::saveSymbol( QString name, QgsSymbolV2* symbol, QDomDocument& doc, QgsSymbolV2Map* subSymbols )
QDomElement QgsSymbolLayerV2Utils::saveSymbol( QString name, QgsSymbolV2* symbol, QDomDocument& doc )
{
Q_ASSERT( symbol );
QDomElement symEl = doc.createElement( "symbol" );
symEl.setAttribute( "type", _nameForSymbolType( symbol->type() ) );
symEl.setAttribute( "name", name );
symEl.setAttribute( "outputUnit", encodeOutputUnit( symbol->outputUnit() ) );
symEl.setAttribute( "alpha", QString::number( symbol->alpha() ) );
QgsDebugMsg( "num layers " + QString::number( symbol->symbolLayerCount() ) );
for ( int i = 0; i < symbol->symbolLayerCount(); i++ )
{
QgsSymbolLayerV2* layer = symbol->symbolLayer( i );
@ -735,14 +735,13 @@ QDomElement QgsSymbolLayerV2Utils::saveSymbol( QString name, QgsSymbolV2* symbol
layerEl.setAttribute( "class", layer->layerType() );
layerEl.setAttribute( "locked", layer->isLocked() );
layerEl.setAttribute( "pass", layer->renderingPass() );
if ( subSymbols != NULL && layer->subSymbol() != NULL )
saveProperties( layer->properties(), doc, layerEl );
if ( layer->subSymbol() != NULL )
{
QString subname = QString( "@%1@%2" ).arg( name ).arg( i );
subSymbols->insert( subname, layer->subSymbol() );
QDomElement subEl = saveSymbol( subname, layer->subSymbol(), doc );
layerEl.appendChild( subEl );
}
saveProperties( layer->properties(), doc, layerEl );
symEl.appendChild( layerEl );
}
@ -2270,22 +2269,13 @@ QDomElement QgsSymbolLayerV2Utils::saveSymbols( QgsSymbolV2Map& symbols, QString
{
QDomElement symbolsElem = doc.createElement( tagName );
QMap<QString, QgsSymbolV2*> subSymbols;
// save symbols
for ( QMap<QString, QgsSymbolV2*>::iterator its = symbols.begin(); its != symbols.end(); ++its )
{
QDomElement symEl = saveSymbol( its.key(), its.value(), doc, &subSymbols );
QDomElement symEl = saveSymbol( its.key(), its.value(), doc );
symbolsElem.appendChild( symEl );
}
// add subsymbols, don't allow subsymbols for them (to keep things simple)
for ( QMap<QString, QgsSymbolV2*>::iterator itsub = subSymbols.begin(); itsub != subSymbols.end(); ++itsub )
{
QDomElement subsymEl = saveSymbol( itsub.key(), itsub.value(), doc );
symbolsElem.appendChild( subsymEl );
}
return symbolsElem;
}

View File

@ -99,7 +99,7 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
static QgsSymbolV2* loadSymbol( QDomElement& element );
static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
static QDomElement saveSymbol( QString name, QgsSymbolV2* symbol, QDomDocument& doc, QgsSymbolV2Map* subSymbols = NULL );
static QDomElement saveSymbol( QString symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );