Skip to content

Commit 603b3a5

Browse files
committed
Fixed problems with style switching not always working correctly
Caused by internal storage (QByteArray) not re-adjusting its size to new content, resulting in corrupted XML data
1 parent c11319b commit 603b3a5

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/core/qgsmaplayerstylemanager.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ void QgsMapLayerStyle::readFromLayer( QgsMapLayer* layer )
166166
return;
167167
}
168168

169+
mXmlData.clear();
169170
QTextStream stream( &mXmlData );
170171
doc.save( stream, 0 );
171172
}
@@ -186,6 +187,7 @@ void QgsMapLayerStyle::writeToLayer( QgsMapLayer* layer ) const
186187

187188
void QgsMapLayerStyle::readXml( const QDomElement& styleElement )
188189
{
190+
mXmlData.clear();
189191
QTextStream stream( &mXmlData );
190192
styleElement.firstChildElement().save( stream, 0 );
191193
}

tests/src/core/testqgsmaplayerstylemanager.cpp

+46-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TestQgsMapLayerStyleManager : public QObject
2222
void testDefault();
2323
void testStyle();
2424
void testReadWrite();
25+
void testSwitchingStyles();
2526

2627
private:
2728
QgsVectorLayer* mVL;
@@ -134,17 +135,17 @@ void TestQgsMapLayerStyleManager::testReadWrite()
134135
sm0.writeXml( mgrElem );
135136

136137
QString xml;
137-
QTextStream ts(&xml);
138-
doc.save(ts, 2);
139-
qDebug("%s", xml.toAscii().data());
138+
QTextStream ts( &xml );
139+
doc.save( ts, 2 );
140+
qDebug( "%s", xml.toAscii().data() );
140141

141142
QgsMapLayerStyleManager sm1( mVL );
142143
sm1.readXml( mgrElem );
143144

144145
QCOMPARE( sm1.styles().count(), 2 );
145-
QCOMPARE( sm1.style(QString()).isValid(), true );
146-
QCOMPARE( sm1.style("blue").isValid(), true );
147-
QCOMPARE( sm1.currentStyle(), QString("blue") );
146+
QCOMPARE( sm1.style( QString() ).isValid(), true );
147+
QCOMPARE( sm1.style( "blue" ).isValid(), true );
148+
QCOMPARE( sm1.currentStyle(), QString( "blue" ) );
148149

149150
// now use the default style - the symbol should get red color
150151
sm1.setCurrentStyle( QString() );
@@ -153,5 +154,44 @@ void TestQgsMapLayerStyleManager::testReadWrite()
153154
QCOMPARE( r2->symbol()->color(), QColor( Qt::red ) );
154155
}
155156

157+
static void _setVLColor( QgsVectorLayer* vl, const QColor& c )
158+
{
159+
dynamic_cast<QgsSingleSymbolRendererV2*>( vl->rendererV2() )->symbol()->setColor( c );
160+
}
161+
162+
static QColor _getVLColor( QgsVectorLayer* vl )
163+
{
164+
return dynamic_cast<QgsSingleSymbolRendererV2*>( vl->rendererV2() )->symbol()->color();
165+
}
166+
167+
void TestQgsMapLayerStyleManager::testSwitchingStyles()
168+
{
169+
_setVLColor( mVL, Qt::red );
170+
171+
mVL->enableStyleManager();
172+
mVL->styleManager()->addStyleFromLayer( "s1" );
173+
mVL->styleManager()->setCurrentStyle( "s1" );
174+
175+
QCOMPARE( mVL->styleManager()->currentStyle(), QString( "s1" ) );
176+
QCOMPARE( _getVLColor( mVL ), QColor( Qt::red ) );
177+
178+
_setVLColor( mVL, Qt::green );
179+
180+
mVL->styleManager()->setCurrentStyle( QString() );
181+
QCOMPARE( _getVLColor( mVL ), QColor( Qt::red ) );
182+
183+
mVL->styleManager()->setCurrentStyle( "s1" );
184+
QCOMPARE( _getVLColor( mVL ), QColor( Qt::green ) );
185+
186+
_setVLColor( mVL, Qt::blue );
187+
188+
mVL->styleManager()->setCurrentStyle( QString() );
189+
QCOMPARE( _getVLColor( mVL ), QColor( Qt::red ) );
190+
191+
mVL->styleManager()->setCurrentStyle( "s1" );
192+
QCOMPARE( _getVLColor( mVL ), QColor( Qt::blue ) );
193+
}
194+
195+
156196
QTEST_MAIN( TestQgsMapLayerStyleManager )
157197
#include "testqgsmaplayerstylemanager.moc"

0 commit comments

Comments
 (0)