Code modenization

This commit is contained in:
Matthias Kuhn 2019-04-05 11:22:56 +02:00
parent 870337871a
commit 2bb049ecd1
No known key found for this signature in database
GPG Key ID: 7A7F1A1C90C3E6A7
3 changed files with 24 additions and 34 deletions

View File

@ -28,10 +28,10 @@ Map canvas is a class for displaying all GIS data types on a canvas.
#include "qgsmapcanvas.h" #include "qgsmapcanvas.h"
%End %End
%ConvertToSubClassCode %ConvertToSubClassCode
if ( dynamic_cast<QgsMapCanvas *>( sipCpp ) != NULL ) if ( qobject_cast<QgsMapCanvas *>( sipCpp ) != nullptr )
sipType = sipType_QgsMapCanvas; sipType = sipType_QgsMapCanvas;
else else
sipType = NULL; sipType = nullptr;
%End %End
public: public:

View File

@ -243,14 +243,7 @@ QgsMapCanvas::~QgsMapCanvas()
// delete canvas items prior to deleting the canvas // delete canvas items prior to deleting the canvas
// because they might try to update canvas when it's // because they might try to update canvas when it's
// already being destructed, ends with segfault // already being destructed, ends with segfault
QList<QGraphicsItem *> list = mScene->items(); qDeleteAll( mScene->items() );
QList<QGraphicsItem *>::iterator it = list.begin();
while ( it != list.end() )
{
QGraphicsItem *item = *it;
delete item;
++it;
}
mScene->deleteLater(); // crashes in python tests on windows mScene->deleteLater(); // crashes in python tests on windows
@ -1567,18 +1560,15 @@ void QgsMapCanvas::paintEvent( QPaintEvent *e )
void QgsMapCanvas::updateCanvasItemPositions() void QgsMapCanvas::updateCanvasItemPositions()
{ {
QList<QGraphicsItem *> list = mScene->items(); const QList<QGraphicsItem *> items = mScene->items();
QList<QGraphicsItem *>::iterator it = list.begin(); for ( QGraphicsItem *gi : items )
while ( it != list.end() )
{ {
QgsMapCanvasItem *item = dynamic_cast<QgsMapCanvasItem *>( *it ); QgsMapCanvasItem *item = dynamic_cast<QgsMapCanvasItem *>( gi );
if ( item ) if ( item )
{ {
item->updatePosition(); item->updatePosition();
} }
++it;
} }
} }
@ -1912,7 +1902,8 @@ void QgsMapCanvas::updateAutoRefreshTimer()
// min auto refresh interval stores the smallest interval between layer auto refreshes. We automatically // min auto refresh interval stores the smallest interval between layer auto refreshes. We automatically
// trigger a map refresh on this minimum interval // trigger a map refresh on this minimum interval
int minAutoRefreshInterval = -1; int minAutoRefreshInterval = -1;
Q_FOREACH ( QgsMapLayer *layer, mSettings.layers() ) const auto layers = mSettings.layers();
for ( QgsMapLayer *layer : layers )
{ {
if ( layer->hasAutoRefreshEnabled() && layer->autoRefreshInterval() > 0 ) if ( layer->hasAutoRefreshEnabled() && layer->autoRefreshInterval() > 0 )
minAutoRefreshInterval = minAutoRefreshInterval > 0 ? std::min( layer->autoRefreshInterval(), minAutoRefreshInterval ) : layer->autoRefreshInterval(); minAutoRefreshInterval = minAutoRefreshInterval > 0 ? std::min( layer->autoRefreshInterval(), minAutoRefreshInterval ) : layer->autoRefreshInterval();
@ -2194,11 +2185,10 @@ bool QgsMapCanvas::event( QEvent *e )
void QgsMapCanvas::refreshAllLayers() void QgsMapCanvas::refreshAllLayers()
{ {
// reload all layers in canvas // reload all layers in canvas
for ( int i = 0; i < layerCount(); i++ ) const QList<QgsMapLayer *> layers = mapSettings().layers();
for ( QgsMapLayer *layer : layers )
{ {
QgsMapLayer *l = layer( i ); layer->reload();
if ( l )
l->reload();
} }
// clear the cache // clear the cache
@ -2229,11 +2219,10 @@ void QgsMapCanvas::setSegmentationToleranceType( QgsAbstractGeometry::Segmentati
QList<QgsMapCanvasAnnotationItem *> QgsMapCanvas::annotationItems() const QList<QgsMapCanvasAnnotationItem *> QgsMapCanvas::annotationItems() const
{ {
QList<QgsMapCanvasAnnotationItem *> annotationItemList; QList<QgsMapCanvasAnnotationItem *> annotationItemList;
QList<QGraphicsItem *> itemList = mScene->items(); const QList<QGraphicsItem *> items = mScene->items();
QList<QGraphicsItem *>::iterator it = itemList.begin(); for ( QGraphicsItem *gi : items )
for ( ; it != itemList.end(); ++it )
{ {
QgsMapCanvasAnnotationItem *aItem = dynamic_cast< QgsMapCanvasAnnotationItem *>( *it ); QgsMapCanvasAnnotationItem *aItem = dynamic_cast< QgsMapCanvasAnnotationItem *>( gi );
if ( aItem ) if ( aItem )
{ {
annotationItemList.push_back( aItem ); annotationItemList.push_back( aItem );
@ -2246,7 +2235,8 @@ QList<QgsMapCanvasAnnotationItem *> QgsMapCanvas::annotationItems() const
void QgsMapCanvas::setAnnotationsVisible( bool show ) void QgsMapCanvas::setAnnotationsVisible( bool show )
{ {
mAnnotationsVisible = show; mAnnotationsVisible = show;
Q_FOREACH ( QgsMapCanvasAnnotationItem *item, annotationItems() ) const QList<QgsMapCanvasAnnotationItem *> items = annotationItems();
for ( QgsMapCanvasAnnotationItem *item : items )
{ {
item->setVisible( show ); item->setVisible( show );
} }
@ -2322,14 +2312,14 @@ void QgsMapCanvas::startPreviewJob( int number )
void QgsMapCanvas::stopPreviewJobs() void QgsMapCanvas::stopPreviewJobs()
{ {
mPreviewTimer.stop(); mPreviewTimer.stop();
QList< QgsMapRendererQImageJob * >::const_iterator it = mPreviewJobs.constBegin(); const auto previewJobs = mPreviewJobs;
for ( ; it != mPreviewJobs.constEnd(); ++it ) for ( auto previewJob : previewJobs )
{ {
if ( *it ) if ( previewJob )
{ {
disconnect( *it, &QgsMapRendererJob::finished, this, &QgsMapCanvas::previewJobFinished ); disconnect( previewJob, &QgsMapRendererJob::finished, this, &QgsMapCanvas::previewJobFinished );
connect( *it, &QgsMapRendererQImageJob::finished, *it, &QgsMapRendererQImageJob::deleteLater ); connect( previewJob, &QgsMapRendererQImageJob::finished, previewJob, &QgsMapRendererQImageJob::deleteLater );
( *it )->cancelWithoutBlocking(); previewJob->cancelWithoutBlocking();
} }
} }
mPreviewJobs.clear(); mPreviewJobs.clear();

View File

@ -75,10 +75,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
#ifdef SIP_RUN #ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE SIP_CONVERT_TO_SUBCLASS_CODE
if ( dynamic_cast<QgsMapCanvas *>( sipCpp ) != NULL ) if ( qobject_cast<QgsMapCanvas *>( sipCpp ) != nullptr )
sipType = sipType_QgsMapCanvas; sipType = sipType_QgsMapCanvas;
else else
sipType = NULL; sipType = nullptr;
SIP_END SIP_END
#endif #endif