Skip to content
Permalink
Browse files

[FEATURE] Add option to take extent from project map layer

to QgsExtentGroupBox

This allows matching another layer's extent in the save vector/
raster layer dialog, among others

Fix #16357
  • Loading branch information
nyalldawson committed May 31, 2017
1 parent 6eb3570 commit 4e4f232ad49c0600c8b05747e51cbc9f47c01607
Showing with 143 additions and 61 deletions.
  1. +2 −0 python/gui/qgsextentgroupbox.sip
  2. +58 −4 src/gui/qgsextentgroupbox.cpp
  3. +17 −0 src/gui/qgsextentgroupbox.h
  4. +66 −57 src/ui/qgsextentgroupboxwidget.ui
@@ -34,6 +34,7 @@ class QgsExtentGroupBox : QgsCollapsibleGroupBox
OriginalExtent,
CurrentExtent,
UserExtent,
ProjectLayerExtent,
};

void setOriginalExtent( const QgsRectangle &originalExtent, const QgsCoordinateReferenceSystem &originalCrs );
@@ -130,6 +131,7 @@ emitted when extent is changed




};

/************************************************************************
@@ -16,6 +16,10 @@

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

QgsExtentGroupBox::QgsExtentGroupBox( QWidget *parent )
: QgsCollapsibleGroupBox( parent )
@@ -24,6 +28,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 ) );
@@ -67,8 +76,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 +128,8 @@ void QgsExtentGroupBox::updateTitle()
case UserExtent:
msg = tr( "user defined" );
break;
default:
case ProjectLayerExtent:
msg = mExtentLayerName;
break;
}
if ( isCheckable() && !isChecked() )
@@ -121,6 +139,43 @@ 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 );
QAction *act = new QAction( qvariant_cast<QIcon>( mMapLayerModel->data( index, Qt::DecorationRole ) ),
mMapLayerModel->data( index, Qt::DisplayRole ).toString() );
act->setToolTip( mMapLayerModel->data( index, Qt::ToolTipRole ).toString() );
QString layerId = mMapLayerModel->data( index, QgsMapLayerModel::LayerIdRole ).toString();
if ( mExtentState == ProjectLayerExtent && mExtentLayerId == 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;

mExtentLayerId = layerId;
mExtentLayerName = layer->name();

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

void QgsExtentGroupBox::setOutputExtentFromCurrent()
{
@@ -133,7 +188,6 @@ void QgsExtentGroupBox::setOutputExtentFromOriginal()
setOutputExtent( mOriginalExtent, mOriginalCrs, OriginalExtent );
}


void QgsExtentGroupBox::setOutputExtentFromUser( const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
{
setOutputExtent( extent, crs, UserExtent );
@@ -25,6 +25,7 @@
#include "qgis_gui.h"

class QgsCoordinateReferenceSystem;
class QgsMapLayerModel;

/** \ingroup gui
* Collapsible group box for configuration of extent, typically for a save operation.
@@ -49,6 +50,7 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
OriginalExtent, //!< Layer's extent
CurrentExtent, //!< Map canvas extent
UserExtent, //!< Extent manually entered/modified by the user
ProjectLayerExtent, //!< Extent taken from a layer within the project
};

//! Setup original extent - should be called as part of initialization
@@ -119,6 +121,21 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::

QgsRectangle mOriginalExtent;
QgsCoordinateReferenceSystem mOriginalCrs;

private slots:

void layerMenuAboutToShow();

private:

QMenu *mLayerMenu = nullptr;
QgsMapLayerModel *mMapLayerModel = nullptr;
QList< QAction * > mMenuActions;
QString mExtentLayerId;
QString mExtentLayerName;

void setExtentToLayerExtent( const QString &layerId );

};

#endif // QGSEXTENTGROUPBOX_H
123 src/ui/qgsextentgroupboxwidget.ui 100644 → 100755
@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>388</width>
<width>380</width>
<height>164</height>
</rect>
</property>
@@ -16,68 +16,81 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_4">
<item row="1" column="0">
<widget class="QLabel" name="mXMinLabel">
<item row="0" column="1">
<widget class="QLabel" name="mYMaxLabel">
<property name="text">
<string>West</string>
<string>North</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="mXMinLineEdit"/>
<item row="0" column="2">
<widget class="QLineEdit" name="mYMaxLineEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="mXMaxLabel">
<item row="2" column="1">
<widget class="QLabel" name="mYMinLabel">
<property name="text">
<string>East</string>
<string>South</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLineEdit" name="mXMaxLineEdit"/>
<item row="2" column="2">
<widget class="QLineEdit" name="mYMinLineEdit"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="mYMaxLabel">
<item row="1" column="1">
<widget class="QLineEdit" name="mXMinLineEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mXMinLabel">
<property name="text">
<string>North</string>
<string>West</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="mYMaxLineEdit">
<property name="text">
<string/>
</property>
</widget>
<item row="1" column="3">
<widget class="QLineEdit" name="mXMaxLineEdit"/>
</item>
<item row="2" column="1">
<widget class="QLabel" name="mYMinLabel">
<item row="1" column="2">
<widget class="QLabel" name="mXMaxLabel">
<property name="text">
<string>South</string>
<string>East</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QLineEdit" name="mYMinLineEdit"/>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="4">
<widget class="QPushButton" name="mCurrentExtentButton">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Map view extent</string>
</property>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -90,60 +103,57 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="mOriginalExtentButton">
<property name="minimumSize">
<item row="0" column="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>150</width>
<height>0</height>
<width>40</width>
<height>20</height>
</size>
</property>
<property name="text">
<string>Layer extent</string>
</property>
</widget>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_4">
<item row="0" column="5">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="mCurrentExtentButton">
<item row="0" column="2">
<widget class="QPushButton" name="mOriginalExtentButton">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Map view extent</string>
<string>Current layer extent</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<item row="1" column="2">
<widget class="QPushButton" name="mButtonCalcFromLayer">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<property name="text">
<string>Calculate from layer...</string>
</property>
</spacer>
</widget>
</item>
</layout>
</widget>
@@ -155,7 +165,6 @@
<tabstop>mXMinLineEdit</tabstop>
<tabstop>mXMaxLineEdit</tabstop>
<tabstop>mYMinLineEdit</tabstop>
<tabstop>mOriginalExtentButton</tabstop>
<tabstop>mCurrentExtentButton</tabstop>
</tabstops>
<resources/>

0 comments on commit 4e4f232

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