Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saving and restoring of 3D map view settings #219

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion kadas/app/3d/kadas3dintegration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QGuiApplication>
#include <QMessageBox>
#include <QScreen>
#include <QDomDocument>

#include "kadas/app/3d/kadas3dintegration.h"
#include <qgis/qgs3dmapcanvas.h>
Expand All @@ -33,19 +34,99 @@
#include <qgsprojectviewsettings.h>
#include <qgsdirectionallightsettings.h>
#include <qgsdockablewidgethelper.h>
#include <qgsmapviewsmanager.h>

void write3DMapViewSettings( Kadas3DMapCanvasWidget *widget, QDomDocument &doc, QDomElement &elem3DMap )
{
QgsReadWriteContext readWriteContext;
readWriteContext.setPathResolver( QgsProject::instance()->pathResolver() );
elem3DMap.setAttribute( QStringLiteral( "name" ), widget->canvasName() );
QDomElement elem3DMapSettings = widget->mapCanvas3D()->mapSettings()->writeXml( doc, readWriteContext );
elem3DMap.appendChild( elem3DMapSettings );
QDomElement elemCamera = widget->mapCanvas3D()->cameraController()->writeXml( doc );
elem3DMap.appendChild( elemCamera );

widget->dockableWidgetHelper()->writeXml( elem3DMap );
}

void read3DMapViewSettings( Kadas3DMapCanvasWidget *widget, QDomElement &elem3DMap )
{
QgsReadWriteContext readWriteContext;
readWriteContext.setPathResolver( QgsProject::instance()->pathResolver() );

QDomElement elem3D = elem3DMap.firstChildElement( QStringLiteral( "qgis3d" ) );
Qgs3DMapSettings *map = new Qgs3DMapSettings;
map->readXml( elem3D, readWriteContext );
map->resolveReferences( *QgsProject::instance() );

map->setTransformContext( QgsProject::instance()->transformContext() );
map->setPathResolver( QgsProject::instance()->pathResolver() );
map->setMapThemeCollection( QgsProject::instance()->mapThemeCollection() );
QObject::connect( QgsProject::instance(), &QgsProject::transformContextChanged, map, [map] {
map->setTransformContext( QgsProject::instance()->transformContext() );
} );

#if 0
// these things are not saved in project
map->setSelectionColor( mMapCanvas->selectionColor() );
map->setBackgroundColor( mMapCanvas->canvasColor() );
#endif
map->setOutputDpi( QGuiApplication::primaryScreen()->logicalDotsPerInch() );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the right place for this?
what happens with a secondary screen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is based on simplified assumptions (straight 1:1 copy from the QGIS source) that the 3D is shown on the same screen as the main application window.
I can adapt, but I think to be really meaningful, this would also need to react when the widget is dragged to another screen which afaik is one of the tricky things to do with Qt. So maybe we can assume that people who work with multiple screens have identical ones?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I've always had troubles with QGIS on multiple screens.
Fine with me like this, we can wait for any bugreport and properly fix this upstream.


widget->setMapSettings( map );

QDomElement elemCamera = elem3DMap.firstChildElement( QStringLiteral( "camera" ) );
if ( !elemCamera.isNull() )
{
widget->mapCanvas3D()->cameraController()->readXml( elemCamera );
}

widget->dockableWidgetHelper()->readXml( elem3DMap );
}

Kadas3DIntegration::Kadas3DIntegration( QAction *action3D, QgsMapCanvas *mapCanvas, QObject *parent )
: QObject( parent ), mAction3D( action3D ), mMapCanvas( mapCanvas )
{
Qgs3D::initialize();

connect( QgsProject::instance(), &QgsProject::readProject, [this] {
if ( m3DMapCanvasWidget )
{
QDomElement viewConfig = QgsProject::instance()->viewsManager()->get3DViewSettings( "kadas-3d" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use static string for this identifier?

read3DMapViewSettings( m3DMapCanvasWidget, viewConfig );
}
} );

connect( mAction3D, &QAction::triggered, [this]() {
if ( !m3DMapCanvasWidget )
{
m3DMapCanvasWidget = createNewMapCanvas3D( "3D Map" );
if ( !QgsProject::instance()->viewsManager()->get3DViewSettings( "kadas-3d" ).isNull() )
{
m3DMapCanvasWidget = new Kadas3DMapCanvasWidget( "kadas-3d", true );
m3DMapCanvasWidget->setMainCanvas( mMapCanvas );
QDomElement viewConfig = QgsProject::instance()->viewsManager()->get3DViewSettings( "kadas-3d" );
read3DMapViewSettings( m3DMapCanvasWidget, viewConfig );
}
else
{
m3DMapCanvasWidget = createNewMapCanvas3D( "kadas-3d" );
}

if ( m3DMapCanvasWidget )
{
connect( m3DMapCanvasWidget->dockableWidgetHelper(), &QgsDockableWidgetHelper::closed, [this]() {
QDomImplementation DomImplementation;
QDomDocumentType documentType = DomImplementation.createDocumentType(
QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" )
);
QDomDocument doc( documentType );

QDomElement elem3DMap;
elem3DMap = doc.createElement( QStringLiteral( "view" ) );
write3DMapViewSettings( m3DMapCanvasWidget, doc, elem3DMap );

QgsProject::instance()->viewsManager()->register3DViewSettings( "kadas-3d", elem3DMap );

m3DMapCanvasWidget->deleteLater();
m3DMapCanvasWidget = nullptr;
mAction3D->setChecked( false );
Expand Down