Skip to content

Commit

Permalink
Add option for adding units to color map labels
Browse files Browse the repository at this point in the history
  • Loading branch information
pierstitus authored and nyalldawson committed Jun 2, 2016
1 parent 3bd77c7 commit c032d16
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
70 changes: 65 additions & 5 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -186,6 +186,7 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
void QgsSingleBandPseudoColorRendererWidget::autoLabel()
{
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
QString unit = mUnitLineEdit->text();
QString label = "";
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* myCurrentItem;
Expand All @@ -202,16 +203,16 @@ void QgsSingleBandPseudoColorRendererWidget::autoLabel()
{
if ( i == 0)
{
label = "< " + myCurrentItem->text( 0 );
label = "< " + myCurrentItem->text( 0 ) + unit;
}
else
{
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 );
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 ) + unit;
}
}
else
{
label = myCurrentItem->text( 0 );
label = myCurrentItem->text( 0 ) + unit;
}

if ( myCurrentItem->text( 2 ) == "" || myCurrentItem->text( 2 ) == label || myCurrentItem->foreground( 2 ).color() == QColor( Qt::gray ) )
Expand All @@ -222,6 +223,65 @@ void QgsSingleBandPseudoColorRendererWidget::autoLabel()
}
}

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 )
{
myCurrentItem = mColormapTreeWidget->topLevelItem( i );
//If the item is null or does not have a pixel values set, skip
if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
{
continue;
}

if ( discrete )
{
if ( i == 0)
{
label = "< " + myCurrentItem->text( 0 );
}
else
{
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 );
}
}
else
{
label = myCurrentItem->text( 0 );
}

if ( myCurrentItem->text( 2 ).startsWith( label ) )
{
allSuffixes.append( myCurrentItem->text( 2 ).mid( label.length() ) );
}
}
// find most common suffix
QStringList suffixes = QStringList( allSuffixes );
suffixes.removeDuplicates();
int max = 0;
QString unit;
for( int i = 0; i < suffixes.count(); ++i )
{
int n = allSuffixes.count( suffixes[i] );
if ( n > max )
{
max = n;
unit = suffixes[i];
}
}
// Set this suffix as unit if at least used twice
if ( max >= 2 )
{
mUnitLineEdit->setText(unit);
}
autoLabel();
}

void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
{
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
Expand Down Expand Up @@ -409,7 +469,7 @@ void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const Q
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
}
autoLabel();
setUnitFromLabels();
}

void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
Expand Down Expand Up @@ -676,7 +736,7 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
}
autoLabel();
setUnitFromLabels();
mClipCheckBox->setChecked( colorRampShader->clip() );
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.h
Expand Up @@ -48,6 +48,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
private:
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
void autoLabel();
void setUnitFromLabels();

private slots:
void on_mAddEntryButton_clicked();
Expand All @@ -57,6 +58,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
void on_mLoadFromBandButton_clicked();
void on_mLoadFromFileButton_clicked();
void on_mExportToFileButton_clicked();
void on_mUnitLineEdit_textEdited( const QString & text ) { Q_UNUSED( text ); autoLabel(); }
void on_mColorInterpolationComboBox_currentIndexChanged();
void on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column );
void mColormapTreeWidget_itemEdited( QTreeWidgetItem* item, int column );
Expand Down
7 changes: 7 additions & 0 deletions src/ui/qgssinglebandpseudocolorrendererwidgetbase.ui
Expand Up @@ -281,6 +281,13 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mUnitLineEdit">
<property name="toolTip">
<string>Unit suffix</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="1">
Expand Down

0 comments on commit c032d16

Please sign in to comment.