52
52
#include " qgslogger.h"
53
53
54
54
// QWT Charting widget
55
+ #include < qwt_plot_canvas.h>
55
56
#include < qwt_array.h>
56
57
#include < qwt_legend.h>
57
58
#include < qwt_plot.h>
@@ -167,97 +168,23 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
167
168
168
169
QgsDebugMsg ( " Populating band combo boxes" );
169
170
170
- //
171
- // Get a list of band names
172
- //
173
- QStringList myBandNameList;
174
-
175
171
int myBandCountInt = mRasterLayer ->bandCount ();
176
-
177
- QgsDebugMsg ( QString ( " Looping though %1 image layers to get their names " ).arg ( myBandCountInt ) );
178
-
179
- for ( int myIteratorInt = 1 ;
180
- myIteratorInt <= myBandCountInt;
181
- ++myIteratorInt )
182
- {
183
- // find out the name of this band
184
- QString myRasterBandNameQString = mRasterLayer ->bandName ( myIteratorInt ) ;
185
-
186
- // add the band to the histogram tab
187
- //
188
- QPixmap myPixmap ( 10 , 10 );
189
-
190
- if ( myBandCountInt == 1 ) // draw single band images with black
191
- {
192
- myPixmap.fill ( Qt::black );
193
- }
194
- else if ( myIteratorInt == 1 )
195
- {
196
- myPixmap.fill ( Qt::red );
197
- }
198
- else if ( myIteratorInt == 2 )
199
- {
200
- myPixmap.fill ( Qt::green );
201
- }
202
- else if ( myIteratorInt == 3 )
203
- {
204
- myPixmap.fill ( Qt::blue );
205
- }
206
- else if ( myIteratorInt == 4 )
207
- {
208
- myPixmap.fill ( Qt::magenta );
209
- }
210
- else if ( myIteratorInt == 5 )
211
- {
212
- myPixmap.fill ( Qt::darkRed );
213
- }
214
- else if ( myIteratorInt == 6 )
215
- {
216
- myPixmap.fill ( Qt::darkGreen );
217
- }
218
- else if ( myIteratorInt == 7 )
219
- {
220
- myPixmap.fill ( Qt::darkBlue );
221
- }
222
- else
223
- {
224
- myPixmap.fill ( Qt::gray );
225
- }
226
- lstHistogramLabels->addItem ( new QListWidgetItem ( myPixmap, myRasterBandNameQString ) );
227
- // keep a list of band names for later use
228
- // ! @note band names should not be translated!
229
- myBandNameList.append ( myRasterBandNameQString );
230
- }
231
-
232
- // select all histogram layers list items by default
233
172
for ( int myIteratorInt = 1 ;
234
173
myIteratorInt <= myBandCountInt;
235
174
++myIteratorInt )
236
175
{
237
- QListWidgetItem *myItem = lstHistogramLabels->item ( myIteratorInt - 1 );
238
- myItem->setSelected ( true );
239
- }
240
-
241
- for ( QStringList::Iterator myIterator = myBandNameList.begin ();
242
- myIterator != myBandNameList.end ();
243
- ++myIterator )
244
- {
245
- QString myQString = *myIterator;
246
-
247
- QgsDebugMsg ( QString ( " Inserting : %1" ).arg ( myQString ) );
248
-
249
- cboGray->addItem ( myQString );
250
- cboRed->addItem ( myQString );
251
- cboGreen->addItem ( myQString );
252
- cboBlue->addItem ( myQString );
253
- cboxColorMapBand->addItem ( myQString );
176
+ QString myRasterBandName = mRasterLayer ->bandName ( myIteratorInt ) ;
177
+ cboGray->addItem ( myRasterBandName );
178
+ cboRed->addItem ( myRasterBandName );
179
+ cboGreen->addItem ( myRasterBandName );
180
+ cboBlue->addItem ( myRasterBandName );
181
+ cboxColorMapBand->addItem ( myRasterBandName );
254
182
}
255
183
256
184
cboRed->addItem ( TRSTRING_NOT_SET );
257
185
cboGreen->addItem ( TRSTRING_NOT_SET );
258
186
cboBlue->addItem ( TRSTRING_NOT_SET );
259
187
cboGray->addItem ( TRSTRING_NOT_SET );
260
-
261
188
cboxTransparencyBand->addItem ( TRSTRING_NOT_SET );
262
189
263
190
QIcon myPyramidPixmap ( QgisApp::getThemeIcon ( " /mIconPyramid.png" ) );
@@ -1911,13 +1838,40 @@ void QgsRasterLayerProperties::on_pbnExportTransparentPixelValues_clicked()
1911
1838
}
1912
1839
}
1913
1840
1914
- void QgsRasterLayerProperties::on_pbnHistRefresh_clicked ()
1841
+ void QgsRasterLayerProperties::on_tabBar_currentChanged ( int theTab )
1842
+ {
1843
+ int myHistogramTab = 6 ;
1844
+ if ( theTab == myHistogramTab )
1845
+ {
1846
+ refreshHistogram ();
1847
+ }
1848
+ }
1849
+
1850
+ void QgsRasterLayerProperties::refreshHistogram ()
1915
1851
{
1852
+ mHistogramProgress ->show ();
1916
1853
connect ( mRasterLayer , SIGNAL ( progressUpdate ( int ) ), mHistogramProgress , SLOT ( setValue ( int ) ) );
1917
1854
QApplication::setOverrideCursor ( Qt::WaitCursor );
1918
1855
QgsDebugMsg ( " entered." );
1919
- int myBandCountInt = mRasterLayer ->bandCount ();
1920
1856
1857
+ QwtPlot * mypPlot = new QwtPlot ( mChartWidget );
1858
+ mypPlot->canvas ()->setCursor (Qt::ArrowCursor);
1859
+ // ensure all children get removed
1860
+ mypPlot->setAutoDelete ( true );
1861
+ QVBoxLayout *mpHistogramLayout = new QVBoxLayout ( mChartWidget );
1862
+ mpHistogramLayout->setContentsMargins ( 0 , 0 , 0 , 0 );
1863
+ mpHistogramLayout->addWidget ( mypPlot );
1864
+ mChartWidget ->setLayout ( mpHistogramLayout );
1865
+ mypPlot->setTitle ( QObject::tr ( " Raster Histogram" ) );
1866
+ mypPlot->insertLegend ( new QwtLegend (), QwtPlot::BottomLegend );
1867
+ // Set axis titles
1868
+ mypPlot->setAxisTitle ( QwtPlot::xBottom, QObject::tr (" Pixel Value" ) );
1869
+ mypPlot->setAxisTitle ( QwtPlot::yLeft, QObject::tr (" Frequency" ) );
1870
+ mypPlot->setAxisAutoScale ( QwtPlot::xBottom );
1871
+ mypPlot->setAxisAutoScale ( QwtPlot::yLeft );
1872
+ // add a grid
1873
+ QwtPlotGrid * myGrid = new QwtPlotGrid ();
1874
+ myGrid->attach (mypPlot);
1921
1875
// Explanation:
1922
1876
// We use the gdal histogram creation routine is called for each selected
1923
1877
// layer. Currently the hist is hardcoded
@@ -1928,125 +1882,48 @@ void QgsRasterLayerProperties::on_pbnHistRefresh_clicked()
1928
1882
// bin in all selected layers, and the min. It then draws a scaled line between min
1929
1883
// and max - scaled to image height. 1 line drawn per selected band
1930
1884
//
1931
- const int BINCOUNT = spinHistBinCount-> value () ;
1885
+ const int BINCOUNT = 255 ;
1932
1886
enum GRAPH_TYPE { BAR_CHART, LINE_CHART } myGraphType;
1933
- if ( radHistTypeBar->isChecked () ) myGraphType = BAR_CHART; else myGraphType = LINE_CHART;
1934
- bool myIgnoreOutOfRangeFlag = chkHistIgnoreOutOfRange->isChecked ();
1935
- bool myThoroughBandScanFlag = chkHistAllowApproximation->isChecked ();
1936
-
1937
- #ifdef QGISDEBUG
1938
- long myCellCount = mRasterLayer ->width () * mRasterLayer ->height ();
1939
- #endif
1940
-
1941
- QgsDebugMsg ( " Computing histogram minima and maxima" );
1942
- // somtimes there are more bins than needed
1943
- // we find out the last on that actually has data in it
1944
- // so we can discard the rest adn the x-axis scales correctly
1887
+ myGraphType = BAR_CHART;
1888
+ bool myIgnoreOutOfRangeFlag = true ;
1889
+ bool myThoroughBandScanFlag = false ;
1945
1890
int myLastBinWithData = 0 ;
1891
+ int myBandCountInt = mRasterLayer ->bandCount ();
1892
+ QList<QColor> myColors;
1893
+ myColors << Qt::black << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkRed << Qt::darkGreen << Qt::darkBlue;
1946
1894
//
1947
- // First scan through to get max and min cell counts from among selected layers' histograms
1895
+ // now draw actual graphs
1948
1896
//
1949
- double myYAxisMax = 0 ;
1950
- double myYAxisMin = 0 ;
1951
- int myXAxisMin = 0 ;
1952
- int myXAxisMax = 0 ;
1953
- bool myFirstItemFlag = true ;
1954
- for ( int myIteratorInt = 1 ;
1955
- myIteratorInt <= myBandCountInt;
1956
- ++myIteratorInt )
1957
- {
1958
- QgsRasterBandStats myRasterBandStats = mRasterLayer ->bandStatistics ( myIteratorInt );
1959
- // calculate the x axis min max
1960
- if ( myRasterBandStats.minimumValue < myXAxisMin || myIteratorInt == 1 )
1961
- {
1962
- myXAxisMin = static_cast < unsigned int >( myRasterBandStats.minimumValue );
1963
- }
1964
- if ( myRasterBandStats.maximumValue < myXAxisMax || myIteratorInt == 1 )
1965
- {
1966
- myXAxisMax = static_cast < unsigned int >( myRasterBandStats.maximumValue );
1967
- }
1968
- QListWidgetItem *myItem = lstHistogramLabels->item ( myIteratorInt - 1 );
1969
- if ( myItem->isSelected () )
1970
- {
1971
- QgsDebugMsg ( " Ensuring hist is populated for this layer" );
1972
- mRasterLayer ->populateHistogram ( myIteratorInt, BINCOUNT, myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
1973
-
1974
- QgsDebugMsg ( QString ( " ...done...%1 bins filled" ).arg ( myRasterBandStats.histogramVector ->size () ) );
1975
- for ( int myBin = 0 ; myBin < BINCOUNT; myBin++ )
1976
- {
1977
- int myBinValue = myRasterBandStats.histogramVector ->at ( myBin );
1978
- if ( myBinValue > 0 && myBin > myLastBinWithData )
1979
- {
1980
- myLastBinWithData = myBin;
1981
- }
1982
- QgsDebugMsg ( QString ( " Testing if %1 is less than %2or greater then %3" ).arg ( myBinValue ).arg ( myYAxisMin ).arg ( myYAxisMax ) );
1983
- if ( myBin == 0 && myFirstItemFlag )
1984
- {
1985
- myYAxisMin = myBinValue;
1986
- myYAxisMax = myBinValue;
1987
- }
1988
-
1989
- if ( myBinValue > myYAxisMax )
1990
- {
1991
- myYAxisMax = myBinValue;
1992
- }
1993
- if ( myBinValue < myYAxisMin )
1994
- {
1995
- myYAxisMin = myBinValue;
1996
- }
1997
- }
1998
- myFirstItemFlag = false ;
1999
- }
2000
- }
2001
- disconnect ( mRasterLayer , SIGNAL ( progressUpdate ( int ) ), mHistogramProgress , SLOT ( setValue ( int ) ) );
2002
- QgsDebugMsg ( QString ( " max %1" ).arg ( myYAxisMax ) );
2003
- QgsDebugMsg ( QString ( " min %1" ).arg ( myYAxisMin ) );
2004
1897
2005
- QwtPlot * mypPlot = new QwtPlot ( mChartWidget );
2006
- // ensure all children get removed
2007
- mypPlot->setAutoDelete ( true );
2008
- QVBoxLayout *mpHistogramLayout = new QVBoxLayout ( mChartWidget );
2009
- mpHistogramLayout->setContentsMargins ( 0 , 0 , 0 , 0 );
2010
- mpHistogramLayout->addWidget ( mypPlot );
2011
- mChartWidget ->setLayout ( mpHistogramLayout );
2012
- mypPlot->setTitle (QObject::tr (" Raster Histogram" ));
2013
- mypPlot->insertLegend (new QwtLegend (), QwtPlot::BottomLegend);
2014
- // Set axis titles
2015
- mypPlot->setAxisTitle (QwtPlot::xBottom, QObject::tr (" Pixel Value" ));
2016
- mypPlot->setAxisTitle (QwtPlot::yLeft, QObject::tr (" Frequency" ));
2017
- //
2018
- // add a grid
2019
- //
2020
- QwtPlotGrid * myGrid = new QwtPlotGrid ();
2021
- myGrid->attach (mypPlot);
1898
+ // somtimes there are more bins than needed
1899
+ // we find out the last one that actually has data in it
1900
+ // so we can discard the rest and set the x-axis scales correctly
2022
1901
//
2023
- // now draw actual graphs
1902
+ // scan through to get counts from layers' histograms
2024
1903
//
2025
- QList<QColor> myColors;
2026
- myColors << Qt::black << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkRed << Qt::darkGreen << Qt::darkBlue;
2027
1904
for ( int myIteratorInt = 1 ;
2028
1905
myIteratorInt <= myBandCountInt;
2029
1906
++myIteratorInt )
2030
1907
{
2031
1908
QgsRasterBandStats myRasterBandStats = mRasterLayer ->bandStatistics ( myIteratorInt );
2032
- QListWidgetItem *myItem = lstHistogramLabels-> item ( myIteratorInt - 1 );
2033
- if ( myItem-> isSelected () )
2034
- {
2035
- QwtPlotCurve * mypCurve = new QwtPlotCurve ( tr ( " Band %i " ). arg ( myIteratorInt ) );
2036
- mypCurve-> setRenderHint ( QwtPlotItem::RenderAntialiased );
2037
- mypCurve-> setPen ( QPen ( myColors. at ( myIteratorInt ) ) );
2038
- QwtArray< double > myX2Data; // qwtarray is just a wrapped qvector
2039
- QwtArray< double > myY2Data; // qwtarray is just a wrapped qvector
2040
- for ( int myBin = 0 ; myBin < myLastBinWithData; myBin++ )
2041
- {
2042
- double myBinValue = myRasterBandStats. histogramVector -> at ( myBin );
2043
- myX2Data. append (myBin);
2044
- myY2Data. append (myBinValue );
2045
- }
2046
- mypCurve-> setData (myX2Data,myY2Data);
2047
- mypCurve-> attach (mypPlot );
2048
- }
2049
- }
1909
+ mRasterLayer -> populateHistogram ( myIteratorInt, BINCOUNT, myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
1910
+ QwtPlotCurve * mypCurve = new QwtPlotCurve ( tr ( " Band %1 " ). arg ( myIteratorInt ) );
1911
+ mypCurve-> setRenderHint ( QwtPlotItem::RenderAntialiased );
1912
+ mypCurve-> setPen ( QPen ( myColors. at ( myIteratorInt ) ) );
1913
+ QwtArray< double > myX2Data; // qwtarray is just a wrapped qvector
1914
+ QwtArray< double > myY2Data; // qwtarray is just a wrapped qvector
1915
+ for ( int myBin = 0 ; myBin < BINCOUNT; myBin++ )
1916
+ {
1917
+ int myBinValue = myRasterBandStats. histogramVector -> at ( myBin );
1918
+ myX2Data. append ( double ( myBin) );
1919
+ myY2Data. append ( double ( myBinValue) );
1920
+ }
1921
+ mypCurve-> setData (myX2Data,myY2Data );
1922
+ mypCurve-> attach (mypPlot);
1923
+ }
1924
+ mypPlot-> replot ( );
1925
+ disconnect ( mRasterLayer , SIGNAL ( progressUpdate ( int ) ), mHistogramProgress , SLOT ( setValue ( int ) ) );
1926
+ mHistogramProgress -> hide ();
2050
1927
QApplication::restoreOverrideCursor ();
2051
1928
}
2052
1929
0 commit comments