Skip to content

Commit b4d7a30

Browse files
committedApr 14, 2012
Add code for add/delete/sort buttons in singleband pseudocolor widget
1 parent ba66ca6 commit b4d7a30

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
 

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,76 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
138138
return new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), bandNumber, rasterShader );
139139
}
140140

141+
void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
142+
{
143+
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
144+
newItem->setText( 0, "0.0" );
145+
newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
146+
newItem->setText( 2, tr( "Custom color map entry" ) );
147+
}
148+
149+
void QgsSingleBandPseudoColorRendererWidget::on_mDeleteEntryButton_clicked()
150+
{
151+
QTreeWidgetItem* currentItem = mColormapTreeWidget->currentItem();
152+
if ( currentItem )
153+
{
154+
delete currentItem;
155+
}
156+
}
157+
158+
void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
159+
{
160+
bool inserted = false;
161+
int myCurrentIndex = 0;
162+
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
163+
QTreeWidgetItem* myCurrentItem;
164+
QList<QgsColorRampShader::ColorRampItem> myColorRampItems;
165+
for ( int i = 0; i < myTopLevelItemCount; ++i )
166+
{
167+
myCurrentItem = mColormapTreeWidget->topLevelItem( i );
168+
//If the item is null or does not have a pixel values set, skip
169+
if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
170+
{
171+
continue;
172+
}
173+
174+
//Create a copy of the new Color ramp Item
175+
QgsColorRampShader::ColorRampItem myNewColorRampItem;
176+
myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble();
177+
myNewColorRampItem.color = myCurrentItem->background( 1 ).color();
178+
myNewColorRampItem.label = myCurrentItem->text( 2 );
179+
180+
//Simple insertion sort - speed is not a huge factor here
181+
inserted = false;
182+
myCurrentIndex = 0;
183+
while ( !inserted )
184+
{
185+
if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
186+
{
187+
myColorRampItems.push_back( myNewColorRampItem );
188+
inserted = true;
189+
}
190+
else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
191+
{
192+
myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
193+
inserted = true;
194+
}
195+
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 )
196+
{
197+
myColorRampItems.push_back( myNewColorRampItem );
198+
inserted = true;
199+
}
200+
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value )
201+
{
202+
myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
203+
inserted = true;
204+
}
205+
myCurrentIndex++;
206+
}
207+
}
208+
populateColormapTreeWidget( myColorRampItems );
209+
}
210+
141211
void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
142212
{
143213
int bandComboIndex = mBandComboBox->currentIndex();

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendererWidget,
3737
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
3838

3939
private slots:
40+
void on_mAddEntryButton_clicked();
41+
void on_mDeleteEntryButton_clicked();
42+
void on_mSortButton_clicked();
4043
void on_mClassifyButton_clicked();
4144
void on_mLoadFromBandButton_clicked();
4245
void on_mLoadFromFileButton_clicked();

0 commit comments

Comments
 (0)
Please sign in to comment.