Skip to content
Permalink
Browse files

Add a "load values" button to graduated histogram. The previous

approach of automatically fetching the values on demand was
resulting in graphical corruption to the parent tab widget.
  • Loading branch information
nyalldawson committed Jun 24, 2015
1 parent 3d978be commit 0016ab6230f7707a2975106ff33c43df332cf965
@@ -107,13 +107,14 @@ class QgsHistogramWidget : QWidget

public slots:

/** Triggers a refresh of the histogram when the widget is next repainted.
/** Refreshes the values for the histogram by fetching them from the layer.
*/
void refreshHistogram();
void refreshValues();

/** Triggers a refresh and immediate redraw of the histogram.
/** Redraws the histogram. Calling this slot does not update the values
* for the histogram, use @link refreshValues @endlink to do this.
*/
void refreshAndRedraw();
void refresh();

/** Sets the vector layer associated with the histogram.
* @param layer source vector layer
@@ -44,7 +44,6 @@

QgsHistogramWidget::QgsHistogramWidget( QWidget *parent, QgsVectorLayer* layer, const QString& fieldOrExp )
: QWidget( parent )
, mRedrawRequired( true )
, mVectorLayer( layer )
, mSourceFieldExp( fieldOrExp )
, mXAxisTitle( QObject::tr( "Value" ) )
@@ -67,9 +66,10 @@ QgsHistogramWidget::QgsHistogramWidget( QWidget *parent, QgsVectorLayer* layer,
mMeanCheckBox->setChecked( settings.value( "/HistogramWidget/showMean", false ).toBool() );
mStdevCheckBox->setChecked( settings.value( "/HistogramWidget/showStdev", false ).toBool() );

connect( mBinsSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( refreshAndRedraw() ) );
connect( mMeanCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( refreshAndRedraw() ) );
connect( mStdevCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( refreshAndRedraw() ) );
connect( mBinsSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( refresh() ) );
connect( mMeanCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( refresh() ) );
connect( mStdevCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( refresh() ) );
connect( mLoadValuesButton, SIGNAL( clicked() ), this, SLOT( refreshValues() ) );

mGridPen = QPen( QColor( 0, 0, 0, 40 ) );
mMeanPen = QPen( QColor( 10, 10, 10, 220 ) );
@@ -79,7 +79,7 @@ QgsHistogramWidget::QgsHistogramWidget( QWidget *parent, QgsVectorLayer* layer,

if ( layer && !mSourceFieldExp.isEmpty() )
{
refreshHistogram();
refresh();
}
}

@@ -101,25 +101,69 @@ void QgsHistogramWidget::setGraduatedRanges( const QgsRangeList &ranges )
qSort( mRanges.begin(), mRanges.end(), _rangesByLower );
}

void QgsHistogramWidget::refreshValues()
{
mValues.clear();

if ( !mVectorLayer || mSourceFieldExp.isEmpty() )
return;

QApplication::setOverrideCursor( Qt::WaitCursor );

bool ok;
mValues = mVectorLayer->getDoubleValues( mSourceFieldExp, ok );

if ( ! ok )
{
QApplication::restoreOverrideCursor();
return;
}


qSort( mValues.begin(), mValues.end() );
mHistogram.setValues( mValues );
mBinsSpinBox->blockSignals( true );
mBinsSpinBox->setValue( qMax( mHistogram.optimalNumberBins(), 30 ) );
mBinsSpinBox->blockSignals( false );

mStats.setStatistics( QgsStatisticalSummary::StDev );
mStats.calculate( mValues );

mpPlot->setEnabled( true );
mMeanCheckBox->setEnabled( true );
mStdevCheckBox->setEnabled( true );
mBinsSpinBox->setEnabled( true );

QApplication::restoreOverrideCursor();

//also force a redraw
refresh();
}

void QgsHistogramWidget::refresh()
{
drawHistogram();
}

void QgsHistogramWidget::setLayer( QgsVectorLayer *layer )
{
if ( layer == mVectorLayer )
return;

mVectorLayer = layer;
mValues.clear();
mRedrawRequired = true;
clearHistogram();
}

void QgsHistogramWidget::refreshHistogram()
void QgsHistogramWidget::clearHistogram()
{
mRedrawRequired = true;
}
mValues.clear();
mHistogram.setValues( mValues );
refresh();

void QgsHistogramWidget::refreshAndRedraw()
{
refreshHistogram();
drawHistogram();
mpPlot->setEnabled( false );
mMeanCheckBox->setEnabled( false );
mStdevCheckBox->setEnabled( false );
mBinsSpinBox->setEnabled( false );
}

void QgsHistogramWidget::setSourceFieldExp( const QString &fieldOrExp )
@@ -128,37 +172,11 @@ void QgsHistogramWidget::setSourceFieldExp( const QString &fieldOrExp )
return;

mSourceFieldExp = fieldOrExp;
mValues.clear();
mRedrawRequired = true;
clearHistogram();
}

void QgsHistogramWidget::drawHistogram()
{
if ( !mVectorLayer || mSourceFieldExp.isEmpty() )
return;

QApplication::setOverrideCursor( Qt::WaitCursor );

if ( mValues.empty() )
{
bool ok;
mValues = mVectorLayer->getDoubleValues( mSourceFieldExp, ok );

if ( ! ok )
{
QApplication::restoreOverrideCursor();
return;
}
qSort( mValues.begin(), mValues.end() );
mHistogram.setValues( mValues );
mBinsSpinBox->blockSignals( true );
mBinsSpinBox->setValue( qMax( mHistogram.optimalNumberBins(), 30 ) );
mBinsSpinBox->blockSignals( false );

mStats.setStatistics( QgsStatisticalSummary::StDev );
mStats.calculate( mValues );
}

// clear plot
mpPlot->detachItems();

@@ -321,19 +339,6 @@ void QgsHistogramWidget::drawHistogram()

mpPlot->setEnabled( true );
mpPlot->replot();

QApplication::restoreOverrideCursor();

mRedrawRequired = false;
}

void QgsHistogramWidget::paintEvent( QPaintEvent *event )
{
if ( mRedrawRequired )
{
drawHistogram();
}
QWidget::paintEvent( event );
}

#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
@@ -146,13 +146,14 @@ class GUI_EXPORT QgsHistogramWidget : public QWidget, private Ui::QgsHistogramWi

public slots:

/** Triggers a refresh of the histogram when the widget is next repainted.
/** Refreshes the values for the histogram by fetching them from the layer.
*/
void refreshHistogram();
void refreshValues();

