Skip to content
Permalink
Browse files

[FEATURE] Draw extent onto canvas in save as image/PDF dialog (#4878)

  • Loading branch information
nirvn committed Jul 18, 2017
1 parent 71b9ce2 commit 6acd326a8f02e7a15ed87fc81971683e68052155
@@ -137,6 +137,7 @@
%Include qgsmaptoolcapture.sip
%Include qgsmaptooledit.sip
%Include qgsmaptoolemitpoint.sip
%Include qgsmaptoolextent.sip
%Include qgsmaptoolidentify.sip
%Include qgsmaptoolidentifyfeature.sip
%Include qgsmaptoolpan.sip
@@ -10,6 +10,8 @@





class QgsExtentGroupBox : QgsCollapsibleGroupBox
{
%Docstring
@@ -34,6 +36,7 @@ class QgsExtentGroupBox : QgsCollapsibleGroupBox
CurrentExtent,
UserExtent,
ProjectLayerExtent,
DrawOnCanvas,
};

explicit QgsExtentGroupBox( QWidget *parent /TransferThis/ = 0 );
@@ -124,6 +127,13 @@ class QgsExtentGroupBox : QgsCollapsibleGroupBox
:rtype: str
%End

void setMapCanvas( QgsMapCanvas *canvas );
%Docstring
Sets the map canvas to enable dragging of extent on a canvas.
\param canvas the map canvas
.. versionadded:: 3.0
%End

public slots:

void setOutputExtentFromOriginal();
@@ -145,6 +155,12 @@ class QgsExtentGroupBox : QgsCollapsibleGroupBox
%Docstring
Sets the output extent to match a ``layer``'s extent (may be transformed to output CRS).
.. versionadded:: 3.0
%End

void setOutputExtentFromDrawOnCanvas();
%Docstring
Sets the output extent by dragging on the canvas.
.. versionadded:: 3.0
%End

signals:
@@ -0,0 +1,75 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsmaptoolextent.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsMapToolExtent : QgsMapTool
{
%Docstring
A map tool that emits an extent from a rectangle drawn onto the map canvas.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsmaptoolextent.h"
%End
public:

QgsMapToolExtent( QgsMapCanvas *canvas );
%Docstring
constructor
%End

virtual Flags flags() const;
virtual void canvasMoveEvent( QgsMapMouseEvent *e );
virtual void canvasPressEvent( QgsMapMouseEvent *e );
virtual void canvasReleaseEvent( QgsMapMouseEvent *e );
virtual void activate();
virtual void deactivate();

void setRatio( QSize ratio );
%Docstring
Sets a fixed aspect ratio to be used when dragging extent onto the canvas.
To unset a fixed aspect ratio, set the width and height to zero.
\param ratio aspect ratio's width and height
*
%End

QSize ratio() const;
%Docstring
Returns the current fixed aspect ratio to be used when dragging extent onto the canvas.
If the aspect ratio isn't fixed, the width and height will be set to zero.
*
:rtype: QSize
%End

QgsRectangle extent() const;
%Docstring
Returns the current extent drawn onto the canvas.
:rtype: QgsRectangle
%End

signals:

void extentChanged( const QgsRectangle &extent );
%Docstring
signal emitted on extent change
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsmaptoolextent.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -59,6 +59,7 @@ QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, QL
mExtentGroupBox->setOutputCrs( ms.destinationCrs() );
mExtentGroupBox->setCurrentExtent( mExtent, ms.destinationCrs() );
mExtentGroupBox->setOutputExtentFromCurrent();
mExtentGroupBox->setMapCanvas( mapCanvas );

mScaleWidget->setScale( ms.scale() );
mScaleWidget->setMapCanvas( mMapCanvas );
@@ -283,6 +283,7 @@ SET(QGIS_GUI_SRCS
qgsmaptoolcapture.cpp
qgsmaptooledit.cpp
qgsmaptoolemitpoint.cpp
qgsmaptoolextent.cpp
qgsmaptoolidentify.cpp
qgsmaptoolidentifyfeature.cpp
qgsmaptoolpan.cpp
@@ -437,6 +438,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsmaptoolcapture.h
qgsmaptooledit.h
qgsmaptoolemitpoint.h
qgsmaptoolextent.h
qgsmaptoolidentify.h
qgsmaptoolidentifyfeature.h
qgsmaptoolpan.h
@@ -12,13 +12,17 @@
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsextentgroupbox.h"

#include "qgslogger.h"
#include "qgscoordinatetransform.h"
#include "qgsrasterblock.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayermodel.h"
#include "qgsexception.h"
#include "qgsproject.h"

#include <QMenu>
#include <QAction>

@@ -40,13 +44,15 @@ QgsExtentGroupBox::QgsExtentGroupBox( QWidget *parent )
mYMaxLineEdit->setValidator( new QDoubleValidator( this ) );

mOriginalExtentButton->setVisible( false );
mButtonDrawOnCanvas->setVisible( false );

connect( mCurrentExtentButton, &QAbstractButton::clicked, this, &QgsExtentGroupBox::setOutputExtentFromCurrent );
connect( mOriginalExtentButton, &QAbstractButton::clicked, this, &QgsExtentGroupBox::setOutputExtentFromOriginal );
connect( mButtonDrawOnCanvas, &QAbstractButton::clicked, this, &QgsExtentGroupBox::setOutputExtentFromDrawOnCanvas );

connect( this, &QGroupBox::clicked, this, &QgsExtentGroupBox::groupBoxClicked );
}


void QgsExtentGroupBox::setOriginalExtent( const QgsRectangle &originalExtent, const QgsCoordinateReferenceSystem &originalCrs )
{
mOriginalExtent = originalExtent;
@@ -83,6 +89,11 @@ void QgsExtentGroupBox::setOutputCrs( const QgsCoordinateReferenceSystem &output
setOutputExtentFromLayer( mExtentLayer.data() );
break;

case DrawOnCanvas:
mOutputCrs = outputCrs;
extentDrawn( outputExtent() );
break;

case UserExtent:
try
{
@@ -167,6 +178,9 @@ void QgsExtentGroupBox::updateTitle()
case ProjectLayerExtent:
msg = mExtentLayerName;
break;
case DrawOnCanvas:
msg = tr( "drawn on canvas" );
break;
}
if ( isCheckable() && !isChecked() )
msg = tr( "none" );
@@ -213,7 +227,17 @@ void QgsExtentGroupBox::setExtentToLayerExtent( const QString &layerId )

void QgsExtentGroupBox::setOutputExtentFromCurrent()
{
setOutputExtent( mCurrentExtent, mCurrentCrs, CurrentExtent );
if ( mCanvas )
{
// Use unrotated visible extent to insure output size and scale matches canvas
QgsMapSettings ms = mCanvas->mapSettings();
ms.setRotation( 0 );
setOutputExtent( ms.visibleExtent(), ms.destinationCrs(), CurrentExtent );
}
else
{
setOutputExtent( mCurrentExtent, mCurrentCrs, CurrentExtent );
}
}


@@ -238,6 +262,34 @@ void QgsExtentGroupBox::setOutputExtentFromLayer( const QgsMapLayer *layer )
setOutputExtent( layer->extent(), layer->crs(), ProjectLayerExtent );
}

void QgsExtentGroupBox::setOutputExtentFromDrawOnCanvas()
{
if ( mCanvas )
{
mMapToolPrevious = mCanvas->mapTool();
if ( !mMapToolExtent )
{
mMapToolExtent.reset( new QgsMapToolExtent( mCanvas ) );
connect( mMapToolExtent.get(), &QgsMapToolExtent::extentChanged, this, &QgsExtentGroupBox::extentDrawn );
connect( mMapToolExtent.get(), &QgsMapTool::deactivated, this, [ = ]
{
window()->setVisible( true );
mMapToolPrevious = nullptr;
} );
}
mCanvas->setMapTool( mMapToolExtent.get() );
window()->setVisible( false );
}
}

void QgsExtentGroupBox::extentDrawn( const QgsRectangle &extent )
{
setOutputExtent( extent, mCanvas->mapSettings().destinationCrs(), DrawOnCanvas );
mCanvas->setMapTool( mMapToolPrevious );
window()->setVisible( true );
mMapToolPrevious = nullptr;
}

void QgsExtentGroupBox::groupBoxClicked()
{
if ( !isCheckable() )
@@ -269,3 +321,16 @@ QString QgsExtentGroupBox::titleBase() const
{
return mTitleBase;
}

void QgsExtentGroupBox::setMapCanvas( QgsMapCanvas *canvas )
{
if ( canvas )
{
mCanvas = canvas;
mButtonDrawOnCanvas->setVisible( true );
}
else
{
mButtonDrawOnCanvas->setVisible( false );
}
}
@@ -12,10 +12,13 @@
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSEXTENTGROUPBOX_H
#define QGSEXTENTGROUPBOX_H

#include "qgscollapsiblegroupbox.h"
#include "qgsmaptool.h"
#include "qgsmaptoolextent.h"
#include "qgis.h"

#include "ui_qgsextentgroupboxwidget.h"
@@ -24,6 +27,8 @@
#include "qgsrectangle.h"
#include "qgis_gui.h"

#include <memory>

class QgsCoordinateReferenceSystem;
class QgsMapLayerModel;
class QgsMapLayer;
@@ -52,6 +57,7 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
CurrentExtent, //!< Map canvas extent
UserExtent, //!< Extent manually entered/modified by the user
ProjectLayerExtent, //!< Extent taken from a layer within the project
DrawOnCanvas, //!< Extent taken from a rectangled drawn onto the map canvas
};

/**
@@ -135,6 +141,13 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
*/
QString titleBase() const;

/**
* Sets the map canvas to enable dragging of extent on a canvas.
* \param canvas the map canvas
* \since QGIS 3.0
*/
void setMapCanvas( QgsMapCanvas *canvas );

public slots:

/**
@@ -158,6 +171,12 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
*/
void setOutputExtentFromLayer( const QgsMapLayer *layer );

/**
* Sets the output extent by dragging on the canvas.
* \since QGIS 3.0
*/
void setOutputExtentFromDrawOnCanvas();

signals:

/**
@@ -175,6 +194,8 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
void groupBoxClicked();
void layerMenuAboutToShow();

void extentDrawn( const QgsRectangle &extent );

private:
void setOutputExtent( const QgsRectangle &r, const QgsCoordinateReferenceSystem &srcCrs, QgsExtentGroupBox::ExtentState state );
void setOutputExtentFromLineEdit();
@@ -199,6 +220,10 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
QPointer< const QgsMapLayer > mExtentLayer;
QString mExtentLayerName;

std::unique_ptr< QgsMapToolExtent > mMapToolExtent;
QPointer< QgsMapTool > mMapToolPrevious = nullptr;
QgsMapCanvas *mCanvas = nullptr;

void setExtentToLayerExtent( const QString &layerId );

};

0 comments on commit 6acd326

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