Showing with 288 additions and 70 deletions.
  1. +94 −11 src/core/symbology-ng/qgsstylev2.cpp
  2. +177 −45 src/core/symbology-ng/qgsstylev2.h
  3. +5 −5 src/gui/symbology-ng/qgsstylev2managerdialog.cpp
  4. +12 −9 tests/src/core/testqgsstylev2.cpp
105 changes: 94 additions & 11 deletions src/core/symbology-ng/qgsstylev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,26 @@ void QgsStyleV2::clear()
sqlite3_close( mCurrentDB );
}

bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol, bool update )
{
if ( !symbol || name.isEmpty() )
return false;

delete mSymbols.value( name );

mSymbols.insert( name, symbol );
// delete previous symbol (if any)
if ( mSymbols.contains( name ) )
{
// TODO remove groups and tags?
delete mSymbols.value( name );
mSymbols.insert( name, symbol );
if ( update )
updateSymbol( SymbolEntity, name );
}
else
{
mSymbols.insert( name, symbol );
if ( update )
saveSymbol( name, symbol, 0, QStringList() );
}

return true;
}
Expand All @@ -108,7 +120,7 @@ bool QgsStyleV2::saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QSt
QByteArray xmlArray;
QTextStream stream( &xmlArray );
symEl.save( stream, 4 );
char *query = sqlite3_mprintf( "INSERT INTO symbol VALUES (NULL, '%q', '%q', %d)",
char *query = sqlite3_mprintf( "INSERT INTO symbol VALUES (NULL, '%q', '%q', %d);",
name.toUtf8().constData(), xmlArray.constData(), groupid );

if ( !runEmptyQuery( query ) )
Expand Down Expand Up @@ -170,16 +182,26 @@ QStringList QgsStyleV2::symbolNames()
}


bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp, bool update )
{
if ( !colorRamp || name.isEmpty() )
return false;

// delete previous symbol (if any)
delete mColorRamps.value( name );

QgsDebugMsg( "Inserted " + name );
mColorRamps.insert( name, colorRamp );
if ( mSymbols.contains( name ) )
{
// TODO remove groups and tags?
delete mColorRamps.value( name );
mColorRamps.insert( name, colorRamp );
if ( update )
updateSymbol( ColorrampEntity, name );
}
else
{
mColorRamps.insert( name, colorRamp );
if ( update )
saveColorRamp( name, colorRamp, 0, QStringList() );
}

return true;
}
Expand All @@ -201,7 +223,7 @@ bool QgsStyleV2::saveColorRamp( QString name, QgsVectorColorRampV2* ramp, int gr
QByteArray xmlArray;
QTextStream stream( &xmlArray );
rampEl.save( stream, 4 );
char *query = sqlite3_mprintf( "INSERT INTO colorramp VALUES (NULL, '%q', '%q', %d)",
char *query = sqlite3_mprintf( "INSERT INTO colorramp VALUES (NULL, '%q', '%q', %d);",
name.toUtf8().constData(), xmlArray.constData(), groupid );

if ( !runEmptyQuery( query ) )
Expand Down Expand Up @@ -1378,3 +1400,64 @@ bool QgsStyleV2::importXML( QString filename )
mFileName = filename;
return true;
}

bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
{
QDomDocument doc( "dummy" );
QDomElement symEl;
QByteArray xmlArray;
QTextStream stream( &xmlArray );

char *query;

if ( type == SymbolEntity )
{
// check if it is an existing symbol
if ( !symbolNames().contains( name ) )
{
QgsDebugMsg( "Update request received for unavailable symbol" );
return false;
}

symEl = QgsSymbolLayerV2Utils::saveSymbol( name, symbol( name ), doc );
if ( symEl.isNull() )
{
QgsDebugMsg( "Couldn't convert symbol to valid XML!" );
return false;
}
symEl.save( stream, 4 );
query = sqlite3_mprintf( "UPDATE symbol SET xml='%q' WHERE name='%q';",
xmlArray.constData(), name.toUtf8().constData() );
}
else if ( type == ColorrampEntity )
{
if ( !colorRampNames().contains( name ) )
{
QgsDebugMsg( "Update requested for unavailable color ramp." );
return false;
}

symEl = QgsSymbolLayerV2Utils::saveColorRamp( name, colorRamp( name ), doc );
if ( symEl.isNull() )
{
QgsDebugMsg( "Couldn't convert color ramp to valid XML!" );
return false;
}
symEl.save( stream, 4 );
query = sqlite3_mprintf( "UPDATE colorramp SET xml='%q' WHERE name='%q';",
xmlArray.constData(), name.toUtf8().constData() );
}
else
{
QgsDebugMsg( "Updating the unsupported StyleEntity" );
return false;
}


if ( !runEmptyQuery( query ) )
{
QgsDebugMsg( "Couldn't insert symbol into the database!" );
return false;
}
return true;
}
Loading