Skip to content

Commit

Permalink
Fixed problems with style switching not always working correctly
Browse files Browse the repository at this point in the history
Caused by internal storage (QByteArray) not re-adjusting its size
to new content, resulting in corrupted XML data
  • Loading branch information
wonder-sk committed Jan 8, 2015
1 parent c11319b commit 603b3a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/core/qgsmaplayerstylemanager.cpp
Expand Up @@ -166,6 +166,7 @@ void QgsMapLayerStyle::readFromLayer( QgsMapLayer* layer )
return;
}

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

void QgsMapLayerStyle::readXml( const QDomElement& styleElement )
{
mXmlData.clear();
QTextStream stream( &mXmlData );
styleElement.firstChildElement().save( stream, 0 );
}
Expand Down
52 changes: 46 additions & 6 deletions tests/src/core/testqgsmaplayerstylemanager.cpp
Expand Up @@ -22,6 +22,7 @@ class TestQgsMapLayerStyleManager : public QObject
void testDefault();
void testStyle();
void testReadWrite();
void testSwitchingStyles();

private:
QgsVectorLayer* mVL;
Expand Down Expand Up @@ -134,17 +135,17 @@ void TestQgsMapLayerStyleManager::testReadWrite()
sm0.writeXml( mgrElem );

QString xml;
QTextStream ts(&xml);
doc.save(ts, 2);
qDebug("%s", xml.toAscii().data());
QTextStream ts( &xml );
doc.save( ts, 2 );
qDebug( "%s", xml.toAscii().data() );

QgsMapLayerStyleManager sm1( mVL );
sm1.readXml( mgrElem );

QCOMPARE( sm1.styles().count(), 2 );
QCOMPARE( sm1.style(QString()).isValid(), true );
QCOMPARE( sm1.style("blue").isValid(), true );
QCOMPARE( sm1.currentStyle(), QString("blue") );
QCOMPARE( sm1.style( QString() ).isValid(), true );
QCOMPARE( sm1.style( "blue" ).isValid(), true );
QCOMPARE( sm1.currentStyle(), QString( "blue" ) );

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

static void _setVLColor( QgsVectorLayer* vl, const QColor& c )
{
dynamic_cast<QgsSingleSymbolRendererV2*>( vl->rendererV2() )->symbol()->setColor( c );
}

static QColor _getVLColor( QgsVectorLayer* vl )
{
return dynamic_cast<QgsSingleSymbolRendererV2*>( vl->rendererV2() )->symbol()->color();
}

void TestQgsMapLayerStyleManager::testSwitchingStyles()
{
_setVLColor( mVL, Qt::red );

mVL->enableStyleManager();
mVL->styleManager()->addStyleFromLayer( "s1" );
mVL->styleManager()->setCurrentStyle( "s1" );

QCOMPARE( mVL->styleManager()->currentStyle(), QString( "s1" ) );
QCOMPARE( _getVLColor( mVL ), QColor( Qt::red ) );

_setVLColor( mVL, Qt::green );

mVL->styleManager()->setCurrentStyle( QString() );
QCOMPARE( _getVLColor( mVL ), QColor( Qt::red ) );

mVL->styleManager()->setCurrentStyle( "s1" );
QCOMPARE( _getVLColor( mVL ), QColor( Qt::green ) );

_setVLColor( mVL, Qt::blue );

mVL->styleManager()->setCurrentStyle( QString() );
QCOMPARE( _getVLColor( mVL ), QColor( Qt::red ) );

mVL->styleManager()->setCurrentStyle( "s1" );
QCOMPARE( _getVLColor( mVL ), QColor( Qt::blue ) );
}


QTEST_MAIN( TestQgsMapLayerStyleManager )
#include "testqgsmaplayerstylemanager.moc"

0 comments on commit 603b3a5

Please sign in to comment.