Skip to content

Commit

Permalink
Updated overview to use rendering jobs, fixed cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Nov 6, 2013
1 parent bc45b98 commit 3c05976
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 116 deletions.
7 changes: 3 additions & 4 deletions src/app/maprenderertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -63,7 +63,6 @@ int main(int argc, char* argv[])

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

return app.exec();
}
1 change: 0 additions & 1 deletion src/app/maprenderertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
65 changes: 40 additions & 25 deletions src/core/qgsmaprendererjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -47,7 +52,11 @@ void QgsMapRendererSequentialJob::start()
void QgsMapRendererSequentialJob::cancel()
{
if (mInternalJob)
{
mInternalJob->cancel();
delete mInternalJob;
mInternalJob = 0;
}
}


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

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

mPainter->end();
delete mPainter;
mPainter = 0;
Expand All @@ -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);
}
Expand All @@ -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." );
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsmapsettings.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef QGSMAPSETTINGS_H
#define QGSMAPSETTINGS_H

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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -129,6 +139,10 @@ class QgsMapSettings
bool mProjectionsEnabled;
QgsCoordinateReferenceSystem mDestCRS;

QColor mBackgroundColor;
QColor mSelectionColor;
bool mAntiAliasing;

//! Output units
OutputUnits mOutputUnits;

Expand Down
9 changes: 4 additions & 5 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
5 changes: 1 addition & 4 deletions src/gui/qgsmapcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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


Expand Down
6 changes: 0 additions & 6 deletions src/gui/qgsmapcanvasmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions src/gui/qgsmapcanvasmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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
Expand Down
Loading

0 comments on commit 3c05976

Please sign in to comment.