Skip to content

Commit 14ee392

Browse files
committed
allow to plot histogram as boxes with qwt5, using HistogramItem
1 parent 4c1e866 commit 14ee392

File tree

1 file changed

+63
-32
lines changed

1 file changed

+63
-32
lines changed

src/gui/raster/qgsrasterhistogramwidget.cpp

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
4343
#include <qwt_plot_renderer.h>
4444
#include <qwt_plot_histogram.h>
45+
#else
46+
#include "qwt5_histogram_item.h"
4547
#endif
4648

4749
// this has been removed, now we let the provider/raster interface decide
@@ -165,12 +167,8 @@ QgsRasterHistogramWidget::QgsRasterHistogramWidget( QgsRasterLayer* lyr, QWidget
165167
action = new QAction( tr( "Draw as lines" ), group );
166168
action->setData( QVariant( "Draw lines" ) );
167169
action->setCheckable( true );
168-
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
169-
// should we plot as histogram instead of line plot? (QWT>=6 and byte data only)
170+
// should we plot as histogram instead of line plot? (int data only)
170171
action->setChecked( mHistoDrawLines );
171-
#else
172-
action->setDisabled( true );
173-
#endif
174172
menu->addAction( action );
175173

176174
// actions
@@ -331,11 +329,9 @@ void QgsRasterHistogramWidget::refreshHistogram()
331329
return;
332330
}
333331

334-
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
332+
// clear plot
335333
mpPlot->detachItems();
336-
#else
337-
mpPlot->clear();
338-
#endif
334+
339335
//ensure all children get removed
340336
mpPlot->setAutoDelete( true );
341337
mpPlot->setTitle( QObject::tr( "Raster Histogram" ) );
@@ -469,19 +465,44 @@ void QgsRasterHistogramWidget::refreshHistogram()
469465

470466
QgsDebugMsg( QString( "got raster histo for band %1 : min=%2 max=%3 count=%4" ).arg( myIteratorInt ).arg( myHistogram.minimum ).arg( myHistogram.maximum ).arg( myHistogram.binCount ) );
471467

468+
QGis::DataType mySrcDataType = mRasterLayer->dataProvider()->srcDataType( myIteratorInt );
472469
bool myDrawLines = true;
470+
if ( ! mHistoDrawLines &&
471+
( mySrcDataType == QGis::Byte ||
472+
mySrcDataType == QGis::Int16 || mySrcDataType == QGis::Int32 ||
473+
mySrcDataType == QGis::UInt16 || mySrcDataType == QGis::UInt32 ) )
474+
{
475+
myDrawLines = false;
476+
}
473477

474-
QwtPlotCurve * mypCurve = new QwtPlotCurve( tr( "Band %1" ).arg( myIteratorInt ) );
475-
//mypCurve->setCurveAttribute( QwtPlotCurve::Fitted );
476-
mypCurve->setRenderHint( QwtPlotItem::RenderAntialiased );
477-
mypCurve->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
478+
QwtPlotCurve * mypCurve = 0;
479+
if ( myDrawLines )
480+
{
481+
mypCurve = new QwtPlotCurve( tr( "Band %1" ).arg( myIteratorInt ) );
482+
//mypCurve->setCurveAttribute( QwtPlotCurve::Fitted );
483+
mypCurve->setRenderHint( QwtPlotItem::RenderAntialiased );
484+
mypCurve->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
485+
}
478486

