Skip to content

Commit e4469c8

Browse files
committed
Save/restore extra map views in project
1 parent ee969df commit e4469c8

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/app/qgisapp.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,7 +3112,7 @@ QgsMapCanvas *QgisApp::mapCanvas()
31123112
return mMapCanvas;
31133113
}
31143114

3115-
QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name )
3115+
QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating, const QRect &dockGeometry )
31163116
{
31173117
Q_FOREACH ( QgsMapCanvas *canvas, mapCanvases() )
31183118
{
@@ -3127,6 +3127,16 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name )
31273127
QgsMapCanvasDockWidget *mapCanvasWidget = new QgsMapCanvasDockWidget( name, this );
31283128
mapCanvasWidget->setAllowedAreas( Qt::AllDockWidgetAreas );
31293129

3130+
mapCanvasWidget->setFloating( isFloating );
3131+
if ( dockGeometry.isEmpty() )
3132+
{
3133+
mapCanvasWidget->resize( 400, 400 );
3134+
}
3135+
else
3136+
{
3137+
mapCanvasWidget->setGeometry( dockGeometry );
3138+
}
3139+
31303140
QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas();
31313141
mapCanvas->freeze( true );
31323142
mapCanvas->setObjectName( name );
@@ -11686,10 +11696,26 @@ void QgisApp::writeProject( QDomDocument &doc )
1168611696
QDomElement oldLegendElem = QgsLayerTreeUtils::writeOldLegend( doc, QgsLayerTree::toGroup( clonedRoot ),
1168711697
mLayerTreeCanvasBridge->hasCustomLayerOrder(), mLayerTreeCanvasBridge->customLayerOrder() );
1168811698
delete clonedRoot;
11689-
doc.firstChildElement( QStringLiteral( "qgis" ) ).appendChild( oldLegendElem );
11699+
QDomElement qgisNode = doc.firstChildElement( QStringLiteral( "qgis" ) );
11700+
qgisNode.appendChild( oldLegendElem );
1169011701

1169111702
QgsProject::instance()->writeEntry( QStringLiteral( "Legend" ), QStringLiteral( "filterByMap" ), static_cast< bool >( layerTreeView()->layerTreeModel()->legendFilterMapSettings() ) );
1169211703

11704+
// Save the position of the map view docks
11705+
QDomElement mapViewNode = doc.createElement( QStringLiteral( "mapViewDocks" ) );
11706+
Q_FOREACH ( QgsMapCanvasDockWidget *w, findChildren< QgsMapCanvasDockWidget * >() )
11707+
{
11708+
QDomElement node = doc.createElement( QStringLiteral( "view" ) );
11709+
node.setAttribute( QStringLiteral( "name" ), w->mapCanvas()->objectName() );
11710+
node.setAttribute( QStringLiteral( "x" ), w->x() );
11711+
node.setAttribute( QStringLiteral( "y" ), w->y() );
11712+
node.setAttribute( QStringLiteral( "width" ), w->width() );
11713+
node.setAttribute( QStringLiteral( "height" ), w->height() );
11714+
node.setAttribute( QStringLiteral( "floating" ), w->isFloating() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
11715+
mapViewNode.appendChild( node );
11716+
}
11717+
qgisNode.appendChild( mapViewNode );
11718+
1169311719
projectChanged( doc );
1169411720
}
1169511721

@@ -11705,6 +11731,26 @@ void QgisApp::readProject( const QDomDocument &doc )
1170511731

1170611732
if ( autoSetupOnFirstLayer )
1170711733
mLayerTreeCanvasBridge->setAutoSetupOnFirstLayer( true );
11734+
11735+
QDomNodeList nodes = doc.elementsByTagName( QStringLiteral( "mapViewDocks" ) );
11736+
if ( !nodes.isEmpty() )
11737+
{
11738+
QDomNode viewNode = nodes.at( 0 );
11739+
nodes = viewNode.childNodes();
11740+
for ( int i = 0; i < nodes.size(); ++i )
11741+
{
11742+
QDomElement elementNode = nodes.at( i ).toElement();
11743+
QString mapName = elementNode.attribute( QStringLiteral( "name" ) );
11744+
int x = elementNode.attribute( QStringLiteral( "x" ), QStringLiteral( "0" ) ).toInt();
11745+
int y = elementNode.attribute( QStringLiteral( "y" ), QStringLiteral( "0" ) ).toInt();
11746+
int w = elementNode.attribute( QStringLiteral( "width" ), QStringLiteral( "400" ) ).toInt();
11747+
int h = elementNode.attribute( QStringLiteral( "height" ), QStringLiteral( "400" ) ).toInt();
11748+
bool floating = elementNode.attribute( QStringLiteral( "floating" ), QStringLiteral( "0" ) ).toInt();
11749+
11750+
QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ) );
11751+
mapCanvas->readProject( doc );
11752+
}
11753+
}
1170811754
}
1170911755

1171011756
void QgisApp::showLayerProperties( QgsMapLayer *ml )

src/app/qgisapp.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
236236
*/
237237
QList< QgsMapCanvas * > mapCanvases();
238238

239-
//! Create a new map canvas with the specified unique \a name
240-
QgsMapCanvas *createNewMapCanvas( const QString &name );
239+
/**
240+
* Create a new map canvas with the specified unique \a name. The \a isFloating
241+
* and \a dockGeometry arguments can be used to specify an initial floating state
242+
* and widget geometry rect for the dock.
243+
*/
244+
QgsMapCanvas *createNewMapCanvas( const QString &name, bool isFloating = false, const QRect &dockGeometry = QRect() );
241245

242246
/**
243247
* Closes any additional map canvases. The main map canvas will not

0 commit comments

Comments
 (0)