Skip to content

Commit e5ab7ca

Browse files
pierstitusnyalldawson
authored andcommittedJun 2, 2016
use QgsTreeWidgetItem also for sorting color map items
quicker and less code, so enable automatic sorting after every edit
1 parent 6c46b09 commit e5ab7ca

File tree

2 files changed

+11
-50
lines changed

2 files changed

+11
-50
lines changed
 

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ void QgsTreeWidgetItem::setData( int column, int role, const QVariant & value )
3939
}
4040
}
4141

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+
4249
QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer, const QgsRectangle &extent )
4350
: QgsRasterRendererWidget( layer, extent )
4451
, mMinMaxWidget( nullptr )
@@ -223,6 +230,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
223230
newItem->setText( 2, "" );
224231
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
225232
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
233+
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
226234
autoLabel();
227235
emit widgetChanged();
228236
}
@@ -243,56 +251,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mNumberOfEntriesSpinBox_valueCha
243251

244252
void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
245253
{
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 );
296255
}
297256

298257
void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
@@ -672,6 +631,7 @@ void QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited( QTr
672631

673632
if ( column == 0 ) // item value edited
674633
{
634+
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
675635
autoLabel();
676636
}
677637
else if ( column == 2 ) // item label edited

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class QgsTreeWidgetItem: public QObject, public QTreeWidgetItem
8787

8888
public:
8989
virtual void setData( int column, int role, const QVariant & value );
90+
virtual bool operator< ( const QTreeWidgetItem & other ) const;
9091

9192
signals:
9293
void itemEdited( QTreeWidgetItem* item, int column );

0 commit comments

Comments
 (0)
Please sign in to comment.