Skip to content

Commit

Permalink
[FEATURE] Shell functionality for creating new canvases
Browse files Browse the repository at this point in the history
Allows new "map views" to be created in the main QGIS interface.
These are map canvases, but with limited functionality (ie
no use of map tools beyond pan tool).
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent 2654c72 commit 29d77b0
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 1 deletion.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -556,6 +556,7 @@
<file>themes/default/processingAlgorithm.svg</file>
<file>themes/default/processingResult.svg</file>
<file>themes/default/search.svg</file>
<file>themes/default/mActionNewMap.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
98 changes: 98 additions & 0 deletions images/themes/default/mActionNewMap.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion python/gui/qgisinterface.sip
Expand Up @@ -58,7 +58,15 @@ class QgisInterface : QObject

/* Exposed functions */

//! Zoom to full extent of map layers
virtual QList< QgsMapCanvas* > mapCanvases() = 0;

virtual QgsMapCanvas* createNewMapCanvas( const QString& name ) = 0;

public slots:




virtual void zoomFull() = 0;

//! Zoom to previous view extent
Expand Down
59 changes: 59 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1663,6 +1663,7 @@ void QgisApp::createActions()
connect( mActionSaveProject, SIGNAL( triggered() ), this, SLOT( fileSave() ) );
connect( mActionSaveProjectAs, SIGNAL( triggered() ), this, SLOT( fileSaveAs() ) );
connect( mActionSaveMapAsImage, SIGNAL( triggered() ), this, SLOT( saveMapAsImage() ) );
connect( mActionNewMapCanvas, &QAction::triggered, this, &QgisApp::newMapCanvas );
connect( mActionNewPrintComposer, SIGNAL( triggered() ), this, SLOT( newPrintComposer() ) );
connect( mActionShowComposerManager, SIGNAL( triggered() ), this, SLOT( showComposerManager() ) );
connect( mActionExit, SIGNAL( triggered() ), this, SLOT( fileExit() ) );
Expand Down Expand Up @@ -3100,6 +3101,35 @@ QgsMapCanvas *QgisApp::mapCanvas()
return mMapCanvas;
}

QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name )
{
Q_FOREACH ( QgsMapCanvas *canvas, mapCanvases() )
{
if ( canvas->objectName() == name )
{
QString errorMessage = tr( "A map canvas with name '%1' already exists!" ).arg( name );
QgsDebugMsg( errorMessage );
return nullptr;
}
}

QgsMapCanvas *mapCanvas = new QgsMapCanvas( this );
\
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 );
mapCanvas->freeze( false );
return mapCanvas;
}

QgsMessageBar *QgisApp::messageBar()
{
Q_ASSERT( mInfoBar );
Expand Down Expand Up @@ -7042,6 +7072,11 @@ QList<QgsMapCanvasAnnotationItem *> QgisApp::annotationItems()
return itemList;
}

QList<QgsMapCanvas *> QgisApp::mapCanvases()
{
return findChildren< QgsMapCanvas * >();
}

void QgisApp::removeAnnotationItems()
{
if ( !mMapCanvas )
Expand Down Expand Up @@ -9595,6 +9630,30 @@ void QgisApp::embedLayers()
}
}

void QgisApp::newMapCanvas()
{
int i = 1;

bool existing = true;
QList< QgsMapCanvas * > existingCanvases = mapCanvases();
QString name;
while ( existing )
{
name = tr( "Map %1" ).arg( i++ );
existing = false;
Q_FOREACH ( QgsMapCanvas *canvas, existingCanvases )
{
if ( canvas->objectName() == name )
{
existing = true;
break;
}
}
}

createNewMapCanvas( name );
}

