Skip to content

Commit 016f39e

Browse files
committedNov 6, 2013
Map canvas uses renderer to QImage, fix for jumping image after pan
1 parent 3c05976 commit 016f39e

File tree

8 files changed

+40
-107
lines changed

8 files changed

+40
-107
lines changed
 

‎src/app/maprenderertest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int main(int argc, char* argv[])
5757
overview.show();
5858

5959
QList<QgsMapCanvasLayer> layers;
60-
layers.append(QgsMapCanvasLayer(layer2, false, true));
60+
layers.append(QgsMapCanvasLayer(layer2, true, true));
6161
layers.append(QgsMapCanvasLayer(layer));
6262
canvas.setLayerSet(layers);
6363

‎src/app/qgisapp.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8120,13 +8120,6 @@ void QgisApp::projectProperties()
81208120
long newCRSID = ms.destinationCrs().srsid();
81218121
bool isProjected = ms.hasCrsTransformEnabled();
81228122

8123-
// projections have been turned on/off or dest CRS has changed while projections are on
8124-
if ( wasProjected != isProjected || ( isProjected && oldCRSID != newCRSID ) )
8125-
{
8126-
// TODO: would be better to try to reproject current extent to the new one
8127-
mMapCanvas->updateFullExtent();
8128-
}
8129-
81308123
int myRedInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorRedPart", 255 );
81318124
int myGreenInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorGreenPart", 255 );
81328125
int myBlueInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorBluePart", 255 );

‎src/gui/qgsmapcanvas.cpp

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
113113
mScene->addItem( mMap );
114114
mScene->update(); // porting??
115115

116-
moveCanvasContents( true );
117-
118116
connect( mMapRenderer, SIGNAL( drawError( QgsMapLayer* ) ), this, SLOT( showError( QgsMapLayer* ) ) );
119117

120118
// TODO: propagate signals to map renderer?
@@ -126,9 +124,12 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
126124
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ),
127125
this, SLOT( writeProject( QDomDocument & ) ) );
128126

129-
mSettings.setOutputDpi(120); // TODO: what to set ???
130127
mSettings.setOutputSize( size() );
131128
mMap->resize( size() );
129+
setSceneRect( 0, 0, size().width(), size().height() );
130+
mScene->setSceneRect( QRectF( 0, 0, size().width(), size().height() ) );
131+
132+
moveCanvasContents( true );
132133

133134
#ifdef Q_OS_WIN
134135
// Enable touch event on Windows.
@@ -287,7 +288,6 @@ void QgsMapCanvas::setLayerSet( QList<QgsMapCanvasLayer> &layers )
287288
}
288289

289290
mSettings.setLayers( layerSet );
290-
//mMapRenderer->setLayerSet( layerSet );
291291

