Skip to content
Permalink
Browse files

fix too numerous itemChanged events using QTreeWidgetItem subclass

  • Loading branch information
pierstitus authored and nyalldawson committed Apr 6, 2016
1 parent af1af4c commit 6c46b09c7547eb31243f8fb5e36c2dc22d85d11e
@@ -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 )
@@ -166,7 +176,7 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
return renderer;
}

void QgsSingleBandPseudoColorRendererWidget::autolabel()
void QgsSingleBandPseudoColorRendererWidget::autoLabel()
{
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
QString label = "";
@@ -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();
}

@@ -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();
}

@@ -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()
@@ -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();
}
}

@@ -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() );
}
}
@@ -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 )
@@ -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();
@@ -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(); }
@@ -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.
You can’t perform that action at this time.