/
qgsrasterhistogramwidget.cpp
1220 lines (1092 loc) · 39.2 KB
/
qgsrasterhistogramwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsrasterhistogramwidget.cpp
---------------------------
begin : July 2012
copyright : (C) 2012 by Etienne Tourigny
email : etourigny dot dev at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsapplication.h"
#include "qgisgui.h"
#include "qgsrasterrendererregistry.h"
#include "qgsrasterrendererwidget.h"
#include "qgsrasterhistogramwidget.h"
#include <QMenu>
#include <QFileInfo>
#include <QDir>
#include <QPainter>
#include <QSettings>
// QWT Charting widget
#include <qwt_global.h>
#include <qwt_plot_canvas.h>
#include <qwt_legend.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_picker.h>
#include <qwt_picker_machine.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_layout.h>
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
#include <qwt_plot_renderer.h>
#include <qwt_plot_histogram.h>
#else
#include "qwt5_histogram_item.h"
#endif
// this has been removed, now we let the provider/raster interface decide
// how many bins are suitable depending on data type and range
//#define RASTER_HISTOGRAM_BINS 256
QgsRasterHistogramWidget::QgsRasterHistogramWidget( QgsRasterLayer* lyr, QWidget *parent )
: QWidget( parent ),
mRasterLayer( lyr ), mRendererWidget( 0 )
{
setupUi( this );
mSaveAsImageButton->setIcon( QgsApplication::getThemeIcon( "/mActionFileSave.svg" ) );
mRendererWidget = 0;
mRendererName = "singlebandgray";
mHistoMin = 0;
mHistoMax = 0;
mHistoPicker = NULL;
mHistoZoomer = NULL;
mHistoMarkerMin = NULL;
mHistoMarkerMax = NULL;
QSettings settings;
mHistoShowMarkers = settings.value( "/Raster/histogram/showMarkers", false ).toBool();
// mHistoLoadApplyAll = settings.value( "/Raster/histogram/loadApplyAll", false ).toBool();
mHistoZoomToMinMax = settings.value( "/Raster/histogram/zoomToMinMax", false ).toBool();
mHistoUpdateStyleToMinMax = settings.value( "/Raster/histogram/updateStyleToMinMax", true ).toBool();
mHistoDrawLines = settings.value( "/Raster/histogram/drawLines", true ).toBool();
// mHistoShowBands = (HistoShowBands) settings.value( "/Raster/histogram/showBands", (int) ShowAll ).toInt();
mHistoShowBands = ShowAll;
if ( true )
{
//band selector
int myBandCountInt = mRasterLayer->bandCount();
for ( int myIteratorInt = 1;
myIteratorInt <= myBandCountInt;
++myIteratorInt )
{
cboHistoBand->addItem( mRasterLayer->bandName( myIteratorInt ) );
}
// histo min/max selectors
leHistoMin->setValidator( new QDoubleValidator( this ) );
leHistoMax->setValidator( new QDoubleValidator( this ) );
// this might generate many refresh events! test..
// connect( leHistoMin, SIGNAL( textChanged( const QString & ) ), this, SLOT( updateHistoMarkers() ) );
// connect( leHistoMax, SIGNAL( textChanged( const QString & ) ), this, SLOT( updateHistoMarkers() ) );
// connect( leHistoMin, SIGNAL( textChanged( const QString & ) ), this, SLOT( applyHistoMin() ) );
// connect( leHistoMax, SIGNAL( textChanged( const QString & ) ), this, SLOT( applyHistoMax() ) );
connect( leHistoMin, SIGNAL( editingFinished() ), this, SLOT( applyHistoMin() ) );
connect( leHistoMax, SIGNAL( editingFinished() ), this, SLOT( applyHistoMax() ) );
// histo actions
// TODO move/add options to qgis options dialog
QMenu* menu = new QMenu( this );
menu->setSeparatorsCollapsible( false );
btnHistoActions->setMenu( menu );
QActionGroup* group;
QAction* action;
// min/max options
group = new QActionGroup( this );
group->setExclusive( false );
connect( group, SIGNAL( triggered( QAction* ) ), this, SLOT( histoActionTriggered( QAction* ) ) );
action = new QAction( tr( "Min/Max options" ), group );
action->setSeparator( true );
menu->addAction( action );
action = new QAction( tr( "Always show min/max markers" ), group );
action->setData( QVariant( "Show markers" ) );
action->setCheckable( true );
action->setChecked( mHistoShowMarkers );
menu->addAction( action );
action = new QAction( tr( "Zoom to min/max" ), group );
action->setData( QVariant( "Zoom min_max" ) );
action->setCheckable( true );
action->setChecked( mHistoZoomToMinMax );
menu->addAction( action );
action = new QAction( tr( "Update style to min/max" ), group );
action->setData( QVariant( "Update min_max" ) );
action->setCheckable( true );
action->setChecked( mHistoUpdateStyleToMinMax );
menu->addAction( action );
// visibility options
group = new QActionGroup( this );
group->setExclusive( false );
connect( group, SIGNAL( triggered( QAction* ) ), this, SLOT( histoActionTriggered( QAction* ) ) );
action = new QAction( tr( "Visibility" ), group );
action->setSeparator( true );
menu->addAction( action );
group = new QActionGroup( this );
group->setExclusive( true ); // these options are exclusive
connect( group, SIGNAL( triggered( QAction* ) ), this, SLOT( histoActionTriggered( QAction* ) ) );
action = new QAction( tr( "Show all bands" ), group );
action->setData( QVariant( "Show all" ) );
action->setCheckable( true );
action->setChecked( mHistoShowBands == ShowAll );
menu->addAction( action );
action = new QAction( tr( "Show RGB/Gray band(s)" ), group );
action->setData( QVariant( "Show RGB" ) );
action->setCheckable( true );
action->setChecked( mHistoShowBands == ShowRGB );
menu->addAction( action );
action = new QAction( tr( "Show selected band" ), group );
action->setData( QVariant( "Show selected" ) );
action->setCheckable( true );
action->setChecked( mHistoShowBands == ShowSelected );
menu->addAction( action );
// display options
group = new QActionGroup( this );
group->setExclusive( false );
connect( group, SIGNAL( triggered( QAction* ) ), this, SLOT( histoActionTriggered( QAction* ) ) );
action = new QAction( tr( "Display" ), group );
action->setSeparator( true );
menu->addAction( action );
action = new QAction( tr( "Draw as lines" ), group );
action->setData( QVariant( "Draw lines" ) );
action->setCheckable( true );
// should we plot as histogram instead of line plot? (int data only)
action->setChecked( mHistoDrawLines );
menu->addAction( action );
// actions
action = new QAction( tr( "Actions" ), group );
action->setSeparator( true );
menu->addAction( action );
// load actions
group = new QActionGroup( this );
group->setExclusive( false );
connect( group, SIGNAL( triggered( QAction* ) ), this, SLOT( histoActionTriggered( QAction* ) ) );
action = new QAction( tr( "Reset" ), group );
action->setData( QVariant( "Load reset" ) );
menu->addAction( action );
// these actions have been disabled for api cleanup, restore them eventually
// TODO restore these in qgis 2.4
#if 0
// Load min/max needs 3 params (method, extent, accuracy), cannot put it in single item
action = new QAction( tr( "Load min/max" ), group );
action->setSeparator( true );
menu->addAction( action );
action = new QAction( tr( "Estimate (faster)" ), group );
action->setData( QVariant( "Load estimate" ) );
menu->addAction( action );
action = new QAction( tr( "Actual (slower)" ), group );
action->setData( QVariant( "Load actual" ) );
menu->addAction( action );
action = new QAction( tr( "Current extent" ), group );
action->setData( QVariant( "Load extent" ) );
menu->addAction( action );
action = new QAction( tr( "Use stddev (1.0)" ), group );
action->setData( QVariant( "Load 1 stddev" ) );
menu->addAction( action );
action = new QAction( tr( "Use stddev (custom)" ), group );
action->setData( QVariant( "Load stddev" ) );
menu->addAction( action );
action = new QAction( tr( "Load for each band" ), group );
action->setData( QVariant( "Load apply all" ) );
action->setCheckable( true );
action->setChecked( mHistoLoadApplyAll );
menu->addAction( action );
#endif
//others
action = new QAction( tr( "Recompute Histogram" ), group );
action->setData( QVariant( "Compute histogram" ) );
menu->addAction( action );
}
} // QgsRasterHistogramWidget ctor
QgsRasterHistogramWidget::~QgsRasterHistogramWidget()
{
}
void QgsRasterHistogramWidget::setRendererWidget( const QString& name, QgsRasterRendererWidget* rendererWidget )
{
mRendererName = name;
mRendererWidget = rendererWidget;
refreshHistogram();
on_cboHistoBand_currentIndexChanged( -1 );
}
void QgsRasterHistogramWidget::setActive( bool theActiveFlag )
{
if ( theActiveFlag )
{
refreshHistogram();
on_cboHistoBand_currentIndexChanged( -1 );
}
else
{
if ( QApplication::overrideCursor() )
QApplication::restoreOverrideCursor();
btnHistoMin->setChecked( false );
btnHistoMax->setChecked( false );
}
}
void QgsRasterHistogramWidget::on_btnHistoCompute_clicked()
{
// Histogram computation can be called either by clicking the "Compute Histogram" button
// which is only visible if there is no cached histogram or by calling the
// "Compute Histogram" action. Due to limitations in the gdal api, it is not possible
// to re-calculate the histogram if it has already been calculated
computeHistogram( true );
refreshHistogram();
}
bool QgsRasterHistogramWidget::computeHistogram( bool forceComputeFlag )
{
QgsDebugMsg( "entered." );
//bool myIgnoreOutOfRangeFlag = true;
//bool myThoroughBandScanFlag = false;
int myBandCountInt = mRasterLayer->bandCount();
// if forceComputeFlag = false make sure raster has cached histogram, else return false
if ( ! forceComputeFlag )
{
for ( int myIteratorInt = 1;
myIteratorInt <= myBandCountInt;
++myIteratorInt )
{
int sampleSize = 250000; // number of sample cells
if ( !mRasterLayer->dataProvider()->hasHistogram( myIteratorInt, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), QgsRectangle(), sampleSize ) )
{
QgsDebugMsg( QString( "band %1 does not have cached histo" ).arg( myIteratorInt ) );
return false;
}
}
}
// compute histogram
stackedWidget2->setCurrentIndex( 1 );
connect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
QApplication::setOverrideCursor( Qt::WaitCursor );
for ( int myIteratorInt = 1;
myIteratorInt <= myBandCountInt;
++myIteratorInt )
{
int sampleSize = 250000; // number of sample cells
mRasterLayer->dataProvider()->histogram( myIteratorInt, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), QgsRectangle(), sampleSize );
}
disconnect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
// mHistogramProgress->hide();
stackedWidget2->setCurrentIndex( 0 );
QApplication::restoreOverrideCursor();
return true;
}
void QgsRasterHistogramWidget::refreshHistogram()
{
// Explanation:
// We use the gdal histogram creation routine is called for each selected
// layer. Currently the hist is hardcoded to create 256 bins. Each bin stores
// the total number of cells that fit into the range defined by that bin.
//
// The graph routine below determines the greatest number of pixels in any given
// bin in all selected layers, and the min. It then draws a scaled line between min
// and max - scaled to image height. 1 line drawn per selected band
//
int myBandCountInt = mRasterLayer->bandCount();
QgsDebugMsg( "entered." );
if ( ! computeHistogram( false ) )
{
QgsDebugMsg( QString( "raster does not have cached histogram" ) );
stackedWidget2->setCurrentIndex( 2 );
return;
}
// clear plot
mpPlot->detachItems();
//ensure all children get removed
mpPlot->setAutoDelete( true );
mpPlot->setTitle( QObject::tr( "Raster Histogram" ) );
mpPlot->insertLegend( new QwtLegend(), QwtPlot::BottomLegend );
// Set axis titles
mpPlot->setAxisTitle( QwtPlot::xBottom, QObject::tr( "Pixel Value" ) );
mpPlot->setAxisTitle( QwtPlot::yLeft, QObject::tr( "Frequency" ) );
mpPlot->setAxisAutoScale( QwtPlot::yLeft );
// x axis scale only set after computing global min/max across bands (see below)
// add a grid
QwtPlotGrid * myGrid = new QwtPlotGrid();
myGrid->attach( mpPlot );
// make colors list
mHistoColors.clear();
mHistoColors << Qt::black; // first element, not used
QVector<QColor> myColors;
myColors << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkYellow << Qt::cyan;
srand( myBandCountInt * 100 ); // make sure colors are always the same for a given band count
while ( myColors.size() <= myBandCountInt )
{
myColors <<
QColor( 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ),
1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ),
1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ) );
}
// assign colors to each band, depending on the current RGB/gray band selection
// grayscale
QList< int > mySelectedBands = rendererSelectedBands();
if ( mRendererName == "singlebandgray" )
{
int myGrayBand = mySelectedBands[0];
for ( int i = 1; i <= myBandCountInt; i++ )
{
if ( i == myGrayBand )
{
mHistoColors << Qt::darkGray;
cboHistoBand->setItemData( i - 1, Qt::darkGray, Qt::ForegroundRole );
}
else
{
if ( ! myColors.isEmpty() )
{
mHistoColors << myColors.first();
myColors.pop_front();
}
else
{
mHistoColors << Qt::black;
}
cboHistoBand->setItemData( i - 1, Qt::black, Qt::ForegroundRole );
}
}
}
// RGB
else if ( mRendererName == "multibandcolor" )
{
int myRedBand = mySelectedBands[0];
int myGreenBand = mySelectedBands[1];
int myBlueBand = mySelectedBands[2];
// remove RGB, which are reserved for the actual RGB bands
// show name of RGB bands in appropriate color in bold
myColors.remove( 0, 3 );
for ( int i = 1; i <= myBandCountInt; i++ )
{
QColor myColor;
if ( i == myRedBand )
myColor = Qt::red;
else if ( i == myGreenBand )
myColor = Qt::green;
else if ( i == myBlueBand )
myColor = Qt::blue;
else
{
if ( ! myColors.isEmpty() )
{
myColor = myColors.first();
myColors.pop_front();
}
else
{
myColor = Qt::black;
}
cboHistoBand->setItemData( i - 1, Qt::black, Qt::ForegroundRole );
}
if ( i == myRedBand || i == myGreenBand || i == myBlueBand )
{
cboHistoBand->setItemData( i - 1, myColor, Qt::ForegroundRole );
}
mHistoColors << myColor;
}
}
else
{
mHistoColors << myColors;
}
//
//now draw actual graphs
//
//somtimes there are more bins than needed
//we find out the last one that actually has data in it
//so we can discard the rest and set the x-axis scales correctly
//
// scan through to get counts from layers' histograms
//
mHistoMin = 0;
mHistoMax = 0;
bool myFirstIteration = true;
/* get selected band list, if mHistoShowBands != ShowAll */
mySelectedBands = histoSelectedBands();
double myBinXStep = 1;
double myBinX = 0;
for ( int myIteratorInt = 1;
myIteratorInt <= myBandCountInt;
++myIteratorInt )
{
/* skip this band if mHistoShowBands != ShowAll and this band is not selected */
if ( mHistoShowBands != ShowAll )
{
if ( ! mySelectedBands.contains( myIteratorInt ) )
continue;
}
int sampleSize = 250000; // number of sample cells
QgsRasterHistogram myHistogram = mRasterLayer->dataProvider()->histogram( myIteratorInt, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), QgsRectangle(), sampleSize );
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 ) );
QGis::DataType mySrcDataType = mRasterLayer->dataProvider()->srcDataType( myIteratorInt );
bool myDrawLines = true;
if ( ! mHistoDrawLines &&
( mySrcDataType == QGis::Byte ||
mySrcDataType == QGis::Int16 || mySrcDataType == QGis::Int32 ||
mySrcDataType == QGis::UInt16 || mySrcDataType == QGis::UInt32 ) )
{
myDrawLines = false;
}
QwtPlotCurve * mypCurve = 0;
if ( myDrawLines )
{
mypCurve = new QwtPlotCurve( tr( "Band %1" ).arg( myIteratorInt ) );
//mypCurve->setCurveAttribute( QwtPlotCurve::Fitted );
mypCurve->setRenderHint( QwtPlotItem::RenderAntialiased );
mypCurve->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
}
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
QwtPlotHistogram * mypHisto = 0;
if ( ! myDrawLines )
{
mypHisto = new QwtPlotHistogram( tr( "Band %1" ).arg( myIteratorInt ) );
mypHisto->setRenderHint( QwtPlotItem::RenderAntialiased );
//mypHisto->setPen( QPen( mHistoColors.at( myIteratorInt ) ) );
mypHisto->setPen( QPen( Qt::lightGray ) );
// this is needed in order to see the colors in the legend
mypHisto->setBrush( QBrush( mHistoColors.at( myIteratorInt ) ) );
}
#else
HistogramItem *mypHistoItem = 0;
if ( ! myDrawLines )
{
mypHistoItem = new HistogramItem( tr( "Band %1" ).arg( myIteratorInt ) );
mypHistoItem->setRenderHint( QwtPlotItem::RenderAntialiased );
mypHistoItem->setColor( mHistoColors.at( myIteratorInt ) );
}
#endif
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
QVector<QPointF> data;
QVector<QwtIntervalSample> dataHisto;
#else
QVector<double> myX2Data;
QVector<double> myY2Data;
// we safely assume that QT>=4.0 (min version is 4.7), therefore QwtArray is a QVector, so don't set size here
QwtArray<QwtDoubleInterval> intervalsHisto;
QwtArray<double> valuesHisto;
#endif
// calculate first bin x value and bin step size if not Byte data
if ( mySrcDataType != QGis::Byte )
{
myBinXStep = ( myHistogram.maximum - myHistogram.minimum ) / myHistogram.binCount;
myBinX = myHistogram.minimum + myBinXStep / 2.0;
}
else
{
myBinXStep = 1;
myBinX = 0;
}
for ( int myBin = 0; myBin < myHistogram.binCount; myBin++ )
{
int myBinValue = myHistogram.histogramVector.at( myBin );
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
if ( myDrawLines )
{
data << QPointF( myBinX, myBinValue );
}
else
{
dataHisto << QwtIntervalSample( myBinValue, myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 );
}
#else
if ( myDrawLines )
{
myX2Data.append( double( myBinX ) );
myY2Data.append( double( myBinValue ) );
}
else
{
intervalsHisto.append( QwtDoubleInterval( myBinX - myBinXStep / 2.0, myBinX + myBinXStep / 2.0 ) );
valuesHisto.append( double( myBinValue ) );
}
#endif
myBinX += myBinXStep;
}
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
if ( myDrawLines )
{
mypCurve->setSamples( data );
mypCurve->attach( mpPlot );
}
else
{
mypHisto->setSamples( dataHisto );
mypHisto->attach( mpPlot );
}
#else
if ( myDrawLines )
{
mypCurve->setData( myX2Data, myY2Data );
mypCurve->attach( mpPlot );
}
else
{
mypHistoItem->setData( QwtIntervalData( intervalsHisto, valuesHisto ) );
mypHistoItem->attach( mpPlot );
}
#endif
if ( myFirstIteration || mHistoMin > myHistogram.minimum )
{
mHistoMin = myHistogram.minimum;
}
if ( myFirstIteration || mHistoMax < myHistogram.maximum )
{
mHistoMax = myHistogram.maximum;
}
QgsDebugMsg( QString( "computed histo min = %1 max = %2" ).arg( mHistoMin ).arg( mHistoMax ) );
myFirstIteration = false;
}
// for x axis use band pixel values rather than gdal hist. bin values
// subtract -0.5 to prevent rounding errors
// see http://www.gdal.org/classGDALRasterBand.html#3f8889607d3b2294f7e0f11181c201c8
// fix x range for non-Byte data
mpPlot->setAxisScale( QwtPlot::xBottom,
mHistoMin - myBinXStep / 2,
mHistoMax + myBinXStep / 2 );
mpPlot->replot();
// histo plot markers
// memory leak?
mHistoMarkerMin = new QwtPlotMarker();
mHistoMarkerMin->attach( mpPlot );
mHistoMarkerMax = new QwtPlotMarker();
mHistoMarkerMax->attach( mpPlot );
updateHistoMarkers();
// histo picker
if ( ! mHistoPicker )
{
mHistoPicker = new QwtPlotPicker( mpPlot->canvas() );
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
mHistoPicker->setEnabled( false );
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
connect( mHistoPicker, SIGNAL( selected( const QPointF & ) ), this, SLOT( histoPickerSelected( const QPointF & ) ) );
#else
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelectedQwt5( const QwtDoublePoint & ) ) );
#endif
}
// plot zoomer
if ( ! mHistoZoomer )
{
mHistoZoomer = new QwtPlotZoomer( mpPlot->canvas() );
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mHistoZoomer->setStateMachine( new QwtPickerDragRectMachine );
#else
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
#endif
mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
mHistoZoomer->setEnabled( true );
}
disconnect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
stackedWidget2->setCurrentIndex( 0 );
// icon from http://findicons.com/icon/169577/14_zoom?id=171427
mpPlot->canvas()->setCursor( QCursor( QgsApplication::getThemePixmap( "/mIconZoom.svg" ) ) );
// on_cboHistoBand_currentIndexChanged( -1 );
QApplication::restoreOverrideCursor();
}
void QgsRasterHistogramWidget::on_mSaveAsImageButton_clicked()
{
if ( mpPlot == 0 )
{
return;
}
QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
QFileInfo myInfo( myFileNameAndFilter.first );
if ( QFileInfo( myFileNameAndFilter.first ).baseName() != "" )
{
histoSaveAsImage( myFileNameAndFilter.first );
}
}
bool QgsRasterHistogramWidget::histoSaveAsImage( const QString& theFilename,
int width, int height, int quality )
{
// make sure dir. exists
QFileInfo myInfo( theFilename );
QDir myDir( myInfo.dir() );
if ( ! myDir.exists() )
{
QgsDebugMsg( QString( "Error, directory %1 non-existent (theFilename = %2)" ).arg( myDir.absolutePath() ).arg( theFilename ) );
return false;
}
// prepare the pixmap
QPixmap myPixmap( width, height );
QRect myQRect( 5, 5, width - 10, height - 10 ); // leave a 5px border on all sides
myPixmap.fill( Qt::white ); // Qt::transparent ?
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
QwtPlotRenderer myRenderer;
myRenderer.setDiscardFlags( QwtPlotRenderer::DiscardBackground |
QwtPlotRenderer::DiscardCanvasBackground );
myRenderer.setLayoutFlags( QwtPlotRenderer::FrameWithScales );
QPainter myPainter;
myPainter.begin( &myPixmap );
myRenderer.render( mpPlot, &myPainter, myQRect );
myPainter.end();
#else
QwtPlotPrintFilter myFilter;
int myOptions = QwtPlotPrintFilter::PrintAll;
myOptions &= ~QwtPlotPrintFilter::PrintBackground;
myOptions |= QwtPlotPrintFilter::PrintFrameWithScales;
myFilter.setOptions( myOptions );
QPainter myPainter;
myPainter.begin( &myPixmap );
mpPlot->print( &myPainter, myQRect, myFilter );
myPainter.end();
// "fix" for bug in qwt5 - legend and plot shifts a bit
// can't see how to avoid this without picking qwt5 apart...
refreshHistogram();
refreshHistogram();
#endif
// save pixmap to file
myPixmap.save( theFilename, 0, quality );
// should do more error checking
return true;
}
void QgsRasterHistogramWidget::setSelectedBand( int theBandNo )
{
cboHistoBand->setCurrentIndex( theBandNo - 1 );
}
void QgsRasterHistogramWidget::on_cboHistoBand_currentIndexChanged( int index )
{
if ( mHistoShowBands == ShowSelected )
refreshHistogram();
// get the current index value, index can be -1
index = cboHistoBand->currentIndex();
if ( mHistoPicker != NULL )
{
mHistoPicker->setEnabled( false );
mHistoPicker->setRubberBandPen( QPen( mHistoColors.at( index + 1 ) ) );
}
if ( mHistoZoomer != NULL )
mHistoZoomer->setEnabled( true );
btnHistoMin->setEnabled( true );
btnHistoMax->setEnabled( true );
QPair< QString, QString > myMinMax = rendererMinMax( index + 1 );
leHistoMin->setText( myMinMax.first );
leHistoMax->setText( myMinMax.second );
applyHistoMin();
applyHistoMax();
}
void QgsRasterHistogramWidget::histoActionTriggered( QAction* action )
{
if ( ! action )
return;
histoAction( action->data().toString(), action->isChecked() );
}
void QgsRasterHistogramWidget::histoAction( const QString actionName, bool actionFlag )
{
if ( actionName == "" )
return;
// this approach is a bit of a hack, but this way we don't have to define slots for each action
QgsDebugMsg( QString( "band = %1 action = %2" ).arg( cboHistoBand->currentIndex() + 1 ).arg( actionName ) );
// checkeable actions
if ( actionName == "Show markers" )
{
mHistoShowMarkers = actionFlag;
QSettings settings;
settings.setValue( "/Raster/histogram/showMarkers", mHistoShowMarkers );
updateHistoMarkers();
return;
}
else if ( actionName == "Zoom min_max" )
{
mHistoZoomToMinMax = actionFlag;
QSettings settings;
settings.setValue( "/Raster/histogram/zoomToMinMax", mHistoZoomToMinMax );
return;
}
else if ( actionName == "Update min_max" )
{
mHistoUpdateStyleToMinMax = actionFlag;
QSettings settings;
settings.setValue( "/Raster/histogram/updateStyleToMinMax", mHistoUpdateStyleToMinMax );
return;
}
else if ( actionName == "Show all" )
{
mHistoShowBands = ShowAll;
// settings.setValue( "/Raster/histogram/showBands", (int)mHistoShowBands );
refreshHistogram();
return;
}
else if ( actionName == "Show selected" )
{
mHistoShowBands = ShowSelected;
// settings.setValue( "/Raster/histogram/showBands", (int)mHistoShowBands );
refreshHistogram();
return;
}
else if ( actionName == "Show RGB" )
{
mHistoShowBands = ShowRGB;
// settings.setValue( "/Raster/histogram/showBands", (int)mHistoShowBands );
refreshHistogram();
return;
}
else if ( actionName == "Draw lines" )
{
mHistoDrawLines = actionFlag;
QSettings settings;
settings.setValue( "/Raster/histogram/drawLines", mHistoDrawLines );
on_btnHistoCompute_clicked(); // refresh
return;
}
#if 0
else if ( actionName == "Load apply all" )
{
mHistoLoadApplyAll = actionFlag;
settings.setValue( "/Raster/histogram/loadApplyAll", mHistoLoadApplyAll );
return;
}
#endif
// Load actions
// TODO - separate calculations from rendererwidget so we can do them without
else if ( actionName.left( 5 ) == "Load " && mRendererWidget )
{
QVector<int> myBands;
bool ok = false;
#if 0
double minMaxValues[2];
// find which band(s) need updating (all or current)
if ( mHistoLoadApplyAll )
{
int myBandCountInt = mRasterLayer->bandCount();
for ( int i = 1; i <= myBandCountInt; i++ )
{
if ( i != cboHistoBand->currentIndex() + 1 )
myBands << i;
}
}
#endif
// add current band to the end
myBands << cboHistoBand->currentIndex() + 1;
// get stddev value once if needed
/*
double myStdDev = 1.0;
if ( actionName == "Load stddev" )
{
myStdDev = mRendererWidget->stdDev().toDouble();
}
*/
// don't update markers every time
leHistoMin->blockSignals( true );
leHistoMax->blockSignals( true );
// process each band
foreach ( int theBandNo, myBands )
{
ok = false;
#if 0
if ( actionName == "Load actual" )
{
ok = mRendererWidget->bandMinMax( QgsRasterRendererWidget::Actual,
theBandNo, minMaxValues );
}
else if ( actionName == "Load estimate" )
{
ok = mRendererWidget->bandMinMax( QgsRasterRendererWidget::Estimate,
theBandNo, minMaxValues );
}
else if ( actionName == "Load extent" )
{
ok = mRendererWidget->bandMinMax( QgsRasterRendererWidget::CurrentExtent,
theBandNo, minMaxValues );
}
else if ( actionName == "Load 1 stddev" ||
actionName == "Load stddev" )
{
ok = mRendererWidget->bandMinMaxFromStdDev( myStdDev, theBandNo, minMaxValues );
}
#endif
// apply current item
cboHistoBand->setCurrentIndex( theBandNo - 1 );
if ( !ok || actionName == "Load reset" )
{
leHistoMin->clear();
leHistoMax->clear();
#if 0
// TODO - fix gdal provider: changes data type when nodata value is not found
// this prevents us from getting proper min and max values here
minMaxValues[0] = QgsContrastEnhancement::minimumValuePossible(
( QGis::DataType ) mRasterLayer->dataProvider()->dataType( theBandNo ) );
minMaxValues[1] = QgsContrastEnhancement::maximumValuePossible(
( QGis::DataType ) mRasterLayer->dataProvider()->dataType( theBandNo ) );
}
else
{
leHistoMin->setText( QString::number( minMaxValues[0] ) );
leHistoMax->setText( QString::number( minMaxValues[1] ) );
#endif
}
applyHistoMin( );
applyHistoMax( );
}
// update markers
leHistoMin->blockSignals( false );
leHistoMax->blockSignals( false );
updateHistoMarkers();
}
else if ( actionName == "Compute histogram" )
{
on_btnHistoCompute_clicked();
}
else
{
QgsDebugMsg( "Invalid action " + actionName );
return;
}
}
void QgsRasterHistogramWidget::applyHistoMin( )
{
if ( ! mRendererWidget )
return;
int theBandNo = cboHistoBand->currentIndex() + 1;
QList< int > mySelectedBands = rendererSelectedBands();
QString min;
for ( int i = 0; i <= mySelectedBands.size(); i++ )
{
if ( theBandNo == mRendererWidget->selectedBand( i ) )
{
min = leHistoMin->text();
if ( mHistoUpdateStyleToMinMax )
mRendererWidget->setMin( min, i );
}
}
updateHistoMarkers();
if ( ! min.isEmpty() && mHistoZoomToMinMax && mHistoZoomer )
{
QRectF rect = mHistoZoomer->zoomRect();
rect.setLeft( min.toDouble() );
mHistoZoomer->zoom( rect );
}
}
void QgsRasterHistogramWidget::applyHistoMax( )
{
if ( ! mRendererWidget )
return;
int theBandNo = cboHistoBand->currentIndex() + 1;
QList< int > mySelectedBands = rendererSelectedBands();
QString max;
for ( int i = 0; i <= mySelectedBands.size(); i++ )
{
if ( theBandNo == mRendererWidget->selectedBand( i ) )
{
max = leHistoMax->text();
if ( mHistoUpdateStyleToMinMax )
mRendererWidget->setMax( max, i );
}
}
updateHistoMarkers();
if ( ! max.isEmpty() && mHistoZoomToMinMax && mHistoZoomer )
{
QRectF rect = mHistoZoomer->zoomRect();
rect.setRight( max.toDouble() );
mHistoZoomer->zoom( rect );
}
}
void QgsRasterHistogramWidget::on_btnHistoMin_toggled()
{
if ( mpPlot != NULL && mHistoPicker != NULL )
{
if ( QApplication::overrideCursor() )
QApplication::restoreOverrideCursor();
if ( btnHistoMin->isChecked() )
{
btnHistoMax->setChecked( false );
QApplication::setOverrideCursor( Qt::PointingHandCursor );
}
if ( mHistoZoomer != NULL )
mHistoZoomer->setEnabled( ! btnHistoMax->isChecked() );
mHistoPicker->setEnabled( btnHistoMin->isChecked() );
}
updateHistoMarkers();
}