292292
for ( i = 0; i < layerCount(); i++ )
293293
{
@@ -540,19 +540,9 @@ QgsRectangle QgsMapCanvas::extent() const
540540

541541
QgsRectangle QgsMapCanvas::fullExtent() const
542542
{
543-
return mapSettings().fullExtent(); //mMapRenderer->fullExtent();
543+
return mapSettings().fullExtent();
544544
} // extent
545545

546-
void QgsMapCanvas::updateFullExtent()
547-
{
548-
// projection settings have changed
549-
/*
550-
QgsDebugMsg( "updating full extent" );
551-
552-
mMapRenderer->updateFullExtent();
553-
refresh();
554-
*/
555-
}
556546

557547
void QgsMapCanvas::setExtent( QgsRectangle const & r )
558548
{
@@ -564,12 +554,10 @@ void QgsMapCanvas::setExtent( QgsRectangle const & r )
564554
QgsRectangle e( QgsPoint( r.center().x() - current.width() / 2.0, r.center().y() - current.height() / 2.0 ),
565555
QgsPoint( r.center().x() + current.width() / 2.0, r.center().y() + current.height() / 2.0 ) );
566556
mSettings.setExtent( e );
567-
//mMapRenderer->setExtent( e );
568557
}
569558
else
570559
{
571560
mSettings.setExtent( r );
572-
//mMapRenderer->setExtent( r );
573561
}
574562
emit extentsChanged();
575563
updateScale();
@@ -641,7 +629,6 @@ void QgsMapCanvas::zoomToPreviousExtent()
641629
{
642630
mLastExtentIndex--;
643631
mSettings.setExtent( mLastExtent[mLastExtentIndex] );
644-
//mMapRenderer->setExtent( mLastExtent[mLastExtentIndex] );
645632
emit extentsChanged();
646633
updateScale();
647634
if ( mMapOverview )
@@ -662,7 +649,6 @@ void QgsMapCanvas::zoomToNextExtent()
662649
{
663650
mLastExtentIndex++;
664651
mSettings.setExtent( mLastExtent[mLastExtentIndex] );
665-
//mMapRenderer->setExtent( mLastExtent[mLastExtentIndex] );
666652
emit extentsChanged();
667653
updateScale();
668654
if ( mMapOverview )
@@ -993,6 +979,8 @@ void QgsMapCanvas::resizeEvent( QResizeEvent * e )
993979
mMap->resize( lastSize );
994980
mScene->setSceneRect( QRectF( 0, 0, lastSize.width(), lastSize.height() ) );
995981

982+
moveCanvasContents( true );
983+
996984
// notify canvas items of change
997985
updateCanvasItemPositions();
998986

@@ -1005,22 +993,6 @@ void QgsMapCanvas::resizeEvent( QResizeEvent * e )
1005993

1006994
void QgsMapCanvas::paintEvent( QPaintEvent *e )
1007995
{
1008-
/*
1009-
if ( mPainting || mDrawing )
1010-
{
1011-
//cancel current render progress
1012-
if ( mMapRenderer )
1013-
{
1014-
QgsRenderContext* theRenderContext = mMapRenderer->rendererContext();
1015-
if ( theRenderContext )
1016-
{
1017-
theRenderContext->setRenderingStopped( true );
1018-
}
1019-
}
1020-
return;
1021-
}
1022-
*/
1023-
1024996
QGraphicsView::paintEvent( e );
1025997
} // paintEvent
1026998

@@ -1284,7 +1256,6 @@ double QgsMapCanvas::mapUnitsPerPixel() const
12841256
void QgsMapCanvas::setMapUnits( QGis::UnitType u )
12851257
{
12861258
QgsDebugMsg( "Setting map units to " + QString::number( static_cast<int>( u ) ) );
1287-
//mMapRenderer->setMapUnits( u );
12881259
mSettings.setMapUnits( u );
12891260
}
12901261

@@ -1331,6 +1302,8 @@ void QgsMapCanvas::panActionEnd( QPoint releasePoint )
13311302
// move map image and other items to standard position
13321303
moveCanvasContents( true ); // true means reset
13331304

1305+
mMap->mapDragged( releasePoint - mCanvasProperties->rubberStartPoint );
1306+
13341307
// use start and end box points to calculate the extent
13351308
QgsPoint start = getCoordinateTransform()->toMapCoordinates( mCanvasProperties->rubberStartPoint );
13361309
QgsPoint end = getCoordinateTransform()->toMapCoordinates( releasePoint );
@@ -1341,7 +1314,6 @@ void QgsMapCanvas::panActionEnd( QPoint releasePoint )
13411314
double dx = qAbs( end.x() - start.x() );
13421315
double dy = qAbs( end.y() - start.y() );
13431316

1344-
13451317
// modify the extent
13461318
QgsRectangle r = mapSettings().visibleExtent();
13471319

@@ -1386,9 +1358,6 @@ void QgsMapCanvas::panAction( QMouseEvent * e )
13861358

13871359
// move all map canvas items
13881360
moveCanvasContents();
1389-
1390-
// update canvas
1391-
//updateContents(); // TODO: need to update?
13921361
}
13931362

13941363
void QgsMapCanvas::moveCanvasContents( bool reset )
@@ -1397,28 +1366,7 @@ void QgsMapCanvas::moveCanvasContents( bool reset )
13971366
if ( !reset )
13981367
pnt += mCanvasProperties->mouseLastXY - mCanvasProperties->rubberStartPoint;
13991368

1400-
mMap->setPanningOffset( pnt );
1401-
1402-
QList<QGraphicsItem*> list = mScene->items();
1403-
QList<QGraphicsItem*>::iterator it = list.begin();
1404-
while ( it != list.end() )
1405-
{
1406-
QGraphicsItem* item = *it;
1407-
1408-
if ( item != mMap )
1409-
{
1410-
// this tells map canvas item to draw with offset
1411-
QgsMapCanvasItem* canvasItem = dynamic_cast<QgsMapCanvasItem *>( item );
1412-
if ( canvasItem )
1413-
canvasItem->setPanningOffset( pnt );
1414-
}
1415-
1416-
it++;
1417-
}
1418-
1419-
// show items
1420-
updateCanvasItemPositions();
1421-
1369+
setSceneRect( -pnt.x(), -pnt.y(), size().width(), size().height() );
14221370
}
14231371

14241372
void QgsMapCanvas::showError( QgsMapLayer * mapLayer )

‎src/gui/qgsmapcanvas.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
201201
void updateScale();
202202

203203
/** Updates the full extent */
204+
//! @deprecated since v2.1 - does nothing
204205
void updateFullExtent();
205206

206207
//! return the map layer at position index in the layer stack

‎src/gui/qgsmapcanvasitem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class GUI_EXPORT QgsMapCanvasItem : public QGraphicsItem
6363
virtual QRectF boundingRect() const;
6464

6565
//! sets current offset, to be called from QgsMapCanvas
66-
void setPanningOffset( const QPoint& point );
66+
//! @deprecated since v2.1 - not called by QgsMapCanvas anymore
67+
Q_DECL_DEPRECATED void setPanningOffset( const QPoint& point );
6768

6869
//! returns canvas item rectangle
6970
QgsRectangle rect() const;

‎src/gui/qgsmapcanvasmap.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas* canvas )
2626
: mCanvas( canvas )
2727
, mDirty(true)
2828
, mJob(0)
29+
, mOffset()
2930
{
3031
setZValue( -10 );
3132
setPos( 0, 0 );
@@ -59,8 +60,6 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
5960
{
6061
qDebug("paint()");
6162

62-
bool paintedAlready = false;
63-
6463
if (mDirty)
6564
{
6665
if (mJob)
@@ -72,31 +71,24 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
7271
qDebug("need to render");
7372

7473
// draw the image before it will be wiped out
75-
// TODO: does not work correctly with panning by dragging
76-
p->drawImage( 0, 0, mImage );
77-
paintedAlready = true;
78-
79-
const QgsMapSettings& s = mCanvas->mapSettings();
80-
81-
qDebug("----------> EXTENT %f,%f", s.extent().xMinimum(), s.extent().yMinimum());
82-
83-
mPainter = new QPainter(&mImage);
74+
p->drawImage( mOffset, mImage );
8475

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

8879
// create the renderer job
8980
Q_ASSERT(mJob == 0);
90-
mJob = new QgsMapRendererCustomPainterJob(s, mPainter);
81+
mJob = new QgsMapRendererSequentialJob( mCanvas->mapSettings() );
9182
connect(mJob, SIGNAL(finished()), SLOT(finish()));
9283
mJob->start();
9384

9485
mTimer.start();
86+
87+
mOffset = QPoint();
88+
return;
9589
}
9690
}
9791

98-
if (!paintedAlready)
99-
{
10092
#ifdef EGA_MODE
10193
QImage i2( mImage.size()/3, mImage.format() );
10294
QPainter p2(&i2);
@@ -106,12 +98,12 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
10698
#else
10799
p->drawImage( 0, 0, mImage );
108100
#endif
109-
}
110101
}
111102

