Skip to content

Commit

Permalink
Map canvas uses renderer to QImage, fix for jumping image after pan
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Nov 6, 2013
1 parent 3c05976 commit 016f39e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/app/maprenderertest.cpp
Expand Up @@ -57,7 +57,7 @@ int main(int argc, char* argv[])
overview.show();

QList<QgsMapCanvasLayer> layers;
layers.append(QgsMapCanvasLayer(layer2, false, true));
layers.append(QgsMapCanvasLayer(layer2, true, true));
layers.append(QgsMapCanvasLayer(layer));
canvas.setLayerSet(layers);

Expand Down
7 changes: 0 additions & 7 deletions src/app/qgisapp.cpp
Expand Up @@ -8120,13 +8120,6 @@ void QgisApp::projectProperties()
long newCRSID = ms.destinationCrs().srsid();
bool isProjected = ms.hasCrsTransformEnabled();

// projections have been turned on/off or dest CRS has changed while projections are on
if ( wasProjected != isProjected || ( isProjected && oldCRSID != newCRSID ) )
{
// TODO: would be better to try to reproject current extent to the new one
mMapCanvas->updateFullExtent();
}

int myRedInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorRedPart", 255 );
int myGreenInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorGreenPart", 255 );
int myBlueInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorBluePart", 255 );
Expand Down
72 changes: 10 additions & 62 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -113,8 +113,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
mScene->addItem( mMap );
mScene->update(); // porting??

moveCanvasContents( true );

connect( mMapRenderer, SIGNAL( drawError( QgsMapLayer* ) ), this, SLOT( showError( QgsMapLayer* ) ) );

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

mSettings.setOutputDpi(120); // TODO: what to set ???
mSettings.setOutputSize( size() );
mMap->resize( size() );
setSceneRect( 0, 0, size().width(), size().height() );
mScene->setSceneRect( QRectF( 0, 0, size().width(), size().height() ) );

moveCanvasContents( true );

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

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

for ( i = 0; i < layerCount(); i++ )
{
Expand Down Expand Up @@ -540,19 +540,9 @@ QgsRectangle QgsMapCanvas::extent() const

QgsRectangle QgsMapCanvas::fullExtent() const
{
return mapSettings().fullExtent(); //mMapRenderer->fullExtent();
return mapSettings().fullExtent();
} // extent

void QgsMapCanvas::updateFullExtent()
{
// projection settings have changed
/*
QgsDebugMsg( "updating full extent" );
mMapRenderer->updateFullExtent();
refresh();
*/
}

void QgsMapCanvas::setExtent( QgsRectangle const & r )
{
Expand All @@ -564,12 +554,10 @@ void QgsMapCanvas::setExtent( QgsRectangle const & r )
QgsRectangle e( QgsPoint( r.center().x() - current.width() / 2.0, r.center().y() - current.height() / 2.0 ),
QgsPoint( r.center().x() + current.width() / 2.0, r.center().y() + current.height() / 2.0 ) );
mSettings.setExtent( e );
//mMapRenderer->setExtent( e );
}
else
{
mSettings.setExtent( r );
//mMapRenderer->setExtent( r );
}
emit extentsChanged();
updateScale();
Expand Down Expand Up @@ -641,7 +629,6 @@ void QgsMapCanvas::zoomToPreviousExtent()
{
mLastExtentIndex--;
mSettings.setExtent( mLastExtent[mLastExtentIndex] );
//mMapRenderer->setExtent( mLastExtent[mLastExtentIndex] );
emit extentsChanged();
updateScale();
if ( mMapOverview )
Expand All @@ -662,7 +649,6 @@ void QgsMapCanvas::zoomToNextExtent()
{
mLastExtentIndex++;
mSettings.setExtent( mLastExtent[mLastExtentIndex] );
//mMapRenderer->setExtent( mLastExtent[mLastExtentIndex] );
emit extentsChanged();
updateScale();
if ( mMapOverview )
Expand Down Expand Up @@ -993,6 +979,8 @@ void QgsMapCanvas::resizeEvent( QResizeEvent * e )
mMap->resize( lastSize );
mScene->setSceneRect( QRectF( 0, 0, lastSize.width(), lastSize.height() ) );

moveCanvasContents( true );

// notify canvas items of change
updateCanvasItemPositions();

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

void QgsMapCanvas::paintEvent( QPaintEvent *e )
{
/*
if ( mPainting || mDrawing )
{
//cancel current render progress
if ( mMapRenderer )
{
QgsRenderContext* theRenderContext = mMapRenderer->rendererContext();
if ( theRenderContext )
{
theRenderContext->setRenderingStopped( true );
}
}
return;
}
*/

QGraphicsView::paintEvent( e );
} // paintEvent

Expand Down Expand Up @@ -1284,7 +1256,6 @@ double QgsMapCanvas::mapUnitsPerPixel() const
void QgsMapCanvas::setMapUnits( QGis::UnitType u )
{
QgsDebugMsg( "Setting map units to " + QString::number( static_cast<int>( u ) ) );
//mMapRenderer->setMapUnits( u );
mSettings.setMapUnits( u );
}

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

mMap->mapDragged( releasePoint - mCanvasProperties->rubberStartPoint );

// use start and end box points to calculate the extent
QgsPoint start = getCoordinateTransform()->toMapCoordinates( mCanvasProperties->rubberStartPoint );
QgsPoint end = getCoordinateTransform()->toMapCoordinates( releasePoint );
Expand All @@ -1341,7 +1314,6 @@ void QgsMapCanvas::panActionEnd( QPoint releasePoint )
double dx = qAbs( end.x() - start.x() );
double dy = qAbs( end.y() - start.y() );


// modify the extent
QgsRectangle r = mapSettings().visibleExtent();

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

// move all map canvas items
moveCanvasContents();

// update canvas
//updateContents(); // TODO: need to update?
}

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

mMap->setPanningOffset( pnt );

QList<QGraphicsItem*> list = mScene->items();
QList<QGraphicsItem*>::iterator it = list.begin();
while ( it != list.end() )
{
QGraphicsItem* item = *it;

if ( item != mMap )
{
// this tells map canvas item to draw with offset
QgsMapCanvasItem* canvasItem = dynamic_cast<QgsMapCanvasItem *>( item );
if ( canvasItem )
canvasItem->setPanningOffset( pnt );
}

it++;
}

// show items
updateCanvasItemPositions();

setSceneRect( -pnt.x(), -pnt.y(), size().width(), size().height() );
}

void QgsMapCanvas::showError( QgsMapLayer * mapLayer )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -201,6 +201,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
void updateScale();

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

//! return the map layer at position index in the layer stack
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsmapcanvasitem.h
Expand Up @@ -63,7 +63,8 @@ class GUI_EXPORT QgsMapCanvasItem : public QGraphicsItem
virtual QRectF boundingRect() const;

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

//! returns canvas item rectangle
QgsRectangle rect() const;
Expand Down
47 changes: 18 additions & 29 deletions src/gui/qgsmapcanvasmap.cpp
Expand Up @@ -26,6 +26,7 @@ QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas* canvas )
: mCanvas( canvas )
, mDirty(true)
, mJob(0)
, mOffset()
{
setZValue( -10 );
setPos( 0, 0 );
Expand Down Expand Up @@ -59,8 +60,6 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
{
qDebug("paint()");

bool paintedAlready = false;

if (mDirty)
{
if (mJob)
Expand All @@ -72,31 +71,24 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
qDebug("need to render");

// draw the image before it will be wiped out
// TODO: does not work correctly with panning by dragging
p->drawImage( 0, 0, mImage );
paintedAlready = true;

const QgsMapSettings& s = mCanvas->mapSettings();

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

mPainter = new QPainter(&mImage);
p->drawImage( mOffset, mImage );

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

// create the renderer job
Q_ASSERT(mJob == 0);
mJob = new QgsMapRendererCustomPainterJob(s, mPainter);
mJob = new QgsMapRendererSequentialJob( mCanvas->mapSettings() );
connect(mJob, SIGNAL(finished()), SLOT(finish()));
mJob->start();

mTimer.start();

mOffset = QPoint();
return;
}
}

if (!paintedAlready)
{
#ifdef EGA_MODE
QImage i2( mImage.size()/3, mImage.format() );
QPainter p2(&i2);
Expand All @@ -106,12 +98,12 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
#else
p->drawImage( 0, 0, mImage );
#endif
}
}

