Skip to content
Permalink
Browse files

Merge pull request #4652 from nyalldawson/extent

[FEATURE] Add option to take extent from project map layer to QgsExtentGroupBox
  • Loading branch information
nyalldawson committed Jun 1, 2017
2 parents 451bda0 + 9e14741 commit 5077e1285ed0a582c54042a3f7ad7e615049bab9
@@ -27,108 +27,132 @@ class QgsExtentGroupBox : QgsCollapsibleGroupBox
#include "qgsextentgroupbox.h"
%End
public:
explicit QgsExtentGroupBox( QWidget *parent /TransferThis/ = 0 );

enum ExtentState
{
OriginalExtent,
CurrentExtent,
UserExtent,
ProjectLayerExtent,
};

explicit QgsExtentGroupBox( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsExtentGroupBox.
%End

void setOriginalExtent( const QgsRectangle &originalExtent, const QgsCoordinateReferenceSystem &originalCrs );
%Docstring
Setup original extent - should be called as part of initialization
Sets the original extent and coordinate reference system for the widget. This should be called as part of initialization.
.. seealso:: originalExtent()
.. seealso:: originalCrs()
%End

QgsRectangle originalExtent() const;
%Docstring
Returns the original extent set for the widget.
.. seealso:: setOriginalExtent()
.. seealso:: originalCrs()
:rtype: QgsRectangle
%End
const QgsCoordinateReferenceSystem &originalCrs() const;

QgsCoordinateReferenceSystem originalCrs() const;
%Docstring
Returns the original coordinate reference system set for the widget.
.. seealso:: originalExtent()
.. seealso:: setOriginalExtent()
:rtype: QgsCoordinateReferenceSystem
%End

void setCurrentExtent( const QgsRectangle &currentExtent, const QgsCoordinateReferenceSystem &currentCrs );
%Docstring
Setup current extent - should be called as part of initialization (or whenever current extent changes)
Sets the current extent to show in the widget - should be called as part of initialization (or whenever current extent changes).
The current extent is usually set to match the current map canvas extent.
.. seealso:: currentExtent()
.. seealso:: currentCrs()
%End

QgsRectangle currentExtent() const;
%Docstring
Returns the current extent set for the widget. The current extent is usually set to match the
current map canvas extent.
.. seealso:: setCurrentExtent()
.. seealso:: currentCrs()
:rtype: QgsRectangle
%End
const QgsCoordinateReferenceSystem &currentCrs() const;

QgsCoordinateReferenceSystem currentCrs() const;
%Docstring
Returns the coordinate reference system for the current extent set for the widget. The current
extent and CRS usually reflects the map canvas extent and CRS.
.. seealso:: setCurrentExtent()
.. seealso:: currentExtent()
:rtype: QgsCoordinateReferenceSystem
%End

void setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs );
%Docstring
Should be called as part of initialization and whenever the the output CRS is changed
Sets the output CRS - may need to be used for transformation from original/current extent.
Should be called as part of initialization and whenever the the output CRS is changed.
The current extent will be reprojected into the new output CRS.
%End

QgsRectangle outputExtent() const;
%Docstring
Get the resulting extent - in output CRS coordinates
Returns the extent shown in the widget - in output CRS coordinates.
:rtype: QgsRectangle
%End

QgsExtentGroupBox::ExtentState extentState() const;
%Docstring
Returns the currently selected state for the widget's extent.
:rtype: QgsExtentGroupBox.ExtentState
%End

void setTitleBase( const QString &title );
%Docstring
Sets the base part of ``title`` of the group box (will be appended with extent state)
.. versionadded:: 2.12
.. seealso:: titleBase()
%End

QString titleBase() const;
%Docstring
Returns the base part of title of the group box (will be appended with extent state).
.. versionadded:: 2.12
.. seealso:: setTitleBase()
:rtype: str
%End

public slots:

void setOutputExtentFromOriginal();
%Docstring
set output extent to be the same as original extent (may be transformed to output CRS)
Sets the output extent to be the same as original extent (may be transformed to output CRS).
%End

void setOutputExtentFromCurrent();
%Docstring
set output extent to be the same as current extent (may be transformed to output CRS)
Sets the output extent to be the same as current extent (may be transformed to output CRS).
%End

void setOutputExtentFromUser( const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs );
%Docstring
set output extent to custom extent (may be transformed to output CRS)
Sets the output extent to a custom extent (may be transformed to output CRS).
%End

signals:
void extentChanged( const QgsRectangle &r );
void setOutputExtentFromLayer( const QgsMapLayer *layer );
%Docstring
emitted when extent is changed
Sets the output extent to match a ``layer``'s extent (may be transformed to output CRS).
.. versionadded:: 3.0
%End

protected slots:

void on_mXMinLineEdit_textEdited( const QString & );
void on_mXMaxLineEdit_textEdited( const QString & );
void on_mYMinLineEdit_textEdited( const QString & );
void on_mYMaxLineEdit_textEdited( const QString & );

void groupBoxClicked();

protected:
void setOutputExtent( const QgsRectangle &r, const QgsCoordinateReferenceSystem &srcCrs, QgsExtentGroupBox::ExtentState state );
void setOutputExtentFromLineEdit();
void updateTitle();



signals:

void extentChanged( const QgsRectangle &r );
%Docstring
Emitted when the widget's extent is changed.
%End

};

@@ -16,6 +16,11 @@