112103
QRectF QgsMapCanvasMap::boundingRect() const
113104
{
114-
return QRectF( 0, 0, mImage.width(), mImage.height() );
105+
QSize s = mCanvas->mapSettings().outputSize();
106+
return QRectF( 0, 0, s.width(), s.height() ); // mImage.width(), mImage.height() );
115107
}
116108

117109

@@ -127,15 +119,6 @@ void QgsMapCanvasMap::resize( QSize size )
127119

128120
QgsDebugMsg( QString( "resizing to %1x%2" ).arg( size.width() ).arg( size.height() ) );
129121
prepareGeometryChange(); // to keep QGraphicsScene indexes up to date on size change
130-
131-
mImage = QImage( size, QImage::Format_ARGB32_Premultiplied );
132-
//mCanvas->mapRenderer()->setOutputSize( size, mImage.logicalDpiX() );
133-
}
134-
135-
void QgsMapCanvasMap::setPanningOffset( const QPoint& point )
136-
{
137-
mOffset = point;
138-
setPos( mOffset );
139122
}
140123

141124
QPaintDevice& QgsMapCanvasMap::paintDevice()
@@ -152,10 +135,7 @@ void QgsMapCanvasMap::finish()
152135

153136
mDirty = false;
154137

155-
delete mPainter;
156-
mPainter = 0;
157-
158-
//mLastImage = mImage;
138+
mImage = mJob->renderedImage();
159139

