19
19
#include " qgs3dmapcanvas.h"
20
20
#include " qgs3dmapcanvasdockwidget.h"
21
21
#include " qgs3dmapsettings.h"
22
+ #include " qgscameracontroller.h"
22
23
#include < QMenu>
23
24
25
+
26
+ float _normalizedAngle ( float x )
27
+ {
28
+ x = std::fmod ( x, 360 );
29
+ if ( x < 0 ) x += 360 ;
30
+ return x;
31
+ }
32
+
33
+ template <typename Func1>
34
+ void _prepare3DViewsMenu ( QMenu *menu, QgsLayout3DMapWidget *w, Func1 slot )
35
+ {
36
+ QObject::connect ( menu, &QMenu::aboutToShow, w, [menu, w, slot]
37
+ {
38
+ const QList<Qgs3DMapCanvasDockWidget *> lst = QgisApp::instance ()->findChildren <Qgs3DMapCanvasDockWidget *>();
39
+ menu->clear ();
40
+ for ( auto dock : lst )
41
+ {
42
+ QAction *a = menu->addAction ( dock->mapCanvas3D ()->objectName (), w, slot );
43
+ // need to use a custom property for identification because Qt likes to add "&" to the action text
44
+ a->setProperty ( " name" , dock->mapCanvas3D ()->objectName () );
45
+ }
46
+ if ( lst.isEmpty () )
47
+ {
48
+ menu->addAction ( QObject::tr ( " No 3D maps defined" ) )->setEnabled ( false );
49
+ }
50
+ } );
51
+ }
52
+
53
+ Qgs3DMapCanvasDockWidget *_dock3DViewFromSender ( QObject *sender )
54
+ {
55
+ QAction *action = qobject_cast<QAction *>( sender );
56
+ if ( !action )
57
+ return nullptr ;
58
+
59
+ QString actionText = action->property ( " name" ).toString ();
60
+ const QList<Qgs3DMapCanvasDockWidget *> lst = QgisApp::instance ()->findChildren <Qgs3DMapCanvasDockWidget *>();
61
+ for ( auto dock : lst )
62
+ {
63
+ QString objName = dock->mapCanvas3D ()->objectName ();
64
+ if ( objName == actionText )
65
+ {
66
+ return dock;
67
+ }
68
+ }
69
+ return nullptr ;
70
+ }
71
+
72
+
24
73
QgsLayout3DMapWidget::QgsLayout3DMapWidget ( QgsLayoutItem3DMap *map3D )
25
74
: QgsLayoutItemBaseWidget( nullptr , map3D )
26
75
, mMap3D( map3D )
@@ -38,52 +87,45 @@ QgsLayout3DMapWidget::QgsLayout3DMapWidget( QgsLayoutItem3DMap *map3D )
38
87
39
88
mMenu3DCanvases = new QMenu ( this );
40
89
mCopySettingsButton ->setMenu ( mMenu3DCanvases );
41
- connect ( mMenu3DCanvases , &QMenu::aboutToShow, this , [ = ]
42
- {
43
- const QList<Qgs3DMapCanvasDockWidget *> lst = QgisApp::instance ()->findChildren <Qgs3DMapCanvasDockWidget *>();
44
- mMenu3DCanvases ->clear ();
45
- for ( auto dock : lst )
46
- {
47
- QAction *a = mMenu3DCanvases ->addAction ( dock->mapCanvas3D ()->objectName (), this , &QgsLayout3DMapWidget::copy3DMapSettings );
48
- // need to use a custom property for identification because Qt likes to add "&" to the action text
49
- a->setProperty ( " name" , dock->mapCanvas3D ()->objectName () );
50
- }
51
- if ( lst.isEmpty () )
52
- {
53
- mMenu3DCanvases ->addAction ( tr ( " No 3D maps defined" ) )->setEnabled ( false );
54
- }
55
- } );
90
+ _prepare3DViewsMenu ( mMenu3DCanvases , this , &QgsLayout3DMapWidget::copy3DMapSettings );
56
91
57
- QgsCameraPose pose = mMap3D ->cameraPose ();
58
- mCenterXSpinBox ->setValue ( pose.centerPoint ().x () );
59
- mCenterYSpinBox ->setValue ( pose.centerPoint ().y () );
60
- mCenterZSpinBox ->setValue ( pose.centerPoint ().z () );
61
- mDistanceToCenterSpinBox ->setValue ( pose.distanceFromCenterPoint () );
62
- mPitchAngleSpinBox ->setValue ( pose.pitchAngle () );
63
- mHeadingAngleSpinBox ->setValue ( pose.headingAngle () );
92
+ mMenu3DCanvasesPose = new QMenu ( this );
93
+ mPoseFromViewButton ->setMenu ( mMenu3DCanvasesPose );
94
+ _prepare3DViewsMenu ( mMenu3DCanvasesPose , this , &QgsLayout3DMapWidget::copeCameraPose );
64
95
65
96
QList<QgsDoubleSpinBox *> lst;
66
97
lst << mCenterXSpinBox << mCenterYSpinBox << mCenterZSpinBox << mDistanceToCenterSpinBox << mPitchAngleSpinBox << mHeadingAngleSpinBox ;
67
98
for ( QgsDoubleSpinBox *spinBox : lst )
68
99
connect ( spinBox, qgis::overload<double >::of ( &QgsDoubleSpinBox::valueChanged ), this , &QgsLayout3DMapWidget::updateCameraPose );
100
+
101
+ updateCameraPoseWidgetsFromItem ();
102
+ }
103
+
104
+ void QgsLayout3DMapWidget::updateCameraPoseWidgetsFromItem ()
105
+ {
106
+ QgsCameraPose pose = mMap3D ->cameraPose ();
107
+ whileBlocking ( mCenterXSpinBox )->setValue ( pose.centerPoint ().x () );
108
+ whileBlocking ( mCenterYSpinBox )->setValue ( pose.centerPoint ().y () );
109
+ whileBlocking ( mCenterZSpinBox )->setValue ( pose.centerPoint ().z () );
110
+ whileBlocking ( mDistanceToCenterSpinBox )->setValue ( pose.distanceFromCenterPoint () );
111
+ whileBlocking ( mPitchAngleSpinBox )->setValue ( _normalizedAngle ( pose.pitchAngle () ) );
112
+ whileBlocking ( mHeadingAngleSpinBox )->setValue ( _normalizedAngle ( pose.headingAngle () ) );
69
113
}
70
114
71
115
void QgsLayout3DMapWidget::copy3DMapSettings ()
72
116
{
73
- QAction *action = qobject_cast<QAction *>( sender () );
74
- if ( !action )
75
- return ;
117
+ Qgs3DMapCanvasDockWidget *dock = _dock3DViewFromSender ( sender () );
118
+ if ( dock )
119
+ mMap3D ->setMapSettings ( new Qgs3DMapSettings ( *dock->mapCanvas3D ()->map () ) );
120
+ }
76
121
77
- QString actionText = action->property ( " name" ).toString ();
78
- const QList<Qgs3DMapCanvasDockWidget *> lst = QgisApp::instance ()->findChildren <Qgs3DMapCanvasDockWidget *>();
79
- for ( auto dock : lst )
122
+ void QgsLayout3DMapWidget::copeCameraPose ()
123
+ {
124
+ Qgs3DMapCanvasDockWidget *dock = _dock3DViewFromSender ( sender () );
125
+ if ( dock )
80
126
{
81
- QString objName = dock->mapCanvas3D ()->objectName ();
82
- if ( objName == actionText )
83
- {
84
- mMap3D ->setMapSettings ( new Qgs3DMapSettings ( *dock->mapCanvas3D ()->map () ) );
85
- break ;
86
- }
127
+ mMap3D ->setCameraPose ( dock->mapCanvas3D ()->cameraController ()->cameraPose () );
128
+ updateCameraPoseWidgetsFromItem ();
87
129
}
88
130
}
89
131
0 commit comments