void QgisApp::setExtent( const QgsRectangle &rect )
{
mMapCanvas->setExtent( rect );
Expand Down
13 changes: 13 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -231,6 +231,14 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Get the mapcanvas object from the app
QgsMapCanvas *mapCanvas();

/**
* Returns a list of all map canvases open in the app.
*/
QList< QgsMapCanvas * > mapCanvases();

//! Create a new map canvas with the specified unique \a name
QgsMapCanvas *createNewMapCanvas( const QString &name );

//! Return the messageBar object which allows displaying unobtrusive messages to the user.
QgsMessageBar *messageBar();

Expand Down Expand Up @@ -1024,6 +1032,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void showAlignRasterTool();
void embedLayers();

//! Creates a new map canvas view
void newMapCanvas();

//! Create a new empty vector layer
void newVectorLayer();
//! Create a new memory layer
Expand Down Expand Up @@ -1513,6 +1524,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

//! Returns all annotation items in the canvas
QList<QgsMapCanvasAnnotationItem *> annotationItems();

//! Removes annotation items in the canvas
void removeAnnotationItems();

Expand Down Expand Up @@ -1754,6 +1766,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QSplashScreen *mSplash = nullptr;
//! list of recently opened/saved project files
QList<QgsWelcomePageItemsModel::RecentProjectData> mRecentProjects;

//! Print composers of this project, accessible by id string
QSet<QgsComposer *> mPrintComposers;
//! QGIS-internal vector feature clipboard
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgisappinterface.cpp
Expand Up @@ -331,6 +331,16 @@ QgsMapCanvas *QgisAppInterface::mapCanvas()
return qgis->mapCanvas();
}

QList<QgsMapCanvas *> QgisAppInterface::mapCanvases()
{
return qgis->mapCanvases();
}

QgsMapCanvas *QgisAppInterface::createNewMapCanvas( const QString &name )
{
return qgis->createNewMapCanvas( name );
}

QgsLayerTreeMapCanvasBridge *QgisAppInterface::layerTreeCanvasBridge()
{
return qgis->layerTreeCanvasBridge();
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgisappinterface.h
Expand Up @@ -179,6 +179,9 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
//! Return a pointer to the map canvas used by qgisapp
QgsMapCanvas *mapCanvas() override;

QList< QgsMapCanvas * > mapCanvases() override;
QgsMapCanvas *createNewMapCanvas( const QString &name ) override;

/**
* Returns a pointer to the layer tree canvas bridge
*
Expand Down
12 changes: 12 additions & 0 deletions src/gui/qgisinterface.h
Expand Up @@ -104,6 +104,18 @@ class GUI_EXPORT QgisInterface : public QObject
*/
virtual bool removeCustomActionForLayerType( QAction *action ) = 0;

/**
* Returns a list of all map canvases open in the app.
* @note added in QGIS 3.0
*/
virtual QList< QgsMapCanvas * > mapCanvases() = 0;

/**
* Create a new map canvas with the specified unique \a name.
* @note added in QGIS 3.0
*/
virtual QgsMapCanvas *createNewMapCanvas( const QString &name ) = 0;

public slots: // TODO: do these functions really need to be slots?

/* Exposed functions */
Expand Down
18 changes: 18 additions & 0 deletions src/ui/qgisapp.ui
Expand Up @@ -53,7 +53,9 @@
<addaction name="mActionDxfExport"/>
<addaction name="mActionDwgImport"/>
<addaction name="separator"/>
<addaction name="mActionNewMapCanvas"/>
<addaction name="mActionSnappingOptions"/>
<addaction name="separator"/>
<addaction name="mActionNewPrintComposer"/>
<addaction name="mActionShowComposerManager"/>
<addaction name="mPrintComposersMenu"/>
Expand Down Expand Up @@ -332,6 +334,7 @@
<addaction name="mActionOpenProject"/>
<addaction name="mActionSaveProject"/>
<addaction name="mActionSaveProjectAs"/>
<addaction name="mActionNewMapCanvas"/>
<addaction name="mActionNewPrintComposer"/>
<addaction name="mActionShowComposerManager"/>
</widget>
Expand Down Expand Up @@ -640,6 +643,21 @@
<string>Save as &amp;Image...</string>
</property>
</action>
<action name="mActionNewMapCanvas">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionNewMap.svg</normaloff>:/images/themes/default/mActionNewMap.svg</iconset>
</property>
<property name="text">
<string>New &amp;Map View</string>
</property>
<property name="toolTip">
<string>New Map View</string>
</property>
<property name="shortcut">
<string>Ctrl+M</string>
</property>
</action>
<action name="mActionNewPrintComposer">
<property name="icon">
<iconset resource="../../images/images.qrc">
Expand Down

0 comments on commit 29d77b0

Please sign in to comment.