Skip to content
Permalink
Browse files

Implementation of sequential rendering to QImage

  • Loading branch information
wonder-sk committed Nov 6, 2013
1 parent fd10fae commit bc45b9807522dd0df1491d2302cdb77c0496dd39
@@ -10,6 +10,7 @@
#include "qgsmaplayerregistry.h"

#include "qgsmapcanvas.h"
#include "qgsmapoverviewcanvas.h"
#include "qgsmaptoolpan.h"

int main(int argc, char* argv[])
@@ -39,23 +40,30 @@ int main(int argc, char* argv[])
QgsMapLayerRegistry::instance()->addMapLayer(layer2);

// open a window and do the rendering!
/*TestWidget l(layer);
TestWidget l(layer);
l.resize(360,360);
l.show();*/
l.show();

/*
QgsMapCanvas canvas;
canvas.setCanvasColor(Qt::white);
canvas.setExtent(layer->extent());
canvas.show();
// test overview
QgsMapOverviewCanvas overview( 0, &canvas );
overview.resize(200,200);
canvas.enableOverviewMode( &overview );
overview.show();
QList<QgsMapCanvasLayer> layers;
layers.append(QgsMapCanvasLayer(layer2));
layers.append(QgsMapCanvasLayer(layer2, false, true));
layers.append(QgsMapCanvasLayer(layer));
canvas.setLayerSet(layers);
QgsMapTool* pan = new QgsMapToolPan(&canvas);
canvas.setMapTool(pan);

canvas.show();
*/

return app.exec();
}
@@ -17,25 +17,17 @@ class TestWidget : public QLabel
public:
TestWidget(QgsMapLayer* layer)
{
//p = QPixmap(200,200);
//p.fill(Qt::red);

i = QImage(size(), QImage::Format_ARGB32_Premultiplied);
i.fill(Qt::gray);

job = 0;

// init renderer
ms.setLayers(QStringList(layer->id()));
ms.setExtent(layer->extent());
ms.setOutputSize(i.size());
ms.setOutputSize(size());
ms.setOutputDpi(120);

if (ms.hasValidSettings())
qDebug("map renderer settings valid");

setPixmap(QPixmap::fromImage(i));

connect(&timer, SIGNAL(timeout()), SLOT(onMapUpdateTimeout()));
timer.setInterval(100);
}
@@ -65,13 +57,11 @@ class TestWidget : public QLabel
job = 0;
}

i.fill(Qt::gray);

painter = new QPainter(&i);

job = new QgsMapRendererCustomPainterJob(ms, painter);
job = new QgsMapRendererSequentialJob(ms);
connect(job, SIGNAL(finished()), SLOT(f()));

job->start();

timer.start();
}
}
@@ -81,29 +71,23 @@ protected slots:
{
qDebug("finished!");

painter->end();
delete painter;

timer.stop();

//update();

setPixmap(QPixmap::fromImage(i));
if (job)
setPixmap(QPixmap::fromImage( job->renderedImage() ));
}

void onMapUpdateTimeout()
{
qDebug("update timer!");

setPixmap(QPixmap::fromImage(i));
if (job)
setPixmap(QPixmap::fromImage( job->renderedImage() ));
}

protected:
//QPixmap p;
QImage i;
QPainter* painter;
QgsMapSettings ms;
QgsMapRendererJob* job;
QgsMapRendererQImageJob* job;
QTimer timer;
};

