Skip to content
Permalink
Browse files

Emit a signal when layer symbology changes

  • Loading branch information
Hugo Mercier authored and m-kuhn committed Sep 9, 2013
1 parent 8d0d7df commit bf076a0c1f1bf52aca88205307e95ec8fba53d5e
@@ -418,6 +418,9 @@ class QgsMapLayer : QObject
* added in 1.5 */
void dataChanged();

/** Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode() */
void blendModeChanged( const QPainter::CompositionMode blendMode );

protected:
/** Set the extent */
virtual void setExtent( const QgsRectangle &rect );
@@ -1062,6 +1062,15 @@ class QgsVectorLayer : QgsMapLayer
*/
void labelingFontNotFound( QgsVectorLayer* layer, const QString& fontfamily );

/** Signal emitted on symbology changes, when setRendererV2() is called */
void rendererChanged();

/** Signal emitted when setFeatureBlendMode() is called */
void featureBlendModeChanged( const QPainter::CompositionMode blendMode );

/** Signal emitted when setLayerTransparency() is called */
void layerTransparencyChanged( int layerTransparency );

protected:
/** Set the extent */
void setExtent( const QgsRectangle &rect );
@@ -118,6 +118,9 @@ class QgsRasterLayer : QgsMapLayer
//void setResampleFilter( QgsRasterResampleFilter* resampleFilter /Transfer/ );
QgsRasterResampleFilter * resampleFilter() const;

QgsBrightnessContrastFilter * brightnessFilter() const;
QgsHueSaturationFilter * hueSaturationFilter() const;

/** Get raster pipe */
QgsRasterPipe * pipe();

@@ -246,6 +249,9 @@ class QgsRasterLayer : QgsMapLayer
*/
void dataChanged();

/** Signal emitted when the symbology changes, through call to setRenderer() */
void rendererChanged();

protected:

/** \brief Read the symbology for the current layer from the Dom node supplied */
@@ -138,6 +138,7 @@ QgsRectangle QgsMapLayer::extent()
void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
{
mBlendMode = blendMode;
emit blendModeChanged( blendMode );
}

/** Read blend mode for layer */
@@ -432,6 +432,9 @@ class CORE_EXPORT QgsMapLayer : public QObject
* added in 1.5 */
void dataChanged();

/** Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode() */
void blendModeChanged( const QPainter::CompositionMode blendMode );

protected:
/** Set the extent */
virtual void setExtent( const QgsRectangle &rect );
@@ -3168,6 +3168,8 @@ void QgsVectorLayer::setRendererV2( QgsFeatureRendererV2 *r )
mRendererV2 = r;
mSymbolFeatureCounted = false;
mSymbolFeatureCountMap.clear();

emit rendererChanged();
}
}

@@ -3453,6 +3455,7 @@ QVariant QgsVectorLayer::maximumValue( int index )
void QgsVectorLayer::setFeatureBlendMode( const QPainter::CompositionMode featureBlendMode )
{
mFeatureBlendMode = featureBlendMode;
emit featureBlendModeChanged( featureBlendMode );
}

/** Read blend mode for layer */
@@ -3465,6 +3468,7 @@ QPainter::CompositionMode QgsVectorLayer::featureBlendMode() const
void QgsVectorLayer::setLayerTransparency( int layerTransparency )
{
mLayerTransparency = layerTransparency;
emit layerTransparencyChanged( layerTransparency );
}

/** Read transparency for layer */
@@ -1451,6 +1451,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
void labelingFontNotFound( QgsVectorLayer* layer, const QString& fontfamily );

/** Signal emitted on symbology changes, when setRendererV2() is called */
void rendererChanged();

/** Signal emitted when setFeatureBlendMode() is called */
void featureBlendModeChanged( const QPainter::CompositionMode blendMode );

/** Signal emitted when setLayerTransparency() is called */
void layerTransparencyChanged( int layerTransparency );

protected:
/** Set the extent */
void setExtent( const QgsRectangle &rect );
@@ -1204,6 +1204,7 @@ void QgsRasterLayer::setRenderer( QgsRasterRenderer* theRenderer )
QgsDebugMsg( "Entered" );
if ( !theRenderer ) { return; }
mPipe.set( theRenderer );
emit rendererChanged();
}

void QgsRasterLayer::showProgress( int theValue )
@@ -388,6 +388,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
*/
void dataChanged();

/** Signal emitted when the symbology changes, through call to setRenderer() */
void rendererChanged();

protected:
/** \brief Read the symbology for the current layer from the Dom node supplied */
bool readSymbology( const QDomNode& node, QString& errorMessage );
@@ -28,6 +28,20 @@
#include <qgsapplication.h>
#include <qgsproviderregistry.h>

