Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement comments from nyalldawson
  • Loading branch information
pierstitus authored and nyalldawson committed Jun 2, 2016
1 parent 2556c7c commit c84dc8d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 68 deletions.
4 changes: 3 additions & 1 deletion python/gui/raster/qgsrasterminmaxwidget.sip
Expand Up @@ -10,8 +10,10 @@ class QgsRasterMinMaxWidget: QWidget
void setExtent( const QgsRectangle & theExtent );

void setBands( const QList<int> & theBands );

/** Return the extent selected by the user.
Either an empty extent for 'full' or the current visible extent. */
QgsRectangle extent();
/** Return the selected sample size. */
int sampleSize();

// Load programmaticaly with current values
Expand Down
7 changes: 7 additions & 0 deletions python/gui/raster/qgssinglebandpseudocolorrendererwidget.sip
Expand Up @@ -25,16 +25,23 @@ class QgsSingleBandPseudoColorRendererWidget : QgsRasterRendererWidget

};

/**
* Custom QTreeWidgetItem with extra signal when item is edited and numeric sorting.
*/
class QgsTreeWidgetItem: QObject, QTreeWidgetItem
{
%TypeHeaderCode
#include <qgssinglebandpseudocolorrendererwidget.h>
%End
public:
/** Constructs a tree widget item of the specified type and appends it to the items in the given parent. */
explicit QgsTreeWidgetItem( QTreeWidget * parent, int type = Type );

/** Sets the value for the item's column and role to the given value. */
virtual void setData( int column, int role, const QVariant & value );
virtual bool operator< ( const QTreeWidgetItem & other ) const;

signals:
/** This signal is emitted when the contents of the column in the specified item has been edited by the user. */
void itemEdited( QTreeWidgetItem* item, int column );
};
2 changes: 1 addition & 1 deletion src/gui/raster/qgsrasterminmaxwidget.h
Expand Up @@ -34,7 +34,7 @@ class GUI_EXPORT QgsRasterMinMaxWidget: public QWidget, private Ui::QgsRasterMin
void setBands( const QList<int> & theBands ) { mBands = theBands; }
/** Return the extent selected by the user.
Either an empty extent for 'full' or the current visible extent. */
QgsRectangle extent() { QgsRectangle myExtent; return mCurrentExtentRadioButton->isChecked() ? mExtent : myExtent; }
QgsRectangle extent() { return mCurrentExtentRadioButton->isChecked() ? mExtent : QgsRectangle(); }
/** Return the selected sample size. */
int sampleSize() { return mEstimateRadioButton->isChecked() ? 250000 : 0; }

Expand Down
130 changes: 65 additions & 65 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -73,7 +73,7 @@ QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget(

setupUi( this );

mColormapTreeWidget->setColumnWidth( 1, 50 );
mColormapTreeWidget->setColumnWidth( ColorColumn, 50 );

QString defaultPalette = settings.value( "/Raster/defaultPalette", "Spectral" ).toString();

Expand Down Expand Up @@ -169,9 +169,9 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
continue;
}
QgsColorRampShader::ColorRampItem newColorRampItem;
newColorRampItem.value = currentItem->text( 0 ).toDouble();
newColorRampItem.color = currentItem->background( 1 ).color();
newColorRampItem.label = currentItem->text( 2 );
newColorRampItem.value = currentItem->text( ValueColumn ).toDouble();
newColorRampItem.color = currentItem->background( ColorColumn ).color();
newColorRampItem.label = currentItem->text( LabelColumn );
colorRampItems.append( newColorRampItem );
}
// sort the shader items
Expand Down Expand Up @@ -209,14 +209,14 @@ void QgsSingleBandPseudoColorRendererWidget::autoLabel()
{
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
QString unit = mUnitLineEdit->text();
QString label = "";
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* myCurrentItem;
for ( int i = 0; i < myTopLevelItemCount; ++i )
QString label;
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* currentItem;
for ( int i = 0; i < topLevelItemCount; ++i )
{
myCurrentItem = mColormapTreeWidget->topLevelItem( i );
currentItem = mColormapTreeWidget->topLevelItem( i );
//If the item is null or does not have a pixel values set, skip
if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
if ( !currentItem || currentItem->text( ValueColumn ).isEmpty() )
{
continue;
}
Expand All @@ -225,22 +225,22 @@ void QgsSingleBandPseudoColorRendererWidget::autoLabel()
{
if ( i == 0 )
{
label = "< " + myCurrentItem->text( 0 ) + unit;
label = "<= " + currentItem->text( ValueColumn ) + unit;
}
else
{
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 ) + unit;
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) + " - " + currentItem->text( ValueColumn ) + unit;
}
}
else
{
label = myCurrentItem->text( 0 ) + unit;
label = currentItem->text( ValueColumn ) + unit;
}

