Skip to content

Commit 70debe9

Browse files
ptitjanonyalldawson
authored andcommittedMar 23, 2023
qgs3dmapcanvaswidget: Allow to draw the extent on the 2D map canvas
This adds a QgsRubberband in the 2D map canvas similar to the one used to display the Frustum. The option can be enabled in the General section of the 3D configuration widget.
1 parent 06e575f commit 70debe9

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed
 

‎python/3d/auto_generated/qgs3dmapsettings.sip.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,22 @@ This parameter is transient. It is not saved in the project parameters.
671671
.. seealso:: :py:func:`isDebugOverlayEnabled`
672672

673673
.. versionadded:: 3.26
674+
%End
675+
676+
bool showExtentIn2DView() const;
677+
%Docstring
678+
Returns whether the extent is displayed on the main 2D map canvas
679+
680+
.. seealso:: :py:func:`setShowExtentIn2DView`
681+
682+
.. versionadded:: 3.32
683+
%End
684+
685+
void setShowExtentIn2DView( bool show );
686+
%Docstring
687+
Sets whether the extent is displayed on the main 2D map canvas
688+
689+
.. versionadded:: 3.32
674690
%End
675691

676692
signals:
@@ -925,6 +941,15 @@ Emitted when the 3d view's 2d extent has changed
925941
.. seealso:: :py:func:`setExtent`
926942

927943
.. versionadded:: 3.30
944+
%End
945+
946+
void showExtentIn2DViewChanged();
947+
%Docstring
948+
Emitted when the parameter to display 3d view's extent in the 2D canvas has changed
949+
950+
.. seealso:: :py:func:`setShowExtentIn2DView`
951+
952+
.. versionadded:: 3.32
928953
%End
929954

930955
private:

‎src/3d/qgs3dmapsettings.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Qgs3DMapSettings::Qgs3DMapSettings( const Qgs3DMapSettings &other )
9898
, m3dAxisSettings( other.m3dAxisSettings )
9999
, mIsDebugOverlayEnabled( other.mIsDebugOverlayEnabled )
100100
, mExtent( other.mExtent )
101+
, mShowExtentIn2DView( other.mShowExtentIn2DView )
101102
{
102103
for ( QgsLightSource *source : std::as_const( other.mLightSources ) )
103104
{
@@ -134,6 +135,8 @@ void Qgs3DMapSettings::readXml( const QDomElement &elem, const QgsReadWriteConte
134135
elemExtent.attribute( QStringLiteral( "yMin" ) ).toDouble(),
135136
elemExtent.attribute( QStringLiteral( "xMax" ) ).toDouble(),
136137
elemExtent.attribute( QStringLiteral( "yMax" ) ).toDouble() );
138+
139+
mShowExtentIn2DView = elemExtent.attribute( QStringLiteral( "showIn2dView" ), QStringLiteral( "0" ) ).toInt();
137140
}
138141
else
139142
{
@@ -327,6 +330,7 @@ QDomElement Qgs3DMapSettings::writeXml( QDomDocument &doc, const QgsReadWriteCon
327330
elemExtent.setAttribute( QStringLiteral( "yMin" ), mExtent.yMinimum() );
328331
elemExtent.setAttribute( QStringLiteral( "xMax" ), mExtent.xMaximum() );
329332
elemExtent.setAttribute( QStringLiteral( "yMax" ), mExtent.yMaximum() );
333+
elemExtent.setAttribute( QStringLiteral( "showIn2dView" ), mShowExtentIn2DView );
330334
elem.appendChild( elemExtent );
331335

332336
QDomElement elemCamera = doc.createElement( QStringLiteral( "camera" ) );
@@ -954,6 +958,7 @@ void Qgs3DMapSettings::connectChangedSignalsToSettingsChanged()
954958
connect( this, &Qgs3DMapSettings::axisSettingsChanged, this, &Qgs3DMapSettings::settingsChanged );
955959
connect( this, &Qgs3DMapSettings::ambientOcclusionSettingsChanged, this, &Qgs3DMapSettings::settingsChanged );
956960
connect( this, &Qgs3DMapSettings::extentChanged, this, &Qgs3DMapSettings::settingsChanged );
961+
connect( this, &Qgs3DMapSettings::showExtentIn2DViewChanged, this, &Qgs3DMapSettings::settingsChanged );
957962
}
958963

959964

@@ -975,3 +980,12 @@ void Qgs3DMapSettings::set3DAxisSettings( const Qgs3DAxisSettings &axisSettings,
975980
emit axisSettingsChanged();
976981
}
977982
}
983+
984+
void Qgs3DMapSettings::setShowExtentIn2DView( bool show )
985+
{
986+
if ( show == mShowExtentIn2DView )
987+
return;
988+
989+
mShowExtentIn2DView = show;
990+
emit showExtentIn2DViewChanged();
991+
}

‎src/3d/qgs3dmapsettings.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,19 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
676676
*/
677677
void setIsDebugOverlayEnabled( bool debugOverlayEnabled );
678678

679+
/**
680+
* Returns whether the extent is displayed on the main 2D map canvas
681+
* \see setShowExtentIn2DView()
682+
* \since QGIS 3.32
683+
*/
684+
bool showExtentIn2DView() const { return mShowExtentIn2DView; }
685+
686+
/**
687+
* Sets whether the extent is displayed on the main 2D map canvas
688+
* \since QGIS 3.32
689+
*/
690+
void setShowExtentIn2DView( bool show );
691+
679692
signals:
680693

681694
/**
@@ -882,6 +895,13 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
882895
*/
883896
void extentChanged();
884897

898+
/**
899+
* Emitted when the parameter to display 3d view's extent in the 2D canvas has changed
900+
* \see setShowExtentIn2DView()
901+
* \since QGIS 3.32
902+
*/
903+
void showExtentIn2DViewChanged();
904+
885905
private:
886906
#ifdef SIP_RUN
887907
Qgs3DMapSettings &operator=( const Qgs3DMapSettings & );
@@ -955,6 +975,8 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
955975

956976
QgsRectangle mExtent; //!< 2d extent used to limit the 3d view
957977

978+
bool mShowExtentIn2DView = false;
979+
958980
};
959981

