@@ -330,8 +330,9 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
330330 QMenu* menu = new QMenu ( this );
331331 menu->setSeparatorsCollapsible ( false );
332332 btnHistoActions->setMenu ( menu );
333+ QActionGroup* group;
333334
334- QActionGroup* group = new QActionGroup ( this );
335+ group = new QActionGroup ( this );
335336 group->setExclusive ( false );
336337 connect ( group, SIGNAL ( triggered ( QAction* ) ), this , SLOT ( histoActionTriggered ( QAction* ) ) );
337338 QAction* action;
@@ -365,6 +366,13 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
365366 action->setCheckable ( true );
366367 action->setChecked ( true );
367368 menu->addAction ( action );
369+
370+ menu->addSeparator ( );
371+ group = new QActionGroup ( this );
372+ connect ( group, SIGNAL ( triggered ( QAction* ) ), this , SLOT ( histoActionTriggered ( QAction* ) ) );
373+ action = new QAction ( tr ( " Compute Histogram" ), group );
374+ action->setData ( QVariant ( " Compute Histogram" ) );
375+ menu->addAction ( action );
368376 }
369377
370378 // update based on lyr's current state
@@ -1194,16 +1202,85 @@ void QgsRasterLayerProperties::on_tabBar_currentChanged( int theTab )
11941202 }
11951203}
11961204
1197- void QgsRasterLayerProperties::refreshHistogram ()
1205+ void QgsRasterLayerProperties::on_btnHistoCompute_clicked ()
11981206{
1199- #if !defined(QWT_VERSION) || QWT_VERSION<0x060000
1200- mpPlot->clear ();
1201- #endif
1202- // mHistogramProgress->show();
1207+ // Histogram computation can be called either by clicking the "Compute Histogram" button
1208+ // which is only visible if there is no cached histogram or by calling the
1209+ // "Compute Histogram" action. Due to limitations in the gdal api, it is not possible
1210+ // to re-calculate the histogramif it has already been calculated
1211+ QgsDebugMsg ( " Entered" );
1212+ computeHistogram ( true );
1213+ refreshHistogram ();
1214+ }
1215+
1216+ bool QgsRasterLayerProperties::computeHistogram ( bool forceComputeFlag )
1217+ {
1218+ const int BINCOUNT = RASTER_HISTOGRAM_BINS ; // 256 - defined in qgsrasterdataprovider.h
1219+ bool myIgnoreOutOfRangeFlag = true ;
1220+ bool myThoroughBandScanFlag = false ;
1221+ int myBandCountInt = mRasterLayer ->bandCount ();
1222+
1223+ // if forceComputeFlag = false make sure raster has cached histogram, else return false
1224+ if ( ! forceComputeFlag )
1225+ {
1226+ for ( int myIteratorInt = 1 ;
1227+ myIteratorInt <= myBandCountInt;
1228+ ++myIteratorInt )
1229+ {
1230+ if ( ! mRasterLayer ->hasCachedHistogram ( myIteratorInt, BINCOUNT ) )
1231+ {
1232+ QgsDebugMsg ( QString ( " band %1 does not have cached histo" ).arg ( myIteratorInt ) );
1233+ return false ;
1234+ }
1235+ }
1236+ }
1237+
1238+ // compute histogram
12031239 stackedWidget2->setCurrentIndex ( 1 );
12041240 connect ( mRasterLayer , SIGNAL ( progressUpdate ( int ) ), mHistogramProgress , SLOT ( setValue ( int ) ) );
12051241 QApplication::setOverrideCursor ( Qt::WaitCursor );
1242+
1243+ for ( int myIteratorInt = 1 ;
1244+ myIteratorInt <= myBandCountInt;
1245+ ++myIteratorInt )
1246+ {
1247+ mRasterLayer ->populateHistogram ( myIteratorInt, BINCOUNT , myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
1248+ }
1249+
1250+ disconnect ( mRasterLayer , SIGNAL ( progressUpdate ( int ) ), mHistogramProgress , SLOT ( setValue ( int ) ) );
1251+ // mHistogramProgress->hide();
1252+ stackedWidget2->setCurrentIndex ( 0 );
1253+ QApplication::restoreOverrideCursor ();
1254+
1255+ return true ;
1256+ }
1257+
1258+ void QgsRasterLayerProperties::refreshHistogram ()
1259+ {
1260+ // Explanation:
1261+ // We use the gdal histogram creation routine is called for each selected
1262+ // layer. Currently the hist is hardcoded to create 256 bins. Each bin stores
1263+ // the total number of cells that fit into the range defined by that bin.
1264+ //
1265+ // The graph routine below determines the greatest number of pixels in any given
1266+ // bin in all selected layers, and the min. It then draws a scaled line between min
1267+ // and max - scaled to image height. 1 line drawn per selected band
1268+ //
1269+ const int BINCOUNT = RASTER_HISTOGRAM_BINS ; // 256 - defined in qgsrasterdataprovider.h
1270+ int myBandCountInt = mRasterLayer ->bandCount ();
1271+
12061272 QgsDebugMsg ( " entered." );
1273+
1274+ if ( ! computeHistogram ( false ) )
1275+ {
1276+ QgsDebugMsg ( QString ( " raster does not have cached histo" ) );
1277+ stackedWidget2->setCurrentIndex ( 2 );
1278+ return ;
1279+ }
1280+
1281+ #if !defined(QWT_VERSION) || QWT_VERSION<0x060000
1282+ mpPlot->clear ();
1283+ #endif
12071284 // ensure all children get removed
12081285 mpPlot->setAutoDelete ( true );
12091286 mpPlot->setTitle ( QObject::tr ( " Raster Histogram" ) );
@@ -1216,20 +1293,6 @@ void QgsRasterLayerProperties::refreshHistogram()
12161293 // add a grid
12171294 QwtPlotGrid * myGrid = new QwtPlotGrid ();
12181295 myGrid->attach ( mpPlot );
1219- // Explanation:
1220- // We use the gdal histogram creation routine is called for each selected
1221- // layer. Currently the hist is hardcoded
1222- // to create 256 bins. Each bin stores the total number of cells that
1223- // fit into the range defined by that bin.
1224- //
1225- // The graph routine below determines the greatest number of pixels in any given
1226- // bin in all selected layers, and the min. It then draws a scaled line between min
1227- // and max - scaled to image height. 1 line drawn per selected band
1228- //
1229- const int BINCOUNT = 256 ;
1230- bool myIgnoreOutOfRangeFlag = true ;
1231- bool myThoroughBandScanFlag = false ;
1232- int myBandCountInt = mRasterLayer ->bandCount ();
12331296
12341297 // make colors list
12351298 mHistoColors .clear ();
@@ -1327,7 +1390,7 @@ void QgsRasterLayerProperties::refreshHistogram()
13271390 ++myIteratorInt )
13281391 {
13291392 QgsRasterBandStats myRasterBandStats = mRasterLayer ->bandStatistics ( myIteratorInt );
1330- mRasterLayer ->populateHistogram ( myIteratorInt, BINCOUNT , myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
1393+ // mRasterLayer->populateHistogram( myIteratorInt, BINCOUNT, myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
13311394 QwtPlotCurve * mypCurve = new QwtPlotCurve ( tr ( " Band %1" ).arg ( myIteratorInt ) );
13321395 mypCurve->setCurveAttribute ( QwtPlotCurve::Fitted );
13331396 mypCurve->setRenderHint ( QwtPlotItem::RenderAntialiased );
@@ -1938,6 +2001,10 @@ void QgsRasterLayerProperties::histoActionTriggered( QAction* action )
19382001 updateHistoMarkers ();
19392002 }
19402003 }
2004+ else if ( actionName == " Compute Histogram" )
2005+ {
2006+ on_btnHistoCompute_clicked ();
2007+ }
19412008 else
19422009 {
19432010 return ;
0 commit comments