20 #include <QApplication>
24 #include <QGraphicsItem>
25 #include <QGraphicsScene>
26 #include <QGraphicsView>
28 #include <QMouseEvent>
30 #include <QPaintEvent>
34 #include <QTextStream>
35 #include <QResizeEvent>
37 #include <QStringList>
38 #include <QWheelEvent>
85 : QGraphicsView( parent )
89 , mAntiAliasing( false )
91 setObjectName( name );
92 mScene =
new QGraphicsScene();
94 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
95 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
112 setMouseTracking(
true );
113 setFocusPolicy( Qt::StrongFocus );
116 connect(
mMapRenderer, SIGNAL( datumTransformInfoRequested(
const QgsMapLayer*,
const QString&,
const QString& ) ),
136 this, SLOT(
readProject(
const QDomDocument & ) ) );
144 grabGesture( Qt::PinchGesture );
145 viewport()->setAttribute( Qt::WA_AcceptTouchEvents );
162 QList<QGraphicsItem*> list =
mScene->items();
163 QList<QGraphicsItem*>::iterator it = list.begin();
164 while ( it != list.end() )
166 QGraphicsItem* item = *it;
207 if ( index >= 0 && index < (
int ) layers.size() )
253 QgsDebugMsg(
"NOT updating layer set while drawing" );
258 QStringList layerSet, layerSetOverview;
261 for ( i = 0; i < layers.size(); i++ )
271 layerSet.push_back( lyr.
layer()->
id() );
276 layerSetOverview.push_back( lyr.
layer()->
id() );
282 bool layerSetChanged = layerSetOld != layerSet;
285 if ( layerSetChanged )
287 QgsDebugMsg(
"Layers changed to: " + layerSet.join(
", " ) );
294 disconnect( currentLayer, SIGNAL( repaintRequested() ),
this, SLOT(
refresh() ) );
295 disconnect( currentLayer, SIGNAL( screenUpdateRequested() ),
this, SLOT(
updateMap() ) );
310 connect( currentLayer, SIGNAL( repaintRequested() ),
this, SLOT(
refresh() ) );
311 connect( currentLayer, SIGNAL( screenUpdateRequested() ),
this, SLOT(
updateMap() ) );
328 if ( layerSetOvOld != layerSetOverview )
344 disconnect(
mMapRenderer, SIGNAL( hasCrsTransformEnabledChanged(
bool ) ),
346 disconnect(
mMapRenderer, SIGNAL( destinationSrsChanged() ),
357 connect(
mMapRenderer, SIGNAL( hasCrsTransformEnabledChanged(
bool ) ),
359 connect(
mMapRenderer, SIGNAL( destinationSrsChanged() ),
360 overview, SLOT( destinationSrsChanged() ) );
390 bool logRefresh = settings.value(
"/Map/logCanvasRefreshEvent",
false ).toBool();
398 bool enableBackbufferSetting = settings.value(
"/Map/enableBackbuffer", 1 ).toBool();
408 qDebug() <<
"Enable back buffering: " << enableBackbufferSetting;
409 if ( enableBackbufferSetting )
411 viewport()->setAttribute( Qt::WA_PaintOnScreen,
false );
415 viewport()->setAttribute( Qt::WA_PaintOnScreen,
true );
433 QApplication::setOverrideCursor( Qt::WaitCursor );
452 QApplication::restoreOverrideCursor();
462 QString logMsg =
tr(
"Canvas refresh: %1 ms" ).arg( t.elapsed() );
463 QObject* senderObj = QObject::sender();
466 logMsg +=
tr(
", sender '%1'" ).arg( senderObj->metaObject()->className() );
487 if ( theQPixmap != NULL )
491 painter.begin( theQPixmap );
496 theQPixmap->save( theFileName, theFormat.toLocal8Bit().data() );
504 pixmap->save( theFileName, theFormat.toLocal8Bit().data() );
513 myHeader +=
"0 \r\n";
515 myHeader +=
"0 \r\n";
523 QFileInfo myInfo = QFileInfo( theFileName );
525 QString myWorldFileName = myInfo.absolutePath() +
"/" + myInfo.completeBaseName() +
"." + theFormat +
"w";
526 QFile myWorldFile( myWorldFileName );
527 if ( !myWorldFile.open( QIODevice::WriteOnly ) )
531 QTextStream myStream( &myWorldFile );
532 myStream << myHeader;
568 QgsDebugMsg(
"Empty extent - keeping old extent with new center!" );
639 extent.
scale( 1.05 );
762 rect.
scale( 1.0, &c );
820 QPen pen( Qt::gray );
828 double dx = qAbs(( currentExtent.
xMaximum() - currentExtent.
xMinimum() ) / 4 );
829 double dy = qAbs(( currentExtent.
yMaximum() - currentExtent.
yMinimum() ) / 4 );
875 if ( ! e->isAutoRepeat() )
887 case Qt::Key_PageDown:
900 QgsDebugMsg(
"Ignoring key: " + QString::number( e->key() ) );
937 QgsDebugMsg(
"Ignoring key release: " + QString::number( e->key() ) );
966 if ( e->button() == Qt::MidButton )
998 if ( e->button() == Qt::MidButton )
1011 QgsDebugMsg(
"Right click in map tool zoom or pan, last tool is " +
1054 if ( theRenderContext )
1072 mScene->setSceneRect( QRectF( 0, 0, lastSize.width(), lastSize.height() ) );
1092 QList<QGraphicsItem*> list =
mScene->items();
1093 QList<QGraphicsItem*>::iterator it = list.begin();
1094 while ( it != list.end() )
1113 QgsDebugMsg(
"Wheel event delta " + QString::number( e->delta() ) );
1125 if ( QgsApplication::keyboardModifiers() )
1135 if ( e->delta() > 0 )
1153 QgsPoint newCenter( mousePos.x() + (( oldCenter.x() - mousePos.x() ) * scaleFactor ),
1154 mousePos.y() + (( oldCenter.y() - mousePos.y() ) * scaleFactor ) );
1158 extent.
scale( scaleFactor, &newCenter );
1203 r.
scale( scaleFactor, ¢er );
1229 QPoint xy = e->pos();
1278 setCursor( Qt::ArrowCursor );
1294 QBrush bgBrush( theColor );
1295 setBackgroundBrush( bgBrush );
1298 palette.setColor( backgroundRole(), theColor );
1299 setPalette( palette );
1303 mScene->setBackgroundBrush( bgBrush );
1308 return mScene->backgroundBrush().color();
1319 QList<QgsMapLayer*> lst;
1324 lst.append( layer );
1364 QgsDebugMsg(
"Setting map units to " + QString::number( static_cast<int>( u ) ) );
1396 QgsDebugMsg(
"QgsMapCanvas connected to " + QString( signal ) );
1420 double dx = qAbs( end.
x() - start.
x() );
1421 double dy = qAbs( end.
y() - start.
y() );
1426 if ( end.
x() < start.
x() )
1429 r.setXMaximum( r.xMaximum() + dx );
1433 r.setXMinimum( r.xMinimum() - dx );
1434 r.setXMaximum( r.xMaximum() - dx );
1437 if ( end.
y() < start.
y() )
1439 r.setYMaximum( r.yMaximum() + dy );
1440 r.setYMinimum( r.yMinimum() + dy );
1445 r.setYMaximum( r.yMaximum() - dy );
1446 r.setYMinimum( r.yMinimum() - dy );
1483 QList<QGraphicsItem*> list =
mScene->items();
1484 QList<QGraphicsItem*>::iterator it = list.begin();
1485 while ( it != list.end() )
1487 QGraphicsItem* item = *it;
1508 QMessageBox::warning(
1511 tr(
"Could not draw %1 because:\n%2",
"COMMENTED OUT" ).arg( mapLayer->
name() ).arg( mapLayer->
lastError() )
1532 QDomNodeList nodes = doc.elementsByTagName(
"mapcanvas" );
1533 if ( nodes.count() )
1535 QDomNode node = nodes.item( 0 );
1541 QgsDebugMsg(
"Couldn't read mapcanvas information from project" );
1549 QDomNodeList nl = doc.elementsByTagName(
"qgis" );
1552 QgsDebugMsg(
"Unable to find qgis element in project file" );
1555 QDomNode qgisNode = nl.item( 0 );
1557 QDomElement mapcanvasNode = doc.createElement(
"mapcanvas" );
1558 qgisNode.appendChild( mapcanvasNode );
1572 QString settingsString =
"/Projections/" + srcAuthId +
"//" + destAuthId;
1573 QVariant defaultSrcTransform = s.value( settingsString +
"_srcTransform" );
1574 QVariant defaultDestTransform = s.value( settingsString +
"_destTransform" );
1575 if ( defaultSrcTransform.isValid() && defaultDestTransform.isValid() )
1584 if ( !s.value(
"/Projections/showDatumTransformDialog",
false ).toBool() )
1593 if ( dt.size() < 2 )
1600 if (
mMapRenderer && ( d.exec() == QDialog::Accepted ) )
1602 int srcTransform = -1;
1603 int destTransform = -1;
1607 srcTransform = t.at( 0 );
1611 destTransform = t.at( 1 );
1614 if ( d.rememberSelection() )
1616 s.setValue( settingsString +
"_srcTransform", srcTransform );
1617 s.setValue( settingsString +
"_destTransform", destTransform );
1634 r.
scale( scaleFactor );
1659 QgsDebugMsg(
"refreshing after reprojection was enabled" );
1674 bool QgsMapCanvas::event( QEvent * e )
1681 if ( e->type() == QEvent::Gesture )
1686 done =
mMapTool->gestureEvent( static_cast<QGestureEvent*>( e ) );
1692 done = QGraphicsView::event( e );