class TestSignalReceiver : public QObject
{
Q_OBJECT;

public:
TestSignalReceiver() : QObject( 0 ), blendMode( QPainter::CompositionMode_SourceOver ) {}
QPainter::CompositionMode blendMode;
public slots:
void onBlendModeChanged( const QPainter::CompositionMode blendMode )
{
this->blendMode = blendMode;
}
};

/** \ingroup UnitTests
* This is a unit test for the QgsMapLayer class.
*/
@@ -41,6 +55,8 @@ class TestQgsMapLayer: public QObject
void cleanup() {};// will be called after every testfunction.

void isValid();

void setBlendMode();
private:
QgsMapLayer * mpLayer;
};
@@ -69,5 +85,18 @@ void TestQgsMapLayer::isValid()
QVERIFY( mpLayer->isValid() );
}

void TestQgsMapLayer::setBlendMode()
{
TestSignalReceiver receiver;
QObject::connect( mpLayer, SIGNAL( blendModeChanged( const QPainter::CompositionMode ) ),
&receiver, SLOT( onBlendModeChanged( const QPainter::CompositionMode ) ) );
QCOMPARE( int(receiver.blendMode), 0 );
mpLayer->setBlendMode( QPainter::CompositionMode_Screen );
// check the signal has been correctly emitted
QCOMPARE( receiver.blendMode, QPainter::CompositionMode_Screen );
// check accessor
QCOMPARE( mpLayer->blendMode(), QPainter::CompositionMode_Screen );
}

QTEST_MAIN( TestQgsMapLayer )
#include "moc_testqgsmaplayer.cxx"
@@ -44,7 +44,6 @@
//qgis unit test includes
#include <qgsrenderchecker.h>


/** \ingroup UnitTests
* This is a unit test for the QgsRasterLayer class.
*/
@@ -70,6 +69,7 @@ class TestQgsRasterLayer: public QObject
void buildExternalOverviews();
void registry();
void transparency();
void setRenderer();
private:
bool render( QString theFileName );
bool setQml( QString theType );
@@ -86,6 +86,22 @@ class TestQgsRasterLayer: public QObject
QString mReport;
};

class TestSignalReceiver : public QObject
{
Q_OBJECT;

public:
TestSignalReceiver() : QObject( 0 ),
rendererChanged( false )
{}
bool rendererChanged;
public slots:
void onRendererChanged()
{
rendererChanged = true;
}
};

//runs before all tests
void TestQgsRasterLayer::initTestCase()
{
@@ -146,7 +162,6 @@ void TestQgsRasterLayer::cleanupTestCase()
myFile.close();
//QDesktopServices::openUrl( "file:///" + myReportFile );
}

}

void TestQgsRasterLayer::isValid()
@@ -297,6 +312,7 @@ void TestQgsRasterLayer::landsatBasic()
mpMapRenderer->setExtent( mpLandsatRasterLayer->extent() );
QVERIFY( render( "landsat_basic" ) );
}

