Skip to content

Commit

Permalink
fix too numerous itemChanged events using QTreeWidgetItem subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
pierstitus authored and nyalldawson committed Jun 2, 2016
1 parent af1af4c commit 6c46b09
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
53 changes: 36 additions & 17 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
#include <QSettings>
#include <QTextStream>

// override setData to emit signal when edited. By default the itemChanged signal fires way too often
void QgsTreeWidgetItem::setData( int column, int role, const QVariant & value )
{
QTreeWidgetItem::setData( column, role, value );
if ( role == Qt::EditRole )
{
emit itemEdited( this, column );
}
}

QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer, const QgsRectangle &extent )
: QgsRasterRendererWidget( layer, extent )
, mMinMaxWidget( nullptr )
Expand Down Expand Up @@ -166,7 +176,7 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
return renderer;
}

void QgsSingleBandPseudoColorRendererWidget::autolabel()
void QgsSingleBandPseudoColorRendererWidget::autoLabel()
{
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
QString label = "";
Expand Down Expand Up @@ -207,11 +217,13 @@ void QgsSingleBandPseudoColorRendererWidget::autolabel()

void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, "0" );
newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
newItem->setText( 2, "" );
autolabel();
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
autoLabel();
emit widgetChanged();
}

Expand Down Expand Up @@ -393,13 +405,15 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()

for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( *value_it, 'g' ) );
newItem->setBackground( 1, QBrush( *color_it ) );
newItem->setText( 2, "" );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
}
autolabel();
autoLabel();
emit widgetChanged();
}

Expand Down Expand Up @@ -432,12 +446,14 @@ void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const Q
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItems.constBegin();
for ( ; it != colorRampItems.constEnd(); ++it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'g' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
}
autolabel();
autoLabel();
}

void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
Expand Down Expand Up @@ -650,18 +666,18 @@ void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleCl
}
}

void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemChanged( QTreeWidgetItem* item, int column )
void QgsSingleBandPseudoColorRendererWidget::mColormapTreeWidget_itemEdited( QTreeWidgetItem* item, int column )
{
if ( column == 0 ) // change item value
Q_UNUSED( item );

if ( column == 0 ) // item value edited
{
autolabel();
autoLabel();
}
else if ( column == 2 ) // change item label
else if ( column == 2 ) // item label edited
{
if ( item->text( 2 ).isEmpty() )
{
autolabel();
}
// call autoLabel to fill when empty or gray out when same as autoLabel
autoLabel();
}
}

Expand Down Expand Up @@ -695,11 +711,14 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItemList.constBegin();
for ( ; it != colorRampItemList.end(); ++it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
QgsTreeWidgetItem* newItem = new QgsTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'g' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
connect( newItem, SIGNAL( itemEdited( QTreeWidgetItem*, int ) ),
this, SLOT( mColormapTreeWidget_itemEdited( QTreeWidgetItem*, int ) ) );
}
autoLabel();
mClipCheckBox->setChecked( colorRampShader->clip() );
}
}
Expand All @@ -720,7 +739,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChange
void QgsSingleBandPseudoColorRendererWidget::on_mColorInterpolationComboBox_currentIndexChanged( int index )
{
Q_UNUSED(index);
autolabel();
autoLabel();
}

void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
Expand Down
17 changes: 15 additions & 2 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere

private:
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
void autolabel();
void autoLabel();

private slots:
void on_mAddEntryButton_clicked();
Expand All @@ -60,7 +60,7 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
void on_mExportToFileButton_clicked();
void on_mColorInterpolationComboBox_currentIndexChanged();
void on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column );
void on_mColormapTreeWidget_itemChanged( QTreeWidgetItem* item, int column );
void mColormapTreeWidget_itemEdited( QTreeWidgetItem* item, int column );
void on_mBandComboBox_currentIndexChanged( int index );
void on_mColorInterpolationComboBox_currentIndexChanged( int index );
void on_mMinLineEdit_textChanged( const QString & text ) { Q_UNUSED( text ); resetClassifyButton(); }
Expand All @@ -79,4 +79,17 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere
int mMinMaxOrigin;
};

class QgsTreeWidgetItem: public QObject, public QTreeWidgetItem
{
Q_OBJECT
public:
explicit QgsTreeWidgetItem( QTreeWidget * parent, int type = Type ) : QTreeWidgetItem( parent, type ) {}

public:
virtual void setData( int column, int role, const QVariant & value );

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

#endif // QGSSINGLEBANDCOLORRENDERERWIDGET_H

0 comments on commit 6c46b09

Please sign in to comment.