Skip to content

Commit

Permalink
Add a "load values" button to graduated histogram. The previous
Browse files Browse the repository at this point in the history
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 0016ab6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 76 deletions.
9 changes: 5 additions & 4 deletions python/gui/qgshistogramwidget.sip
Expand Up @@ -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
Expand Down
113 changes: 59 additions & 54 deletions src/gui/qgshistogramwidget.cpp
Expand Up @@ -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" ) )
Expand All @@ -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 ) );
Expand All @@ -79,7 +79,7 @@ QgsHistogramWidget::QgsHistogramWidget( QWidget *parent, QgsVectorLayer* layer,

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

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

Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/gui/qgshistogramwidget.h
Expand Up @@ -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
Expand All @@ -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:

Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/gui/symbology-ng/qgsgraduatedhistogramwidget.cpp
Expand Up @@ -76,7 +76,6 @@ QgsGraduatedHistogramWidget::~QgsGraduatedHistogramWidget()
void QgsGraduatedHistogramWidget::setRenderer( QgsGraduatedSymbolRendererV2 *renderer )
{
mRenderer = renderer;
mRedrawRequired = true;
}

void QgsGraduatedHistogramWidget::drawHistogram()
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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;
}

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

refreshAndRedraw();
refresh();
}

void QgsGraduatedHistogramWidget::findClosestRange( double value, int &closestRangeIndex, int& pixelDistance ) const
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
Expand Up @@ -605,7 +605,7 @@ void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer( bool updateCount
viewGraduated->resizeColumnToContents( 1 );
viewGraduated->resizeColumnToContents( 2 );

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

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

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

void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -973,7 +973,6 @@ void QgsGraduatedSymbolRendererV2Widget::toggleBoundariesLink( bool linked )
mRenderer->updateRangeLowerValue( i, mRenderer->ranges()[i-1].upperValue() );
}
refreshSymbolView();
mHistogramWidget->refreshHistogram();
}
}

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

void QgsGraduatedSymbolRendererV2Widget::showSymbolLevels()
Expand Down
7 changes: 7 additions & 0 deletions src/ui/qgshistogramwidgetbase.ui
Expand Up @@ -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>
Expand Down

0 comments on commit 0016ab6

Please sign in to comment.