if ( myCurrentItem->text( 2 ) == "" || myCurrentItem->text( 2 ) == label || myCurrentItem->foreground( 2 ).color() == QColor( Qt::gray ) )
if ( currentItem->text( LabelColumn ).isEmpty() || currentItem->text( LabelColumn ) == label || currentItem->foreground( LabelColumn ).color() == QColor( Qt::gray ) )
{
myCurrentItem->setText( 2, label );
myCurrentItem->setForeground( 2, QBrush( QColor( Qt::gray ) ) );
currentItem->setText( LabelColumn, label );
currentItem->setForeground( LabelColumn, QBrush( QColor( Qt::gray ) ) );
}
}
}
Expand All @@ -250,14 +250,14 @@ void QgsSingleBandPseudoColorRendererWidget::setUnitFromLabels()
{
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
QStringList allSuffixes;
QString label = "";
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* myCurrentItem;
for ( int i = 0; i < myTopLevelItemCount; ++i )
QString label;
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* currentItem;
for ( int i = 0; i < topLevelItemCount; ++i )
{
myCurrentItem = mColormapTreeWidget->topLevelItem( i );
currentItem = mColormapTreeWidget->topLevelItem( i );
//If the item is null or does not have a pixel values set, skip
if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
if ( !currentItem || currentItem->text( ValueColumn ).isEmpty() )
{
continue;
}
Expand All @@ -266,21 +266,21 @@ void QgsSingleBandPseudoColorRendererWidget::setUnitFromLabels()
{
if ( i == 0 )
{
label = "< " + myCurrentItem->text( 0 );
label = "<= " + currentItem->text( ValueColumn );
}
else
{
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 );
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) + " - " + currentItem->text( ValueColumn );
}
}
else
{
label = myCurrentItem->text( 0 );
label = currentItem->text( ValueColumn );
}

if ( myCurrentItem->text( 2 ).startsWith( label ) )
if ( currentItem->text( LabelColumn ).startsWith( label ) )
{
allSuffixes.append( myCurrentItem->text( 2 ).mid( label.length() ) );
allSuffixes.append( currentItem->text( LabelColumn ).mid( label.length() ) );
}
}
// find most common suffix
Expand Down Expand Up @@ -308,13 +308,13 @@ void QgsSingleBandPseudoColorRendererWidget::setUnitFromLabels()
void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
{
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, "0" );
newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
newItem->setText( 2, "" );
newItem->setText( ValueColumn, "0" );
newItem->setBackground( ColorColumn, QBrush( QColor( Qt::magenta ) ) );
newItem->setText( LabelColumn, QString() );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
mColormapTreeWidget->sortItems( ValueColumn, Qt::AscendingOrder );
autoLabel();
emit widgetChanged();
}
Expand Down Expand Up @@ -373,13 +373,13 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
numberOfEntries = mNumberOfEntriesSpinBox->value();

int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
//QgsRasterHistogram myRasterHistogram = mRasterLayer->dataProvider()->histogram( bandNr );
//QgsRasterHistogram rasterHistogram = mRasterLayer->dataProvider()->histogram( bandNr );

double myMin = std::numeric_limits<double>::quiet_NaN();
double myMax = std::numeric_limits<double>::quiet_NaN();
double cut1 = std::numeric_limits<double>::quiet_NaN();
double cut2 = std::numeric_limits<double>::quiet_NaN();

QgsRectangle myExtent = mMinMaxWidget->extent();
int mySampleSize = mMinMaxWidget->sampleSize();
QgsRectangle extent = mMinMaxWidget->extent();
int sampleSize = mMinMaxWidget->sampleSize();

double intervalDiff;
if ( numberOfEntries > 1 )
Expand All @@ -388,14 +388,14 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
entryValues.reserve( numberOfEntries );
for ( int i = 0; i < numberOfEntries; ++i )
{
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, myMin, myMax, myExtent, mySampleSize );
entryValues.push_back( myMax );
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
}
}
else if ( numberOfEntries == 1 )
{
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 0.5, myMin, myMax, myExtent, mySampleSize );
entryValues.push_back( myMax );
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 0.5, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
}
}
else // EqualInterval
Expand Down Expand Up @@ -474,15 +474,15 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
QVector<QColor>::const_iterator color_it = entryColors.begin();

// calculate a reasonable number of decimals to display
double maxabs = log10( std::max<double>( std::fabs( max ), std::fabs( min ) ) );
int nDecimals = round( std::max<double>( 3.0 + maxabs - log10( max - min ), maxabs <= 6.0 ? maxabs + 0.49 : 0.0 ) );
double maxabs = log10( qMax( qAbs( max ), qAbs( min ) ) );
int nDecimals = qRound( qMax( 3.0 + maxabs - log10( max - min ), maxabs <= 6.0 ? maxabs + 0.49 : 0.0 ) );

