Skip to content

Commit

Permalink
[FEATURE] Draw extent onto canvas in save as image/PDF dialog (#4878)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jul 18, 2017
1 parent 71b9ce2 commit 6acd326
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions python/gui/qgsextentgroupbox.sip
Expand Up @@ -10,6 +10,8 @@





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

explicit QgsExtentGroupBox( QWidget *parent /TransferThis/ = 0 );
Expand Down Expand Up @@ -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();
Expand All @@ -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:
Expand Down
75 changes: 75 additions & 0 deletions python/gui/qgsmaptoolextent.sip
@@ -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 *
************************************************************************/
1 change: 1 addition & 0 deletions src/app/qgsmapsavedialog.cpp
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -283,6 +283,7 @@ SET(QGIS_GUI_SRCS
qgsmaptoolcapture.cpp
qgsmaptooledit.cpp
qgsmaptoolemitpoint.cpp
qgsmaptoolextent.cpp
qgsmaptoolidentify.cpp
qgsmaptoolidentifyfeature.cpp
qgsmaptoolpan.cpp
Expand Down Expand Up @@ -437,6 +438,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsmaptoolcapture.h
qgsmaptooledit.h
qgsmaptoolemitpoint.h
qgsmaptoolextent.h
qgsmaptoolidentify.h
qgsmaptoolidentifyfeature.h
qgsmaptoolpan.h
Expand Down
69 changes: 67 additions & 2 deletions src/gui/qgsextentgroupbox.cpp
Expand Up @@ -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>

Expand All @@ -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;
Expand Down Expand Up @@ -83,6 +89,11 @@ void QgsExtentGroupBox::setOutputCrs( const QgsCoordinateReferenceSystem &output
setOutputExtentFromLayer( mExtentLayer.data() );
break;

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

case UserExtent:
try
{
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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 );
}
}


Expand All @@ -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() )
Expand Down Expand Up @@ -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 );
}
}
25 changes: 25 additions & 0 deletions src/gui/qgsextentgroupbox.h
Expand Up @@ -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"
Expand All @@ -24,6 +27,8 @@
#include "qgsrectangle.h"
#include "qgis_gui.h"

#include <memory>

class QgsCoordinateReferenceSystem;
class QgsMapLayerModel;
class QgsMapLayer;
Expand Down Expand Up @@ -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
};

/**
Expand Down Expand Up @@ -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:

/**
Expand All @@ -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:

/**
Expand All @@ -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();
Expand All @@ -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 );

};
Expand Down

0 comments on commit 6acd326

Please sign in to comment.