@@ -48,6 +48,7 @@ email : sherman at mrcc.com
4848#include " qgsmaptopixel.h"
4949#include " qgsmapoverviewcanvas.h"
5050#include " qgsmaprenderer.h"
51+ #include " qgsmaprendererjob.h"
5152#include " qgsmessagelog.h"
5253#include " qgsmessageviewer.h"
5354#include " qgspallabeling.h"
@@ -56,6 +57,7 @@ email : sherman at mrcc.com
5657#include " qgsvectorlayer.h"
5758#include < math.h>
5859
60+
5961/* * @deprecated to be deleted, stuff from here should be moved elsewhere */
6062class QgsMapCanvas ::CanvasProperties
6163{
@@ -82,9 +84,9 @@ class QgsMapCanvas::CanvasProperties
8284QgsMapCanvas::QgsMapCanvas ( QWidget * parent, const char *name )
8385 : QGraphicsView( parent )
8486 , mCanvasProperties( new CanvasProperties )
87+ , mJob( 0 )
88+ , mJobCancelled( false )
8589 , mLabelingResults( 0 )
86- // , mNewSize( QSize() )
87- // , mPainting( false )
8890{
8991 setObjectName ( name );
9092 mScene = new QGraphicsScene ();
@@ -113,7 +115,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
113115 // create map canvas item which will show the map
114116 mMap = new QgsMapCanvasMap ( this );
115117 mScene ->addItem ( mMap );
116- mScene ->update (); // porting??
117118
118119 connect ( mMapRenderer , SIGNAL ( drawError ( QgsMapLayer* ) ), this , SLOT ( showError ( QgsMapLayer* ) ) );
119120
@@ -129,12 +130,14 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
129130 mSettings .setFlag ( QgsMapSettings::DrawEditingInfo );
130131
131132 mSettings .setOutputSize ( size () );
132- mMap ->resize ( size () );
133133 setSceneRect ( 0 , 0 , size ().width (), size ().height () );
134134 mScene ->setSceneRect ( QRectF ( 0 , 0 , size ().width (), size ().height () ) );
135135
136136 moveCanvasContents ( true );
137137
138+ connect (&mMapUpdateTimer , SIGNAL ( timeout () ), SLOT ( mapUpdateTimeout () ) );
139+ mMapUpdateTimer .setInterval ( 400 );
140+
138141#ifdef Q_OS_WIN
139142 // Enable touch event on Windows.
140143 // Qt on Windows needs to be told it can take touch events or else it ignores them.
@@ -171,6 +174,12 @@ QgsMapCanvas::~QgsMapCanvas()
171174 // mCanvasProperties auto-deleted via std::auto_ptr
172175 // CanvasProperties struct has its own dtor for freeing resources
173176
177+ if ( mJob )
178+ {
179+ mJob ->cancel ();
180+ Q_ASSERT ( mJob == 0 );
181+ }
182+
174183 delete mLabelingResults ;
175184
176185} // dtor
@@ -221,7 +230,8 @@ double QgsMapCanvas::scale()
221230
222231void QgsMapCanvas::setDirty ( bool dirty )
223232{
224- mDirty = dirty;
233+ if ( dirty )
234+ refresh ();
225235}
226236
227237bool QgsMapCanvas::isDirty () const
@@ -233,7 +243,7 @@ bool QgsMapCanvas::isDirty() const
233243
234244bool QgsMapCanvas::isDrawing ()
235245{
236- return false ;
246+ return mJob != 0 ;
237247} // isDrawing
238248
239249
@@ -399,12 +409,6 @@ const QgsLabelingResults *QgsMapCanvas::labelingResults() const
399409 return mLabelingResults ;
400410}
401411
402- void QgsMapCanvas::assignLabelingResults ( QgsLabelingResults* results )
403- {
404- delete mLabelingResults ;
405- mLabelingResults = results;
406- }
407-
408412
409413void QgsMapCanvas::updateOverview ()
410414{
@@ -424,7 +428,11 @@ QgsMapLayer* QgsMapCanvas::currentLayer()
424428
425429void QgsMapCanvas::refresh ()
426430{
427- mMap ->refresh ();
431+ stopRendering (); // if any...
432+
433+ qDebug (" CANVAS calling update" );
434+ mDirty = true ;
435+ update ();
428436
429437 /*
430438 // we can't draw again if already drawing...
@@ -478,9 +486,51 @@ void QgsMapCanvas::refresh()
478486
479487} // refresh
480488
489+
490+ void QgsMapCanvas::rendererJobFinished ()
491+ {
492+ qDebug (" CANVAS finish! %d" , !mJobCancelled );
493+
494+ mMapUpdateTimer .stop ();
495+
496+ mDirty = false ;
497+
498+ if ( !mJobCancelled )
499+ {
500+ QImage img = mJob ->renderedImage ();
501+
502+ // emit renderComplete to get our decorations drawn
503+ QPainter p ( &img );
504+ emit renderComplete ( &p );
505+ p.end ();
506+
507+ mMap ->setContent ( img, mSettings .visibleExtent () );
508+
509+ delete mLabelingResults ;
510+ mLabelingResults = mJob ->takeLabelingResults ();
511+ }
512+
513+ delete mJob ;
514+ mJob = 0 ;
515+ }
516+
517+ void QgsMapCanvas::mapUpdateTimeout ()
518+ {
519+ qDebug (" CANVAS update timer!" );
520+
521+ mMap ->setContent ( mJob ->renderedImage (), mSettings .visibleExtent () );
522+ }
523+
524+
481525void QgsMapCanvas::stopRendering ()
482526{
483- // TODO: implement stopping (?)
527+ if ( mJob )
528+ {
529+ qDebug (" CANVAS stop rendering!" );
530+ mJobCancelled = true ;
531+ mJob ->cancel ();
532+ Q_ASSERT ( mJob == 0 ); // no need to delete here: already deleted in finished()
533+ }
484534}
485535
486536void QgsMapCanvas::updateMap ()
@@ -507,11 +557,11 @@ void QgsMapCanvas::saveAsImage( QString theFileName, QPixmap * theQPixmap, QStri
507557 else // use the map view
508558 {
509559 // TODO[MD]: fix
510- QPixmap *pixmap = dynamic_cast <QPixmap *>( &mMap ->paintDevice () );
511- if ( !pixmap )
560+ QImage *img = dynamic_cast <QImage *>( &mMap ->paintDevice () );
561+ if ( !img )
512562 return ;
513563
514- pixmap ->save ( theFileName, theFormat.toLocal8Bit ().data () );
564+ img ->save ( theFileName, theFormat.toLocal8Bit ().data () );
515565 }
516566 // create a world file to go with the image...
517567 QgsRectangle myRect = mapSettings ().visibleExtent ();
@@ -611,9 +661,7 @@ void QgsMapCanvas::updateScale()
611661
612662void QgsMapCanvas::clear ()
613663{
614- // Indicate to the next paint event that we need to rebuild the canvas contents
615- setDirty ( true );
616-
664+ refresh ();
617665} // clear
618666
619667
@@ -985,9 +1033,6 @@ void QgsMapCanvas::resizeEvent( QResizeEvent * e )
9851033
9861034 mSettings .setOutputSize ( lastSize );
9871035
988- // set map size before scene size helps keep scene indexes updated properly
989- // this was the cause of rubberband artifacts
990- mMap ->resize ( lastSize );
9911036 mScene ->setSceneRect ( QRectF ( 0 , 0 , lastSize.width (), lastSize.height () ) );
9921037
9931038 moveCanvasContents ( true );
@@ -1004,6 +1049,30 @@ void QgsMapCanvas::resizeEvent( QResizeEvent * e )
10041049
10051050void QgsMapCanvas::paintEvent ( QPaintEvent *e )
10061051{
1052+ qDebug (" CANVAS paint()" );
1053+
1054+ if ( mDirty )
1055+ {
1056+ if ( mJob )
1057+ {
1058+ qDebug (" CANVAS already rendering" );
1059+ }
1060+ else
1061+ {
1062+ qDebug (" CANVAS need to render" );
1063+
1064+ // create the renderer job
1065+ Q_ASSERT ( mJob == 0 );
1066+ mJobCancelled = false ;
1067+ mJob = new QgsMapRendererSequentialJob ( mSettings );
1068+ connect (mJob , SIGNAL ( finished () ), SLOT ( rendererJobFinished () ) );
1069+ mJob ->start ();
1070+
1071+ mMapUpdateTimer .start ();
1072+ }
1073+ }
1074+
1075+
10071076 QGraphicsView::paintEvent ( e );
10081077} // paintEvent
10091078
@@ -1312,8 +1381,6 @@ void QgsMapCanvas::panActionEnd( QPoint releasePoint )
13121381 // move map image and other items to standard position
13131382 moveCanvasContents ( true ); // true means reset
13141383
1315- mMap ->mapDragged ( releasePoint - mCanvasProperties ->rubberStartPoint );
1316-
13171384 // use start and end box points to calculate the extent
13181385 QgsPoint start = getCoordinateTransform ()->toMapCoordinates ( mCanvasProperties ->rubberStartPoint );
13191386 QgsPoint end = getCoordinateTransform ()->toMapCoordinates ( releasePoint );
0 commit comments