31 #include <qwt_global.h>
32 #include <qwt_plot_canvas.h>
33 #include <qwt_legend.h>
35 #include <qwt_plot_curve.h>
36 #include <qwt_plot_grid.h>
37 #include <qwt_plot_marker.h>
38 #include <qwt_plot_picker.h>
39 #include <qwt_picker_machine.h>
40 #include <qwt_plot_zoomer.h>
41 #include <qwt_plot_layout.h>
42 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
43 #include <qwt_plot_renderer.h>
44 #include <qwt_plot_histogram.h>
59 mRasterLayer( lyr ), mRendererWidget( 0 )
66 mRendererName =
"singlebandgray";
73 mHistoMarkerMin = NULL;
74 mHistoMarkerMax = NULL;
77 mHistoShowMarkers = settings.value(
"/Raster/histogram/showMarkers",
false ).toBool();
79 mHistoZoomToMinMax = settings.value(
"/Raster/histogram/zoomToMinMax",
false ).toBool();
80 mHistoUpdateStyleToMinMax = settings.value(
"/Raster/histogram/updateStyleToMinMax",
true ).toBool();
81 mHistoDrawLines = settings.value(
"/Raster/histogram/drawLines",
true ).toBool();
83 mHistoShowBands = ShowAll;
89 int myBandCountInt = mRasterLayer->
bandCount();
90 for (
int myIteratorInt = 1;
91 myIteratorInt <= myBandCountInt;
94 cboHistoBand->addItem( mRasterLayer->
bandName( myIteratorInt ) );
103 leHistoMin->setValidator(
new QDoubleValidator(
this ) );
104 leHistoMax->setValidator(
new QDoubleValidator(
this ) );
110 connect( leHistoMin, SIGNAL( editingFinished() ),
this, SLOT( applyHistoMin() ) );
111 connect( leHistoMax, SIGNAL( editingFinished() ),
this, SLOT( applyHistoMax() ) );
115 QMenu* menu =
new QMenu(
this );
116 menu->setSeparatorsCollapsible(
false );
117 btnHistoActions->setMenu( menu );
122 group =
new QActionGroup(
this );
123 group->setExclusive(
false );
124 connect( group, SIGNAL( triggered( QAction* ) ),
this, SLOT( histoActionTriggered( QAction* ) ) );
125 action =
new QAction(
tr(
"Min/Max options" ), group );
126 action->setSeparator(
true );
127 menu->addAction( action );
128 action =
new QAction(
tr(
"Always show min/max markers" ), group );
129 action->setData( QVariant(
"Show markers" ) );
130 action->setCheckable(
true );
131 action->setChecked( mHistoShowMarkers );
132 menu->addAction( action );
133 action =
new QAction(
tr(
"Zoom to min/max" ), group );
134 action->setData( QVariant(
"Zoom min_max" ) );
135 action->setCheckable(
true );
136 action->setChecked( mHistoZoomToMinMax );
137 menu->addAction( action );
138 action =
new QAction(
tr(
"Update style to min/max" ), group );
139 action->setData( QVariant(
"Update min_max" ) );
140 action->setCheckable(
true );
141 action->setChecked( mHistoUpdateStyleToMinMax );
142 menu->addAction( action );
145 group =
new QActionGroup(
this );
146 group->setExclusive(
false );
147 connect( group, SIGNAL( triggered( QAction* ) ),
this, SLOT( histoActionTriggered( QAction* ) ) );
148 action =
new QAction(
tr(
"Visibility" ), group );
149 action->setSeparator(
true );
150 menu->addAction( action );
151 group =
new QActionGroup(
this );
152 group->setExclusive(
true );
153 connect( group, SIGNAL( triggered( QAction* ) ),
this, SLOT( histoActionTriggered( QAction* ) ) );
154 action =
new QAction(
tr(
"Show all bands" ), group );
155 action->setData( QVariant(
"Show all" ) );
156 action->setCheckable(
true );
157 action->setChecked( mHistoShowBands == ShowAll );
158 menu->addAction( action );
159 action =
new QAction(
tr(
"Show RGB/Gray band(s)" ), group );
160 action->setData( QVariant(
"Show RGB" ) );
161 action->setCheckable(
true );
162 action->setChecked( mHistoShowBands == ShowRGB );
163 menu->addAction( action );
164 action =
new QAction(
tr(
"Show selected band" ), group );
165 action->setData( QVariant(
"Show selected" ) );
166 action->setCheckable(
true );
167 action->setChecked( mHistoShowBands == ShowSelected );
168 menu->addAction( action );
171 group =
new QActionGroup(
this );
172 group->setExclusive(
false );
173 connect( group, SIGNAL( triggered( QAction* ) ),
this, SLOT( histoActionTriggered( QAction* ) ) );
174 action =
new QAction(
tr(
"Display" ), group );
175 action->setSeparator(
true );
176 menu->addAction( action );
178 action =
new QAction(
"", group );
179 action->setData( QVariant(
"Draw lines" ) );
182 action->setText(
tr(
"Draw as lines" ) );
183 action->setCheckable(
true );
184 action->setChecked( mHistoDrawLines );
188 action->setText(
tr(
"Draw as lines (only int layers)" ) );
189 action->setEnabled(
false );
191 menu->addAction( action );
194 action =
new QAction(
tr(
"Actions" ), group );
195 action->setSeparator(
true );
196 menu->addAction( action );
199 group =
new QActionGroup(
this );
200 group->setExclusive(
false );
201 connect( group, SIGNAL( triggered( QAction* ) ),
this, SLOT( histoActionTriggered( QAction* ) ) );
202 action =
new QAction(
tr(
"Reset" ), group );
203 action->setData( QVariant(
"Load reset" ) );
204 menu->addAction( action );
210 action =
new QAction(
tr(
"Load min/max" ), group );
211 action->setSeparator(
true );
212 menu->addAction( action );
213 action =
new QAction(
tr(
"Estimate (faster)" ), group );
214 action->setData( QVariant(
"Load estimate" ) );
215 menu->addAction( action );
216 action =
new QAction(
tr(
"Actual (slower)" ), group );
217 action->setData( QVariant(
"Load actual" ) );
218 menu->addAction( action );
219 action =
new QAction(
tr(
"Current extent" ), group );
220 action->setData( QVariant(
"Load extent" ) );
221 menu->addAction( action );
222 action =
new QAction(
tr(
"Use stddev (1.0)" ), group );
223 action->setData( QVariant(
"Load 1 stddev" ) );
224 menu->addAction( action );
225 action =
new QAction(
tr(
"Use stddev (custom)" ), group );
226 action->setData( QVariant(
"Load stddev" ) );
227 menu->addAction( action );
228 action =
new QAction(
tr(
"Load for each band" ), group );
229 action->setData( QVariant(
"Load apply all" ) );
230 action->setCheckable(
true );
231 action->setChecked( mHistoLoadApplyAll );
232 menu->addAction( action );
236 action =
new QAction(
tr(
"Recompute Histogram" ), group );
237 action->setData( QVariant(
"Compute histogram" ) );
238 menu->addAction( action );
251 mRendererName = name;
252 mRendererWidget = rendererWidget;
254 on_cboHistoBand_currentIndexChanged( -1 );
262 on_cboHistoBand_currentIndexChanged( -1 );
266 if ( QApplication::overrideCursor() )
267 QApplication::restoreOverrideCursor();
268 btnHistoMin->setChecked(
false );
269 btnHistoMax->setChecked(
false );
273 void QgsRasterHistogramWidget::on_btnHistoCompute_clicked()
289 int myBandCountInt = mRasterLayer->
bandCount();
292 if ( ! forceComputeFlag )
294 for (
int myIteratorInt = 1;
295 myIteratorInt <= myBandCountInt;
298 int sampleSize = 250000;
299 if ( !mRasterLayer->
dataProvider()->
hasHistogram( myIteratorInt, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
QgsRectangle(), sampleSize ) )
301 QgsDebugMsg( QString(
"band %1 does not have cached histo" ).arg( myIteratorInt ) );
308 stackedWidget2->setCurrentIndex( 1 );
309 connect( mRasterLayer, SIGNAL( progressUpdate(
int ) ), mHistogramProgress, SLOT( setValue(
int ) ) );
310 QApplication::setOverrideCursor( Qt::WaitCursor );
312 for (
int myIteratorInt = 1;
313 myIteratorInt <= myBandCountInt;
316 int sampleSize = 250000;
317 mRasterLayer->
dataProvider()->
histogram( myIteratorInt, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
QgsRectangle(), sampleSize );
320 disconnect( mRasterLayer, SIGNAL( progressUpdate(
int ) ), mHistogramProgress, SLOT( setValue(
int ) ) );
322 stackedWidget2->setCurrentIndex( 0 );
323 QApplication::restoreOverrideCursor();
340 int myBandCountInt = mRasterLayer->
bandCount();
346 QgsDebugMsg( QString(
"raster does not have cached histogram" ) );
347 stackedWidget2->setCurrentIndex( 2 );
352 mpPlot->detachItems();
355 mpPlot->setAutoDelete(
true );
356 mpPlot->setTitle(
QObject::tr(
"Raster Histogram" ) );
357 mpPlot->insertLegend(
new QwtLegend(), QwtPlot::BottomLegend );
359 mpPlot->setAxisTitle( QwtPlot::xBottom,
QObject::tr(
"Pixel Value" ) );
360 mpPlot->setAxisTitle( QwtPlot::yLeft,
QObject::tr(
"Frequency" ) );
361 mpPlot->setAxisAutoScale( QwtPlot::yLeft );
365 QwtPlotGrid * myGrid =
new QwtPlotGrid();
366 myGrid->attach( mpPlot );
369 mHistoColors.clear();
370 mHistoColors << Qt::black;
371 QVector<QColor> myColors;
372 myColors << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkYellow << Qt::cyan;
373 qsrand( myBandCountInt * 100 );
374 while ( myColors.size() <= myBandCountInt )
377 QColor( 1 + (
int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ),
378 1 + (
int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ),
379 1 + (
int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ) );
382 qsrand( time( NULL ) );
386 QList< int > mySelectedBands = rendererSelectedBands();
387 if ( mRendererName ==
"singlebandgray" )
389 int myGrayBand = mySelectedBands[0];
390 for (
int i = 1; i <= myBandCountInt; i++ )
392 if ( i == myGrayBand )
394 mHistoColors << Qt::darkGray;
395 cboHistoBand->setItemData( i - 1, QColor( Qt::darkGray ), Qt::ForegroundRole );
399 if ( ! myColors.isEmpty() )
401 mHistoColors << myColors.first();
402 myColors.pop_front();
406 mHistoColors << Qt::black;
408 cboHistoBand->setItemData( i - 1, QColor( Qt::black ), Qt::ForegroundRole );
413 else if ( mRendererName ==
"multibandcolor" )
415 int myRedBand = mySelectedBands[0];
416 int myGreenBand = mySelectedBands[1];
417 int myBlueBand = mySelectedBands[2];
420 myColors.remove( 0, 3 );
421 for (
int i = 1; i <= myBandCountInt; i++ )
424 if ( i == myRedBand )
426 else if ( i == myGreenBand )
428 else if ( i == myBlueBand )
432 if ( ! myColors.isEmpty() )
434 myColor = myColors.first();
435 myColors.pop_front();
441 cboHistoBand->setItemData( i - 1, QColor( Qt::black ), Qt::ForegroundRole );
443 if ( i == myRedBand || i == myGreenBand || i == myBlueBand )
445 cboHistoBand->setItemData( i - 1, myColor, Qt::ForegroundRole );
447 mHistoColors << myColor;
452 mHistoColors << myColors;
467 bool myFirstIteration =
true;
469 mySelectedBands = histoSelectedBands();
470 double myBinXStep = 1;
473 for (
int myIteratorInt = 1;
474 myIteratorInt <= myBandCountInt;
478 if ( mHistoShowBands != ShowAll )
480 if ( ! mySelectedBands.contains( myIteratorInt ) )
484 int sampleSize = 250000;
487 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 ) );
490 bool myDrawLines =
true;
491 if ( ! mHistoDrawLines &&
499 QwtPlotCurve * mypCurve = 0;
502 mypCurve =
new QwtPlotCurve(
tr(
"Band %1" ).arg( myIteratorInt ) );
504 mypCurve->setRenderHint( QwtPlotItem::RenderAntialiased );
505 mypCurve->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
508 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
509 QwtPlotHistogram * mypHisto = 0;
512 mypHisto =
new QwtPlotHistogram(
tr(
"Band %1" ).arg( myIteratorInt ) );
513 mypHisto->setRenderHint( QwtPlotItem::RenderAntialiased );
515 mypHisto->setPen( QPen( Qt::lightGray ) );
517 mypHisto->setBrush( QBrush( mHistoColors.at( myIteratorInt ) ) );
523 mypHistoItem =
new HistogramItem(
tr(
"Band %1" ).arg( myIteratorInt ) );
524 mypHistoItem->setRenderHint( QwtPlotItem::RenderAntialiased );
525 mypHistoItem->
setColor( mHistoColors.at( myIteratorInt ) );
529 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
530 QVector<QPointF> data;
531 QVector<QwtIntervalSample> dataHisto;
533 QVector<double> myX2Data;
534 QVector<double> myY2Data;
536 QwtArray<QwtDoubleInterval> intervalsHisto;
537 QwtArray<double> valuesHisto;
545 myBinX = myHistogram.
minimum + myBinXStep / 2.0;
553 for (
int myBin = 0; myBin < myHistogram.
binCount; myBin++ )
556 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
559 data << QPointF( myBinX, myBinValue );
563 dataHisto << QwtIntervalSample( myBinValue, myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 );
568 myX2Data.append(
double( myBinX ) );
569 myY2Data.append(
double( myBinValue ) );
573 intervalsHisto.append( QwtDoubleInterval( myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 ) );
574 valuesHisto.append(
double( myBinValue ) );
577 myBinX += myBinXStep;
580 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
583 mypCurve->setSamples( data );
584 mypCurve->attach( mpPlot );
588 mypHisto->setSamples( dataHisto );
589 mypHisto->attach( mpPlot );
594 mypCurve->setData( myX2Data, myY2Data );
595 mypCurve->attach( mpPlot );
599 mypHistoItem->
setData( QwtIntervalData( intervalsHisto, valuesHisto ) );
600 mypHistoItem->attach( mpPlot );
604 if ( myFirstIteration || mHistoMin > myHistogram.
minimum )
606 mHistoMin = myHistogram.
minimum;
608 if ( myFirstIteration || mHistoMax < myHistogram.
maximum )
610 mHistoMax = myHistogram.
maximum;
612 QgsDebugMsg( QString(
"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 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
644 mHistoPicker->setStateMachine(
new QwtPickerDragPointMachine );
645 connect( mHistoPicker, SIGNAL( selected(
const QPointF & ) ),
this, SLOT( histoPickerSelected(
const QPointF & ) ) );
647 mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
648 connect( mHistoPicker, SIGNAL( selected(
const QwtDoublePoint & ) ),
this, SLOT( histoPickerSelectedQwt5(
const QwtDoublePoint & ) ) );
651 mHistoPicker->setEnabled(
false );
656 mHistoZoomer =
new QwtPlotZoomer( mpPlot->canvas() );
657 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
658 mHistoZoomer->setStateMachine(
new QwtPickerDragRectMachine );
660 mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
662 mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
664 mHistoZoomer->setEnabled(
true );
668 mpPlot->setDisabled(
true );
670 mHistoPicker->setEnabled(
false );
672 mHistoZoomer->setEnabled(
false );
675 disconnect( mRasterLayer, SIGNAL( progressUpdate(
int ) ), mHistogramProgress, SLOT( setValue(
int ) ) );
676 stackedWidget2->setCurrentIndex( 0 );
680 QApplication::restoreOverrideCursor();
691 QFileInfo myInfo( myFileNameAndFilter.first );
692 if ( QFileInfo( myFileNameAndFilter.first ).baseName() !=
"" )
699 int width,
int height,
int quality )
702 QFileInfo myInfo( theFilename );
703 QDir myDir( myInfo.dir() );
704 if ( ! myDir.exists() )
706 QgsDebugMsg( QString(
"Error, directory %1 non-existent (theFilename = %2)" ).arg( myDir.absolutePath() ).arg( theFilename ) );
711 QPixmap myPixmap( width, height );
712 QRect myQRect( 5, 5, width - 10, height - 10 );
713 myPixmap.fill( Qt::white );
715 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
716 QwtPlotRenderer myRenderer;
717 myRenderer.setDiscardFlags( QwtPlotRenderer::DiscardBackground |
718 QwtPlotRenderer::DiscardCanvasBackground );
719 myRenderer.setLayoutFlags( QwtPlotRenderer::FrameWithScales );
722 myPainter.begin( &myPixmap );
723 myRenderer.render( mpPlot, &myPainter, myQRect );
726 QwtPlotPrintFilter myFilter;
727 int myOptions = QwtPlotPrintFilter::PrintAll;
728 myOptions &= ~QwtPlotPrintFilter::PrintBackground;
729 myOptions |= QwtPlotPrintFilter::PrintFrameWithScales;
730 myFilter.setOptions( myOptions );
733 myPainter.begin( &myPixmap );
734 mpPlot->print( &myPainter, myQRect, myFilter );
744 myPixmap.save( theFilename, 0, quality );
752 cboHistoBand->setCurrentIndex( theBandNo - 1 );
755 void QgsRasterHistogramWidget::on_cboHistoBand_currentIndexChanged(
int index )
757 if ( mHistoShowBands == ShowSelected )
761 index = cboHistoBand->currentIndex();
762 if ( mHistoPicker != NULL )
764 mHistoPicker->setEnabled(
false );
765 mHistoPicker->setRubberBandPen( QPen( mHistoColors.at( index + 1 ) ) );
767 if ( mHistoZoomer != NULL )
768 mHistoZoomer->setEnabled(
true );
769 btnHistoMin->setEnabled(
true );
770 btnHistoMax->setEnabled(
true );
772 QPair< QString, QString > myMinMax = rendererMinMax( index + 1 );
773 leHistoMin->setText( myMinMax.first );
774 leHistoMax->setText( myMinMax.second );
780 void QgsRasterHistogramWidget::histoActionTriggered( QAction* action )
784 histoAction( action->data().toString(), action->isChecked() );
789 if ( actionName ==
"" )
793 QgsDebugMsg( QString(
"band = %1 action = %2" ).arg( cboHistoBand->currentIndex() + 1 ).arg( actionName ) );
796 if ( actionName ==
"Show markers" )
798 mHistoShowMarkers = actionFlag;
800 settings.setValue(
"/Raster/histogram/showMarkers", mHistoShowMarkers );
801 updateHistoMarkers();
804 else if ( actionName ==
"Zoom min_max" )
806 mHistoZoomToMinMax = actionFlag;
808 settings.setValue(
"/Raster/histogram/zoomToMinMax", mHistoZoomToMinMax );
811 else if ( actionName ==
"Update min_max" )
813 mHistoUpdateStyleToMinMax = actionFlag;
815 settings.setValue(
"/Raster/histogram/updateStyleToMinMax", mHistoUpdateStyleToMinMax );
818 else if ( actionName ==
"Show all" )
820 mHistoShowBands = ShowAll;
825 else if ( actionName ==
"Show selected" )
827 mHistoShowBands = ShowSelected;
832 else if ( actionName ==
"Show RGB" )
834 mHistoShowBands = ShowRGB;
839 else if ( actionName ==
"Draw lines" )
841 mHistoDrawLines = actionFlag;
843 settings.setValue(
"/Raster/histogram/drawLines", mHistoDrawLines );
844 on_btnHistoCompute_clicked();
848 else if ( actionName ==
"Load apply all" )
850 mHistoLoadApplyAll = actionFlag;
851 settings.setValue(
"/Raster/histogram/loadApplyAll", mHistoLoadApplyAll );
857 else if ( actionName.left( 5 ) ==
"Load " && mRendererWidget )
859 QVector<int> myBands;
863 double minMaxValues[2];
866 if ( mHistoLoadApplyAll )
868 int myBandCountInt = mRasterLayer->
bandCount();
869 for (
int i = 1; i <= myBandCountInt; i++ )
871 if ( i != cboHistoBand->currentIndex() + 1 )
878 myBands << cboHistoBand->currentIndex() + 1;
890 leHistoMin->blockSignals(
true );
891 leHistoMax->blockSignals(
true );
894 foreach (
int theBandNo, myBands )
898 if ( actionName ==
"Load actual" )
901 theBandNo, minMaxValues );
903 else if ( actionName ==
"Load estimate" )
906 theBandNo, minMaxValues );
908 else if ( actionName ==
"Load extent" )
911 theBandNo, minMaxValues );
913 else if ( actionName ==
"Load 1 stddev" ||
914 actionName ==
"Load stddev" )
916 ok = mRendererWidget->bandMinMaxFromStdDev( myStdDev, theBandNo, minMaxValues );
921 cboHistoBand->setCurrentIndex( theBandNo - 1 );
922 if ( !ok || actionName ==
"Load reset" )
936 leHistoMin->setText( QString::number( minMaxValues[0] ) );
937 leHistoMax->setText( QString::number( minMaxValues[1] ) );
944 leHistoMin->blockSignals(
false );
945 leHistoMax->blockSignals(
false );
946 updateHistoMarkers();
948 else if ( actionName ==
"Compute histogram" )
950 on_btnHistoCompute_clicked();
959 void QgsRasterHistogramWidget::applyHistoMin()
961 if ( ! mRendererWidget )
964 int theBandNo = cboHistoBand->currentIndex() + 1;
965 QList< int > mySelectedBands = rendererSelectedBands();
967 for (
int i = 0; i <= mySelectedBands.size(); i++ )
971 min = leHistoMin->text();
972 if ( mHistoUpdateStyleToMinMax )
973 mRendererWidget->
setMin( min, i );
977 updateHistoMarkers();
979 if ( ! min.isEmpty() && mHistoZoomToMinMax && mHistoZoomer )
981 QRectF rect = mHistoZoomer->zoomRect();
982 rect.setLeft( min.toDouble() );
983 mHistoZoomer->zoom( rect );
988 void QgsRasterHistogramWidget::applyHistoMax()
990 if ( ! mRendererWidget )
993 int theBandNo = cboHistoBand->currentIndex() + 1;
994 QList< int > mySelectedBands = rendererSelectedBands();
996 for (
int i = 0; i <= mySelectedBands.size(); i++ )
1000 max = leHistoMax->text();
1001 if ( mHistoUpdateStyleToMinMax )
1002 mRendererWidget->
setMax( max, i );
1006 updateHistoMarkers();
1008 if ( ! max.isEmpty() && mHistoZoomToMinMax && mHistoZoomer )
1010 QRectF rect = mHistoZoomer->zoomRect();
1011 rect.setRight( max.toDouble() );
1012 mHistoZoomer->zoom( rect );
1016 void QgsRasterHistogramWidget::on_btnHistoMin_toggled()
1018 if ( mpPlot != NULL && mHistoPicker != NULL )
1020 if ( QApplication::overrideCursor() )
1021 QApplication::restoreOverrideCursor();
1022 if ( btnHistoMin->isChecked() )
1024 btnHistoMax->setChecked(
false );
1025 QApplication::setOverrideCursor( Qt::PointingHandCursor );
1027 if ( mHistoZoomer != NULL )
1028 mHistoZoomer->setEnabled( ! btnHistoMin->isChecked() );
1029 mHistoPicker->setEnabled( btnHistoMin->isChecked() );
1031 updateHistoMarkers();
1034 void QgsRasterHistogramWidget::on_btnHistoMax_toggled()
1036 if ( mpPlot != NULL && mHistoPicker != NULL )
1038 if ( QApplication::overrideCursor() )
1039 QApplication::restoreOverrideCursor();
1040 if ( btnHistoMax->isChecked() )
1042 btnHistoMin->setChecked(
false );
1043 QApplication::setOverrideCursor( Qt::PointingHandCursor );
1045 if ( mHistoZoomer != NULL )
1046 mHistoZoomer->setEnabled( ! btnHistoMax->isChecked() );
1047 mHistoPicker->setEnabled( btnHistoMax->isChecked() );
1049 updateHistoMarkers();
1056 if ( !scale )
return "";
1058 QList< double > minorTicks = scale->ticks( QwtScaleDiv::MinorTick );
1059 QList< double > majorTicks = scale->ticks( QwtScaleDiv::MajorTick );
1060 double diff = ( minorTicks[1] - minorTicks[0] ) / div;
1061 double min = majorTicks[0] - diff;
1063 min -= ( majorTicks[1] - majorTicks[0] );
1064 #if defined(QWT_VERSION) && QWT_VERSION<0x050200
1065 double max = scale->hBound();
1067 double max = scale->upperBound();
1069 double closest = target;
1070 double current =
min;
1072 while ( current < max )
1075 if ( current > target )
1077 closest = ( qAbs( target - current + diff ) < qAbs( target - current ) ) ? current - diff : current;
1083 return QString::number( closest );
1086 void QgsRasterHistogramWidget::histoPickerSelected(
const QPointF & pos )
1088 if ( btnHistoMin->isChecked() || btnHistoMax->isChecked() )
1090 #if defined(QWT_VERSION) && QWT_VERSION>=0x060100
1091 const QwtScaleDiv * scale = &mpPlot->axisScaleDiv( QwtPlot::xBottom );
1093 const QwtScaleDiv * scale = mpPlot->axisScaleDiv( QwtPlot::xBottom );
1096 if ( btnHistoMin->isChecked() )
1100 btnHistoMin->setChecked(
false );
1106 btnHistoMax->setChecked(
false );
1109 if ( QApplication::overrideCursor() )
1110 QApplication::restoreOverrideCursor();
1113 void QgsRasterHistogramWidget::histoPickerSelectedQwt5(
const QwtDoublePoint & pos )
1115 histoPickerSelected( QPointF( pos.x(), pos.y() ) );
1118 void QgsRasterHistogramWidget::updateHistoMarkers()
1121 if ( leHistoMin->signalsBlocked() )
1124 if ( mpPlot == NULL || mHistoMarkerMin == NULL || mHistoMarkerMax == NULL )
1127 int theBandNo = cboHistoBand->currentIndex() + 1;
1128 QList< int > mySelectedBands = histoSelectedBands();
1130 if (( ! mHistoShowMarkers && ! btnHistoMin->isChecked() && ! btnHistoMax->isChecked() ) ||
1131 ( ! mySelectedBands.isEmpty() && ! mySelectedBands.contains( theBandNo ) ) )
1133 mHistoMarkerMin->hide();
1134 mHistoMarkerMax->hide();
1139 double minVal = mHistoMin;
1140 double maxVal = mHistoMax;
1141 QString minStr = leHistoMin->text();
1142 QString maxStr = leHistoMax->text();
1144 minVal = minStr.toDouble();
1146 maxVal = maxStr.toDouble();
1148 QPen linePen = QPen( mHistoColors.at( theBandNo ) );
1149 linePen.setStyle( Qt::DashLine );
1150 mHistoMarkerMin->setLineStyle( QwtPlotMarker::VLine );
1151 mHistoMarkerMin->setLinePen( linePen );
1152 mHistoMarkerMin->setXValue( minVal );
1153 mHistoMarkerMin->show();
1154 mHistoMarkerMax->setLineStyle( QwtPlotMarker::VLine );
1155 mHistoMarkerMax->setLinePen( linePen );
1156 mHistoMarkerMax->setXValue( maxVal );
1157 mHistoMarkerMax->show();
1163 QList< int > QgsRasterHistogramWidget::histoSelectedBands()
1165 QList< int > mySelectedBands;
1167 if ( mHistoShowBands != ShowAll )
1169 if ( mHistoShowBands == ShowSelected )
1171 mySelectedBands << cboHistoBand->currentIndex() + 1;
1173 else if ( mHistoShowBands == ShowRGB )
1175 mySelectedBands = rendererSelectedBands();
1179 return mySelectedBands;
1182 QList< int > QgsRasterHistogramWidget::rendererSelectedBands()
1184 QList< int > mySelectedBands;
1186 if ( ! mRendererWidget )
1188 mySelectedBands << -1 << -1 << -1;
1189 return mySelectedBands;
1192 if ( mRendererName ==
"singlebandgray" )
1196 else if ( mRendererName ==
"multibandcolor" )
1198 for (
int i = 0; i <= 2; i++ )
1204 return mySelectedBands;
1207 QPair< QString, QString > QgsRasterHistogramWidget::rendererMinMax(
int theBandNo )
1209 QPair< QString, QString > myMinMax;
1211 if ( ! mRendererWidget )
1214 if ( mRendererName ==
"singlebandgray" )
1218 myMinMax.first = mRendererWidget->
min();
1219 myMinMax.second = mRendererWidget->
max();
1222 else if ( mRendererName ==
"multibandcolor" )
1224 for (
int i = 0; i <= 2; i++ )
1228 myMinMax.first = mRendererWidget->
min( i );
1229 myMinMax.second = mRendererWidget->
max( i );
1245 if ( myMinMax.first.isEmpty() )
1246 myMinMax.first = QString::number( mHistoMin );
1247 if ( myMinMax.second.isEmpty() )
1248 myMinMax.second = QString::number( mHistoMax );
1250 QgsDebugMsg( QString(
"bandNo %1 got min/max [%2] [%3]" ).arg( theBandNo ).arg( myMinMax.first ).arg( myMinMax.second ) );