Skip to content
Permalink
Browse files

Add option for adding units to color map labels

  • Loading branch information
pierstitus authored and nyalldawson committed Apr 6, 2016
1 parent 3bd77c7 commit c032d169296d024a1c157deba51e6ecf11500eb0
@@ -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;
@@ -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 ) )
@@ -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 );
@@ -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()
@@ -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() );
}
}
@@ -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();
@@ -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 );
@@ -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">

0 comments on commit c032d16

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