Skip to content

Commit 6c46b09

Browse files
pierstitusnyalldawson
authored andcommitted
fix too numerous itemChanged events using QTreeWidgetItem subclass
1 parent af1af4c commit 6c46b09

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
#include <QSettings>
3030
#include <QTextStream>
3131

32+
// override setData to emit signal when edited. By default the itemChanged signal fires way too often
33+
void QgsTreeWidgetItem::setData( int column, int role, const QVariant & value )
34+
{
35+
QTreeWidgetItem::setData( column, role, value );
36+
if ( role == Qt::EditRole )
37+
{
38+
emit itemEdited( this, column );
39+
}
40+
}
41+
3242
QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer, const QgsRectangle &extent )
3343
: QgsRasterRendererWidget( layer, extent )
3444
, mMinMaxWidget( nullptr )
@@ -166,7 +176,7 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
166176
return renderer;
167177
}
168178

169-
void QgsSingleBandPseudoColorRendererWidget::autolabel()
179+
void QgsSingleBandPseudoColorRendererWidget::autoLabel()
170180
{
171181
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
172182
QString label = "";
@@ -207,11 +217,13 @@ void QgsSingleBandPseudoColorRendererWidget::autolabel()
207217

208218
void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
209219
{
210-
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
220+
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
211221
newItem->setText( 0, "0" );
212222
newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
213223
newItem->setText( 2, "" );
214-
autolabel();
224+
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
225+
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
226+
autoLabel();
215227
emit widgetChanged();
216228
}
217229

@@ -393,13 +405,15 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
393405

394406
for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
395407
{
396-
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
408+
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
397409
newItem->setText( 0, QString::number( *value_it, 'g' ) );
398410
newItem->setBackground( 1, QBrush( *color_it ) );
399411
newItem->setText( 2, "" );
400412
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
413+
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
414+
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
401415
}
402-
autolabel();
416+
autoLabel();
403417
emit widgetChanged();
404418
}
405419

@@ -432,12 +446,14 @@ void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const Q
432446
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItems.constBegin();
433447
for ( ; it != colorRampItems.constEnd(); ++it )
434448
{
435-
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
449+
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
436450
newItem->setText( 0, QString::number( it->value, 'g' ) );
437451
newItem->setBackground( 1, QBrush( it->color ) );
438452
newItem->setText( 2, it->label );
453+
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
454+
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
439455
}
440-
autolabel();
456+
autoLabel();
441457
}
442458

443459
void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
@@ -650,18 +666,18 @@ void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleCl
650666
}
651667
}
652668

653-
void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemChanged( QTreeWidgetItem* item, int column )
669+
void QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited( QTreeWidgetItem* item, int column )
654670
{
655-
if ( column == 0 ) // change item value
671+
Q_UNUSED( item );
672+
673+
if ( column == 0 ) // item value edited
656674
{
657-
autolabel();
675+
autoLabel();
658676
}
659-
else if ( column == 2 ) // change item label
677+
else if ( column == 2 ) // item label edited
660678
{
661-
if ( item->text( 2 ).isEmpty() )
662-
{
663-
autolabel();
664-
}
679+
// call autoLabel to fill when empty or gray out when same as autoLabel
680+
autoLabel();
665681
}
666682
}
667683

@@ -695,11 +711,14 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen
695711
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItemList.constBegin();
696712
for ( ; it != colorRampItemList.end(); ++it )
697713
{
698-
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
714+
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
699715
newItem->setText( 0, QString::number( it->value, 'g' ) );
700716
newItem->setBackground( 1, QBrush( it->color ) );
701717
newItem->setText( 2, it->label );
718+
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
719+
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
702720
}
721+
autoLabel();
703722
mClipCheckBox->setChecked( colorRampShader->clip() );
704723
}
705724
}
@@ -720,7 +739,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChange
720739
void QgsSingleBandPseudoColorRendererWidget::on_mColorInterpolationComboBox_currentIndexChanged( int index )
721740
{
722741
Q_UNUSED(index);
723-
autolabel();
742+
autoLabel();
724743
}
725744

726745
void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )

src/gui/raster/qgssinglebandpseudocolorrendererwidget.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
4747

4848
private:
4949
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
50-
void autolabel();
50+
void autoLabel();
5151

5252
private slots:
5353
void on_mAddEntryButton_clicked();
@@ -60,7 +60,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
6060
void on_mExportToFileButton_clicked();
6161
void on_mColorInterpolationComboBox_currentIndexChanged();
6262
void on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column );
63-
void on_mColormapTreeWidget_itemChanged( QTreeWidgetItem* item, int column );
63+
void mColormapTreeWidget_itemEdited( QTreeWidgetItem* item, int column );
6464
void on_mBandComboBox_currentIndexChanged( int index );
6565
void on_mColorInterpolationComboBox_currentIndexChanged( int index );
6666
void on_mMinLineEdit_textChanged( const QString & text ) { Q_UNUSED( text ); resetClassifyButton(); }
@@ -79,4 +79,17 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
7979
int mMinMaxOrigin;
8080
};
8181

82+
class QgsTreeWidgetItem: public QObject, public QTreeWidgetItem
83+
{
84+
Q_OBJECT
85+
public:
86+
explicit QgsTreeWidgetItem( QTreeWidget * parent, int type = Type ) : QTreeWidgetItem( parent, type ) {}
87+
88+
public:
89+
virtual void setData( int column, int role, const QVariant & value );
90+
91+
signals:
92+
void itemEdited( QTreeWidgetItem* item, int column );
93+
};
94+
8295
#endif // QGSSINGLEBANDCOLORRENDERERWIDGET_H

0 commit comments

Comments
 (0)