Skip to content

Commit

Permalink
Save/restore extra map views in project
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent ee969df commit e4469c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
50 changes: 48 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -3112,7 +3112,7 @@ QgsMapCanvas *QgisApp::mapCanvas()
return mMapCanvas;
}

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

mapCanvasWidget->setFloating( isFloating );
if ( dockGeometry.isEmpty() )
{
mapCanvasWidget->resize( 400, 400 );
}
else
{
mapCanvasWidget->setGeometry( dockGeometry );
}

QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas();
mapCanvas->freeze( true );
mapCanvas->setObjectName( name );
Expand Down Expand Up @@ -11686,10 +11696,26 @@ void QgisApp::writeProject( QDomDocument &doc )
QDomElement oldLegendElem = QgsLayerTreeUtils::writeOldLegend( doc, QgsLayerTree::toGroup( clonedRoot ),
mLayerTreeCanvasBridge->hasCustomLayerOrder(), mLayerTreeCanvasBridge->customLayerOrder() );
delete clonedRoot;
doc.firstChildElement( QStringLiteral( "qgis" ) ).appendChild( oldLegendElem );
QDomElement qgisNode = doc.firstChildElement( QStringLiteral( "qgis" ) );
qgisNode.appendChild( oldLegendElem );

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

// Save the position of the map view docks
QDomElement mapViewNode = doc.createElement( QStringLiteral( "mapViewDocks" ) );
Q_FOREACH ( QgsMapCanvasDockWidget *w, findChildren< QgsMapCanvasDockWidget * >() )
{
QDomElement node = doc.createElement( QStringLiteral( "view" ) );
node.setAttribute( QStringLiteral( "name" ), w->mapCanvas()->objectName() );
node.setAttribute( QStringLiteral( "x" ), w->x() );
node.setAttribute( QStringLiteral( "y" ), w->y() );
node.setAttribute( QStringLiteral( "width" ), w->width() );
node.setAttribute( QStringLiteral( "height" ), w->height() );
node.setAttribute( QStringLiteral( "floating" ), w->isFloating() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
mapViewNode.appendChild( node );
}
qgisNode.appendChild( mapViewNode );

projectChanged( doc );
}

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

if ( autoSetupOnFirstLayer )
mLayerTreeCanvasBridge->setAutoSetupOnFirstLayer( true );

QDomNodeList nodes = doc.elementsByTagName( QStringLiteral( "mapViewDocks" ) );
if ( !nodes.isEmpty() )
{
QDomNode viewNode = nodes.at( 0 );
nodes = viewNode.childNodes();
for ( int i = 0; i < nodes.size(); ++i )
{
QDomElement elementNode = nodes.at( i ).toElement();
QString mapName = elementNode.attribute( QStringLiteral( "name" ) );
int x = elementNode.attribute( QStringLiteral( "x" ), QStringLiteral( "0" ) ).toInt();
int y = elementNode.attribute( QStringLiteral( "y" ), QStringLiteral( "0" ) ).toInt();
int w = elementNode.attribute( QStringLiteral( "width" ), QStringLiteral( "400" ) ).toInt();
int h = elementNode.attribute( QStringLiteral( "height" ), QStringLiteral( "400" ) ).toInt();
bool floating = elementNode.attribute( QStringLiteral( "floating" ), QStringLiteral( "0" ) ).toInt();

QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ) );
mapCanvas->readProject( doc );
}
}
}

void QgisApp::showLayerProperties( QgsMapLayer *ml )
Expand Down
8 changes: 6 additions & 2 deletions src/app/qgisapp.h
Expand Up @@ -236,8 +236,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
QList< QgsMapCanvas * > mapCanvases();

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

/**
* Closes any additional map canvases. The main map canvas will not
Expand Down

0 comments on commit e4469c8

Please sign in to comment.