Skip to content
Permalink
Browse files

Allow annotations to be toggled in additional map views

  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent 76b9d94 commit ea10cc68f00bfcd10f0a3b182a9cb300c57debbf
@@ -97,6 +97,10 @@ class QgsMapCanvas : QGraphicsView
void setSegmentationTolerance( double tolerance );
void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );

QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
bool annotationsVisible() const;
void setAnnotationsVisible( bool visible );

public slots:

//! Repaints the canvas map
@@ -87,9 +87,14 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa

settingsMenu->addSeparator();
settingsMenu->addAction( mActionSetCrs );
settingsMenu->addAction( mActionShowAnnotations );
settingsMenu->addAction( mActionRename );
settingsMenu->addSeparator();

connect( settingsMenu, &QMenu::aboutToShow, this, &QgsMapCanvasDockWidget::settingsMenuAboutToShow );

connect( mActionRename, &QAction::triggered, this, &QgsMapCanvasDockWidget::renameTriggered );
mActionShowAnnotations->setChecked( mMapCanvas->annotationsVisible() );
connect( mActionShowAnnotations, &QAction::toggled, this, [ = ]( bool checked ) { mMapCanvas->setAnnotationsVisible( checked ); } );

mScaleCombo = settingsAction->scaleCombo();
mRotationEdit = settingsAction->rotationSpinBox();
@@ -279,6 +284,11 @@ void QgsMapCanvasDockWidget::menuAboutToShow()
mMenu->addActions( mMenuPresetActions );
}

void QgsMapCanvasDockWidget::settingsMenuAboutToShow()
{
whileBlocking( mActionShowAnnotations )->setChecked( mMapCanvas->annotationsVisible() );
}


QgsMapSettingsAction::QgsMapSettingsAction( QWidget *parent )
: QWidgetAction( parent )
@@ -69,6 +69,7 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
void mapExtentChanged();
void mapCrsChanged();
void menuAboutToShow();
void settingsMenuAboutToShow();

private:

@@ -336,24 +336,14 @@ QgsMapCanvasAnnotationItem *QgsMapToolAnnotation::selectedItem() const

QList<QgsMapCanvasAnnotationItem *> QgsMapToolAnnotation::annotationItems() const
{
QList<QgsMapCanvasAnnotationItem *> annotationItemList;
if ( !mCanvas || !mCanvas->scene() )
if ( !mCanvas )
{
return annotationItemList;
return QList<QgsMapCanvasAnnotationItem *>();
}

QList<QGraphicsItem *> itemList = mCanvas->scene()->items();
QList<QGraphicsItem *>::iterator it = itemList.begin();
for ( ; it != itemList.end(); ++it )
else
{
QgsMapCanvasAnnotationItem *aItem = dynamic_cast<QgsMapCanvasAnnotationItem *>( *it );
if ( aItem )
{
annotationItemList.push_back( aItem );
}
return mCanvas->annotationItems();
}

return annotationItemList;
}

void QgsMapToolAnnotation::toggleTextItemVisibilities()
@@ -1890,6 +1890,7 @@ void QgsMapCanvas::readProject( const QDomDocument &doc )
setTheme( elem.attribute( QStringLiteral( "theme" ) ) );
}
}
setAnnotationsVisible( elem.attribute( QStringLiteral( "annotationsVisible" ), QStringLiteral( "1" ) ).toInt() );
}
else
{
@@ -1910,9 +1911,10 @@ void QgsMapCanvas::writeProject( QDomDocument &doc )
QDomNode qgisNode = nl.item( 0 ); // there should only be one, so zeroth element ok

QDomElement mapcanvasNode = doc.createElement( QStringLiteral( "mapcanvas" ) );
mapcanvasNode.setAttribute( "name", objectName() );
mapcanvasNode.setAttribute( QStringLiteral( "name" ), objectName() );
if ( !mTheme.isEmpty() )
mapcanvasNode.setAttribute( "theme", mTheme );
mapcanvasNode.setAttribute( QStringLiteral( "theme" ), mTheme );
mapcanvasNode.setAttribute( QStringLiteral( "annotationsVisible" ), mAnnotationsVisible );
qgisNode.appendChild( mapcanvasNode );

mSettings.writeXml( mapcanvasNode, doc );
@@ -2074,3 +2076,29 @@ void QgsMapCanvas::setSegmentationToleranceType( QgsAbstractGeometry::Segmentati
{
mSettings.setSegmentationToleranceType( type );
}

QList<QgsMapCanvasAnnotationItem *> QgsMapCanvas::annotationItems() const
{
QList<QgsMapCanvasAnnotationItem *> annotationItemList;
QList<QGraphicsItem *> itemList = mScene->items();
QList<QGraphicsItem *>::iterator it = itemList.begin();
for ( ; it != itemList.end(); ++it )
{
QgsMapCanvasAnnotationItem *aItem = dynamic_cast< QgsMapCanvasAnnotationItem *>( *it );
if ( aItem )
{
annotationItemList.push_back( aItem );
}
}

return annotationItemList;
}

void QgsMapCanvas::setAnnotationsVisible( bool show )
{
mAnnotationsVisible = show;
Q_FOREACH ( QgsMapCanvasAnnotationItem *item, annotationItems() )
{
item->setVisible( show );
}
}
@@ -63,7 +63,7 @@ class QgsMapOverviewCanvas;
class QgsMapTool;
class QgsSnappingUtils;
class QgsRubberBand;

class QgsMapCanvasAnnotationItem;

/** \ingroup gui
* Map canvas is a class for displaying all GIS data types on a canvas.
@@ -468,6 +468,26 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
@param type the segmentation tolerance typename*/
void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );

/**
* Returns a list of all annotation items in the canvas.
* @note added in QGIS 3.0
*/
QList< QgsMapCanvasAnnotationItem *> annotationItems() const;

/**
* Returns true if annotations are visible within the map canvas.
* @note added in QGIS 3.0
* @see setAnnotationsVisible()
*/
bool annotationsVisible() const { return mAnnotationsVisible; }

/**
* Sets whether annotations are \a visible in the canvas.
* @note added in QGIS 3.0
* @see annotationsVisible()
*/
void setAnnotationsVisible( bool visible );

public slots:

//! Repaints the canvas map
@@ -784,6 +804,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

QString mTheme;

bool mAnnotationsVisible = true;

//! Force a resize of the map canvas item
//! @note added in 2.16
void updateMapSize();
@@ -33,6 +33,9 @@ QgsMapCanvasAnnotationItem::QgsMapCanvasAnnotationItem( QgsAnnotation *annotatio
, mAnnotation( annotation )
{
setFlag( QGraphicsItem::ItemIsSelectable, true );
if ( mapCanvas && !mapCanvas->annotationsVisible() )
setVisible( false );

connect( mAnnotation, &QgsAnnotation::appearanceChanged, this, [this] { update(); } );
connect( mAnnotation, &QgsAnnotation::moved, this, [this] { updatePosition(); } );
connect( mAnnotation, &QgsAnnotation::moved, this, &QgsMapCanvasAnnotationItem::setFeatureForMapPosition );
@@ -132,6 +132,17 @@
<string>Zoom &amp;Full</string>
</property>
</action>
<action name="mActionShowAnnotations">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show Annotations</string>
</property>
<property name="toolTip">
<string>Show Annotations</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

0 comments on commit ea10cc6

Please sign in to comment.
You can’t perform that action at this time.