/** Triggers a refresh and immediate redraw of the histogram.
/** Redraws the histogram. Calling this slot does not update the values
* for the histogram, use @link refreshValues @endlink to do this.
*/
void refreshAndRedraw();
void refresh();

/** Sets the vector layer associated with the histogram.
* @param layer source vector layer
@@ -172,12 +173,9 @@ class GUI_EXPORT QgsHistogramWidget : public QWidget, private Ui::QgsHistogramWi
*/
virtual void drawHistogram();

virtual void paintEvent( QPaintEvent * event ) override;

QwtPlot* mPlot;
QgsRangeList mRanges;
QList< QwtPlotMarker* > mRangeMarkers;
bool mRedrawRequired;

private:

@@ -195,6 +193,8 @@ class GUI_EXPORT QgsHistogramWidget : public QWidget, private Ui::QgsHistogramWi
QString mXAxisTitle;
QString mYAxisTitle;

void clearHistogram();

#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
QwtPlotHistogram* createPlotHistogram( const QString& title, const QBrush &brush, const QPen &pen = Qt::NoPen ) const;
#else
@@ -76,7 +76,6 @@ QgsGraduatedHistogramWidget::~QgsGraduatedHistogramWidget()
void QgsGraduatedHistogramWidget::setRenderer( QgsGraduatedSymbolRendererV2 *renderer )
{
mRenderer = renderer;
mRedrawRequired = true;
}

