Skip to content

Commit 3c05976

Browse files
committedNov 6, 2013
Updated overview to use rendering jobs, fixed cancellation
1 parent bc45b98 commit 3c05976

11 files changed

+137
-116
lines changed
 

‎src/app/maprenderertest.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ int main(int argc, char* argv[])
4040
QgsMapLayerRegistry::instance()->addMapLayer(layer2);
4141

4242
// open a window and do the rendering!
43-
TestWidget l(layer);
43+
/*TestWidget l(layer);
4444
l.resize(360,360);
45-
l.show();
45+
l.show();*/
46+
4647

47-
/*
4848
QgsMapCanvas canvas;
4949
canvas.setCanvasColor(Qt::white);
5050
canvas.setExtent(layer->extent());
@@ -63,7 +63,6 @@ int main(int argc, char* argv[])
6363

6464
QgsMapTool* pan = new QgsMapToolPan(&canvas);
6565
canvas.setMapTool(pan);
66-
*/
6766

6867
return app.exec();
6968
}

‎src/app/maprenderertest.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class TestWidget : public QLabel
2323
ms.setLayers(QStringList(layer->id()));
2424
ms.setExtent(layer->extent());
2525
ms.setOutputSize(size());
26-
ms.setOutputDpi(120);
2726

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

‎src/core/qgsmaprendererjob.cpp

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,23 @@ QgsMapRendererSequentialJob::QgsMapRendererSequentialJob(const QgsMapSettings& s
2020
: QgsMapRendererQImageJob(SequentialJob, settings)
2121
, mInternalJob(0)
2222
{
23+
qDebug("SEQUENTIAL construct");
2324
}
2425

2526
QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob()
2627
{
27-
delete mInternalJob;
28+
qDebug("SEQUENTIAL destruct");
29+
//delete mInternalJob;
30+
Q_ASSERT(mInternalJob == 0);
2831
}
2932

3033

3134
void QgsMapRendererSequentialJob::start()
3235
{
36+
qDebug("SEQUENTIAL START");
37+
qDebug("%d,%d", mSettings.outputSize().width(), mSettings.outputSize().height());
38+
3339
mImage = QImage(mSettings.outputSize(), QImage::Format_ARGB32_Premultiplied);
34-
mImage.fill(Qt::blue);
3540

3641
// 1. create an image where we will output all rendering
3742
mPainter = new QPainter(&mImage);
@@ -47,7 +52,11 @@ void QgsMapRendererSequentialJob::start()
4752
void QgsMapRendererSequentialJob::cancel()
4853
{
4954
if (mInternalJob)
55+
{
5056
mInternalJob->cancel();
57+
delete mInternalJob;
58+
mInternalJob = 0;
59+
}
5160
}
5261

5362

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

6069
void QgsMapRendererSequentialJob::internalFinished()
6170
{
71+
qDebug("SEQUENTIAL finished");
72+
6273
mPainter->end();
6374
delete mPainter;
6475
mPainter = 0;
@@ -78,17 +89,22 @@ QgsMapRendererCustomPainterJob::QgsMapRendererCustomPainterJob(const QgsMapSetti
7889
: QgsMapRendererJob(CustomPainterJob, settings)
7990
, mPainter(painter)
8091
{
81-
connect(&mFutureWatcher, SIGNAL(finished()), SLOT(futureFinished()));
92+
qDebug("QPAINTER construct");
8293
}
8394

8495
QgsMapRendererCustomPainterJob::~QgsMapRendererCustomPainterJob()
8596
{
86-
cancel();
97+
qDebug("QPAINTER destruct");
98+
Q_ASSERT(!mFutureWatcher.isRunning());
99+
//cancel();
87100
}
88101

89102
void QgsMapRendererCustomPainterJob::start()
90103
{
91-
qDebug("run!");
104+
qDebug("QPAINTER run!");
105+
106+
connect(&mFutureWatcher, SIGNAL(finished()), SLOT(futureFinished()));
107+
92108
mFuture = QtConcurrent::run(staticRender, this);
93109
mFutureWatcher.setFuture(mFuture);
94110
}
@@ -99,47 +115,46 @@ void QgsMapRendererCustomPainterJob::cancel()
99115
{
100116
if (mFuture.isRunning())
101117
{
118+
qDebug("QPAINTER cancelling");
119+
disconnect(&mFutureWatcher, SIGNAL(finished()), this, SLOT(futureFinished()));
120+
102121
mRenderContext.setRenderingStopped(true);
103122

123+
QTime t;
124+
t.start();
125+
104126
mFutureWatcher.waitForFinished();
105-
qApp->processEvents( QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers ); // TODO: necessary?
127+
128+
qDebug("QPAINER cancel waited %f ms", t.elapsed() / 1000.0);
129+
130+
futureFinished();
131+
132+
qDebug("QPAINTER cancelled");
106133
}
107134
}
108135

109136

110137
void QgsMapRendererCustomPainterJob::futureFinished()
111138
{
112-
qDebug("futureFinished");
139+
qDebug("QPAINTER futureFinished");
113140
emit finished();
114141
}
115142

116143

117144
void QgsMapRendererCustomPainterJob::staticRender(QgsMapRendererCustomPainterJob* self)
118145
{
119-
qDebug("staticRender");
120146
self->startRender();
121147
}
122148

123149

124150
void QgsMapRendererCustomPainterJob::startRender()
125151
{
126-
qDebug("startRender");
127-
/*
128-
for (int i = 0; i < 50; ++i)
129-
{
130-
if (mStopped)
131-
return;
152+
qDebug("QPAINTER startRender");
132153

133-
mPainter->drawLine(rand() % 360, rand() % 360, rand() % 360, rand() % 360);
134-
//QThread::msleep(100);
135-
int x = 0;
136-
for (int i = 0; i < 1000; i++)
137-
{
138-
for (int j = 0; j < 10000; j++)
139-
x *= x+5;
140-
}
141-
qDebug("drawn line");
142-
}*/
154+
// clear the background
155+
mPainter->fillRect( 0, 0, mSettings.outputSize().width(), mSettings.outputSize().height(), mSettings.backgroundColor() );
156+
157+
mPainter->setRenderHint( QPainter::Antialiasing, mSettings.isAntiAliasingEnabled() );
143158

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

168183
//calculate scale factor
169184
//use the specified dpi and not those from the paint device

‎src/core/qgsmapsettings.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ QgsMapSettings::QgsMapSettings()
3333
, mExtent()
3434
, mProjectionsEnabled( false )
3535
, mDestCRS( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId ) // WGS 84
36+
, mBackgroundColor( Qt::white )
37+
, mSelectionColor( Qt::yellow )
38+
, mAntiAliasing( true )
3639
, mOutputUnits( QgsMapSettings::Millimeters )
3740
{
3841
updateDerived();

‎src/core/qgsmapsettings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef QGSMAPSETTINGS_H
22
#define QGSMAPSETTINGS_H
33

4+
#include <QColor>
45
#include <QSize>
56
#include <QStringList>
67

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

63+
void setBackgroundColor( const QColor& color ) { mBackgroundColor = color; }
64+
QColor backgroundColor() const { return mBackgroundColor; }
65+
66+
void setSelectionColor( const QColor& color ) { mSelectionColor = color; }
67+
QColor selectionColor() const { return mSelectionColor; }
68+
69+
void setAntiAliasingEnabled( bool enabled ) { mAntiAliasing = enabled; }
70+
bool isAntiAliasingEnabled() const { return mAntiAliasing; }
71+
6272
bool hasValidSettings() const;
6373
QgsRectangle visibleExtent() const;
6474
double mapUnitsPerPixel() const;
@@ -129,6 +139,10 @@ class QgsMapSettings
129139
bool mProjectionsEnabled;
130140
QgsCoordinateReferenceSystem mDestCRS;
131141

142+
QColor mBackgroundColor;
143+
QColor mSelectionColor;
144+
bool mAntiAliasing;
145+
132146
//! Output units
133147
OutputUnits mOutputUnits;
134148

‎src/gui/qgsmapcanvas.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
8383
, mCanvasProperties( new CanvasProperties )
8484
//, mNewSize( QSize() )
8585
//, mPainting( false )
86-
, mAntiAliasing( false )
8786
{
8887
setObjectName( name );
8988
mScene = new QGraphicsScene();
@@ -171,8 +170,8 @@ QgsMapCanvas::~QgsMapCanvas()
171170

172171
void QgsMapCanvas::enableAntiAliasing( bool theFlag )
173172
{
174-
mAntiAliasing = theFlag;
175-
mMap->enableAntiAliasing( theFlag );
173+
mSettings.setAntiAliasingEnabled( theFlag );
174+
176175
if ( mMapOverview )
177176
mMapOverview->enableAntiAliasing( theFlag );
178177
} // anti aliasing
@@ -311,7 +310,7 @@ void QgsMapCanvas::setLayerSet( QList<QgsMapCanvasLayer> &layers )
311310

312311
if ( mMapOverview )
313312
{
314-
QStringList& layerSetOvOld = mMapOverview->layerSet();
313+
const QStringList& layerSetOvOld = mMapOverview->layerSet();
315314
if ( layerSetOvOld != layerSetOverview )
316315
{
317316
mMapOverview->setLayerSet( layerSetOverview );
@@ -1211,7 +1210,7 @@ void QgsMapCanvas::unsetMapTool( QgsMapTool* tool )
12111210
void QgsMapCanvas::setCanvasColor( const QColor & theColor )
12121211
{
12131212
// background of map's pixmap
1214-
mMap->setBackgroundColor( theColor );
1213+
mSettings.setBackgroundColor( theColor );
12151214

12161215
// background of the QGraphicsView
12171216
QBrush bgBrush( theColor );

‎src/gui/qgsmapcanvas.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
266266
void enableAntiAliasing( bool theFlag );
267267

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

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

510510
//! currently in paint event
511511
//bool mPainting;
512-
513-
//! indicates whether antialiasing will be used for rendering
514-
bool mAntiAliasing;
515512
}; // class QgsMapCanvas
516513

517514

‎src/gui/qgsmapcanvasmap.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,11 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
8080

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

83-
mImage.fill(mBgColor.rgb());
84-
8583
mPainter = new QPainter(&mImage);
8684

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

90-
// antialiasing
91-
if ( mAntiAliasing )
92-
mPainter->setRenderHint( QPainter::Antialiasing );
93-
9488
// create the renderer job
9589
Q_ASSERT(mJob == 0);
9690
mJob = new QgsMapRendererCustomPainterJob(s, mPainter);

‎src/gui/qgsmapcanvasmap.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ class GUI_EXPORT QgsMapCanvasMap : public QObject, public QGraphicsRectItem
4747
//! resize canvas item and pixmap
4848
void resize( QSize size );
4949

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

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

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

5759
void setPanningOffset( const QPoint& point );
5860

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

7577
private:
7678

77-
//! indicates whether antialiasing will be used for rendering
78-
bool mAntiAliasing;
79-
8079
QImage mImage;
8180
QImage mLastImage;
8281

8382
//QgsMapRenderer* mRender;
8483
QgsMapCanvas* mCanvas;
8584

86-
QColor mBgColor;
87-
8885
QPoint mOffset;
8986

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

0 commit comments

Comments
 (0)