960982

‎src/app/3d/qgs3dmapcanvaswidget.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ void Qgs3DMapCanvasWidget::setMapSettings( Qgs3DMapSettings *map )
371371
mLabelFpsCounter->setVisible( map->isFpsCounterEnabled() );
372372

373373
connect( map, &Qgs3DMapSettings::viewFrustumVisualizationEnabledChanged, this, &Qgs3DMapCanvasWidget::onViewFrustumVisualizationEnabledChanged );
374+
connect( map, &Qgs3DMapSettings::extentChanged, this, &Qgs3DMapCanvasWidget::onExtentChanged );
375+
connect( map, &Qgs3DMapSettings::showExtentIn2DViewChanged, this, &Qgs3DMapCanvasWidget::onExtentChanged );
376+
onExtentChanged();
374377
}
375378

376379
void Qgs3DMapCanvasWidget::setMainCanvas( QgsMapCanvas *canvas )
@@ -386,6 +389,12 @@ void Qgs3DMapCanvasWidget::setMainCanvas( QgsMapCanvas *canvas )
386389
mViewFrustumHighlight.reset( new QgsRubberBand( canvas, Qgis::GeometryType::Polygon ) );
387390
mViewFrustumHighlight->setColor( QColor::fromRgba( qRgba( 0, 0, 255, 50 ) ) );
388391
}
392+
393+
if ( !mViewExtentHighlight )
394+
{
395+
mViewExtentHighlight.reset( new QgsRubberBand( canvas, Qgis::GeometryType::Polygon ) );
396+
mViewExtentHighlight->setColor( QColor::fromRgba( qRgba( 255, 0, 0, 50 ) ) );
397+
}
389398
}
390399