160140
update();
161141
}
@@ -165,5 +145,14 @@ void QgsMapCanvasMap::onMapUpdateTimeout()
165145
{
166146
qDebug("update timer!");
167147

148+
mImage = mJob->renderedImage();
149+
150+
update();
151+
}
152+
153+
154+
void QgsMapCanvasMap::mapDragged(const QPoint &diff)
155+
{
156+
mOffset = diff;
168157
update();
169158
}

‎src/gui/qgsmapcanvasmap.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class GUI_EXPORT QgsMapCanvasMap : public QObject, public QGraphicsRectItem
5656
//! @deprecated in 2.1 - does nothing. Kept for API compatibility
5757
void setBackgroundColor( const QColor& color ) {}
5858

59-
void setPanningOffset( const QPoint& point );
59+
//! @deprecated in 2.1 - not called by QgsMapCanvas anymore
60+
Q_DECL_DEPRECATED void setPanningOffset( const QPoint& point ) {}
6061

6162
//! @deprecated in 2.1
6263
QPaintDevice& paintDevice();
@@ -70,25 +71,25 @@ class GUI_EXPORT QgsMapCanvasMap : public QObject, public QGraphicsRectItem
7071

7172
const QgsMapSettings& settings() const;
7273

74+
//! called by map canvas to handle panning with mouse (kind of)
75+
//! @note added in 2.1
76+
void mapDragged( const QPoint& diff);
77+
7378
public slots:
7479
void finish();
7580
void onMapUpdateTimeout();
7681

7782
private:
7883

7984
QImage mImage;
80-
QImage mLastImage;
8185

82-
//QgsMapRenderer* mRender;
8386
QgsMapCanvas* mCanvas;
8487

8588
QPoint mOffset;
8689

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

89-
QgsMapRendererCustomPainterJob* mJob;
90-
91-
QPainter* mPainter;
92+
QgsMapRendererSequentialJob* mJob;
9293

9394
QTimer mTimer;
9495
};

‎src/gui/qgsmapoverviewcanvas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void QgsMapOverviewCanvas::drawExtentRect()
112112
const QgsRectangle& extent = mMapCanvas->extent();
113113

114114
// show only when valid extent is set
115-
if ( extent.isEmpty() || mSettings.extent().isEmpty() )
115+
if ( extent.isEmpty() || mSettings.visibleExtent().isEmpty() )
116116
{
117117
mPanningWidget->hide();
118118
return;

0 commit comments

Comments
 (0)