Skip to content

Commit

Permalink
Save heatmap settings between runs (fix #8078)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 26, 2013
1 parent 5c38ed8 commit 21ff620
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/plugins/heatmap/heatmap.cpp
Expand Up @@ -100,7 +100,7 @@ void Heatmap::help()
// not be enough
void Heatmap::run()
{
HeatmapGui d( mQGisIface->mainWindow(), QgisGui::ModalDialogFlags );
HeatmapGui d( mQGisIface->mainWindow(), QgisGui::ModalDialogFlags, &mSessionSettings );

if ( d.exec() == QDialog::Accepted )
{
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/heatmap/heatmap.h
Expand Up @@ -80,6 +80,8 @@ class Heatmap: public QObject, public QgisPlugin
Epanechnikov
};

QMap<QString, QVariant> mSessionSettings;

public slots:
//! init the gui
virtual void initGui();
Expand Down
130 changes: 128 additions & 2 deletions src/plugins/heatmap/heatmapgui.cpp
Expand Up @@ -36,24 +36,42 @@

//standard includes

HeatmapGui::HeatmapGui( QWidget* parent, Qt::WFlags fl )
HeatmapGui::HeatmapGui( QWidget* parent, Qt::WFlags fl, QMap<QString, QVariant>* temporarySettings )
: QDialog( parent, fl ),
mRows( 500 )
{
setupUi( this );

QgsDebugMsg( QString( "Creating Heatmap Dialog" ) );

blockAllSignals( true );

mHeatmapSessionSettings = temporarySettings;

// Adding point layers to the mInputVectorCombo
QString defaultLayer = mHeatmapSessionSettings->value( QString( "lastInputLayer" ) ).toString();
int defaultLayerIndex = 0;
bool usingLastInputLayer = false;
int currentIndex = -1;
foreach ( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( l );
if ( !vl || vl->geometryType() != QGis::Point )
continue;

currentIndex++;
mInputVectorCombo->addItem( vl->name(), vl->id() );
if ( vl->id() == defaultLayer )
{
// if this layer is the same layer as a heatmap was last generated using,
// then default to this layer
usingLastInputLayer = true;
defaultLayerIndex = currentIndex;
}
}

mInputVectorCombo->setCurrentIndex( defaultLayerIndex );

// Adding GDAL drivers with CREATE to the mFormatCombo
int myTiffIndex = -1;
int myIndex = -1;
Expand All @@ -79,11 +97,17 @@ HeatmapGui::HeatmapGui( QWidget* parent, Qt::WFlags fl )
}
}
}
mFormatCombo->setCurrentIndex( myTiffIndex );
//Restore choice of output format from last run
QSettings s;
int defaultFormatIndex = s.value( "/Heatmap/lastFormat", myTiffIndex ).toInt();
mFormatCombo->setCurrentIndex( defaultFormatIndex );

restoreSettings( usingLastInputLayer );
updateBBox();
updateSize();

blockAllSignals( false );

//finally set right the ok button
enableOrDisableOkButton();
}
Expand All @@ -92,16 +116,117 @@ HeatmapGui::~HeatmapGui()
{
}

void HeatmapGui::blockAllSignals( bool b )
{
mBufferLineEdit->blockSignals( b );
mInputVectorCombo->blockSignals( b );
mRowsSpinBox->blockSignals( b );
radiusFieldCombo->blockSignals( b );
advancedGroupBox->blockSignals( b );
kernelShapeCombo->blockSignals( b );
}

/*
*
* Private Slots
*
*/
void HeatmapGui::on_mButtonBox_accepted()
{
saveSettings();
accept();
}

void HeatmapGui::restoreSettings( bool usingLastInputLayer )
{
// Temporary settings, which are cleared on exit from QGIS
// If we are using the same layer as last run, restore layer specific settings
if ( usingLastInputLayer )
{
// Advanced checkbox
if ( mHeatmapSessionSettings->value( QString( "advancedEnabled" ) ).toBool() )
{
advancedGroupBox->setChecked( true );
}
populateFields();

// Radius controls
mBufferLineEdit->setText( mHeatmapSessionSettings->value( QString( "lastRadius" ) ).toString() );
mRadiusUnitCombo->setCurrentIndex( mHeatmapSessionSettings->value( QString( "lastRadiusUnit" ) ).toInt() );

// Raster size controls
mRows = mHeatmapSessionSettings->value( QString( "lastRows" ) ).toInt();
mRowsSpinBox->setValue( mRows );

// Data defined radius controls
if ( mHeatmapSessionSettings->value( QString( "useRadius" ) ).toBool() )
{
useRadius->setChecked( true );
radiusFieldUnitCombo->setCurrentIndex( mHeatmapSessionSettings->value( QString( "radiusFieldUnit" ) ).toInt() );
// Default to same radius field as last run
QString prevRadiusField = mHeatmapSessionSettings->value( QString( "radiusField" ) ).toString();
for ( int idx = 0; idx < radiusFieldCombo->count(); ++idx )
{
if ( radiusFieldCombo->itemText( idx ) == prevRadiusField )
{
radiusFieldCombo->setCurrentIndex( idx );
break;
}
}
}

// Data defined weight controls
if ( mHeatmapSessionSettings->value( QString( "useWeight" ) ).toBool() )
{
useWeight->setChecked( true );
// Default to same weight field as last run
QString prevWeightField = mHeatmapSessionSettings->value( QString( "weightField" ) ).toString();
for ( int idx = 0; idx < weightFieldCombo->count(); ++idx )
{
if ( weightFieldCombo->itemText( idx ) == prevWeightField )
{
weightFieldCombo->setCurrentIndex( idx );
break;
}
}
}
}
else
{
// Default to estimated radius
mBufferLineEdit->setText( QString::number( estimateRadius() ) );
}

// Kernel setting - not layer specific
if ( mHeatmapSessionSettings->value( QString( "lastKernel" ) ).toInt() )
{
kernelShapeCombo->setCurrentIndex( mHeatmapSessionSettings->value( QString( "lastKernel" ) ).toInt() );
mDecayLineEdit->setText( mHeatmapSessionSettings->value( QString( "decayRatio" ) ).toString() );
mDecayLineEdit->setEnabled( advancedGroupBox->isChecked() && kernelShapeCombo->currentIndex() == Heatmap::Triangular );
}
}