391400
void Qgs3DMapCanvasWidget::resetView()
@@ -618,3 +627,18 @@ void Qgs3DMapCanvasWidget::onViewFrustumVisualizationEnabledChanged()
618627
mViewFrustumHighlight->closePoints();
619628
}
620629
}
630+
631+
void Qgs3DMapCanvasWidget::onExtentChanged()
632+
{
633+
Qgs3DMapSettings *mapSettings = mCanvas->map();
634+
mViewExtentHighlight->reset( Qgis::GeometryType::Polygon );
635+
if ( mapSettings->showExtentIn2DView() )
636+
{
637+
QgsRectangle extent = mapSettings->extent();
638+
mViewExtentHighlight->addPoint( QgsPointXY( extent.xMinimum(), extent.yMinimum() ), false );
639+
mViewExtentHighlight->addPoint( QgsPointXY( extent.xMinimum(), extent.yMaximum() ), false );
640+
mViewExtentHighlight->addPoint( QgsPointXY( extent.xMaximum(), extent.yMaximum() ), false );
641+
mViewExtentHighlight->addPoint( QgsPointXY( extent.xMaximum(), extent.yMinimum() ), false );
642+
mViewExtentHighlight->closePoints();
643+
}
644+
}

‎src/app/3d/qgs3dmapcanvaswidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class APP_EXPORT Qgs3DMapCanvasWidget : public QWidget
9292
void onMainMapCanvasExtentChanged();
9393
void onViewed2DExtentFrom3DChanged( QVector<QgsPointXY> extent );
9494
void onViewFrustumVisualizationEnabledChanged();
95+
void onExtentChanged();
9596

9697
private:
9798
QString mCanvasName;
@@ -118,6 +119,7 @@ class APP_EXPORT Qgs3DMapCanvasWidget : public QWidget
118119
QToolButton *mBtnOptions = nullptr;
119120
QgsDockableWidgetHelper *mDockableWidgetHelper = nullptr;
120121
QObjectUniquePtr< QgsRubberBand > mViewFrustumHighlight;
122+
QObjectUniquePtr< QgsRubberBand > mViewExtentHighlight;
121123
QPointer<QDialog> mConfigureDialog;
122124
};
123125

‎src/app/3d/qgs3dmapconfigwidget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ Qgs3DMapConfigWidget::Qgs3DMapConfigWidget( Qgs3DMapSettings *map, QgsMapCanvas
252252
groupExtent->setCurrentExtent( mMap->extent(), mMap->crs() );
253253
groupExtent->setOutputExtentFromCurrent();
254254
groupExtent->setMapCanvas( mMainCanvas );
255+
256+
// checkbox to display the extent in the 2D Map View
257+
mShowExtentIn2DViewCheckbox = new QCheckBox( tr( "Show in 2D map view" ) );
258+
mShowExtentIn2DViewCheckbox->setChecked( map->showExtentIn2DView() );
259+
groupExtent->layout()->addWidget( mShowExtentIn2DViewCheckbox );
255260
}
256261

257262
Qgs3DMapConfigWidget::~Qgs3DMapConfigWidget()
@@ -264,6 +269,7 @@ Qgs3DMapConfigWidget::~Qgs3DMapConfigWidget()
264269
void Qgs3DMapConfigWidget::apply()
265270
{
266271
mMap->setExtent( groupExtent->outputExtent() );
272+
mMap->setShowExtentIn2DView( mShowExtentIn2DViewCheckbox->isChecked() );
267273

268274
const QgsTerrainGenerator::Type terrainType = static_cast<QgsTerrainGenerator::Type>( cboTerrainType->currentData().toInt() );
269275

‎src/app/3d/qgs3dmapconfigwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <ui_map3dconfigwidget.h>
2222

23+
class QCheckBox;
2324
class Qgs3DMapSettings;
2425
class QgsMapCanvas;
2526
class QgsMesh3dSymbolWidget;
@@ -57,6 +58,7 @@ class Qgs3DMapConfigWidget : public QWidget, private Ui::Map3DConfigWidget
5758
QgsMesh3dSymbolWidget *mMeshSymbolWidget = nullptr;
5859
QgsSkyboxRenderingSettingsWidget *mSkyboxSettingsWidget = nullptr;
5960
QgsShadowRenderingSettingsWidget *mShadowSettingsWidget = nullptr;
61+
QCheckBox *mShowExtentIn2DViewCheckbox = nullptr;
6062

6163
void init3DAxisPage();
6264
};

0 commit comments

Comments
 (0)
Please sign in to comment.