Skip to content

Commit f81b63b

Browse files
committed
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
1 parent 29d77b0 commit f81b63b

5 files changed

+205
-6
lines changed

src/app/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ SET(QGIS_APP_SRCS
4747
qgslabelinggui.cpp
4848
qgslabelingwidget.cpp
4949
qgsloadstylefromdbdialog.cpp
50+
qgsmapcanvasdockwidget.cpp
5051
qgsmaplayerstyleguiutils.cpp
5152
qgsrulebasedlabelingwidget.cpp
5253
qgssavestyletodbdialog.cpp
@@ -225,6 +226,7 @@ SET (QGIS_APP_MOC_HDRS
225226
qgslabelingwidget.h
226227
qgslabelpropertydialog.h
227228
qgsloadstylefromdbdialog.h
229+
qgsmapcanvasdockwidget.h
228230
qgsmaplayerstyleguiutils.h
229231
qgsrulebasedlabelingwidget.h
230232
qgssavestyletodbdialog.h

src/app/qgisapp.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
#include "qgslayertreeviewdefaultactions.h"
186186
#include "qgslogger.h"
187187
#include "qgsmapcanvas.h"
188+
#include "qgsmapcanvasdockwidget.h"
188189
#include "qgsmapcanvassnappingutils.h"
189190
#include "qgsmapcanvastracer.h"
190191
#include "qgsmaplayer.h"
@@ -3113,19 +3114,18 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name )
31133114
}
31143115
}
31153116

3116-
QgsMapCanvas *mapCanvas = new QgsMapCanvas( this );
3117-
\
3117+
QgsMapCanvasDockWidget *mapCanvasWidget = new QgsMapCanvasDockWidget( name, this );
3118+
mapCanvasWidget->setAllowedAreas( Qt::AllDockWidgetAreas );
3119+
3120+
QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas();
31183121
mapCanvas->freeze( true );
31193122
mapCanvas->setObjectName( name );
31203123

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

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

3128-
addDockWidget( Qt::RightDockWidgetArea, mapWidget );
3128+
addDockWidget( Qt::RightDockWidgetArea, mapCanvasWidget );
31293129
mapCanvas->freeze( false );
31303130
return mapCanvas;
31313131
}

src/app/qgsmapcanvasdockwidget.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/***************************************************************************
2+
qgsmapcanvasdockwidget.cpp
3+
--------------------------
4+
begin : February 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include "qgsmapcanvasdockwidget.h"
16+
#include "qgsmapcanvas.h"
17+
#include "qgsprojectionselectiondialog.h"
18+
19+
QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *parent )
20+
: QgsDockWidget( parent )
21+
{
22+
setupUi( this );
23+
24+
mContents->layout()->setContentsMargins( 0, 0, 0, 0 );
25+
mContents->layout()->setMargin( 0 );
26+
static_cast< QVBoxLayout * >( mContents->layout() )->setSpacing( 0 );
27+
28+
setWindowTitle( name );
29+
mMapCanvas = new QgsMapCanvas( this );
30+
31+
mMainWidget->setLayout( new QVBoxLayout() );
32+
mMainWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
33+
mMainWidget->layout()->setMargin( 0 );
34+
35+
mMainWidget->layout()->addWidget( mMapCanvas );
36+
37+
connect( mActionSetCrs, &QAction::triggered, this, &QgsMapCanvasDockWidget::setMapCrs );
38+
}
39+
40+
QgsMapCanvas *QgsMapCanvasDockWidget::mapCanvas()
41+
{
42+
return mMapCanvas;
43+
}
44+
45+
void QgsMapCanvasDockWidget::setMapCrs()
46+
{
47+
QgsProjectionSelectionDialog dlg;
48+
dlg.setCrs( mMapCanvas->mapSettings().destinationCrs() );
49+
50+
if ( dlg.exec() )
51+
{
52+
mMapCanvas->setDestinationCrs( dlg.crs() );
53+
}
54+
}

src/app/qgsmapcanvasdockwidget.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/***************************************************************************
2+
qgsmapcanvasdockwidget.h
3+
------------------------
4+
begin : February 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSMAPCANVASDOCKWIDGET_H
16+
#define QGSMAPCANVASDOCKWIDGET_H
17+
18+
#include <ui_qgsmapcanvasdockwidgetbase.h>
19+
20+
#include "qgsdockwidget.h"
21+
#include "qgis_app.h"
22+
23+
class QgsMapCanvas;
24+
25+
class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsMapCanvasDockWidgetBase
26+
{
27+
Q_OBJECT
28+
public:
29+
explicit QgsMapCanvasDockWidget( const QString &name, QWidget *parent = nullptr );
30+
31+
/**
32+
* Returns the map canvas contained in the dock widget.
33+
*/
34+
QgsMapCanvas *mapCanvas();
35+
36+
private slots:
37+
38+
void setMapCrs();
39+
40+
private:
41+
42+
QgsMapCanvas *mMapCanvas = nullptr;
43+
44+
45+
};
46+
47+
48+
#endif // QGSMAPCANVASDOCKWIDGET_H

