Skip to content

Commit b145998

Browse files
committed
fix symbol and color ramp saving - issues #6222 and #6227
1 parent 6bf56e8 commit b145998

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

src/core/symbology-ng/qgsstylev2.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,21 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol, bool update )
8585
if ( !symbol || name.isEmpty() )
8686
return false;
8787

88-
delete mSymbols.value( name );
89-
90-
mSymbols.insert( name, symbol );
91-
92-
if ( update )
93-
updateSymbol( SymbolEntity, name );
88+
// delete previous symbol (if any)
89+
if ( mSymbols.contains( name ) )
90+
{
91+
// TODO remove groups and tags?
92+
delete mSymbols.value( name );
93+
mSymbols.insert( name, symbol );
94+
if ( update )
95+
updateSymbol( SymbolEntity, name );
96+
}
97+
else
98+
{
99+
mSymbols.insert( name, symbol );
100+
if ( update )
101+
saveSymbol( name, symbol, 0, QStringList() );
102+
}
94103

95104
return true;
96105
}
@@ -179,12 +188,20 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp, bo
179188
return false;
180189

181190
// delete previous symbol (if any)
182-
delete mColorRamps.value( name );
183-
184-
mColorRamps.insert( name, colorRamp );
185-
186-
if ( update )
187-
updateSymbol( ColorrampEntity, name );
191+
if ( mSymbols.contains( name ) )
192+
{
193+
// TODO remove groups and tags?
194+
delete mColorRamps.value( name );
195+
mColorRamps.insert( name, colorRamp );
196+
if ( update )
197+
updateSymbol( ColorrampEntity, name );
198+
}
199+
else
200+
{
201+
mColorRamps.insert( name, colorRamp );
202+
if ( update )
203+
saveColorRamp( name, colorRamp, 0, QStringList() );
204+
}
188205

189206
return true;
190207
}
@@ -1398,7 +1415,7 @@ bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
13981415
// check if it is an existing symbol
13991416
if ( !symbolNames().contains( name ) )
14001417
{
1401-
QgsDebugMsg( "Update request recieved for unavailable symbol" );
1418+
QgsDebugMsg( "Update request received for unavailable symbol" );
14021419
return false;
14031420
}
14041421

@@ -1410,7 +1427,7 @@ bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
14101427
}
14111428
symEl.save( stream, 4 );
14121429
query = sqlite3_mprintf( "UPDATE symbol SET xml='%q' WHERE name='%q';",
1413-
xmlArray.constData(), name.toUtf8().constData() );
1430+
xmlArray.constData(), name.toUtf8().constData() );
14141431
}
14151432
else if ( type == ColorrampEntity )
14161433
{
@@ -1428,7 +1445,7 @@ bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
14281445
}
14291446
symEl.save( stream, 4 );
14301447
query = sqlite3_mprintf( "UPDATE colorramp SET xml='%q' WHERE name='%q';",
1431-
xmlArray.constData(), name.toUtf8().constData() );
1448+
xmlArray.constData(), name.toUtf8().constData() );
14321449
}
14331450
else
14341451
{

src/core/symbology-ng/qgsstylev2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class CORE_EXPORT QgsStyleV2
9090

9191
//! adds a new group and returns the group's id
9292
/*!
93-
* \param groupname the name of the new group as QString
93+
* \param groupName the name of the new group as QString
9494
* \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
9595
* \return returns an int, which is the DB id of the new group created, 0 if the group couldn't be created
9696
*/

src/gui/symbology-ng/qgsstylev2managerdialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,8 @@ bool QgsStyleV2ManagerDialog::addSymbol()
351351
}
352352

353353
// add new symbol to style and re-populate the list
354-
mStyle->addSymbol( name, symbol );
354+
mStyle->addSymbol( name, symbol, true );
355355
// TODO groups and tags
356-
mStyle->saveSymbol( name, symbol, 0, QStringList() );
357356
mModified = true;
358357
return true;
359358
}
@@ -435,7 +434,8 @@ QString QgsStyleV2ManagerDialog::addColorRampStatic( QWidget* parent, QgsStyleV2
435434
}
436435

437436
// add new symbol to style and re-populate the list
438-
style->addColorRamp( name, ramp );
437+
style->addColorRamp( name, ramp, true );
438+
// TODO groups and tags
439439
return name;
440440
}
441441

tests/src/core/testqgsstylev2.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,24 @@ void TestStyleV2::testCreateColorRamps()
113113
QgsVectorGradientColorRampV2::StopsMap stops;
114114
stops[ 0.5 ] = QColor( Qt::white );
115115
gradientRamp->setStops( stops );
116-
QVERIFY( mStyle->addColorRamp( "test_gradient", gradientRamp ) );
116+
QVERIFY( mStyle->addColorRamp( "test_gradient", gradientRamp, true ) );
117117

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

122122
// color brewer ramp
123123
QgsVectorColorBrewerColorRampV2* cb1Ramp = new QgsVectorColorBrewerColorRampV2();
124-
QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp ) );
124+
QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp, true ) );
125125
QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "RdYlGn", 6 );
126-
QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp ) );
126+
QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp, true ) );
127127

128128
// if ( QgsCptCityColorRampV2::hasBasicSchemes() )
129129
// {
130130
QgsCptCityColorRampV2* cc1Ramp = new QgsCptCityColorRampV2( "jjg/misc/temperature", "" );
131-
QVERIFY( mStyle->addColorRamp( "test_cc1", cc1Ramp ) );
131+
QVERIFY( mStyle->addColorRamp( "test_cc1", cc1Ramp, true ) );
132132
QgsCptCityColorRampV2* cc2Ramp = new QgsCptCityColorRampV2( "cb/div/PiYG", "_10" );
133-
QVERIFY( mStyle->addColorRamp( "test_cc2", cc2Ramp ) );
133+
QVERIFY( mStyle->addColorRamp( "test_cc2", cc2Ramp, true ) );
134134
// }
135135
// else
136136
// {
@@ -188,12 +188,12 @@ void TestStyleV2::testLoadColorRamps()
188188

189189
void TestStyleV2::testSaveLoad()
190190
{
191-
QEXPECT_FAIL("", "save() currently disabled in core", Abort);
192-
193-
mStyle->save();
191+
// save not needed anymore, because used update=true in addColorRamp()
192+
// mStyle->save();
194193
mStyle->clear();
195194
mStyle->load( QgsApplication::userStyleV2Path() );
196195

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

@@ -206,6 +206,9 @@ void TestStyleV2::testSaveLoad()
206206
if ( ramp )
207207
delete ramp;
208208
}
209+
210+
// test content again
211+
testLoadColorRamps();
209212
}
210213

211214

0 commit comments

Comments
 (0)