Skip to content
Permalink
Browse files

Create new map canvases using QgsMapCanvasDockWidget

Dock has a toolbar, currently with a single button to
allow users to set the CRS for the canvas
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent 29d77b0 commit f81b63b7b299c68136438af899619a9a8cece36e
@@ -47,6 +47,7 @@ SET(QGIS_APP_SRCS
qgslabelinggui.cpp
qgslabelingwidget.cpp
qgsloadstylefromdbdialog.cpp
qgsmapcanvasdockwidget.cpp
qgsmaplayerstyleguiutils.cpp
qgsrulebasedlabelingwidget.cpp
qgssavestyletodbdialog.cpp
@@ -225,6 +226,7 @@ SET (QGIS_APP_MOC_HDRS
qgslabelingwidget.h
qgslabelpropertydialog.h
qgsloadstylefromdbdialog.h
qgsmapcanvasdockwidget.h
qgsmaplayerstyleguiutils.h
qgsrulebasedlabelingwidget.h
qgssavestyletodbdialog.h
@@ -185,6 +185,7 @@
#include "qgslayertreeviewdefaultactions.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvasdockwidget.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgsmapcanvastracer.h"
#include "qgsmaplayer.h"
@@ -3113,19 +3114,18 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name )
}
}

QgsMapCanvas *mapCanvas = new QgsMapCanvas( this );
\
QgsMapCanvasDockWidget *mapCanvasWidget = new QgsMapCanvasDockWidget( name, this );
mapCanvasWidget->setAllowedAreas( Qt::AllDockWidgetAreas );

QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas();
mapCanvas->freeze( true );
mapCanvas->setObjectName( name );

QDockWidget *mapWidget = new QDockWidget( name, this );
mapWidget->setAllowedAreas( Qt::AllDockWidgetAreas );
mapWidget->setWidget( mapCanvas );
applyProjectSettingsToCanvas( mapCanvas );

mapCanvas->setDestinationCrs( QgsProject::instance()->crs() );

addDockWidget( Qt::RightDockWidgetArea, mapWidget );
addDockWidget( Qt::RightDockWidgetArea, mapCanvasWidget );
mapCanvas->freeze( false );
return mapCanvas;
}
@@ -0,0 +1,54 @@
/***************************************************************************
qgsmapcanvasdockwidget.cpp
--------------------------
begin : February 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson 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 "qgsmapcanvasdockwidget.h"
#include "qgsmapcanvas.h"
#include "qgsprojectionselectiondialog.h"

QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *parent )
: QgsDockWidget( parent )
{
setupUi( this );

mContents->layout()->setContentsMargins( 0, 0, 0, 0 );
mContents->layout()->setMargin( 0 );
static_cast< QVBoxLayout * >( mContents->layout() )->setSpacing( 0 );

setWindowTitle( name );
mMapCanvas = new QgsMapCanvas( this );

mMainWidget->setLayout( new QVBoxLayout() );
mMainWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
mMainWidget->layout()->setMargin( 0 );

mMainWidget->layout()->addWidget( mMapCanvas );

connect( mActionSetCrs, &QAction::triggered, this, &QgsMapCanvasDockWidget::setMapCrs );
}

QgsMapCanvas *QgsMapCanvasDockWidget::mapCanvas()
{
return mMapCanvas;
}

void QgsMapCanvasDockWidget::setMapCrs()
{
QgsProjectionSelectionDialog dlg;
dlg.setCrs( mMapCanvas->mapSettings().destinationCrs() );

if ( dlg.exec() )
{
mMapCanvas->setDestinationCrs( dlg.crs() );
}
}
@@ -0,0 +1,48 @@
/***************************************************************************
qgsmapcanvasdockwidget.h
------------------------
begin : February 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson 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. *
* *
***************************************************************************/
#ifndef QGSMAPCANVASDOCKWIDGET_H
#define QGSMAPCANVASDOCKWIDGET_H

#include <ui_qgsmapcanvasdockwidgetbase.h>

#include "qgsdockwidget.h"
#include "qgis_app.h"

class QgsMapCanvas;

class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsMapCanvasDockWidgetBase
{
Q_OBJECT
public:
explicit QgsMapCanvasDockWidget( const QString &name, QWidget *parent = nullptr );

/**
* Returns the map canvas contained in the dock widget.
*/
QgsMapCanvas *mapCanvas();

private slots:

void setMapCrs();

private:

QgsMapCanvas *mMapCanvas = nullptr;


};


#endif // QGSMAPCANVASDOCKWIDGET_H
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsMapCanvasDockWidgetBase</class>
<widget class="QgsDockWidget" name="QgsMapCanvasDockWidgetBase">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>216</width>
<height>138</height>
</rect>
</property>
<property name="windowTitle">
<string>Map Canvas</string>
</property>
<widget class="QWidget" name="mContents">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QToolBar" name="mToolbar">
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="floatable">
<bool>false</bool>
</property>
<addaction name="mActionSetCrs"/>
</widget>
</item>
<item>
<widget class="QWidget" name="mMainWidget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</item>
</layout>
</widget>
<action name="mActionSetCrs">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/CRS.svg</normaloff>:/images/themes/default/propertyicons/CRS.svg</iconset>
</property>
<property name="text">
<string>Set Map CRS</string>
</property>
<property name="toolTip">
<string>Set Map CRS</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>QgsDockWidget</class>
<extends>QDockWidget</extends>
<header>qgsdockwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit f81b63b

Please sign in to comment.
You can’t perform that action at this time.