#include "qgscoordinatetransform.h"
#include "qgsrasterblock.h"
#include "qgsmaplayermodel.h"
#include "qgscsexception.h"
#include "qgsproject.h"
#include <QMenu>
#include <QAction>

QgsExtentGroupBox::QgsExtentGroupBox( QWidget *parent )
: QgsCollapsibleGroupBox( parent )
@@ -24,6 +29,11 @@ QgsExtentGroupBox::QgsExtentGroupBox( QWidget *parent )
{
setupUi( this );

mLayerMenu = new QMenu( this );
mButtonCalcFromLayer->setMenu( mLayerMenu );
connect( mLayerMenu, &QMenu::aboutToShow, this, &QgsExtentGroupBox::layerMenuAboutToShow );
mMapLayerModel = new QgsMapLayerModel( this );

mXMinLineEdit->setValidator( new QDoubleValidator( this ) );
mXMaxLineEdit->setValidator( new QDoubleValidator( this ) );
mYMinLineEdit->setValidator( new QDoubleValidator( this ) );
@@ -54,9 +64,44 @@ void QgsExtentGroupBox::setCurrentExtent( const QgsRectangle &currentExtent, con

void QgsExtentGroupBox::setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs )
{
mOutputCrs = outputCrs;
}
if ( mOutputCrs != outputCrs )
{
switch ( mExtentState )
{
case CurrentExtent:
mOutputCrs = outputCrs;
setOutputExtentFromCurrent();
break;

case OriginalExtent:
mOutputCrs = outputCrs;
setOutputExtentFromOriginal();
break;

case ProjectLayerExtent:
mOutputCrs = outputCrs;
setOutputExtentFromLayer( mExtentLayer.data() );
break;

case UserExtent:
try
{
QgsCoordinateTransform ct( mOutputCrs, outputCrs );
QgsRectangle extent = ct.transformBoundingBox( outputExtent() );
mOutputCrs = outputCrs;
setOutputExtentFromUser( extent, outputCrs );
}
catch ( QgsCsException & )
{
// can't reproject
mOutputCrs = outputCrs;
}
break;
}

}

}

void QgsExtentGroupBox::setOutputExtent( const QgsRectangle &r, const QgsCoordinateReferenceSystem &srcCrs, ExtentState state )
{
@@ -67,8 +112,16 @@ void QgsExtentGroupBox::setOutputExtent( const QgsRectangle &r, const QgsCoordin
}
else
{
QgsCoordinateTransform ct( srcCrs, mOutputCrs );
extent = ct.transformBoundingBox( r );
try
{
QgsCoordinateTransform ct( srcCrs, mOutputCrs );
extent = ct.transformBoundingBox( r );
}
catch ( QgsCsException & )
{
// can't reproject
extent = r;
}
}

mXMinLineEdit->setText( QgsRasterBlock::printValue( extent.xMinimum() ) );
@@ -111,7 +164,8 @@ void QgsExtentGroupBox::updateTitle()
case UserExtent:
msg = tr( "user defined" );
break;
default:
case ProjectLayerExtent:
msg = mExtentLayerName;
break;
}
if ( isCheckable() && !isChecked() )
@@ -121,6 +175,41 @@ void QgsExtentGroupBox::updateTitle()
setTitle( msg );
}

void QgsExtentGroupBox::layerMenuAboutToShow()
{
qDeleteAll( mMenuActions );
mMenuActions.clear();
mLayerMenu->clear();
for ( int i = 0; i < mMapLayerModel->rowCount(); ++i )
{
QModelIndex index = mMapLayerModel->index( i, 0 );
QIcon icon = qvariant_cast<QIcon>( mMapLayerModel->data( index, Qt::DecorationRole ) );
QString text = mMapLayerModel->data( index, Qt::DisplayRole ).toString();
QAction *act = new QAction( icon, text, mLayerMenu );
act->setToolTip( mMapLayerModel->data( index, Qt::ToolTipRole ).toString() );
QString layerId = mMapLayerModel->data( index, QgsMapLayerModel::LayerIdRole ).toString();
if ( mExtentState == ProjectLayerExtent && mExtentLayer && mExtentLayer->id() == layerId )
{
act->setCheckable( true );
act->setChecked( true );
}
connect( act, &QAction::triggered, this, [this, layerId]
{
setExtentToLayerExtent( layerId );
} );
mLayerMenu->addAction( act );
mMenuActions << act;
}
}

void QgsExtentGroupBox::setExtentToLayerExtent( const QString &layerId )
{
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
if ( !layer )
return;

setOutputExtentFromLayer( layer );
}

void QgsExtentGroupBox::setOutputExtentFromCurrent()
{
@@ -133,12 +222,22 @@ void QgsExtentGroupBox::setOutputExtentFromOriginal()
setOutputExtent( mOriginalExtent, mOriginalCrs, OriginalExtent );
}


void QgsExtentGroupBox::setOutputExtentFromUser( const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
{
setOutputExtent( extent, crs, UserExtent );
}

void QgsExtentGroupBox::setOutputExtentFromLayer( const QgsMapLayer *layer )
{
if ( !layer )
return;

mExtentLayer = layer;
mExtentLayerName = layer->name();

setOutputExtent( layer->extent(), layer->crs(), ProjectLayerExtent );
}

void QgsExtentGroupBox::groupBoxClicked()
{
if ( !isCheckable() )

0 comments on commit 5077e12

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