void TestQgsRasterLayer::landsatBasic875Qml()
{
//a qml that orders the rgb bands as 8,7,5
@@ -320,6 +336,7 @@ void TestQgsRasterLayer::checkDimensions()
}
void TestQgsRasterLayer::checkStats()
{

mReport += "<h2>Check Stats</h2>\n";
QgsRasterBandStats myStatistics = mpRasterLayer->dataProvider()->bandStatistics( 1,
QgsRasterBandStats::Min | QgsRasterBandStats::Max |
@@ -443,6 +460,7 @@ bool TestQgsRasterLayer::setQml( QString theType )

void TestQgsRasterLayer::transparency()
{

QVERIFY( mpFloat32RasterLayer->isValid() );
QgsSingleBandGrayRenderer* renderer = new QgsSingleBandGrayRenderer( mpRasterLayer->dataProvider(), 1 );
mpFloat32RasterLayer->setRenderer( renderer );
@@ -479,5 +497,18 @@ void TestQgsRasterLayer::transparency()
QVERIFY( render( "raster_transparency" ) );
}

void TestQgsRasterLayer::setRenderer()
{
TestSignalReceiver receiver;
QObject::connect( mpRasterLayer, SIGNAL( rendererChanged() ),
&receiver, SLOT( onRendererChanged() ) );
QgsRasterRenderer* renderer = (QgsRasterRenderer*) mpRasterLayer->renderer()->clone();
QCOMPARE( receiver.rendererChanged, false );
mpRasterLayer->setRenderer( renderer );
QCOMPARE( receiver.rendererChanged, true );
QCOMPARE( mpRasterLayer->renderer(), renderer );
delete renderer;
}

QTEST_MAIN( TestQgsRasterLayer )
#include "moc_testqgsrasterlayer.cxx"
@@ -31,9 +31,39 @@
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmaplayerregistry.h>
#include <qgssymbolv2.h>
#include <qgssinglesymbolrendererv2.h>
//qgis test includes
#include "qgsrenderchecker.h"

class TestSignalReceiver : public QObject
{
Q_OBJECT;

public:
TestSignalReceiver() : QObject( 0 ),
rendererChanged( false ),
featureBlendMode( QPainter::CompositionMode(0) ),
transparency( 0 )
{}
bool rendererChanged;
QPainter::CompositionMode featureBlendMode;
int transparency;
public slots:
void onRendererChanged()
{
rendererChanged = true;
}
void onFeatureBlendModeChanged( const QPainter::CompositionMode blendMode )
{
featureBlendMode = blendMode;
}
void onLayerTransparencyChanged( int layerTransparency )
{
transparency = layerTransparency;
}
};

/** \ingroup UnitTests
* This is a unit test for the vector layer class.
*/
@@ -575,11 +605,19 @@ class TestQgsVectorLayer: public QObject
};
void QgsVectorLayersetRendererV2()
{
QgsVectorLayer* vLayer = static_cast< QgsVectorLayer * >( mpPointsLayer );
TestSignalReceiver receiver;
QObject::connect( vLayer, SIGNAL( rendererChanged() ),
&receiver, SLOT( onRendererChanged() ) );
QgsSingleSymbolRendererV2* symbolRenderer = new QgsSingleSymbolRendererV2( QgsSymbolV2::defaultSymbol( QGis::Point ) );

QCOMPARE( receiver.rendererChanged, false );
vLayer->setRendererV2( symbolRenderer );
QCOMPARE( receiver.rendererChanged, true );
QCOMPARE( vLayer->rendererV2(), symbolRenderer );
};
void QgsVectorLayerisUsingRendererV2()
{

};
void QgsVectorLayersetUsingRendererV2()
{
@@ -638,6 +676,31 @@ class TestQgsVectorLayer: public QObject

};

void QgsVectorLayersetFeatureBlendMode()
{
QgsVectorLayer* vLayer = static_cast< QgsVectorLayer * >( mpPointsLayer );
TestSignalReceiver receiver;
QObject::connect( vLayer, SIGNAL( featureBlendModeChanged( const QPainter::CompositionMode ) ),
&receiver, SLOT( onFeatureBlendModeChanged( const QPainter::CompositionMode ) ) );

QCOMPARE( int(receiver.featureBlendMode), 0 );
vLayer->setFeatureBlendMode( QPainter::CompositionMode_Screen );
QCOMPARE( receiver.featureBlendMode, QPainter::CompositionMode_Screen );
QCOMPARE( vLayer->featureBlendMode(), QPainter::CompositionMode_Screen );
}

void QgsVectorLayersetLayerTransparency()
{
QgsVectorLayer* vLayer = static_cast< QgsVectorLayer * >( mpPointsLayer );
TestSignalReceiver receiver;
QObject::connect( vLayer, SIGNAL( layerTransparencyChanged( int ) ),
&receiver, SLOT( onLayerTransparencyChanged( int ) ) );

QCOMPARE( receiver.transparency, 0 );
vLayer->setLayerTransparency( 50 );
QCOMPARE( receiver.transparency, 50 );
QCOMPARE( vLayer->layerTransparency(), 50 );
}
};

QTEST_MAIN( TestQgsVectorLayer )
@@ -16,7 +16,7 @@
import unittest
import qgis

from PyQt4.QtCore import QFileInfo
from PyQt4.QtCore import QFileInfo, QObject, SIGNAL
from PyQt4 import QtGui

from qgis.core import (QgsRaster,
@@ -216,5 +216,25 @@ def testShaderCrash(self):
myRasterLayer.dataProvider(), 1, myRasterShader)
myRasterLayer.setRenderer(myPseudoRenderer)

def onRendererChanged( self ):
self.rendererChanged = True
def test_setRenderer( self ):
myPath = os.path.join(unitTestDataPath('raster'),
'band1_float32_noct_epsg4326.tif')
myFileInfo = QFileInfo(myPath)
myBaseName = myFileInfo.baseName()
layer = QgsRasterLayer(myPath, myBaseName)

self.rendererChanged = False
QObject.connect( layer, SIGNAL( "rendererChanged()" ),
self.onRendererChanged )

rShader = QgsRasterShader()
r = QgsSingleBandPseudoColorRenderer( layer.dataProvider(), 1, rShader )

layer.setRenderer( r )
assert self.rendererChanged == True
assert layer.renderer() == r

if __name__ == '__main__':
unittest.main()

0 comments on commit bf076a0

Please sign in to comment.
You can’t perform that action at this time.