Skip to content
Permalink
Browse files

Updated overview to use rendering jobs, fixed cancellation

  • Loading branch information
wonder-sk committed Nov 6, 2013
1 parent bc45b98 commit 3c05976a709b3276eef0f48fc58083aa6715ef1d
@@ -40,11 +40,11 @@ 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());
@@ -63,7 +63,6 @@ int main(int argc, char* argv[])

QgsMapTool* pan = new QgsMapToolPan(&canvas);
canvas.setMapTool(pan);
*/

return app.exec();
}
@@ -23,7 +23,6 @@ class TestWidget : public QLabel
ms.setLayers(QStringList(layer->id()));
ms.setExtent(layer->extent());
ms.setOutputSize(size());
ms.setOutputDpi(120);

if (ms.hasValidSettings())
qDebug("map renderer settings valid");
@@ -20,18 +20,23 @@ QgsMapRendererSequentialJob::QgsMapRendererSequentialJob(const QgsMapSettings& s
: QgsMapRendererQImageJob(SequentialJob, settings)
, mInternalJob(0)
{
qDebug("SEQUENTIAL construct");
}

QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob()
{
delete mInternalJob;
qDebug("SEQUENTIAL destruct");
//delete mInternalJob;
Q_ASSERT(mInternalJob == 0);
}


void QgsMapRendererSequentialJob::start()
{
qDebug("SEQUENTIAL START");
qDebug("%d,%d", mSettings.outputSize().width(), mSettings.outputSize().height());

mImage = QImage(mSettings.outputSize(), QImage::Format_ARGB32_Premultiplied);
mImage.fill(Qt::blue);

// 1. create an image where we will output all rendering
mPainter = new QPainter(&mImage);
@@ -47,7 +52,11 @@ void QgsMapRendererSequentialJob::start()
void QgsMapRendererSequentialJob::cancel()
{
if (mInternalJob)
{
mInternalJob->cancel();
delete mInternalJob;
mInternalJob = 0;
}
}


@@ -59,6 +68,8 @@ QImage QgsMapRendererSequentialJob::renderedImage()

void QgsMapRendererSequentialJob::internalFinished()
{
qDebug("SEQUENTIAL finished");

mPainter->end();
delete mPainter;
mPainter = 0;
@@ -78,17 +89,22 @@ QgsMapRendererCustomPainterJob::QgsMapRendererCustomPainterJob(const QgsMapSetti
: QgsMapRendererJob(CustomPainterJob, settings)
, mPainter(painter)
{
connect(&mFutureWatcher, SIGNAL(finished()), SLOT(futureFinished()));
qDebug("QPAINTER construct");
}

QgsMapRendererCustomPainterJob::~QgsMapRendererCustomPainterJob()
{
cancel();
qDebug("QPAINTER destruct");
Q_ASSERT(!mFutureWatcher.isRunning());
//cancel();
}

void QgsMapRendererCustomPainterJob::start()
{
qDebug("run!");
qDebug("QPAINTER run!");

connect(&mFutureWatcher, SIGNAL(finished()), SLOT(futureFinished()));

mFuture = QtConcurrent::run(staticRender, this);
mFutureWatcher.setFuture(mFuture);
}
@@ -99,47 +115,46 @@ void QgsMapRendererCustomPainterJob::cancel()
{
if (mFuture.isRunning())
{
qDebug("QPAINTER cancelling");
disconnect(&mFutureWatcher, SIGNAL(finished()), this, SLOT(futureFinished()));

mRenderContext.setRenderingStopped(true);

QTime t;
t.start();

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

qDebug("QPAINER cancel waited %f ms", t.elapsed() / 1000.0);

futureFinished();

qDebug("QPAINTER cancelled");
}
}


void QgsMapRendererCustomPainterJob::futureFinished()
{
qDebug("futureFinished");
qDebug("QPAINTER futureFinished");
emit finished();
}


void QgsMapRendererCustomPainterJob::staticRender(QgsMapRendererCustomPainterJob* self)
{
qDebug("staticRender");
self->startRender();
}


void QgsMapRendererCustomPainterJob::startRender()
{
qDebug("startRender");
/*
for (int i = 0; i < 50; ++i)
{
if (mStopped)
return;
qDebug("QPAINTER startRender");

mPainter->drawLine(rand() % 360, rand() % 360, rand() % 360, rand() % 360);
//QThread::msleep(100);
int x = 0;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 10000; j++)
x *= x+5;
}
qDebug("drawn line");
}*/
// clear the background
mPainter->fillRect( 0, 0, mSettings.outputSize().width(), mSettings.outputSize().height(), mSettings.backgroundColor() );

mPainter->setRenderHint( QPainter::Antialiasing, mSettings.isAntiAliasingEnabled() );

#ifdef QGISDEBUG
QgsDebugMsg( "Starting to render layer stack." );
@@ -163,7 +178,7 @@ void QgsMapRendererCustomPainterJob::startRender()
int myGreen = prj->readNumEntry( "Gui", "/SelectionColorGreenPart", 255 );
int myBlue = prj->readNumEntry( "Gui", "/SelectionColorBluePart", 0 );
int myAlpha = prj->readNumEntry( "Gui", "/SelectionColorAlphaPart", 255 );*/
mRenderContext.setSelectionColor( Qt::red ); // TODO QColor( myRed, myGreen, myBlue, myAlpha ) );
mRenderContext.setSelectionColor( mSettings.selectionColor() ); // TODO QColor( myRed, myGreen, myBlue, myAlpha ) );

//calculate scale factor
//use the specified dpi and not those from the paint device
@@ -33,6 +33,9 @@ QgsMapSettings::QgsMapSettings()
, mExtent()
, mProjectionsEnabled( false )
, mDestCRS( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId ) // WGS 84
, mBackgroundColor( Qt::white )
, mSelectionColor( Qt::yellow )
, mAntiAliasing( true )
, mOutputUnits( QgsMapSettings::Millimeters )
{
updateDerived();
@@ -1,6 +1,7 @@
#ifndef QGSMAPSETTINGS_H
#define QGSMAPSETTINGS_H

#include <QColor>
#include <QSize>
#include <QStringList>

@@ -59,6 +60,15 @@ class QgsMapSettings
void setOutputUnits( OutputUnits u ) { mOutputUnits = u; }
OutputUnits outputUnits() const { return mOutputUnits; }

void setBackgroundColor( const QColor& color ) { mBackgroundColor = color; }
QColor backgroundColor() const { return mBackgroundColor; }

void setSelectionColor( const QColor& color ) { mSelectionColor = color; }
QColor selectionColor() const { return mSelectionColor; }

void setAntiAliasingEnabled( bool enabled ) { mAntiAliasing = enabled; }
bool isAntiAliasingEnabled() const { return mAntiAliasing; }

bool hasValidSettings() const;
QgsRectangle visibleExtent() const;
double mapUnitsPerPixel() const;
@@ -129,6 +139,10 @@ class QgsMapSettings
bool mProjectionsEnabled;
QgsCoordinateReferenceSystem mDestCRS;

QColor mBackgroundColor;
QColor mSelectionColor;
bool mAntiAliasing;

//! Output units
OutputUnits mOutputUnits;

@@ -83,7 +83,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
, mCanvasProperties( new CanvasProperties )
//, mNewSize( QSize() )
//, mPainting( false )
, mAntiAliasing( false )
{
setObjectName( name );
mScene = new QGraphicsScene();
@@ -171,8 +170,8 @@ QgsMapCanvas::~QgsMapCanvas()

void QgsMapCanvas::enableAntiAliasing( bool theFlag )
{
mAntiAliasing = theFlag;
mMap->enableAntiAliasing( theFlag );
mSettings.setAntiAliasingEnabled( theFlag );

if ( mMapOverview )
mMapOverview->enableAntiAliasing( theFlag );
} // anti aliasing
@@ -311,7 +310,7 @@ void QgsMapCanvas::setLayerSet( QList<QgsMapCanvasLayer> &layers )

if ( mMapOverview )
{
QStringList& layerSetOvOld = mMapOverview->layerSet();
const QStringList& layerSetOvOld = mMapOverview->layerSet();
if ( layerSetOvOld != layerSetOverview )
{
mMapOverview->setLayerSet( layerSetOverview );
@@ -1211,7 +1210,7 @@ void QgsMapCanvas::unsetMapTool( QgsMapTool* tool )
void QgsMapCanvas::setCanvasColor( const QColor & theColor )
{
// background of map's pixmap
mMap->setBackgroundColor( theColor );
mSettings.setBackgroundColor( theColor );

// background of the QGraphicsView
QBrush bgBrush( theColor );
@@ -266,7 +266,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
void enableAntiAliasing( bool theFlag );

//! true if antialising is enabled
bool antiAliasingEnabled() const { return mAntiAliasing; }
bool antiAliasingEnabled() const { return mSettings.isAntiAliasingEnabled(); }

//! Select which Qt class to render with
void useImageToRender( bool theFlag );
@@ -509,9 +509,6 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

//! currently in paint event
//bool mPainting;

//! indicates whether antialiasing will be used for rendering
bool mAntiAliasing;
}; // class QgsMapCanvas


@@ -80,17 +80,11 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg

qDebug("----------> EXTENT %f,%f", s.extent().xMinimum(), s.extent().yMinimum());

mImage.fill(mBgColor.rgb());

mPainter = new QPainter(&mImage);

// TODO[MD]: need to setup clipping?
//paint.setClipRect( mImage.rect() );

// antialiasing
if ( mAntiAliasing )
mPainter->setRenderHint( QPainter::Antialiasing );

// create the renderer job
Q_ASSERT(mJob == 0);
mJob = new QgsMapRendererCustomPainterJob(s, mPainter);
@@ -47,12 +47,14 @@ class GUI_EXPORT QgsMapCanvasMap : public QObject, public QGraphicsRectItem
//! resize canvas item and pixmap
void resize( QSize size );

void enableAntiAliasing( bool flag ) { mAntiAliasing = flag; }
//! @deprecated in 2.1 - does nothing. Kept for API compatibility
void enableAntiAliasing( bool flag );

//! @deprecated in 2.1 - does nothing. Kept for API compatibility
void render() {}

void setBackgroundColor( const QColor& color ) { mBgColor = color; }
//! @deprecated in 2.1 - does nothing. Kept for API compatibility
void setBackgroundColor( const QColor& color ) {}

void setPanningOffset( const QPoint& point );

@@ -74,17 +76,12 @@ public slots:

private:

//! indicates whether antialiasing will be used for rendering
bool mAntiAliasing;

QImage mImage;
QImage mLastImage;

//QgsMapRenderer* mRender;
QgsMapCanvas* mCanvas;

QColor mBgColor;

QPoint mOffset;

bool mDirty; //!< whether a new rendering job should be started upon next paint() call

0 comments on commit 3c05976

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