Skip to content

Commit c032d16

Browse files
pierstitusnyalldawson
authored andcommitted
Add option for adding units to color map labels
1 parent 3bd77c7 commit c032d16

3 files changed

+74
-5
lines changed

src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

+65-5
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
186186
void QgsSingleBandPseudoColorRendererWidget::autoLabel()
187187
{
188188
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
189+
QString unit = mUnitLineEdit->text();
189190
QString label = "";
190191
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
191192
QTreeWidgetItem* myCurrentItem;
@@ -202,16 +203,16 @@ void QgsSingleBandPseudoColorRendererWidget::autoLabel()
202203
{
203204
if ( i == 0)
204205
{
205-
label = "< " + myCurrentItem->text( 0 );
206+
label = "< " + myCurrentItem->text( 0 ) + unit;
206207
}
207208
else
208209
{
209-
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 );
210+
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 ) + unit;
210211
}
211212
}
212213
else
213214
{
214-
label = myCurrentItem->text( 0 );
215+
label = myCurrentItem->text( 0 ) + unit;
215216
}
216217

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

226+
void QgsSingleBandPseudoColorRendererWidget::setUnitFromLabels()
227+
{
228+
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
229+
QStringList allSuffixes;
230+
QString label = "";
231+
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
232+
QTreeWidgetItem* myCurrentItem;
233+
for ( int i = 0; i < myTopLevelItemCount; ++i )
234+
{
235+
myCurrentItem = mColormapTreeWidget->topLevelItem( i );
236+
//If the item is null or does not have a pixel values set, skip
237+
if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
238+
{
239+
continue;
240+
}
241+
242+
if ( discrete )
243+
{
244+
if ( i == 0)
245+
{
246+
label = "< " + myCurrentItem->text( 0 );
247+
}
248+
else
249+
{
250+
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( 0 ) + " - " + myCurrentItem->text( 0 );
251+
}
252+
}
253+
else
254+
{
255+
label = myCurrentItem->text( 0 );
256+
}
257+
258+
if ( myCurrentItem->text( 2 ).startsWith( label ) )
259+
{
260+
allSuffixes.append( myCurrentItem->text( 2 ).mid( label.length() ) );
261+
}
262+
}
263+
// find most common suffix
264+
QStringList suffixes = QStringList( allSuffixes );
265+
suffixes.removeDuplicates();
266+
int max = 0;
267+
QString unit;
268+
for( int i = 0; i < suffixes.count(); ++i )
269+
{
270+
int n = allSuffixes.count( suffixes[i] );
271+
if ( n > max )
272+
{
273+
max = n;
274+
unit = suffixes[i];
275+
}
276+
}
277+
// Set this suffix as unit if at least used twice
278+
if ( max >= 2 )
279+
{
280+
mUnitLineEdit->setText(unit);
281+
}
282+
autoLabel();
283+
}
284+
225285
void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
226286
{
227287
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
@@ -409,7 +469,7 @@ void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const Q
409469
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
410470
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
411471
}
412-
autoLabel();
472+
setUnitFromLabels();
413473
}
414474

415475
void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
@@ -676,7 +736,7 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen
676736
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
677737
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
678738
}
679-
autoLabel();
739+
setUnitFromLabels();
680740
mClipCheckBox->setChecked( colorRampShader->clip() );
681741
}
682742
}

src/gui/raster/qgssinglebandpseudocolorrendererwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
4848
private:
4949
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
5050
void autoLabel();
51+
void setUnitFromLabels();
5152

5253
private slots:
5354
void on_mAddEntryButton_clicked();
@@ -57,6 +58,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
5758
void on_mLoadFromBandButton_clicked();
5859
void on_mLoadFromFileButton_clicked();
5960
void on_mExportToFileButton_clicked();
61+
void on_mUnitLineEdit_textEdited( const QString & text ) { Q_UNUSED( text ); autoLabel(); }
6062
void on_mColorInterpolationComboBox_currentIndexChanged();
6163
void on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column );
6264
void mColormapTreeWidget_itemEdited( QTreeWidgetItem* item, int column );

src/ui/qgssinglebandpseudocolorrendererwidgetbase.ui

+7
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@
281281
</property>
282282
</widget>
283283
</item>
284+
<item>
285+
<widget class="QLineEdit" name="mUnitLineEdit">
286+
<property name="toolTip">
287+
<string>Unit suffix</string>
288+
</property>
289+
</widget>
290+
</item>
284291
</layout>
285292
</item>
286293
<item row="6" column="1">

0 commit comments

Comments
 (0)