479487
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
480-
QwtPlotHistogram * mypHisto = new QwtPlotHistogram( tr( "Band %1" ).arg( myIteratorInt ) );
481-
//mypHisto->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
482-
// this is needed in order to see the colors in the legend
483-
mypHisto->setPen( QPen( Qt::black ) );
484-
mypHisto->setBrush( QBrush( mHistoColors.at( myIteratorInt ) ) );
488+
QwtPlotHistogram * mypHisto = 0;
489+
if ( ! myDrawLines )
490+
{
491+
mypHisto = new QwtPlotHistogram( tr( "Band %1" ).arg( myIteratorInt ) );
492+
mypHisto->setRenderHint( QwtPlotItem::RenderAntialiased );
493+
//mypHisto->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
494+
// this is needed in order to see the colors in the legend
495+
mypHisto->setPen( QPen( Qt::black ) );
496+
mypHisto->setBrush( QBrush( mHistoColors.at( myIteratorInt ) ) );
497+
}
498+
#else
499+
HistogramItem *mypHistoItem = 0;
500+
if ( ! myDrawLines )
501+
{
502+
mypHistoItem = new HistogramItem( tr( "Band %1" ).arg( myIteratorInt ) );
503+
mypHistoItem->setRenderHint( QwtPlotItem::RenderAntialiased );
504+
mypHistoItem->setColor( mHistoColors.at( myIteratorInt ) );
505+
}
485506
#endif
486507

487508
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
@@ -490,10 +511,13 @@ void QgsRasterHistogramWidget::refreshHistogram()
490511
#else
491512
QVector<double> myX2Data;
492513
QVector<double> myY2Data;
514+
// we safely assume that QT>=4.0 (min version is 4.7), therefore QwtArray is a QVector, so don't set size here
515+
QwtArray<QwtDoubleInterval> intervalsHisto;
516+
QwtArray<double> valuesHisto;
517+
493518
#endif
494519

495520
// calculate first bin x value and bin step size if not Byte data
496-
QGis::DataType mySrcDataType = mRasterLayer->dataProvider()->srcDataType( myIteratorInt );
497521
if ( mySrcDataType != QGis::Byte )
498522
{
499523
myBinXStep = ( myHistogram.maximum - myHistogram.minimum ) / myHistogram.binCount;
@@ -504,15 +528,6 @@ void QgsRasterHistogramWidget::refreshHistogram()
504528
myBinXStep = 1;
505529
myBinX = 0;
506530
}
507-
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
508-
if ( ! mHistoDrawLines &&
509-
( mySrcDataType == QGis::Byte ||
510-
mySrcDataType == QGis::Int16 || mySrcDataType == QGis::Int32 ||
511-
mySrcDataType == QGis::UInt16 || mySrcDataType == QGis::UInt32 ) )
512-
{
513-
myDrawLines = false;
514-
}
515-
#endif
516531

517532
for ( int myBin = 0; myBin < myHistogram.binCount; myBin++ )
518533
{
@@ -527,8 +542,16 @@ void QgsRasterHistogramWidget::refreshHistogram()
527542
dataHisto << QwtIntervalSample( myBinValue, myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 );
528543
}
529544
#else
530-
myX2Data.append( double( myBinX ) );
531-
myY2Data.append( double( myBinValue ) );
545+
if ( myDrawLines )
546+
{
547+
myX2Data.append( double( myBinX ) );
548+
myY2Data.append( double( myBinValue ) );
549+
}
550+
else
551+
{
552+
intervalsHisto.append( QwtDoubleInterval( myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 ) );
553+
valuesHisto.append( double( myBinValue ) );
554+
}
532555
#endif
533556
myBinX += myBinXStep;
534557
}
@@ -545,8 +568,16 @@ void QgsRasterHistogramWidget::refreshHistogram()
545568
mypHisto->attach( mpPlot );
546569
}
547570
#else
548-
mypCurve->setData( myX2Data, myY2Data );
549-
mypCurve->attach( mpPlot );
571+
if ( myDrawLines )
572+
{
573+
mypCurve->setData( myX2Data, myY2Data );
574+
mypCurve->attach( mpPlot );
575+
}
576+
else
577+
{
578+
mypHistoItem->setData( QwtIntervalData( intervalsHisto, valuesHisto ) );
579+
mypHistoItem->attach( mpPlot );
580+
}
550581
#endif
551582

552583
if ( myFirstIteration || mHistoMin > myHistogram.minimum )

0 commit comments

Comments
 (0)