32 #include <QActionGroup>
33 #include <QRandomGenerator>
36 #include <qwt_global.h>
37 #include <qwt_plot_canvas.h>
38 #include <qwt_legend.h>
40 #include <qwt_plot_curve.h>
41 #include <qwt_plot_grid.h>
42 #include <qwt_plot_marker.h>
43 #include <qwt_plot_picker.h>
44 #include <qwt_picker_machine.h>
45 #include <qwt_plot_zoomer.h>
46 #include <qwt_plot_layout.h>
47 #include <qwt_plot_renderer.h>
48 #include <qwt_plot_histogram.h>
49 #include <qwt_scale_div.h>
63 connect( mSaveAsImageButton, &QToolButton::clicked,
this, &QgsRasterHistogramWidget::mSaveAsImageButton_clicked );
64 connect( cboHistoBand,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsRasterHistogramWidget::cboHistoBand_currentIndexChanged );
65 connect( btnHistoMin, &QToolButton::toggled,
this, &QgsRasterHistogramWidget::btnHistoMin_toggled );
66 connect( btnHistoMax, &QToolButton::toggled,
this, &QgsRasterHistogramWidget::btnHistoMax_toggled );
67 connect( btnHistoCompute, &QPushButton::clicked,
this, &QgsRasterHistogramWidget::btnHistoCompute_clicked );
71 mRendererWidget =
nullptr;
72 mRendererName = QStringLiteral(
"singlebandgray" );
77 mHistoPicker =
nullptr;
78 mHistoZoomer =
nullptr;
79 mHistoMarkerMin =
nullptr;
80 mHistoMarkerMax =
nullptr;
83 mHistoShowMarkers = settings.
value( QStringLiteral(
"Raster/histogram/showMarkers" ),
false ).toBool();
85 mHistoZoomToMinMax = settings.
value( QStringLiteral(
"Raster/histogram/zoomToMinMax" ),
false ).toBool();
86 mHistoUpdateStyleToMinMax = settings.
value( QStringLiteral(
"Raster/histogram/updateStyleToMinMax" ),
true ).toBool();
87 mHistoDrawLines = settings.
value( QStringLiteral(
"Raster/histogram/drawLines" ),
true ).toBool();
89 mHistoShowBands = ShowAll;
95 const int myBandCountInt = mRasterLayer->
bandCount();
96 for (
int myIteratorInt = 1;
97 myIteratorInt <= myBandCountInt;
100 cboHistoBand->addItem( mRasterLayer->
bandName( myIteratorInt ) );
116 connect( leHistoMin, &QLineEdit::editingFinished,
this, &QgsRasterHistogramWidget::applyHistoMin );
117 connect( leHistoMax, &QLineEdit::editingFinished,
this, &QgsRasterHistogramWidget::applyHistoMax );
121 QMenu *menu =
new QMenu(
this );
122 menu->setSeparatorsCollapsible(
false );
123 btnHistoActions->setMenu( menu );
124 QActionGroup *group =
nullptr;
125 QAction *action =
nullptr;
128 group =
new QActionGroup(
this );
129 group->setExclusive(
false );
130 connect( group, &QActionGroup::triggered,
this, &QgsRasterHistogramWidget::histoActionTriggered );
131 action =
new QAction( tr(
"Min/Max options" ), group );
132 action->setSeparator(
true );
133 menu->addAction( action );
134 action =
new QAction( tr(
"Always show min/max markers" ), group );
135 action->setData( QVariant(
"Show markers" ) );
136 action->setCheckable(
true );
137 action->setChecked( mHistoShowMarkers );
138 menu->addAction( action );
139 action =
new QAction( tr(
"Zoom to min/max" ), group );
140 action->setData( QVariant(
"Zoom min_max" ) );
141 action->setCheckable(
true );
142 action->setChecked( mHistoZoomToMinMax );
143 menu->addAction( action );
144 action =
new QAction( tr(
"Update style to min/max" ), group );
145 action->setData( QVariant(
"Update min_max" ) );
146 action->setCheckable(
true );
147 action->setChecked( mHistoUpdateStyleToMinMax );
148 menu->addAction( action );
151 group =
new QActionGroup(
this );
152 group->setExclusive(
false );
153 connect( group, &QActionGroup::triggered,
this, &QgsRasterHistogramWidget::histoActionTriggered );
154 action =
new QAction( tr(
"Visibility" ), group );
155 action->setSeparator(
true );
156 menu->addAction( action );
157 group =
new QActionGroup(
this );
158 group->setExclusive(
true );
159 connect( group, &QActionGroup::triggered,
this, &QgsRasterHistogramWidget::histoActionTriggered );
160 action =
new QAction( tr(
"Show all bands" ), group );
161 action->setData( QVariant(
"Show all" ) );
162 action->setCheckable(
true );
163 action->setChecked( mHistoShowBands == ShowAll );
164 menu->addAction( action );
165 action =
new QAction( tr(
"Show RGB/Gray band(s)" ), group );
166 action->setData( QVariant(
"Show RGB" ) );
167 action->setCheckable(
true );
168 action->setChecked( mHistoShowBands == ShowRGB );
169 menu->addAction( action );
170 action =
new QAction( tr(
"Show selected band" ), group );
171 action->setData( QVariant(
"Show selected" ) );
172 action->setCheckable(
true );
173 action->setChecked( mHistoShowBands == ShowSelected );
174 menu->addAction( action );
177 group =
new QActionGroup(
this );
178 group->setExclusive(
false );
179 connect( group, &QActionGroup::triggered,
this, &QgsRasterHistogramWidget::histoActionTriggered );
180 action =
new QAction( tr(
"Display" ), group );
181 action->setSeparator(
true );
182 menu->addAction( action );
184 action =
new QAction( QString(), group );
185 action->setData( QVariant(
"Draw lines" ) );
188 action->setText( tr(
"Draw as lines" ) );
189 action->setCheckable(
true );
190 action->setChecked( mHistoDrawLines );
194 action->setText( tr(
"Draw as lines (only int layers)" ) );
195 action->setEnabled(
false );
197 menu->addAction( action );
200 action =
new QAction( tr(
"Actions" ), group );
201 action->setSeparator(
true );
202 menu->addAction( action );
205 group =
new QActionGroup(
this );
206 group->setExclusive(
false );
207 connect( group, &QActionGroup::triggered,
this, &QgsRasterHistogramWidget::histoActionTriggered );
208 action =
new QAction( tr(
"Reset" ), group );
209 action->setData( QVariant(
"Load reset" ) );
210 menu->addAction( action );
216 action =
new QAction( tr(
"Load min/max" ), group );
217 action->setSeparator(
true );
218 menu->addAction( action );
219 action =
new QAction( tr(
"Estimate (faster)" ), group );
220 action->setData( QVariant(
"Load estimate" ) );
221 menu->addAction( action );
222 action =
new QAction( tr(
"Actual (slower)" ), group );
223 action->setData( QVariant(
"Load actual" ) );
224 menu->addAction( action );
225 action =
new QAction( tr(
"Current extent" ), group );
226 action->setData( QVariant(
"Load extent" ) );
227 menu->addAction( action );
228 action =
new QAction( tr(
"Use stddev (1.0)" ), group );
229 action->setData( QVariant(
"Load 1 stddev" ) );
230 menu->addAction( action );
231 action =
new QAction( tr(
"Use stddev (custom)" ), group );
232 action->setData( QVariant(
"Load stddev" ) );
233 menu->addAction( action );
234 action =
new QAction( tr(
"Load for each band" ), group );
235 action->setData( QVariant(
"Load apply all" ) );
236 action->setCheckable(
true );
237 action->setChecked( mHistoLoadApplyAll );
238 menu->addAction( action );
242 action =
new QAction( tr(
"Recompute Histogram" ), group );
243 action->setData( QVariant(
"Compute histogram" ) );
244 menu->addAction( action );
252 mRendererName = name;
253 mRendererWidget = rendererWidget;
255 cboHistoBand_currentIndexChanged( -1 );
263 cboHistoBand_currentIndexChanged( -1 );
267 if ( QApplication::overrideCursor() )
268 QApplication::restoreOverrideCursor();
269 btnHistoMin->setChecked(
false );
270 btnHistoMax->setChecked(
false );
274 void QgsRasterHistogramWidget::btnHistoCompute_clicked()
303 xRes = yRes = std::sqrt( (
static_cast<double>( extent.width( ) ) * extent.height() ) / sampleSize );
308 const double srcXRes = extent.width() / rasterInterface->
xSize();
309 const double srcYRes = extent.height() / rasterInterface->
ySize();
310 if ( xRes < srcXRes ) xRes = srcXRes;
311 if ( yRes < srcYRes ) yRes = srcYRes;
314 const int histogramWidth =
static_cast <int>( extent.width() / xRes );
315 const int histogramHeight =
static_cast <int>( extent.height() / yRes );
317 int binCount =
static_cast<int>( std::min(
static_cast<qint64
>( 1000 ),
318 static_cast<qint64
>( histogramWidth ) * histogramHeight ) );
323 binCount =
static_cast<int>( std::min(
static_cast<qint64
>( binCount ),
324 static_cast<qint64
>( std::ceil( statsMax - statsMin + 1 ) ) ) );
335 const int myBandCountInt = mRasterLayer->
bandCount();
338 if ( ! forceComputeFlag )
340 for (
int myIteratorInt = 1;
341 myIteratorInt <= myBandCountInt;
345 const int binCount = getBinCount( mRasterLayer->
dataProvider(), myIteratorInt, sampleSize );
346 if ( !mRasterLayer->
dataProvider()->
hasHistogram( myIteratorInt, binCount, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
QgsRectangle(), sampleSize ) )
348 QgsDebugMsg( QStringLiteral(
"band %1 does not have cached histo" ).arg( myIteratorInt ) );
355 stackedWidget2->setCurrentIndex( 1 );
359 QApplication::setOverrideCursor( Qt::WaitCursor );
361 for (
int myIteratorInt = 1;
362 myIteratorInt <= myBandCountInt;
366 const int binCount = getBinCount( mRasterLayer->
dataProvider(), myIteratorInt, sampleSize );
367 mRasterLayer->
dataProvider()->
histogram( myIteratorInt, binCount, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
QgsRectangle(), sampleSize,
false, feedback.get() );
371 stackedWidget2->setCurrentIndex( 0 );
372 QApplication::restoreOverrideCursor();
389 const int myBandCountInt = mRasterLayer->
bandCount();
394 QgsDebugMsg( QStringLiteral(
"raster does not have cached histogram" ) );
395 stackedWidget2->setCurrentIndex( 2 );
400 mpPlot->detachItems();
403 mpPlot->setAutoDelete(
true );
404 mpPlot->setTitle( QObject::tr(
"Raster Histogram" ) );
405 mpPlot->insertLegend(
new QwtLegend(), QwtPlot::BottomLegend );
407 mpPlot->setAxisTitle( QwtPlot::xBottom, QObject::tr(
"Pixel Value" ) );
408 mpPlot->setAxisTitle( QwtPlot::yLeft, QObject::tr(
"Frequency" ) );
409 mpPlot->setAxisAutoScale( QwtPlot::yLeft );
413 QwtPlotGrid *myGrid =
new QwtPlotGrid();
414 myGrid->attach( mpPlot );
417 mHistoColors.clear();
418 mHistoColors << Qt::black;
419 QVector<QColor> myColors;
420 myColors << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkYellow << Qt::cyan;
423 QRandomGenerator colorGenerator( myBandCountInt * 100 );
424 while ( myColors.size() <= myBandCountInt )
427 QColor( colorGenerator.bounded( 1, 256 ),
428 colorGenerator.bounded( 1, 256 ),
429 colorGenerator.bounded( 1, 256 ) );
434 QList< int > mySelectedBands = rendererSelectedBands();
435 if ( mRendererName == QLatin1String(
"singlebandgray" ) )
437 const int myGrayBand = mySelectedBands[0];
438 for (
int i = 1; i <= myBandCountInt; i++ )
440 if ( i == myGrayBand )
442 mHistoColors << Qt::darkGray;
443 cboHistoBand->setItemData( i - 1, QColor( Qt::darkGray ), Qt::ForegroundRole );
447 if ( ! myColors.isEmpty() )
449 mHistoColors << myColors.first();
450 myColors.pop_front();
454 mHistoColors << Qt::black;
456 cboHistoBand->setItemData( i - 1, QColor( Qt::black ), Qt::ForegroundRole );
461 else if ( mRendererName == QLatin1String(
"multibandcolor" ) )
463 const int myRedBand = mySelectedBands[0];
464 const int myGreenBand = mySelectedBands[1];
465 const int myBlueBand = mySelectedBands[2];
468 myColors.remove( 0, 3 );
469 for (
int i = 1; i <= myBandCountInt; i++ )
472 if ( i == myRedBand )
474 else if ( i == myGreenBand )
476 else if ( i == myBlueBand )
480 if ( ! myColors.isEmpty() )
482 myColor = myColors.first();
483 myColors.pop_front();
489 cboHistoBand->setItemData( i - 1, QColor( Qt::black ), Qt::ForegroundRole );
491 if ( i == myRedBand || i == myGreenBand || i == myBlueBand )
493 cboHistoBand->setItemData( i - 1, myColor, Qt::ForegroundRole );
495 mHistoColors << myColor;
500 mHistoColors << myColors;
515 bool myFirstIteration =
true;
517 mySelectedBands = histoSelectedBands();
518 double myBinXStep = 1;
521 for (
int bandNumber = 1;
522 bandNumber <= myBandCountInt;
526 if ( mHistoShowBands != ShowAll )
528 if ( ! mySelectedBands.contains( bandNumber ) )
537 const int binCount = getBinCount( mRasterLayer->
dataProvider(), bandNumber, sampleSize );
540 QgsDebugMsg( QStringLiteral(
"got raster histo for band %1 : min=%2 max=%3 count=%4" ).arg( bandNumber ).arg( myHistogram.
minimum ).arg( myHistogram.
maximum ).arg( myHistogram.
binCount ) );
543 bool myDrawLines =
true;
544 if ( ! mHistoDrawLines &&
552 QwtPlotCurve *mypCurve =
nullptr;
555 mypCurve =
new QwtPlotCurve( tr(
"Band %1" ).arg( bandNumber ) );
557 mypCurve->setRenderHint( QwtPlotItem::RenderAntialiased );
558 mypCurve->setPen( QPen( mHistoColors.at( bandNumber ) ) );
561 QwtPlotHistogram *mypHisto =
nullptr;
564 mypHisto =
new QwtPlotHistogram( tr(
"Band %1" ).arg( bandNumber ) );
565 mypHisto->setRenderHint( QwtPlotItem::RenderAntialiased );
567 mypHisto->setPen( QPen( Qt::lightGray ) );
569 mypHisto->setBrush( QBrush( mHistoColors.at( bandNumber ) ) );
572 QVector<QPointF> data;
573 QVector<QwtIntervalSample> dataHisto;
577 myBinX = myHistogram.
minimum + myBinXStep / 2.0;
579 for (
int myBin = 0; myBin < myHistogram.
binCount; myBin++ )
584 data << QPointF( myBinX, myBinValue );
588 dataHisto << QwtIntervalSample( myBinValue, myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 );
590 myBinX += myBinXStep;
595 mypCurve->setSamples( data );
596 mypCurve->attach( mpPlot );
600 mypHisto->setSamples( dataHisto );
601 mypHisto->attach( mpPlot );
604 if ( myFirstIteration || mHistoMin > myHistogram.
minimum )
606 mHistoMin = myHistogram.
minimum;
608 if ( myFirstIteration || mHistoMax < myHistogram.
maximum )
610 mHistoMax = myHistogram.
maximum;
612 QgsDebugMsg( QStringLiteral(
"computed histo min = %1 max = %2" ).arg( mHistoMin ).arg( mHistoMax ) );
613 myFirstIteration =
false;
616 if ( mHistoMin < mHistoMax )
622 mpPlot->setAxisScale( QwtPlot::xBottom,
623 mHistoMin - myBinXStep / 2,
624 mHistoMax + myBinXStep / 2 );
625 mpPlot->setEnabled(
true );
630 mHistoMarkerMin =
new QwtPlotMarker();
631 mHistoMarkerMin->attach( mpPlot );
632 mHistoMarkerMax =
new QwtPlotMarker();
633 mHistoMarkerMax->attach( mpPlot );
634 updateHistoMarkers();
639 mHistoPicker =
new QwtPlotPicker( mpPlot->canvas() );
641 mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
642 mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
643 mHistoPicker->setStateMachine(
new QwtPickerDragPointMachine );
644 connect( mHistoPicker,
static_cast < void ( QwtPlotPicker::* )(
const QPointF & )
> ( &QwtPlotPicker::selected ),
this, &QgsRasterHistogramWidget::histoPickerSelected );
646 mHistoPicker->setEnabled(
false );
651 mHistoZoomer =
new QwtPlotZoomer( mpPlot->canvas() );
652 mHistoZoomer->setStateMachine(
new QwtPickerDragRectMachine );
653 mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
655 mHistoZoomer->setEnabled(
true );
659 mpPlot->setDisabled(
true );
661 mHistoPicker->setEnabled(
false );
663 mHistoZoomer->setEnabled(
false );
666 stackedWidget2->setCurrentIndex( 0 );
670 QApplication::restoreOverrideCursor();
673 void QgsRasterHistogramWidget::mSaveAsImageButton_clicked()
679 const QFileInfo myInfo( myFileNameAndFilter.first );
680 if ( !myInfo.baseName().isEmpty() )
687 int width,
int height,
int quality )
690 const QFileInfo myInfo( filename );
691 const QDir myDir( myInfo.dir() );
692 if ( ! myDir.exists() )
694 QgsDebugMsg( QStringLiteral(
"Error, directory %1 non-existent (theFilename = %2)" ).arg( myDir.absolutePath(), filename ) );
699 QPixmap myPixmap( width, height );
700 const QRect myQRect( 5, 5, width - 10, height - 10 );
701 myPixmap.fill( Qt::white );
703 QwtPlotRenderer myRenderer;
704 myRenderer.setDiscardFlags( QwtPlotRenderer::DiscardBackground |
705 QwtPlotRenderer::DiscardCanvasBackground );
706 myRenderer.setLayoutFlags( QwtPlotRenderer::FrameWithScales );
709 myPainter.begin( &myPixmap );
710 myRenderer.render( mpPlot, &myPainter, myQRect );
714 myPixmap.save( filename,
nullptr, quality );
722 cboHistoBand->setCurrentIndex( bandNo - 1 );
725 void QgsRasterHistogramWidget::cboHistoBand_currentIndexChanged(
int index )
727 if ( mHistoShowBands == ShowSelected )
731 index = cboHistoBand->currentIndex();
734 mHistoPicker->setEnabled(
false );
735 mHistoPicker->setRubberBandPen( QPen( mHistoColors.at( index + 1 ) ) );
738 mHistoZoomer->setEnabled(
true );
739 btnHistoMin->setEnabled(
true );
740 btnHistoMax->setEnabled(
true );
742 const QPair< QString, QString > myMinMax = rendererMinMax( index + 1 );
743 leHistoMin->setText( myMinMax.first );
744 leHistoMax->setText( myMinMax.second );
750 void QgsRasterHistogramWidget::histoActionTriggered( QAction *action )
754 histoAction( action->data().toString(), action->isChecked() );
759 if ( actionName.isEmpty() )
763 QgsDebugMsg( QStringLiteral(
"band = %1 action = %2" ).arg( cboHistoBand->currentIndex() + 1 ).arg( actionName ) );
766 if ( actionName == QLatin1String(
"Show markers" ) )
768 mHistoShowMarkers = actionFlag;
770 settings.
setValue( QStringLiteral(
"Raster/histogram/showMarkers" ), mHistoShowMarkers );
771 updateHistoMarkers();
774 else if ( actionName == QLatin1String(
"Zoom min_max" ) )
776 mHistoZoomToMinMax = actionFlag;
778 settings.
setValue( QStringLiteral(
"Raster/histogram/zoomToMinMax" ), mHistoZoomToMinMax );
781 else if ( actionName == QLatin1String(
"Update min_max" ) )
783 mHistoUpdateStyleToMinMax = actionFlag;
785 settings.
setValue( QStringLiteral(
"Raster/histogram/updateStyleToMinMax" ), mHistoUpdateStyleToMinMax );
788 else if ( actionName == QLatin1String(
"Show all" ) )
790 mHistoShowBands = ShowAll;
795 else if ( actionName == QLatin1String(
"Show selected" ) )
797 mHistoShowBands = ShowSelected;
802 else if ( actionName == QLatin1String(
"Show RGB" ) )
804 mHistoShowBands = ShowRGB;
809 else if ( actionName == QLatin1String(
"Draw lines" ) )
811 mHistoDrawLines = actionFlag;
813 settings.
setValue( QStringLiteral(
"Raster/histogram/drawLines" ), mHistoDrawLines );
814 btnHistoCompute_clicked();
818 else if ( actionName ==
"Load apply all" )
820 mHistoLoadApplyAll = actionFlag;
821 settings.setValue(
"/Raster/histogram/loadApplyAll", mHistoLoadApplyAll );
827 else if ( actionName.left( 5 ) == QLatin1String(
"Load " ) && mRendererWidget )
829 QVector<int> myBands;
833 double minMaxValues[2];
836 if ( mHistoLoadApplyAll )
838 int myBandCountInt = mRasterLayer->
bandCount();
839 for (
int i = 1; i <= myBandCountInt; i++ )
841 if ( i != cboHistoBand->currentIndex() + 1 )
848 myBands << cboHistoBand->currentIndex() + 1;
852 double myStdDev = 1.0;
853 if ( actionName ==
"Load stddev" )
855 myStdDev = mRendererWidget->
stdDev().toDouble();
860 leHistoMin->blockSignals(
true );
861 leHistoMax->blockSignals(
true );
864 const auto constMyBands = myBands;
865 for (
const int bandNo : constMyBands )
869 if ( actionName ==
"Load actual" )
871 ok = mRendererWidget->bandMinMax( QgsRasterRendererWidget::Actual,
872 bandNo, minMaxValues );
874 else if ( actionName ==
"Load estimate" )
876 ok = mRendererWidget->bandMinMax( QgsRasterRendererWidget::Estimate,
877 bandNo, minMaxValues );
879 else if ( actionName ==
"Load extent" )
881 ok = mRendererWidget->bandMinMax( QgsRasterRendererWidget::CurrentExtent,
882 bandNo, minMaxValues );
884 else if ( actionName ==
"Load 1 stddev" ||
885 actionName ==
"Load stddev" )
887 ok = mRendererWidget->bandMinMaxFromStdDev( myStdDev, bandNo, minMaxValues );
892 cboHistoBand->setCurrentIndex( bandNo - 1 );
893 if ( !ok || actionName == QLatin1String(
"Load reset" ) )
907 leHistoMin->setText( QString::number( minMaxValues[0] ) );
908 leHistoMax->setText( QString::number( minMaxValues[1] ) );
915 leHistoMin->blockSignals(
false );
916 leHistoMax->blockSignals(
false );
917 updateHistoMarkers();
919 else if ( actionName == QLatin1String(
"Compute histogram" ) )
921 btnHistoCompute_clicked();
930 void QgsRasterHistogramWidget::applyHistoMin()
932 if ( ! mRendererWidget )
935 const int bandNo = cboHistoBand->currentIndex() + 1;
936 const QList< int > mySelectedBands = rendererSelectedBands();
938 for (
int i = 0; i <= mySelectedBands.size(); i++ )
942 min = leHistoMin->text();
943 if ( mHistoUpdateStyleToMinMax && mRendererWidget->
min( i ) != min )
945 mRendererWidget->
setMin( min, i );
954 updateHistoMarkers();
956 if ( ! min.isEmpty() && mHistoZoomToMinMax && mHistoZoomer )
958 QRectF rect = mHistoZoomer->zoomRect();
959 rect.setLeft( min.toDouble() );
960 mHistoZoomer->zoom( rect );
965 void QgsRasterHistogramWidget::applyHistoMax()
967 if ( ! mRendererWidget )
970 const int bandNo = cboHistoBand->currentIndex() + 1;
971 const QList< int > mySelectedBands = rendererSelectedBands();
973 for (
int i = 0; i <= mySelectedBands.size(); i++ )
977 max = leHistoMax->text();
978 if ( mHistoUpdateStyleToMinMax && mRendererWidget->
max( i ) != max )
980 mRendererWidget->
setMax( max, i );
989 updateHistoMarkers();
991 if ( ! max.isEmpty() && mHistoZoomToMinMax && mHistoZoomer )
993 QRectF rect = mHistoZoomer->zoomRect();
994 rect.setRight( max.toDouble() );
995 mHistoZoomer->zoom( rect );
1000 void QgsRasterHistogramWidget::btnHistoMin_toggled()
1002 if ( mpPlot && mHistoPicker )
1004 if ( QApplication::overrideCursor() )
1005 QApplication::restoreOverrideCursor();
1006 if ( btnHistoMin->isChecked() )
1008 btnHistoMax->setChecked(
false );
1009 QApplication::setOverrideCursor( Qt::PointingHandCursor );
1012 mHistoZoomer->setEnabled( ! btnHistoMin->isChecked() );
1013 mHistoPicker->setEnabled( btnHistoMin->isChecked() );
1015 updateHistoMarkers();
1018 void QgsRasterHistogramWidget::btnHistoMax_toggled()
1020 if ( mpPlot && mHistoPicker )
1022 if ( QApplication::overrideCursor() )
1023 QApplication::restoreOverrideCursor();
1024 if ( btnHistoMax->isChecked() )
1026 btnHistoMin->setChecked(
false );
1027 QApplication::setOverrideCursor( Qt::PointingHandCursor );
1030 mHistoZoomer->setEnabled( ! btnHistoMax->isChecked() );
1031 mHistoPicker->setEnabled( btnHistoMax->isChecked() );
1033 updateHistoMarkers();
1040 if ( !scale )
return QString();
1042 QList< double > minorTicks = scale->ticks( QwtScaleDiv::MinorTick );
1043 QList< double > majorTicks = scale->ticks( QwtScaleDiv::MajorTick );
1044 const double diff = ( minorTicks[1] - minorTicks[0] ) / div;
1045 double min = majorTicks[0] - diff;
1047 min -= ( majorTicks[1] - majorTicks[0] );
1048 const double max = scale->upperBound();
1049 double closest = target;
1050 double current = min;
1052 while ( current < max )
1055 if ( current > target )
1057 closest = ( std::fabs( target - current + diff ) < std::fabs( target - current ) ) ? current - diff : current;
1063 return QLocale().toString( closest );
1066 void QgsRasterHistogramWidget::histoPickerSelected( QPointF pos )
1068 if ( btnHistoMin->isChecked() || btnHistoMax->isChecked() )
1070 const QwtScaleDiv *scale = &mpPlot->axisScaleDiv( QwtPlot::xBottom );
1072 if ( btnHistoMin->isChecked() )
1076 btnHistoMin->setChecked(
false );
1082 btnHistoMax->setChecked(
false );
1085 if ( QApplication::overrideCursor() )
1086 QApplication::restoreOverrideCursor();
1089 void QgsRasterHistogramWidget::histoPickerSelectedQwt5(
QwtDoublePoint pos )
1091 histoPickerSelected( QPointF( pos.x(), pos.y() ) );
1094 void QgsRasterHistogramWidget::updateHistoMarkers()
1097 if ( leHistoMin->signalsBlocked() )
1100 if ( !mpPlot || !mHistoMarkerMin || !mHistoMarkerMax )
1103 const int bandNo = cboHistoBand->currentIndex() + 1;
1104 const QList< int > mySelectedBands = histoSelectedBands();
1106 if ( ( ! mHistoShowMarkers && ! btnHistoMin->isChecked() && ! btnHistoMax->isChecked() ) ||
1107 ( ! mySelectedBands.isEmpty() && ! mySelectedBands.contains( bandNo ) ) )
1109 mHistoMarkerMin->hide();
1110 mHistoMarkerMax->hide();
1115 double minVal = mHistoMin;
1116 double maxVal = mHistoMax;
1117 const QString minStr = leHistoMin->text();
1118 const QString maxStr = leHistoMax->text();
1119 if ( !minStr.isEmpty() )
1120 minVal = minStr.toDouble();
1121 if ( !maxStr.isEmpty() )
1122 maxVal = maxStr.toDouble();
1124 QPen linePen = QPen( mHistoColors.at( bandNo ) );
1125 linePen.setStyle( Qt::DashLine );
1126 mHistoMarkerMin->setLineStyle( QwtPlotMarker::VLine );
1127 mHistoMarkerMin->setLinePen( linePen );
1128 mHistoMarkerMin->setXValue( minVal );
1129 mHistoMarkerMin->show();
1130 mHistoMarkerMax->setLineStyle( QwtPlotMarker::VLine );
1131 mHistoMarkerMax->setLinePen( linePen );
1132 mHistoMarkerMax->setXValue( maxVal );
1133 mHistoMarkerMax->show();
1139 QList< int > QgsRasterHistogramWidget::histoSelectedBands()
1141 QList< int > mySelectedBands;
1143 if ( mHistoShowBands != ShowAll )
1145 if ( mHistoShowBands == ShowSelected )
1147 mySelectedBands << cboHistoBand->currentIndex() + 1;
1149 else if ( mHistoShowBands == ShowRGB )
1151 mySelectedBands = rendererSelectedBands();
1155 return mySelectedBands;
1158 QList< int > QgsRasterHistogramWidget::rendererSelectedBands()
1160 QList< int > mySelectedBands;
1162 if ( ! mRendererWidget )
1164 mySelectedBands << -1 << -1 << -1;
1165 return mySelectedBands;
1168 if ( mRendererName == QLatin1String(
"singlebandgray" ) )
1172 else if ( mRendererName == QLatin1String(
"multibandcolor" ) )
1174 for (
int i = 0; i <= 2; i++ )
1180 return mySelectedBands;
1183 QPair< QString, QString > QgsRasterHistogramWidget::rendererMinMax(
int bandNo )
1185 QPair< QString, QString > myMinMax;
1187 if ( ! mRendererWidget )
1190 if ( mRendererName == QLatin1String(
"singlebandgray" ) )
1194 myMinMax.first = mRendererWidget->
min();
1195 myMinMax.second = mRendererWidget->
max();
1198 else if ( mRendererName == QLatin1String(
"multibandcolor" ) )
1200 for (
int i = 0; i <= 2; i++ )
1204 myMinMax.first = mRendererWidget->
min( i );
1205 myMinMax.second = mRendererWidget->
max( i );
1221 if ( myMinMax.first.isEmpty() )
1222 myMinMax.first = QLocale().toString( mHistoMin );
1223 if ( myMinMax.second.isEmpty() )
1224 myMinMax.second = QLocale().toString( mHistoMax );
1226 QgsDebugMsg( QStringLiteral(
"bandNo %1 got min/max [%2] [%3]" ).arg( bandNo ).arg( myMinMax.first, myMinMax.second ) );
DataType
Raster data types.
@ Int16
Sixteen bit signed integer (qint16)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ UInt32
Thirty two bit unsigned integer (quint32)
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static double maximumValuePossible(Qgis::DataType dataType)
Helper function that returns the maximum possible value for a data type.
static double minimumValuePossible(Qgis::DataType dataType)
Helper function that returns the minimum possible value for a data type.
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
double minimumValue
The minimum cell value in the raster band.
double maximumValue
The maximum cell value in the raster band.
Feedback object tailored for raster block reading.
Qgis::DataType sourceDataType(int bandNo) const override=0
Returns source data type for the band specified by number, source data type may be shorter than dataT...
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
The QgsRasterHistogram is a container for histogram of a single raster band.
double minimum
The minimum histogram value.
double maximum
The maximum histogram value.
QgsRasterHistogram::HistogramVector histogramVector
Stores the histogram for a given layer.
int binCount
Number of bins (intervals,buckets) in histogram.
Base class for processing filters like renderers, reprojector, resampler etc.
@ Size
Original data source size (and thus resolution) is known, it is not always available,...
virtual int xSize() const
Gets raster size.
virtual Qgis::DataType sourceDataType(int bandNo) const
Returns source data type for the band specified by number, source data type may be shorter than dataT...
virtual bool hasHistogram(int bandNo, int binCount, double minimum=std::numeric_limits< double >::quiet_NaN(), double maximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, bool includeOutOfRange=false)
Returns true if histogram is available (cached, already calculated)
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
virtual int ySize() const
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterHistogram histogram(int bandNo, int binCount=0, double minimum=std::numeric_limits< double >::quiet_NaN(), double maximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, bool includeOutOfRange=false, QgsRasterBlockFeedback *feedback=nullptr)
Returns a band histogram.
Represents a raster layer.
int bandCount() const
Returns the number of bands in this layer.
QString bandName(int bandNoInt) const
Returns the name of a band given its number.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
A rectangle specified with double values.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QPair< QString, QString > GUI_EXPORT getSaveAsImageName(QWidget *parent, const QString &message, const QString &defaultFilename)
A helper function to get an image name from the user.