QRectF QgsMapCanvasMap::boundingRect() const
{
return QRectF( 0, 0, mImage.width(), mImage.height() );
QSize s = mCanvas->mapSettings().outputSize();
return QRectF( 0, 0, s.width(), s.height() ); // mImage.width(), mImage.height() );
}


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

QgsDebugMsg( QString( "resizing to %1x%2" ).arg( size.width() ).arg( size.height() ) );
prepareGeometryChange(); // to keep QGraphicsScene indexes up to date on size change

mImage = QImage( size, QImage::Format_ARGB32_Premultiplied );
//mCanvas->mapRenderer()->setOutputSize( size, mImage.logicalDpiX() );
}

void QgsMapCanvasMap::setPanningOffset( const QPoint& point )
{
mOffset = point;
setPos( mOffset );
}

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

mDirty = false;

delete mPainter;
mPainter = 0;

//mLastImage = mImage;
mImage = mJob->renderedImage();

update();
}
Expand All @@ -165,5 +145,14 @@ void QgsMapCanvasMap::onMapUpdateTimeout()
{
qDebug("update timer!");

mImage = mJob->renderedImage();

update();
}


void QgsMapCanvasMap::mapDragged(const QPoint &diff)
{
mOffset = diff;
update();
}
13 changes: 7 additions & 6 deletions src/gui/qgsmapcanvasmap.h
Expand Up @@ -56,7 +56,8 @@ class GUI_EXPORT QgsMapCanvasMap : public QObject, public QGraphicsRectItem
//! @deprecated in 2.1 - does nothing. Kept for API compatibility
void setBackgroundColor( const QColor& color ) {}

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

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

const QgsMapSettings& settings() const;

//! called by map canvas to handle panning with mouse (kind of)
//! @note added in 2.1
void mapDragged( const QPoint& diff);

public slots:
void finish();
void onMapUpdateTimeout();

private:

QImage mImage;
QImage mLastImage;

//QgsMapRenderer* mRender;
QgsMapCanvas* mCanvas;

QPoint mOffset;

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

QgsMapRendererCustomPainterJob* mJob;

QPainter* mPainter;
QgsMapRendererSequentialJob* mJob;

QTimer mTimer;
};
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmapoverviewcanvas.cpp
Expand Up @@ -112,7 +112,7 @@ void QgsMapOverviewCanvas::drawExtentRect()
const QgsRectangle& extent = mMapCanvas->extent();

// show only when valid extent is set
if ( extent.isEmpty() || mSettings.extent().isEmpty() )
if ( extent.isEmpty() || mSettings.visibleExtent().isEmpty() )
{
mPanningWidget->hide();
return;
Expand Down

0 comments on commit 016f39e

Please sign in to comment.