Skip to content

Commit 20edfed

Browse files
author
wonder
committed
Sorting of raster color ramp items: better and faster.
... why to use insert sort if we have quicksort for free ...? :) (was taking minutes for one raster layer) git-svn-id: http://svn.osgeo.org/qgis/trunk@10604 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent f86d1d5 commit 20edfed

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

src/app/qgsrasterlayerproperties.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,6 @@ void QgsRasterLayerProperties::apply()
13711371
//iterate through mColormapTreeWidget and set colormap info of layer
13721372
QList<QgsColorRampShader::ColorRampItem> myColorRampItems;
13731373

1374-
bool inserted = false;
1375-
int myCurrentIndex = 0;
13761374
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
13771375
QTreeWidgetItem* myCurrentItem;
13781376
for ( int i = 0; i < myTopLevelItemCount; ++i )
@@ -1386,35 +1384,13 @@ void QgsRasterLayerProperties::apply()
13861384
myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble();
13871385
myNewColorRampItem.color = myCurrentItem->background( 1 ).color();
13881386
myNewColorRampItem.label = myCurrentItem->text( 2 );
1389-
1390-
//Simple insertion sort - speed is not a huge factor here
1391-
inserted = false;
1392-
myCurrentIndex = 0;
1393-
while ( !inserted )
1394-
{
1395-
if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
1396-
{
1397-
myColorRampItems.push_back( myNewColorRampItem );
1398-
inserted = true;
1399-
}
1400-
else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
1401-
{
1402-
myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
1403-
inserted = true;
1404-
}
1405-
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 )
1406-
{
1407-
myColorRampItems.push_back( myNewColorRampItem );
1408-
inserted = true;
1409-
}
1410-
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value )
1411-
{
1412-
myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
1413-
inserted = true;
1414-
}
1415-
myCurrentIndex++;
1416-
}
1387+
1388+
myColorRampItems.append( myNewColorRampItem );
14171389
}
1390+
1391+
// sort the shader items
1392+
qSort(myColorRampItems);
1393+
14181394
myRasterShaderFunction->setColorRampItemList( myColorRampItems );
14191395
//Reload table in GUI because it may have been sorted or contained invalid values
14201396
populateColorMapTable( myColorRampItems );

src/core/raster/qgscolorrampshader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
4444
QString label;
4545
double value;
4646
QColor color;
47+
48+
// compare operator for sorting
49+
bool operator<(const ColorRampItem& other) const { return value < other.value; }
4750
};
4851

4952
enum ColorRamp_TYPE

0 commit comments

Comments
 (0)