20 #include <QApplication>
24 #include <QGraphicsItem>
25 #include <QGraphicsScene>
26 #include <QGraphicsView>
28 #include <QMouseEvent>
30 #include <QPaintEvent>
33 #include <QTextStream>
34 #include <QResizeEvent>
36 #include <QStringList>
37 #include <QWheelEvent>
82 : QGraphicsView( parent )
86 , mAntiAliasing( false )
88 setObjectName( name );
89 mScene =
new QGraphicsScene();
91 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
92 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
109 setMouseTracking(
true );
110 setFocusPolicy( Qt::StrongFocus );
128 this, SLOT(
readProject(
const QDomDocument & ) ) );
136 grabGesture( Qt::PinchGesture );
137 viewport()->setAttribute( Qt::WA_AcceptTouchEvents );
154 QList<QGraphicsItem*> list =
mScene->items();
155 QList<QGraphicsItem*>::iterator it = list.begin();
156 while ( it != list.end() )
158 QGraphicsItem* item = *it;
199 if ( index >= 0 && index < (
int ) layers.size() )
245 QgsDebugMsg(
"NOT updating layer set while drawing" );
250 QStringList layerSet, layerSetOverview;
253 for ( i = 0; i < layers.size(); i++ )
263 layerSet.push_back( lyr.
layer()->
id() );
268 layerSetOverview.push_back( lyr.
layer()->
id() );
274 bool layerSetChanged = layerSetOld != layerSet;
277 if ( layerSetChanged )
279 QgsDebugMsg(
"Layers changed to: " + layerSet.join(
", " ) );
286 disconnect( currentLayer, SIGNAL( repaintRequested() ),
this, SLOT(
refresh() ) );
287 disconnect( currentLayer, SIGNAL( screenUpdateRequested() ),
this, SLOT(
updateMap() ) );
302 connect( currentLayer, SIGNAL( repaintRequested() ),
this, SLOT(
refresh() ) );
303 connect( currentLayer, SIGNAL( screenUpdateRequested() ),
this, SLOT(
updateMap() ) );
320 if ( layerSetOvOld != layerSetOverview )
338 disconnect(
mMapRenderer, SIGNAL( destinationSrsChanged() ),
351 connect(
mMapRenderer, SIGNAL( destinationSrsChanged() ),
352 overview, SLOT( destinationSrsChanged() ) );
380 bool logRefresh = settings.value(
"/Map/logCanvasRefreshEvent",
false ).toBool();
388 bool enableBackbufferSetting = settings.value(
"/Map/enableBackbuffer", 1 ).toBool();
398 qDebug() <<
"Enable back buffering: " << enableBackbufferSetting;
399 if ( enableBackbufferSetting )
401 viewport()->setAttribute( Qt::WA_PaintOnScreen,
false );
405 viewport()->setAttribute( Qt::WA_PaintOnScreen,
true );
420 QApplication::setOverrideCursor( Qt::WaitCursor );
439 QApplication::restoreOverrideCursor();
449 QString logMsg =
tr(
"Canvas refresh: %1 ms" ).arg( t.elapsed() );
450 QObject* senderObj = QObject::sender();
453 logMsg +=
tr(
", sender '%1'" ).arg( senderObj->metaObject()->className() );
474 if ( theQPixmap != NULL )
478 painter.begin( theQPixmap );
483 theQPixmap->save( theFileName, theFormat.toLocal8Bit().data() );
491 pixmap->save( theFileName, theFormat.toLocal8Bit().data() );
500 myHeader +=
"0 \r\n";
502 myHeader +=
"0 \r\n";
510 QFileInfo myInfo = QFileInfo( theFileName );
512 QString myWorldFileName = myInfo.absolutePath() +
"/" + myInfo.completeBaseName() +
"." + theFormat +
"w";
513 QFile myWorldFile( myWorldFileName );
514 if ( !myWorldFile.open( QIODevice::WriteOnly ) )
518 QTextStream myStream( &myWorldFile );
519 myStream << myHeader;
555 QgsDebugMsg(
"Empty extent - keeping old extent with new center!" );
626 extent.
scale( 1.05 );
749 rect.
scale( 1.0, &c );
806 QPen pen( Qt::gray );
814 double dx = qAbs(( currentExtent.
xMaximum() - currentExtent.
xMinimum() ) / 4 );
815 double dy = qAbs(( currentExtent.
yMaximum() - currentExtent.
yMinimum() ) / 4 );
861 if ( ! e->isAutoRepeat() )
873 case Qt::Key_PageDown:
886 QgsDebugMsg(
"Ignoring key: " + QString::number( e->key() ) );
922 QgsDebugMsg(
"Ignoring key release: " + QString::number( e->key() ) );
951 if ( e->button() == Qt::MidButton )
983 if ( e->button() == Qt::MidButton )
996 QgsDebugMsg(
"Right click in map tool zoom or pan, last tool is " +
1038 if ( theRenderContext )
1056 mScene->setSceneRect( QRectF( 0, 0, lastSize.width(), lastSize.height() ) );
1076 QList<QGraphicsItem*> list =
mScene->items();
1077 QList<QGraphicsItem*>::iterator it = list.begin();
1078 while ( it != list.end() )
1097 QgsDebugMsg(
"Wheel event delta " + QString::number( e->delta() ) );
1109 if ( QgsApplication::keyboardModifiers() )
1119 if ( e->delta() > 0 )
1137 QgsPoint newCenter( mousePos.x() + (( oldCenter.x() - mousePos.x() ) * scaleFactor ),
1138 mousePos.y() + (( oldCenter.y() - mousePos.y() ) * scaleFactor ) );
1142 extent.
scale( scaleFactor, &newCenter );
1187 r.
scale( scaleFactor, ¢er );
1213 QPoint xy = e->pos();
1262 setCursor( Qt::ArrowCursor );
1278 QBrush bgBrush( theColor );
1279 setBackgroundBrush( bgBrush );
1282 palette.setColor( backgroundRole(), theColor );
1283 setPalette( palette );
1287 mScene->setBackgroundBrush( bgBrush );
1292 return mScene->backgroundBrush().color();
1303 QList<QgsMapLayer*> lst;
1308 lst.append( layer );
1348 QgsDebugMsg(
"Setting map units to " + QString::number( static_cast<int>( u ) ) );
1380 QgsDebugMsg(
"QgsMapCanvas connected to " + QString( signal ) );
1404 double dx = qAbs( end.
x() - start.
x() );
1405 double dy = qAbs( end.
y() - start.
y() );
1410 if ( end.
x() < start.
x() )
1413 r.setXMaximum( r.xMaximum() + dx );
1417 r.setXMinimum( r.xMinimum() - dx );
1418 r.setXMaximum( r.xMaximum() - dx );
1421 if ( end.
y() < start.
y() )
1423 r.setYMaximum( r.yMaximum() + dy );
1424 r.setYMinimum( r.yMinimum() + dy );
1429 r.setYMaximum( r.yMaximum() - dy );
1430 r.setYMinimum( r.yMinimum() - dy );
1467 QList<QGraphicsItem*> list =
mScene->items();
1468 QList<QGraphicsItem*>::iterator it = list.begin();
1469 while ( it != list.end() )
1471 QGraphicsItem* item = *it;
1492 QMessageBox::warning(
1495 tr(
"Could not draw %1 because:\n%2",
"COMMENTED OUT" ).arg( mapLayer->
name() ).arg( mapLayer->
lastError() )
1516 QDomNodeList nodes = doc.elementsByTagName(
"mapcanvas" );
1517 if ( nodes.count() )
1519 QDomNode node = nodes.item( 0 );
1525 QgsDebugMsg(
"Couldn't read mapcanvas information from project" );
1533 QDomNodeList nl = doc.elementsByTagName(
"qgis" );
1536 QgsDebugMsg(
"Unable to find qgis element in project file" );
1539 QDomNode qgisNode = nl.item( 0 );
1541 QDomElement mapcanvasNode = doc.createElement(
"mapcanvas" );
1542 qgisNode.appendChild( mapcanvasNode );
1554 r.
scale( scaleFactor );
1579 QgsDebugMsg(
"refreshing after reprojection was enabled" );
1594 bool QgsMapCanvas::event( QEvent * e )
1601 if ( e->type() == QEvent::Gesture )
1606 done =
mMapTool->gestureEvent( static_cast<QGestureEvent*>( e ) );
1612 done = QGraphicsView::event( e );