-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
qgs3dmapcanvasdockwidget.cpp
478 lines (386 loc) · 17.7 KB
/
qgs3dmapcanvasdockwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/***************************************************************************
qgs3dmapcanvasdockwidget.cpp
--------------------------------------
Date : July 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgs3dmapcanvasdockwidget.h"
#include <QBoxLayout>
#include <QDialog>
#include <QDialogButtonBox>
#include <QProgressBar>
#include <QToolBar>
#include <QUrl>
#include <QAction>
#include "qgisapp.h"
#include "qgs3dmapcanvas.h"
#include "qgs3dmapconfigwidget.h"
#include "qgs3dmapscene.h"
#include "qgscameracontroller.h"
#include "qgshelp.h"
#include "qgsmapcanvas.h"
#include "qgsmessagebar.h"
#include "qgsapplication.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgsmapthemecollection.h"
#include "qgstemporalcontroller.h"
#include "qgs3danimationsettings.h"
#include "qgs3danimationwidget.h"
#include "qgs3dmapsettings.h"
#include "qgs3dmaptoolidentify.h"
#include "qgs3dmaptoolmeasureline.h"
#include "qgs3dutils.h"
#include "qgs3dsceneexporter.h"
#include "qgsabstract3drenderer.h"
#include "qgsmap3dexportwidget.h"
#include "qgs3dmapexportsettings.h"
Qgs3DMapCanvasDockWidget::Qgs3DMapCanvasDockWidget( QWidget *parent )
: QgsDockWidget( parent )
{
QgsSettings setting;
setAttribute( Qt::WA_DeleteOnClose ); // removes the dock widget from main window when
QWidget *contentsWidget = new QWidget( this );
QToolBar *toolBar = new QToolBar( contentsWidget );
toolBar->setIconSize( QgisApp::instance()->iconSize( true ) );
QAction *actionCameraControl = toolBar->addAction( QIcon( QgsApplication::iconPath( "mActionPan.svg" ) ),
tr( "Camera Control" ), this, &Qgs3DMapCanvasDockWidget::cameraControl );
actionCameraControl->setCheckable( true );
toolBar->addAction( QgsApplication::getThemeIcon( QStringLiteral( "mActionZoomFullExtent.svg" ) ),
tr( "Zoom Full" ), this, &Qgs3DMapCanvasDockWidget::resetView );
QAction *toggleOnScreenNavigation = toolBar->addAction(
QgsApplication::getThemeIcon( QStringLiteral( "mAction3DNavigation.svg" ) ),
tr( "Toggle On-Screen Navigation" ) );
toggleOnScreenNavigation->setCheckable( true );
toggleOnScreenNavigation->setChecked(
setting.value( QStringLiteral( "/3D/navigationWidget/visibility" ), true, QgsSettings::Gui ).toBool()
);
QObject::connect( toggleOnScreenNavigation, &QAction::toggled, this, &Qgs3DMapCanvasDockWidget::toggleNavigationWidget );
toolBar->addSeparator();
QAction *actionIdentify = toolBar->addAction( QIcon( QgsApplication::iconPath( "mActionIdentify.svg" ) ),
tr( "Identify" ), this, &Qgs3DMapCanvasDockWidget::identify );
actionIdentify->setCheckable( true );
QAction *actionMeasurementTool = toolBar->addAction( QIcon( QgsApplication::iconPath( "mActionMeasure.svg" ) ),
tr( "Measurement Line" ), this, &Qgs3DMapCanvasDockWidget::measureLine );
actionMeasurementTool->setCheckable( true );
// Create action group to make the action exclusive
QActionGroup *actionGroup = new QActionGroup( this );
actionGroup->addAction( actionCameraControl );
actionGroup->addAction( actionIdentify );
actionGroup->addAction( actionMeasurementTool );
actionGroup->setExclusive( true );
actionCameraControl->setChecked( true );
QAction *actionAnim = toolBar->addAction( QIcon( QgsApplication::iconPath( "mTaskRunning.svg" ) ),
tr( "Animations" ), this, &Qgs3DMapCanvasDockWidget::toggleAnimations );
actionAnim->setCheckable( true );
toolBar->addAction( QgsApplication::getThemeIcon( QStringLiteral( "mActionSaveMapAsImage.svg" ) ),
tr( "Save as Image…" ), this, &Qgs3DMapCanvasDockWidget::saveAsImage );
toolBar->addAction( QgsApplication::getThemeIcon( QStringLiteral( "3d.svg" ) ),
tr( "Export 3D Scene" ), this, &Qgs3DMapCanvasDockWidget::exportScene );
toolBar->addSeparator();
// Map Theme Menu
mMapThemeMenu = new QMenu( this );
connect( mMapThemeMenu, &QMenu::aboutToShow, this, &Qgs3DMapCanvasDockWidget::mapThemeMenuAboutToShow );
connect( QgsProject::instance()->mapThemeCollection(), &QgsMapThemeCollection::mapThemeRenamed, this, &Qgs3DMapCanvasDockWidget::currentMapThemeRenamed );
mBtnMapThemes = new QToolButton();
mBtnMapThemes->setAutoRaise( true );
mBtnMapThemes->setToolTip( tr( "Set View Theme" ) );
mBtnMapThemes->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) ) );
mBtnMapThemes->setPopupMode( QToolButton::InstantPopup );
mBtnMapThemes->setMenu( mMapThemeMenu );
toolBar->addWidget( mBtnMapThemes );
// Options Menu
mOptionsMenu = new QMenu( this );
mBtnOptions = new QToolButton();
mBtnOptions->setAutoRaise( true );
mBtnOptions->setToolTip( tr( "Options" ) );
mBtnOptions->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionOptions.svg" ) ) );
mBtnOptions->setPopupMode( QToolButton::InstantPopup );
mBtnOptions->setMenu( mOptionsMenu );
toolBar->addWidget( mBtnOptions );
mActionEnableShadows = new QAction( tr( "Show Shadows" ), this );
mActionEnableShadows->setCheckable( true );
connect( mActionEnableShadows, &QAction::toggled, this, [ = ]( bool enabled )
{
QgsShadowSettings settings = mCanvas->map()->shadowSettings();
settings.setRenderShadows( enabled );
mCanvas->map()->setShadowSettings( settings );
} );
mOptionsMenu->addAction( mActionEnableShadows );
mActionEnableEyeDome = new QAction( tr( "Show Eye Dome Lighting" ), this );
mActionEnableEyeDome->setCheckable( true );
connect( mActionEnableEyeDome, &QAction::triggered, this, [ = ]( bool enabled )
{
mCanvas->map()->setEyeDomeLightingEnabled( enabled );
} );
mOptionsMenu->addAction( mActionEnableEyeDome );
mOptionsMenu->addSeparator();
QAction *configureAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "mActionOptions.svg" ) ),
tr( "Configure…" ), this );
connect( configureAction, &QAction::triggered, this, &Qgs3DMapCanvasDockWidget::configure );
mOptionsMenu->addAction( configureAction );
mCanvas = new Qgs3DMapCanvas( contentsWidget );
mCanvas->setMinimumSize( QSize( 200, 200 ) );
mCanvas->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
connect( mCanvas, &Qgs3DMapCanvas::savedAsImage, this, [ = ]( const QString fileName )
{
QgisApp::instance()->messageBar()->pushSuccess( tr( "Save as Image" ), tr( "Successfully saved the 3D map to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( fileName ).toString(), QDir::toNativeSeparators( fileName ) ) );
} );
connect( mCanvas, &Qgs3DMapCanvas::fpsCountChanged, this, &Qgs3DMapCanvasDockWidget::updateFpsCount );
connect( mCanvas, &Qgs3DMapCanvas::fpsCounterEnabledChanged, this, &Qgs3DMapCanvasDockWidget::toggleFpsCounter );
mMapToolIdentify = new Qgs3DMapToolIdentify( mCanvas );
mMapToolMeasureLine = new Qgs3DMapToolMeasureLine( mCanvas );
mLabelPendingJobs = new QLabel( this );
mProgressPendingJobs = new QProgressBar( this );
mProgressPendingJobs->setRange( 0, 0 );
mLabelFpsCounter = new QLabel( this );
mLabelNavigationSpeed = new QLabel( this );
mAnimationWidget = new Qgs3DAnimationWidget( this );
mAnimationWidget->setVisible( false );
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->setContentsMargins( 0, 0, 0, 0 );
topLayout->setSpacing( style()->pixelMetric( QStyle::PM_LayoutHorizontalSpacing ) );
topLayout->addWidget( toolBar );
topLayout->addStretch( 1 );
topLayout->addWidget( mLabelPendingJobs );
topLayout->addWidget( mProgressPendingJobs );
topLayout->addWidget( mLabelNavigationSpeed );
mLabelNavigationSpeed->hide();
topLayout->addWidget( mLabelFpsCounter );
mLabelNavSpeedHideTimeout = new QTimer( this );
mLabelNavSpeedHideTimeout->setInterval( 1000 );
connect( mLabelNavSpeedHideTimeout, &QTimer::timeout, this, [ = ]
{
mLabelNavigationSpeed->hide();
mLabelNavSpeedHideTimeout->stop();
} );
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins( 0, 0, 0, 0 );
layout->setSpacing( 0 );
layout->addLayout( topLayout );
layout->addWidget( mCanvas );
layout->addWidget( mAnimationWidget );
contentsWidget->setLayout( layout );
setWidget( contentsWidget );
onTotalPendingJobsCountChanged();
}
void Qgs3DMapCanvasDockWidget::saveAsImage()
{
QPair< QString, QString> fileNameAndFilter = QgsGuiUtils::getSaveAsImageName( this, tr( "Choose a file name to save the 3D map canvas to an image" ) );
if ( !fileNameAndFilter.first.isEmpty() )
{
mCanvas->saveAsImage( fileNameAndFilter.first, fileNameAndFilter.second );
}
}
void Qgs3DMapCanvasDockWidget::toggleAnimations()
{
if ( mAnimationWidget->isVisible() )
{
mAnimationWidget->setVisible( false );
return;
}
mAnimationWidget->setVisible( true );
// create a dummy animation when first started - better to have something than nothing...
if ( mAnimationWidget->animation().duration() == 0 )
{
mAnimationWidget->setDefaultAnimation();
}
}
void Qgs3DMapCanvasDockWidget::cameraControl()
{
QAction *action = qobject_cast<QAction *>( sender() );
if ( !action )
return;
mCanvas->setMapTool( nullptr );
}
void Qgs3DMapCanvasDockWidget::identify()
{
QAction *action = qobject_cast<QAction *>( sender() );
if ( !action )
return;
mCanvas->setMapTool( action->isChecked() ? mMapToolIdentify : nullptr );
}
void Qgs3DMapCanvasDockWidget::measureLine()
{
QAction *action = qobject_cast<QAction *>( sender() );
if ( !action )
return;
mCanvas->setMapTool( action->isChecked() ? mMapToolMeasureLine : nullptr );
}
void Qgs3DMapCanvasDockWidget::toggleNavigationWidget( bool visibility )
{
mCanvas->setOnScreenNavigationVisibility( visibility );
}
void Qgs3DMapCanvasDockWidget::toggleFpsCounter( bool visibility )
{
mLabelFpsCounter->setVisible( visibility );
}
void Qgs3DMapCanvasDockWidget::setMapSettings( Qgs3DMapSettings *map )
{
whileBlocking( mActionEnableShadows )->setChecked( map->shadowSettings().renderShadows() );
whileBlocking( mActionEnableEyeDome )->setChecked( map->eyeDomeLightingEnabled() );
mCanvas->setMap( map );
connect( mCanvas->scene(), &Qgs3DMapScene::totalPendingJobsCountChanged, this, &Qgs3DMapCanvasDockWidget::onTotalPendingJobsCountChanged );
mAnimationWidget->setCameraController( mCanvas->scene()->cameraController() );
mAnimationWidget->setMap( map );
// Disable button for switching the map theme if the terrain generator is a mesh
mBtnMapThemes->setDisabled( mCanvas->map()->terrainGenerator()->type() == QgsTerrainGenerator::Mesh );
mLabelFpsCounter->setVisible( map->isFpsCounterEnabled() );
connect( mCanvas, &Qgs3DMapCanvas::cameraNavigationSpeedChanged, this, &Qgs3DMapCanvasDockWidget::cameraNavigationSpeedChanged );
}
void Qgs3DMapCanvasDockWidget::setMainCanvas( QgsMapCanvas *canvas )
{
mMainCanvas = canvas;
connect( mMainCanvas, &QgsMapCanvas::layersChanged, this, &Qgs3DMapCanvasDockWidget::onMainCanvasLayersChanged );
connect( mMainCanvas, &QgsMapCanvas::canvasColorChanged, this, &Qgs3DMapCanvasDockWidget::onMainCanvasColorChanged );
}
void Qgs3DMapCanvasDockWidget::resetView()
{
mCanvas->resetView( true );
}
void Qgs3DMapCanvasDockWidget::configure()
{
QDialog dlg( this );
dlg.setWindowTitle( tr( "3D Configuration" ) );
dlg.setObjectName( QStringLiteral( "3DConfigurationDialog" ) );
dlg.setMinimumSize( 600, 460 );
QgsGui::instance()->enableAutoGeometryRestore( &dlg );
Qgs3DMapSettings *map = mCanvas->map();
Qgs3DMapConfigWidget *w = new Qgs3DMapConfigWidget( map, mMainCanvas, mCanvas, &dlg );
QDialogButtonBox *buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::Help, &dlg );
auto applyConfig = [ = ]()
{
QgsVector3D oldOrigin = map->origin();
QgsCoordinateReferenceSystem oldCrs = map->crs();
QgsCameraPose oldCameraPose = mCanvas->cameraController()->cameraPose();
QgsVector3D oldLookingAt = oldCameraPose.centerPoint();
// update map
w->apply();
QgsVector3D p = Qgs3DUtils::transformWorldCoordinates(
oldLookingAt,
oldOrigin, oldCrs,
map->origin(), map->crs(), QgsProject::instance()->transformContext() );
if ( p != oldLookingAt )
{
// apply() call has moved origin of the world so let's move camera so we look still at the same place
QgsCameraPose newCameraPose = oldCameraPose;
newCameraPose.setCenterPoint( p );
mCanvas->cameraController()->setCameraPose( newCameraPose );
}
// Disable map theme button if the terrain generator is a mesh
mBtnMapThemes->setDisabled( map->terrainGenerator()->type() == QgsTerrainGenerator::Mesh );
};
connect( buttons, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
connect( buttons, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
connect( buttons, &QDialogButtonBox::clicked, &dlg, [ = ]( QAbstractButton * button )
{
if ( buttons->buttonRole( button ) == QDialogButtonBox::ApplyRole )
applyConfig();
} );
connect( buttons, &QDialogButtonBox::helpRequested, w, []() { QgsHelp::openHelp( QStringLiteral( "introduction/qgis_gui.html#scene-configuration" ) ); } );
connect( w, &Qgs3DMapConfigWidget::isValidChanged, this, [ = ]( bool valid )
{
buttons->button( QDialogButtonBox::Ok )->setEnabled( valid );
} );
QVBoxLayout *layout = new QVBoxLayout( &dlg );
layout->addWidget( w, 1 );
layout->addWidget( buttons );
if ( !dlg.exec() )
return;
applyConfig();
whileBlocking( mActionEnableShadows )->setChecked( map->shadowSettings().renderShadows() );
whileBlocking( mActionEnableEyeDome )->setChecked( map->eyeDomeLightingEnabled() );
}
void Qgs3DMapCanvasDockWidget::exportScene()
{
QDialog dlg;
dlg.setWindowTitle( tr( "Export 3D Scene" ) );
dlg.setObjectName( QStringLiteral( "3DSceneExportDialog" ) );
QgsGui::instance()->enableAutoGeometryRestore( &dlg );
Qgs3DMapExportSettings exportSettings;
QgsMap3DExportWidget w( mCanvas->scene(), &exportSettings );
QDialogButtonBox *buttons = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, &dlg );
connect( buttons, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
connect( buttons, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
connect( buttons, &QDialogButtonBox::helpRequested, &dlg, [ = ] { QgsHelp::openHelp( QStringLiteral( "introduction/qgis_gui.html#d-map-view" ) ); } );
QVBoxLayout *layout = new QVBoxLayout( &dlg );
layout->addWidget( &w, 1 );
layout->addWidget( buttons );
if ( dlg.exec() )
w.exportScene();
}
void Qgs3DMapCanvasDockWidget::onMainCanvasLayersChanged()
{
mCanvas->map()->setLayers( mMainCanvas->layers() );
mCanvas->map()->setTerrainLayers( mMainCanvas->layers() );
}
void Qgs3DMapCanvasDockWidget::onMainCanvasColorChanged()
{
mCanvas->map()->setBackgroundColor( mMainCanvas->canvasColor() );
}
void Qgs3DMapCanvasDockWidget::onTotalPendingJobsCountChanged()
{
int count = mCanvas->scene() ? mCanvas->scene()->totalPendingJobsCount() : 0;
mProgressPendingJobs->setVisible( count );
mLabelPendingJobs->setVisible( count );
if ( count )
mLabelPendingJobs->setText( tr( "Loading %1 tiles" ).arg( count ) );
}
void Qgs3DMapCanvasDockWidget::updateFpsCount( float fpsCount )
{
mLabelFpsCounter->setText( QStringLiteral( "%1 fps" ).arg( fpsCount, 10, 'f', 2, QLatin1Char( ' ' ) ) );
}
void Qgs3DMapCanvasDockWidget::cameraNavigationSpeedChanged( double speed )
{
mLabelNavigationSpeed->setText( QStringLiteral( "Speed: %1 ×" ).arg( QString::number( speed, 'f', 2 ) ) );
mLabelNavigationSpeed->show();
mLabelNavSpeedHideTimeout->start();
}
void Qgs3DMapCanvasDockWidget::mapThemeMenuAboutToShow()
{
qDeleteAll( mMapThemeMenuPresetActions );
mMapThemeMenuPresetActions.clear();
QString currentTheme = mCanvas->map()->terrainMapTheme();
QAction *actionFollowMain = new QAction( tr( "(none)" ), mMapThemeMenu );
actionFollowMain->setCheckable( true );
if ( currentTheme.isEmpty() || !QgsProject::instance()->mapThemeCollection()->hasMapTheme( currentTheme ) )
{
actionFollowMain->setChecked( true );
}
connect( actionFollowMain, &QAction::triggered, this, [ = ]
{
mCanvas->map()->setTerrainMapTheme( QString() );
} );
mMapThemeMenuPresetActions.append( actionFollowMain );
const auto constMapThemes = QgsProject::instance()->mapThemeCollection()->mapThemes();
for ( const QString &grpName : constMapThemes )
{
QAction *a = new QAction( grpName, mMapThemeMenu );
a->setCheckable( true );
if ( grpName == currentTheme )
{
a->setChecked( true );
}
connect( a, &QAction::triggered, this, [a, this]
{
mCanvas->map()->setTerrainMapTheme( a->text() );
} );
mMapThemeMenuPresetActions.append( a );
}
mMapThemeMenu->addActions( mMapThemeMenuPresetActions );
}
void Qgs3DMapCanvasDockWidget::currentMapThemeRenamed( const QString &theme, const QString &newTheme )
{
if ( theme == mCanvas->map()->terrainMapTheme() )
{
mCanvas->map()->setTerrainMapTheme( newTheme );
}
}