for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
{
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( *value_it, 'g', nDecimals ) );
newItem->setBackground( 1, QBrush( *color_it ) );
newItem->setText( 2, "" );
newItem->setText( ValueColumn, QString::number( *value_it, 'g', nDecimals ) );
newItem->setBackground( ColorColumn, QBrush( *color_it ) );
newItem->setText( LabelColumn, QString() );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
Expand Down Expand Up @@ -521,9 +521,9 @@ void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const Q
for ( ; it != colorRampItems.constEnd(); ++it )
{
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'g' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
newItem->setText( ValueColumn, QString::number( it->value, 'g' ) );
newItem->setBackground( ColorColumn, QBrush( it->color ) );
newItem->setText( LabelColumn, it->label );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
Expand Down Expand Up @@ -685,16 +685,16 @@ void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
{
continue;
}
color = currentItem->background( 1 ).color();
outputStream << currentItem->text( 0 ).toDouble() << ',';
color = currentItem->background( ColorColumn ).color();
outputStream << currentItem->text( ValueColumn ).toDouble() << ',';
outputStream << color.red() << ',' << color.green() << ',' << color.blue() << ',' << color.alpha() << ',';
if ( currentItem->text( 2 ) == "" )
if ( currentItem->text( LabelColumn ).isEmpty() )
{
outputStream << "Color entry " << i + 1 << '\n';
}
else
{
outputStream << currentItem->text( 2 ) << '\n';
outputStream << currentItem->text( LabelColumn ) << '\n';
}
}
outputStream.flush();
Expand All @@ -721,22 +721,22 @@ void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleCl
return;
}

if ( column == 1 ) //change item color
if ( column == ColorColumn )
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QColor newColor = QgsColorDialogV2::getColor( item->background( column ).color(), this, "Change color", true );
if ( newColor.isValid() )
{
item->setBackground( 1, QBrush( newColor ) );
item->setBackground( ColorColumn, QBrush( newColor ) );
emit widgetChanged();
}
}
else
{
if ( column == 2 )
if ( column == LabelColumn )
{
// Set text color to default black, which signifies a manually edited label
item->setForeground( 2, QBrush() );
item->setForeground( LabelColumn, QBrush() );
}
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
Expand All @@ -747,12 +747,12 @@ void QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited( QTr
{
Q_UNUSED( item );

if ( column == 0 ) // item value edited
if ( column == ValueColumn )
{
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
mColormapTreeWidget->sortItems( ValueColumn, Qt::AscendingOrder );
autoLabel();
}
else if ( column == 2 ) // item label edited
else if ( column == LabelColumn )
{
// call autoLabel to fill when empty or gray out when same as autoLabel
autoLabel();
Expand Down Expand Up @@ -790,9 +790,9 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen
for ( ; it != colorRampItemList.end(); ++it )
{
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'g' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
newItem->setText( ValueColumn, QString::number( it->value, 'g' ) );
newItem->setBackground( ColorColumn, QBrush( it->color ) );
newItem->setText( LabelColumn, it->label );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
Expand All @@ -810,9 +810,9 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen

void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged( int index )
{
QList<int> myBands;
myBands.append( mBandComboBox->itemData( index ).toInt() );
mMinMaxWidget->setBands( myBands );
QList<int> bands;
bands.append( mBandComboBox->itemData( index ).toInt() );
mMinMaxWidget->setBands( bands );
}

void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
Expand Down
8 changes: 7 additions & 1 deletion src/gui/raster/qgssinglebandpseudocolorrendererwidget.h
Expand Up @@ -47,6 +47,13 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
void loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin );

private:
enum Column
{
ValueColumn = 0,
ColorColumn = 1,
LabelColumn = 2,
};

void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
void autoLabel();
void setUnitFromLabels();
Expand Down Expand Up @@ -91,7 +98,6 @@ class GUI_EXPORT QgsTreeWidgetItem: public QObject, public QTreeWidgetItem
/** Constructs a tree widget item of the specified type and appends it to the items in the given parent. */
explicit QgsTreeWidgetItem( QTreeWidget * parent, int type = Type ) : QTreeWidgetItem( parent, type ) {}

public:
/** Sets the value for the item's column and role to the given value. */
virtual void setData( int column, int role, const QVariant & value );
virtual bool operator< ( const QTreeWidgetItem & other ) const;
Expand Down

0 comments on commit c84dc8d

Please sign in to comment.