Skip to content

Commit

Permalink
use QgsTreeWidgetItem also for sorting color map items
Browse files Browse the repository at this point in the history
quicker and less code, so enable automatic sorting after every edit
  • Loading branch information
pierstitus authored and nyalldawson committed Jun 2, 2016
1 parent 6c46b09 commit e5ab7ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 50 deletions.
60 changes: 10 additions & 50 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -39,6 +39,13 @@ void QgsTreeWidgetItem::setData( int column, int role, const QVariant & value )
}
}

// override < operator to allow sorting
bool QgsTreeWidgetItem::operator<( const QTreeWidgetItem & other ) const
{
// could use treeWidget()->sortColumn() instead of 0
return text( 0 ).toDouble() < other.text( 0 ).toDouble();
}

QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer, const QgsRectangle &extent )
: QgsRasterRendererWidget( layer, extent )
, mMinMaxWidget( nullptr )
Expand Down Expand Up @@ -223,6 +230,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
newItem->setText( 2, "" );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
autoLabel();
emit widgetChanged();
}
Expand All @@ -243,56 +251,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mNumberOfEntriesSpinBox_valueCha

void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
{
bool inserted = false;
int myCurrentIndex = 0;
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* myCurrentItem;
QList<QgsColorRampShader::ColorRampItem> myColorRampItems;
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;
}

//Create a copy of the new Color ramp Item
QgsColorRampShader::ColorRampItem myNewColorRampItem;
myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble();
myNewColorRampItem.color = myCurrentItem->background( 1 ).color();
myNewColorRampItem.label = myCurrentItem->text( 2 );

//Simple insertion sort - speed is not a huge factor here
inserted = false;
myCurrentIndex = 0;
while ( !inserted )
{
if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
{
myColorRampItems.push_back( myNewColorRampItem );
inserted = true;
}
else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
{
myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
inserted = true;
}
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 )
{
myColorRampItems.push_back( myNewColorRampItem );
inserted = true;
}
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value )
{
myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
inserted = true;
}
myCurrentIndex++;
}
}
populateColormapTreeWidget( myColorRampItems );
emit widgetChanged();
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
}

void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
Expand Down Expand Up @@ -672,6 +631,7 @@ void QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited( QTr

if ( column == 0 ) // item value edited
{
mColormapTreeWidget->sortItems( 0, Qt::AscendingOrder );
autoLabel();
}
else if ( column == 2 ) // item label edited
Expand Down
1 change: 1 addition & 0 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.h
Expand Up @@ -87,6 +87,7 @@ class QgsTreeWidgetItem: public QObject, public QTreeWidgetItem

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

signals:
void itemEdited( QTreeWidgetItem* item, int column );
Expand Down

0 comments on commit e5ab7ca

Please sign in to comment.