Skip to content

Commit

Permalink
[raster calc] use active layer to setup dialog to harmonize
Browse files Browse the repository at this point in the history
the behavior with that of processing
  • Loading branch information
nirvn committed Feb 8, 2018
1 parent 50a103f commit a29d6d2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -5320,7 +5320,7 @@ void QgisApp::newGeoPackageLayer()

void QgisApp::showRasterCalculator()
{
QgsRasterCalcDialog d( this );
QgsRasterCalcDialog d( dynamic_cast<QgsRasterLayer *>( activeLayer() ), this );
if ( d.exec() == QDialog::Accepted )
{
//invoke analysis library
Expand Down
38 changes: 21 additions & 17 deletions src/app/qgsrastercalcdialog.cpp
Expand Up @@ -29,7 +29,7 @@
#include <QFileDialog>
#include <QFontDatabase>

QgsRasterCalcDialog::QgsRasterCalcDialog( QWidget *parent, Qt::WindowFlags f ): QDialog( parent, f )
QgsRasterCalcDialog::QgsRasterCalcDialog( QgsRasterLayer *rasterLayer, QWidget *parent, Qt::WindowFlags f ): QDialog( parent, f )
{
setupUi( this );
QgsGui::instance()->enableAutoGeometryRestore( this );
Expand Down Expand Up @@ -66,16 +66,16 @@ QgsRasterCalcDialog::QgsRasterCalcDialog( QWidget *parent, Qt::WindowFlags f ):
connect( mOrButton, &QPushButton::clicked, this, &QgsRasterCalcDialog::mOrButton_clicked );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsRasterCalcDialog::showHelp );

if ( rasterLayer && rasterLayer->dataProvider() && rasterLayer->dataProvider()->name() == QLatin1String( "gdal" ) )
{
setExtentSize( rasterLayer->width(), rasterLayer->height(), rasterLayer->extent() );
mCrsSelector->setCrs( rasterLayer->crs() );
}

//add supported output formats
insertAvailableOutputFormats();
insertAvailableRasterBands();

if ( !mAvailableRasterBands.isEmpty() )
{
//grab default crs from first raster
mCrsSelector->setCrs( mAvailableRasterBands.at( 0 ).raster->crs() );
}

mExpressionTextEdit->setCurrentFont( QFontDatabase::systemFont( QFontDatabase::FixedFont ) );
}

Expand Down Expand Up @@ -148,27 +148,31 @@ QVector<QgsRasterCalculatorEntry> QgsRasterCalcDialog::rasterEntries() const
return entries;
}

void QgsRasterCalcDialog::setExtentSize( int width, int height, QgsRectangle bbox )
{
mNColumnsSpinBox->setValue( width );
mNRowsSpinBox->setValue( height );
mXMinSpinBox->setValue( bbox.xMinimum() );
mXMaxSpinBox->setValue( bbox.xMaximum() );
mYMinSpinBox->setValue( bbox.yMinimum() );
mYMaxSpinBox->setValue( bbox.yMaximum() );
mExtentSizeSet = true;
}

void QgsRasterCalcDialog::insertAvailableRasterBands()
{
const QMap<QString, QgsMapLayer *> &layers = QgsProject::instance()->mapLayers();
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();

bool firstLayer = true;
for ( ; layerIt != layers.constEnd(); ++layerIt )
{
QgsRasterLayer *rlayer = dynamic_cast<QgsRasterLayer *>( layerIt.value() );
if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->name() == QLatin1String( "gdal" ) )
{
if ( firstLayer ) //set bounding box / resolution of output to the values of the first possible input layer
if ( !mExtentSizeSet ) //set bounding box / resolution of output to the values of the first possible input layer
{
mNColumnsSpinBox->setValue( rlayer->width() );
mNRowsSpinBox->setValue( rlayer->height() );
QgsRectangle bbox = rlayer->extent();
mXMinSpinBox->setValue( bbox.xMinimum() );
mXMaxSpinBox->setValue( bbox.xMaximum() );
mYMinSpinBox->setValue( bbox.yMinimum() );
mYMaxSpinBox->setValue( bbox.yMaximum() );
firstLayer = false;
setExtentSize( rlayer->width(), rlayer->height(), rlayer->extent() );
mCrsSelector->setCrs( rlayer->crs() );
}
//get number of bands
for ( int i = 0; i < rlayer->bandCount(); ++i )
Expand Down
9 changes: 7 additions & 2 deletions src/app/qgsrastercalcdialog.h
Expand Up @@ -28,7 +28,7 @@ class APP_EXPORT QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcD
{
Q_OBJECT
public:
QgsRasterCalcDialog( QWidget *parent = nullptr, Qt::WindowFlags f = nullptr );
QgsRasterCalcDialog( QgsRasterLayer *rasterLayer = nullptr, QWidget *parent = nullptr, Qt::WindowFlags f = nullptr );

QString formulaString() const;
QString outputFile() const;
Expand Down Expand Up @@ -83,7 +83,10 @@ class APP_EXPORT QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcD
void mOrButton_clicked();

private:
//insert available GDAL drivers that support the create() option
//! Sets the extent and size of the output
void setExtentSize( int width, int height, QgsRectangle bbox );

// Insert available GDAL drivers that support the create() option
void insertAvailableOutputFormats();
//! Accesses the available raster layers/bands from the layer registry
void insertAvailableRasterBands();
Expand All @@ -99,6 +102,8 @@ class APP_EXPORT QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcD
QMap<QString, QString> mDriverExtensionMap;

QList<QgsRasterCalculatorEntry> mAvailableRasterBands;

bool mExtentSizeSet = false;
};

#endif // QGSRASTERCALCDIALOG_H

0 comments on commit a29d6d2

Please sign in to comment.