void HeatmapGui::saveSettings()
{
// Save persistant settings
QSettings s;
s.setValue( "/Heatmap/lastFormat", QVariant( mFormatCombo->currentIndex() ) );

// Store temporary settings, which only apply to this session
mHeatmapSessionSettings->insert( QString( "lastInputLayer" ), QVariant( mInputVectorCombo->itemData( mInputVectorCombo->currentIndex() ) ) );
mHeatmapSessionSettings->insert( QString( "lastRadius" ), QVariant( mBufferLineEdit->text().toDouble() ) );
mHeatmapSessionSettings->insert( QString( "lastRadiusUnit" ), QVariant( mRadiusUnitCombo->currentIndex() ) );
mHeatmapSessionSettings->insert( QString( "advancedEnabled" ), QVariant( advancedGroupBox->isChecked() ) );
mHeatmapSessionSettings->insert( QString( "lastRows" ), QVariant( mRowsSpinBox->value() ) );
mHeatmapSessionSettings->insert( QString( "lastKernel" ), QVariant( kernelShapeCombo->currentIndex() ) );
mHeatmapSessionSettings->insert( QString( "useRadius" ), QVariant( useRadius->isChecked() ) );
mHeatmapSessionSettings->insert( QString( "radiusField" ), QVariant( radiusFieldCombo->currentText() ) );
mHeatmapSessionSettings->insert( QString( "radiusFieldUnit" ), QVariant( radiusFieldUnitCombo->currentIndex() ) );
mHeatmapSessionSettings->insert( QString( "useWeight" ), QVariant( useWeight->isChecked() ) );
mHeatmapSessionSettings->insert( QString( "weightField" ), QVariant( weightFieldCombo->currentText() ) );
mHeatmapSessionSettings->insert( QString( "decayRatio" ), QVariant( mDecayLineEdit->text() ) );
}

void HeatmapGui::on_mButtonBox_rejected()
{
reject();
Expand Down Expand Up @@ -151,6 +276,7 @@ void HeatmapGui::on_advancedGroupBox_toggled( bool enabled )
// if there are layers then populate fields
populateFields();
updateBBox();
mDecayLineEdit->setEnabled( kernelShapeCombo->currentIndex() == Heatmap::Triangular );
}
}

Expand Down
25 changes: 18 additions & 7 deletions src/plugins/heatmap/heatmapgui.h
Expand Up @@ -25,7 +25,7 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
{
Q_OBJECT
public:
HeatmapGui( QWidget* parent = 0, Qt::WFlags fl = 0 );
HeatmapGui( QWidget* parent, Qt::WFlags fl, QMap<QString, QVariant>* temporarySettings );
~HeatmapGui();

// Buffer unit type
Expand Down Expand Up @@ -87,11 +87,22 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
private:
QMap<QString, QString> mExtensionMap;

QMap<QString, QVariant> *mHeatmapSessionSettings;

// bbox of layer for lineedit changes
QgsRectangle mBBox;
double mXcellsize, mYcellsize;
int mRows, mColumns;

/** Restores control values */
void restoreSettings( bool usingLastInputLayer );

/** Saves control values */
void saveSettings();

/** Blocks/unblocks signals for controls */
void blockAllSignals( bool b );

/** Function to check wether all constrains are satisfied and enable the OK button */
void enableOrDisableOkButton();

Expand All @@ -107,6 +118,12 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
/** Convert Maters value to the corresponding map units based on Layer projection */
double mapUnitsOf( double meters, QgsCoordinateReferenceSystem layerCrs );

/** Estimate a reasonable starting value for the radius field */
double estimateRadius();

inline double max( double a, double b )
{ return a > b ? a : b; }

private slots:
void on_mButtonBox_accepted();
void on_mButtonBox_rejected();
Expand All @@ -124,12 +141,6 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
void on_mInputVectorCombo_currentIndexChanged( int index );
void on_mBufferLineEdit_editingFinished();
void on_kernelShapeCombo_currentIndexChanged( int index );

/** Estimate a reasonable starting value for the radius field */
double estimateRadius();

inline double max( double a, double b )
{ return a > b ? a : b; }
};

#endif

0 comments on commit 21ff620

Please sign in to comment.