@@ -22,9 +22,15 @@ QgsMapRendererSequentialJob::QgsMapRendererSequentialJob(const QgsMapSettings& s
{
}

QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob()
{
delete mInternalJob;
}


void QgsMapRendererSequentialJob::start()
{
mImage = QImage(mSettings.outputSize(), QImage::Format_ARGB32_Premultiplied);
mImage.fill(Qt::blue);

// 1. create an image where we will output all rendering
@@ -40,7 +46,8 @@ void QgsMapRendererSequentialJob::start()

void QgsMapRendererSequentialJob::cancel()
{
mInternalJob->cancel();
if (mInternalJob)
mInternalJob->cancel();
}


@@ -58,6 +65,8 @@ void QgsMapRendererSequentialJob::internalFinished()

delete mInternalJob;
mInternalJob = 0;

emit finished();
}


@@ -72,6 +81,11 @@ QgsMapRendererCustomPainterJob::QgsMapRendererCustomPainterJob(const QgsMapSetti
connect(&mFutureWatcher, SIGNAL(finished()), SLOT(futureFinished()));
}

QgsMapRendererCustomPainterJob::~QgsMapRendererCustomPainterJob()
{
cancel();
}

void QgsMapRendererCustomPainterJob::start()
{
qDebug("run!");
@@ -83,10 +97,13 @@ void QgsMapRendererCustomPainterJob::start()

void QgsMapRendererCustomPainterJob::cancel()
{
mRenderContext.setRenderingStopped(true);
if (mFuture.isRunning())
{
mRenderContext.setRenderingStopped(true);

mFutureWatcher.waitForFinished();
qApp->processEvents( QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers ); // TODO: necessary?
mFutureWatcher.waitForFinished();
qApp->processEvents( QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers ); // TODO: necessary?
}
}


@@ -78,6 +78,7 @@ class QgsMapRendererSequentialJob : public QgsMapRendererQImageJob
Q_OBJECT
public:
QgsMapRendererSequentialJob(const QgsMapSettings& settings);
~QgsMapRendererSequentialJob();

virtual void start();
virtual void cancel();
@@ -115,6 +116,7 @@ class QgsMapRendererCustomPainterJob : public QgsMapRendererJob
Q_OBJECT
public:
QgsMapRendererCustomPainterJob(const QgsMapSettings& settings, QPainter* painter);
~QgsMapRendererCustomPainterJob();

virtual void start();
virtual void cancel();
@@ -328,9 +328,9 @@ void QgsMapCanvas::enableOverviewMode( QgsMapOverviewCanvas* overview )
if ( mMapOverview )
{
// disconnect old map overview if exists
disconnect( mMapRenderer, SIGNAL( hasCrsTransformEnabled( bool ) ),
disconnect( this, SIGNAL( hasCrsTransformEnabled( bool ) ),
mMapOverview, SLOT( hasCrsTransformEnabled( bool ) ) );
disconnect( mMapRenderer, SIGNAL( destinationSrsChanged() ),
disconnect( this, SIGNAL( destinationSrsChanged() ),
mMapOverview, SLOT( destinationSrsChanged() ) );

// map overview is not owned by map canvas so don't delete it...
@@ -341,9 +341,9 @@ void QgsMapCanvas::enableOverviewMode( QgsMapOverviewCanvas* overview )
if ( overview )
{
// connect to the map render to copy its projection settings
connect( mMapRenderer, SIGNAL( hasCrsTransformEnabled( bool ) ),
connect( this, SIGNAL( hasCrsTransformEnabled( bool ) ),
overview, SLOT( hasCrsTransformEnabled( bool ) ) );
connect( mMapRenderer, SIGNAL( destinationSrsChanged() ),
connect( this, SIGNAL( destinationSrsChanged() ),
overview, SLOT( destinationSrsChanged() ) );
}
}
@@ -107,7 +107,7 @@ void QgsMapOverviewCanvas::paintEvent( QPaintEvent* pe )

void QgsMapOverviewCanvas::drawExtentRect()
{
if ( !mMapCanvas || !mMapRenderer ) return;
if ( !mMapCanvas ) return;

const QgsRectangle& extent = mMapCanvas->extent();

@@ -294,22 +294,18 @@ void QgsMapOverviewCanvas::setBackgroundColor( const QColor& color )
void QgsMapOverviewCanvas::setLayerSet( const QStringList& layerSet )
{
QgsDebugMsg( "layerSet: " + layerSet.join( ", " ) );
if ( !mMapRenderer ) return;
mMapRenderer->setLayerSet( layerSet );
mMapRenderer->updateFullExtent();
updateFullExtent();
}

void QgsMapOverviewCanvas::updateFullExtent()
{
if ( !mMapRenderer ) return;
QgsRectangle rect;
if ( !mMapRenderer->layerSet().isEmpty() )
{
rect = mMapRenderer->fullExtent();
// expand a bit to keep features on margin
rect.scale( 1.1 );
}

rect = mMapCanvas->fullExtent();
// expand a bit to keep features on margin
rect.scale( 1.1 );

mMapRenderer->setExtent( rect );
drawExtentRect();
}
@@ -321,7 +317,7 @@ void QgsMapOverviewCanvas::hasCrsTransformEnabled( bool flag )

void QgsMapOverviewCanvas::destinationSrsChanged()
{
const QgsCoordinateReferenceSystem& srs = mMapCanvas->mapRenderer()->destinationCrs();
const QgsCoordinateReferenceSystem& srs = mMapCanvas->mapSettings().destinationCrs();
mMapRenderer->setDestinationCrs( srs );
}

0 comments on commit bc45b98

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