Allow labels to be toggled in map views

Another step toward feature parity with overview panel
This commit is contained in:
Nyall Dawson 2017-03-28 13:18:24 +10:00
parent 456fdc48a7
commit 5b9dc9dca5
7 changed files with 62 additions and 0 deletions

View File

@ -25,6 +25,7 @@ class QgsMapCanvas : QGraphicsView
void setCurrentLayer( QgsMapLayer *layer );
const QgsMapSettings &mapSettings() const /KeepReference/;
void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
void setMapSettingsFlags( QgsMapSettings::Flags flags );
const QgsLabelingResults *labelingResults() const;
void setCachingEnabled( bool enabled );
bool isCachingEnabled() const;

View File

@ -11851,6 +11851,7 @@ void QgisApp::writeProject( QDomDocument &doc )
node.setAttribute( QStringLiteral( "showExtent" ), w->isMainCanvasExtentVisible() );
node.setAttribute( QStringLiteral( "scaleSynced" ), w->isViewScaleSynchronized() );
node.setAttribute( QStringLiteral( "scaleFactor" ), w->scaleFactor() );
node.setAttribute( QStringLiteral( "showLabels" ), w->labelsVisible() );
mapViewNode.appendChild( node );
}
qgisNode.appendChild( mapViewNode );
@ -11890,6 +11891,7 @@ void QgisApp::readProject( const QDomDocument &doc )
bool showExtent = elementNode.attribute( QStringLiteral( "showExtent" ), QStringLiteral( "0" ) ).toInt();
bool scaleSynced = elementNode.attribute( QStringLiteral( "scaleSynced" ), QStringLiteral( "0" ) ).toInt();
double scaleFactor = elementNode.attribute( QStringLiteral( "scaleFactor" ), QStringLiteral( "1" ) ).toDouble();
bool showLabels = elementNode.attribute( QStringLiteral( "showLabels" ), QStringLiteral( "1" ) ).toInt();
Qt::DockWidgetArea area = static_cast< Qt::DockWidgetArea >( elementNode.attribute( QStringLiteral( "area" ), QString::number( Qt::RightDockWidgetArea ) ).toInt() );
QgsMapCanvasDockWidget *mapCanvasDock = createNewMapCanvasDock( mapName, floating, QRect( x, y, w, h ), area );
@ -11901,6 +11903,7 @@ void QgisApp::readProject( const QDomDocument &doc )
mapCanvasDock->setScaleFactor( scaleFactor );
mapCanvasDock->setViewScaleSynchronized( scaleSynced );
mapCanvasDock->setMainCanvasExtentVisible( showExtent );
mapCanvasDock->setLabelsVisible( showLabels );
}
}
}

View File

@ -105,6 +105,7 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
settingsMenu->addAction( mActionShowAnnotations );
settingsMenu->addAction( mActionShowCursor );
settingsMenu->addAction( mActionShowExtent );
settingsMenu->addAction( mActionShowLabels );
settingsMenu->addSeparator();
settingsMenu->addAction( mActionSetCrs );
settingsMenu->addAction( mActionRename );
@ -118,6 +119,8 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
connect( mActionShowCursor, &QAction::toggled, this, [ = ]( bool checked ) { mXyMarker->setVisible( checked ); } );
mActionShowExtent->setChecked( false );
connect( mActionShowExtent, &QAction::toggled, this, [ = ]( bool checked ) { mExtentRubberBand->setVisible( checked ); updateExtentRect(); } );
mActionShowLabels->setChecked( true );
connect( mActionShowLabels, &QAction::toggled, this, &QgsMapCanvasDockWidget::showLabels );
mScaleCombo = settingsAction->scaleCombo();
mRotationEdit = settingsAction->rotationSpinBox();
@ -273,6 +276,16 @@ bool QgsMapCanvasDockWidget::isViewScaleSynchronized() const
return mSyncScaleCheckBox->isChecked();
}
void QgsMapCanvasDockWidget::setLabelsVisible( bool enabled )
{
mActionShowLabels->setChecked( enabled );
}
bool QgsMapCanvasDockWidget::labelsVisible() const
{
return mActionShowLabels->isChecked();
}
double QgsMapCanvasDockWidget::scaleFactor() const
{
return mScaleFactorWidget->value();
@ -446,6 +459,16 @@ void QgsMapCanvasDockWidget::updateExtentRect()
mExtentRubberBand->setToGeometry( g, nullptr );
}
void QgsMapCanvasDockWidget::showLabels( bool show )
{
QgsMapSettings::Flags flags = mMapCanvas->mapSettings().flags();
if ( show )
flags = flags | QgsMapSettings::DrawLabeling;
else
flags = flags & ~QgsMapSettings::DrawLabeling;
mMapCanvas->setMapSettingsFlags( flags );
}
QgsMapSettingsAction::QgsMapSettingsAction( QWidget *parent )
: QWidgetAction( parent )
{

View File

@ -118,6 +118,18 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
*/
bool isViewScaleSynchronized() const;
/**
* Sets whether labels should be rendered in the view.
* @see labelsVisible()
*/
void setLabelsVisible( bool enabled );
/**
* Returns whether labels are rendered in the view.
* @see setLabelsVisible()
*/
bool labelsVisible() const;
signals:
void renameTriggered();
@ -136,6 +148,8 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
void syncMarker( const QgsPoint &p );
void mapScaleChanged();
void updateExtentRect();
void showLabels( bool show );
private:

View File

@ -385,6 +385,13 @@ void QgsMapCanvas::setDestinationCrs( const QgsCoordinateReferenceSystem &crs )
emit destinationCrsChanged();
}
void QgsMapCanvas::setMapSettingsFlags( QgsMapSettings::Flags flags )
{
mSettings.setFlags( flags );
clearCache();
refresh();
}
const QgsLabelingResults *QgsMapCanvas::labelingResults() const
{
return mLabelingResults;

View File

@ -108,6 +108,12 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! @note added in 2.4
void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
/**
* Resets the \a flags for the canvas' map settings.
* @note added in QGIS 3.0
*/
void setMapSettingsFlags( QgsMapSettings::Flags flags );
//! Get access to the labeling results (may be null)
//! @note added in 2.4
const QgsLabelingResults *labelingResults() const;

View File

@ -159,6 +159,14 @@
<string>Show Main Canvas Extent</string>
</property>
</action>
<action name="mActionShowLabels">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show Labels</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>