Skip to content

Commit

Permalink
fix symbol and color ramp saving - issues #6222 and #6227
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Aug 23, 2012
1 parent 6bf56e8 commit b145998
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
47 changes: 32 additions & 15 deletions src/core/symbology-ng/qgsstylev2.cpp
Expand Up @@ -85,12 +85,21 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol, bool update )
if ( !symbol || name.isEmpty() )
return false;

delete mSymbols.value( name );

mSymbols.insert( name, symbol );

if ( update )
updateSymbol( SymbolEntity, name );
// 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 Down Expand Up @@ -179,12 +188,20 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp, bo
return false;

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

mColorRamps.insert( name, colorRamp );

if ( update )
updateSymbol( ColorrampEntity, name );
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 Down Expand Up @@ -1398,7 +1415,7 @@ bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
// check if it is an existing symbol
if ( !symbolNames().contains( name ) )
{
QgsDebugMsg( "Update request recieved for unavailable symbol" );
QgsDebugMsg( "Update request received for unavailable symbol" );
return false;
}

Expand All @@ -1410,7 +1427,7 @@ bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
}
symEl.save( stream, 4 );
query = sqlite3_mprintf( "UPDATE symbol SET xml='%q' WHERE name='%q';",
xmlArray.constData(), name.toUtf8().constData() );
xmlArray.constData(), name.toUtf8().constData() );
}
else if ( type == ColorrampEntity )
{
Expand All @@ -1428,7 +1445,7 @@ bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
}
symEl.save( stream, 4 );
query = sqlite3_mprintf( "UPDATE colorramp SET xml='%q' WHERE name='%q';",
xmlArray.constData(), name.toUtf8().constData() );
xmlArray.constData(), name.toUtf8().constData() );
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsstylev2.h
Expand Up @@ -90,7 +90,7 @@ class CORE_EXPORT QgsStyleV2

//! adds a new group and returns the group's id
/*!
* \param groupname the name of the new group as QString
* \param groupName the name of the new group as QString
* \param parent is the id of the parent group when a subgrouo is to be created. By default it is 0 indicating it is not a sub-group
* \return returns an int, which is the DB id of the new group created, 0 if the group couldn't be created
*/
Expand Down
6 changes: 3 additions & 3 deletions src/gui/symbology-ng/qgsstylev2managerdialog.cpp
Expand Up @@ -351,9 +351,8 @@ bool QgsStyleV2ManagerDialog::addSymbol()
}

// add new symbol to style and re-populate the list
mStyle->addSymbol( name, symbol );
mStyle->addSymbol( name, symbol, true );
// TODO groups and tags
mStyle->saveSymbol( name, symbol, 0, QStringList() );
mModified = true;
return true;
}
Expand Down Expand Up @@ -435,7 +434,8 @@ QString QgsStyleV2ManagerDialog::addColorRampStatic( QWidget* parent, QgsStyleV2
}

// add new symbol to style and re-populate the list
style->addColorRamp( name, ramp );
style->addColorRamp( name, ramp, true );
// TODO groups and tags
return name;
}

Expand Down
21 changes: 12 additions & 9 deletions tests/src/core/testqgsstylev2.cpp
Expand Up @@ -113,24 +113,24 @@ void TestStyleV2::testCreateColorRamps()
QgsVectorGradientColorRampV2::StopsMap stops;
stops[ 0.5 ] = QColor( Qt::white );
gradientRamp->setStops( stops );
QVERIFY( mStyle->addColorRamp( "test_gradient", gradientRamp ) );
QVERIFY( mStyle->addColorRamp( "test_gradient", gradientRamp, true ) );

// random ramp
QgsVectorRandomColorRampV2* randomRamp = new QgsVectorRandomColorRampV2();
QVERIFY( mStyle->addColorRamp( "test_random", randomRamp ) );
QVERIFY( mStyle->addColorRamp( "test_random", randomRamp, true ) );

// color brewer ramp
QgsVectorColorBrewerColorRampV2* cb1Ramp = new QgsVectorColorBrewerColorRampV2();
QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp ) );
QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp, true ) );
QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "RdYlGn", 6 );
QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp ) );
QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp, true ) );

// if ( QgsCptCityColorRampV2::hasBasicSchemes() )
// {
QgsCptCityColorRampV2* cc1Ramp = new QgsCptCityColorRampV2( "jjg/misc/temperature", "" );
QVERIFY( mStyle->addColorRamp( "test_cc1", cc1Ramp ) );
QVERIFY( mStyle->addColorRamp( "test_cc1", cc1Ramp, true ) );
QgsCptCityColorRampV2* cc2Ramp = new QgsCptCityColorRampV2( "cb/div/PiYG", "_10" );
QVERIFY( mStyle->addColorRamp( "test_cc2", cc2Ramp ) );
QVERIFY( mStyle->addColorRamp( "test_cc2", cc2Ramp, true ) );
// }
// else
// {
Expand Down Expand Up @@ -188,12 +188,12 @@ void TestStyleV2::testLoadColorRamps()

void TestStyleV2::testSaveLoad()
{
QEXPECT_FAIL("", "save() currently disabled in core", Abort);

mStyle->save();
// save not needed anymore, because used update=true in addColorRamp()
// mStyle->save();
mStyle->clear();
mStyle->load( QgsApplication::userStyleV2Path() );

// basic test to see that ramp is present
QStringList colorRamps = mStyle->colorRampNames();
QStringList colorRampsTest = QStringList() << "test_gradient";

Expand All @@ -206,6 +206,9 @@ void TestStyleV2::testSaveLoad()
if ( ramp )
delete ramp;
}

// test content again
testLoadColorRamps();
}


Expand Down

0 comments on commit b145998

Please sign in to comment.