src/ui/qgsmapcanvasdockwidgetbase.ui

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsMapCanvasDockWidgetBase</class>
4+
<widget class="QgsDockWidget" name="QgsMapCanvasDockWidgetBase">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>216</width>
10+
<height>138</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Map Canvas</string>
15+
</property>
16+
<widget class="QWidget" name="mContents">
17+
<layout class="QVBoxLayout" name="verticalLayout">
18+
<property name="spacing">
19+
<number>0</number>
20+
</property>
21+
<property name="leftMargin">
22+
<number>0</number>
23+
</property>
24+
<property name="topMargin">
25+
<number>0</number>
26+
</property>
27+
<property name="rightMargin">
28+
<number>0</number>
29+
</property>
30+
<property name="bottomMargin">
31+
<number>0</number>
32+
</property>
33+
<item>
34+
<widget class="QToolBar" name="mToolbar">
35+
<property name="iconSize">
36+
<size>
37+
<width>16</width>
38+
<height>16</height>
39+
</size>
40+
</property>
41+
<property name="floatable">
42+
<bool>false</bool>
43+
</property>
44+
<addaction name="mActionSetCrs"/>
45+
</widget>
46+
</item>
47+
<item>
48+
<widget class="QWidget" name="mMainWidget" native="true">
49+
<layout class="QHBoxLayout" name="horizontalLayout">
50+
<property name="spacing">
51+
<number>0</number>
52+
</property>
53+
<property name="leftMargin">
54+
<number>0</number>
55+
</property>
56+
<property name="topMargin">
57+
<number>0</number>
58+
</property>
59+
<property name="rightMargin">
60+
<number>0</number>
61+
</property>
62+
<property name="bottomMargin">
63+
<number>0</number>
64+
</property>
65+
</layout>
66+
</widget>
67+
</item>
68+
</layout>
69+
</widget>
70+
<action name="mActionSetCrs">
71+
<property name="icon">
72+
<iconset resource="../../images/images.qrc">
73+
<normaloff>:/images/themes/default/propertyicons/CRS.svg</normaloff>:/images/themes/default/propertyicons/CRS.svg</iconset>
74+
</property>
75+
<property name="text">
76+
<string>Set Map CRS</string>
77+
</property>
78+
<property name="toolTip">
79+
<string>Set Map CRS</string>
80+
</property>
81+
</action>
82+
</widget>
83+
<customwidgets>
84+
<customwidget>
85+
<class>QgsDockWidget</class>
86+
<extends>QDockWidget</extends>
87+
<header>qgsdockwidget.h</header>
88+
<container>1</container>
89+
</customwidget>
90+
</customwidgets>
91+
<resources>
92+
<include location="../../images/images.qrc"/>
93+
</resources>
94+
<connections/>
95+
</ui>

0 commit comments

Comments
 (0)