void QgsGraduatedHistogramWidget::drawHistogram()
@@ -125,7 +124,6 @@ void QgsGraduatedHistogramWidget::mousePress( double value )
{
//moving a break, so hide the break line
mRangeMarkers.at( closestRangeIndex )->hide();
mRedrawRequired = true;
mPlot->replot();
}
}
@@ -147,7 +145,7 @@ void QgsGraduatedHistogramWidget::mouseRelease( double value )
if ( value <= mRenderer->ranges().at( closestRangeIndex ).lowerValue() ||
value >= mRenderer->ranges().at( closestRangeIndex + 1 ).upperValue() )
{
refreshAndRedraw();
refresh();
return;
}

@@ -162,7 +160,7 @@ void QgsGraduatedHistogramWidget::mouseRelease( double value )
emit rangesModified( true );
}

refreshAndRedraw();
refresh();
}

void QgsGraduatedHistogramWidget::findClosestRange( double value, int &closestRangeIndex, int& pixelDistance ) const
@@ -191,6 +189,9 @@ QgsGraduatedHistogramEventFilter::QgsGraduatedHistogramEventFilter( QwtPlot *plo

bool QgsGraduatedHistogramEventFilter::eventFilter( QObject *object, QEvent *event )
{
if ( !mPlot->isEnabled() )
return QObject::eventFilter( object, event );

switch ( event->type() )
{
case QEvent::MouseButtonPress:
@@ -605,7 +605,7 @@ void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer( bool updateCount
viewGraduated->resizeColumnToContents( 1 );
viewGraduated->resizeColumnToContents( 2 );

mHistogramWidget->refreshAndRedraw();
mHistogramWidget->refresh();

connectUpdateHandlers();
}
@@ -873,7 +873,7 @@ void QgsGraduatedSymbolRendererV2Widget::changeRangeSymbol( int rangeIdx )
}

mRenderer->updateRangeSymbol( rangeIdx, newSymbol );
mHistogramWidget->refreshHistogram();
mHistogramWidget->refresh();
}

void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
@@ -909,26 +909,26 @@ void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
}
}
}
mHistogramWidget->refreshHistogram();
mHistogramWidget->refresh();
}

void QgsGraduatedSymbolRendererV2Widget::addClass()
{
mModel->addClass( mGraduatedSymbol );
mHistogramWidget->refreshHistogram();
mHistogramWidget->refresh();
}

void QgsGraduatedSymbolRendererV2Widget::deleteClasses()
{
QList<int> classIndexes = selectedClasses();
mModel->deleteRows( classIndexes );
mHistogramWidget->refreshHistogram();
mHistogramWidget->refresh();
}

void QgsGraduatedSymbolRendererV2Widget::deleteAllClasses()
{
mModel->removeAllRows();
mHistogramWidget->refreshHistogram();
mHistogramWidget->refresh();
}

bool QgsGraduatedSymbolRendererV2Widget::rowsOrdered()
@@ -973,7 +973,6 @@ void QgsGraduatedSymbolRendererV2Widget::toggleBoundariesLink( bool linked )
mRenderer->updateRangeLowerValue( i, mRenderer->ranges()[i-1].upperValue() );
}
refreshSymbolView();
mHistogramWidget->refreshHistogram();
}
}

@@ -1062,6 +1061,7 @@ void QgsGraduatedSymbolRendererV2Widget::refreshSymbolView()
{
mModel->updateSymbology();
}
mHistogramWidget->refresh();
}

void QgsGraduatedSymbolRendererV2Widget::showSymbolLevels()
@@ -91,6 +91,13 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="mLoadValuesButton">
<property name="text">
<string>Load values</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>

0 comments on commit 0016ab6

Please sign in to comment.
You can’t perform that action at this time.