@@ -39,6 +39,13 @@ void QgsTreeWidgetItem::setData( int column, int role, const QVariant & value )
39
39
}
40
40
}
41
41
42
+ // override < operator to allow sorting
43
+ bool QgsTreeWidgetItem::operator <( const QTreeWidgetItem & other ) const
44
+ {
45
+ // could use treeWidget()->sortColumn() instead of 0
46
+ return text ( 0 ).toDouble () < other.text ( 0 ).toDouble ();
47
+ }
48
+
42
49
QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget ( QgsRasterLayer* layer, const QgsRectangle &extent )
43
50
: QgsRasterRendererWidget( layer, extent )
44
51
, mMinMaxWidget( nullptr )
@@ -223,6 +230,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
223
230
newItem->setText ( 2 , " " );
224
231
connect ( newItem, SIGNAL ( itemEdited ( QTreeWidgetItem*, int ) ),
225
232
this , SLOT ( mColormapTreeWidget_itemEdited ( QTreeWidgetItem*, int ) ) );
233
+ mColormapTreeWidget ->sortItems ( 0 , Qt::AscendingOrder );
226
234
autoLabel ();
227
235
emit widgetChanged ();
228
236
}
@@ -243,56 +251,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mNumberOfEntriesSpinBox_valueCha
243
251
244
252
void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked ()
245
253
{
246
- bool inserted = false ;
247
- int myCurrentIndex = 0 ;
248
- int myTopLevelItemCount = mColormapTreeWidget ->topLevelItemCount ();
249
- QTreeWidgetItem* myCurrentItem;
250
- QList<QgsColorRampShader::ColorRampItem> myColorRampItems;
251
- for ( int i = 0 ; i < myTopLevelItemCount; ++i )
252
- {
253
- myCurrentItem = mColormapTreeWidget ->topLevelItem ( i );
254
- // If the item is null or does not have a pixel values set, skip
255
- if ( !myCurrentItem || myCurrentItem->text ( 0 ) == " " )
256
- {
257
- continue ;
258
- }
259
-
260
- // Create a copy of the new Color ramp Item
261
- QgsColorRampShader::ColorRampItem myNewColorRampItem;
262
- myNewColorRampItem.value = myCurrentItem->text ( 0 ).toDouble ();
263
- myNewColorRampItem.color = myCurrentItem->background ( 1 ).color ();
264
- myNewColorRampItem.label = myCurrentItem->text ( 2 );
265
-
266
- // Simple insertion sort - speed is not a huge factor here
267
- inserted = false ;
268
- myCurrentIndex = 0 ;
269
- while ( !inserted )
270
- {
271
- if ( 0 == myColorRampItems.size () || myCurrentIndex == myColorRampItems.size () )
272
- {
273
- myColorRampItems.push_back ( myNewColorRampItem );
274
- inserted = true ;
275
- }
276
- else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
277
- {
278
- myColorRampItems.insert ( myCurrentIndex, myNewColorRampItem );
279
- inserted = true ;
280
- }
281
- else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size () - 1 )
282
- {
283
- myColorRampItems.push_back ( myNewColorRampItem );
284
- inserted = true ;
285
- }
286
- else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1 ].value > myNewColorRampItem.value )
287
- {
288
- myColorRampItems.insert ( myCurrentIndex + 1 , myNewColorRampItem );
289
- inserted = true ;
290
- }
291
- myCurrentIndex++;
292
- }
293
- }
294
- populateColormapTreeWidget ( myColorRampItems );
295
- emit widgetChanged ();
254
+ mColormapTreeWidget ->sortItems ( 0 , Qt::AscendingOrder );
296
255
}
297
256
298
257
void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked ()
@@ -672,6 +631,7 @@ void QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited( QTr
672
631
673
632
if ( column == 0 ) // item value edited
674
633
{
634
+ mColormapTreeWidget ->sortItems ( 0 , Qt::AscendingOrder );
675
635
autoLabel ();
676
636
}
677
637
else if ( column == 2 ) // item label edited
0 commit comments