@@ -330,8 +330,9 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
330
330
QMenu* menu = new QMenu ( this );
331
331
menu->setSeparatorsCollapsible ( false );
332
332
btnHistoActions->setMenu ( menu );
333
+ QActionGroup* group;
333
334
334
- QActionGroup* group = new QActionGroup ( this );
335
+ group = new QActionGroup ( this );
335
336
group->setExclusive ( false );
336
337
connect ( group, SIGNAL ( triggered ( QAction* ) ), this , SLOT ( histoActionTriggered ( QAction* ) ) );
337
338
QAction* action;
@@ -365,6 +366,13 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
365
366
action->setCheckable ( true );
366
367
action->setChecked ( true );
367
368
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 );
368
376
}
369
377
370
378
// update based on lyr's current state
@@ -1194,16 +1202,85 @@ void QgsRasterLayerProperties::on_tabBar_currentChanged( int theTab )
1194
1202
}
1195
1203
}
1196
1204
1197
- void QgsRasterLayerProperties::refreshHistogram ()
1205
+ void QgsRasterLayerProperties::on_btnHistoCompute_clicked ()
1198
1206
{
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
1203
1239
stackedWidget2->setCurrentIndex ( 1 );
1204
1240
connect ( mRasterLayer , SIGNAL ( progressUpdate ( int ) ), mHistogramProgress , SLOT ( setValue ( int ) ) );
1205
1241
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
+
1206
1272
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
1207
1284
// ensure all children get removed
1208
1285
mpPlot->setAutoDelete ( true );
1209
1286
mpPlot->setTitle ( QObject::tr ( " Raster Histogram" ) );
@@ -1216,20 +1293,6 @@ void QgsRasterLayerProperties::refreshHistogram()
1216
1293
// add a grid
1217
1294
QwtPlotGrid * myGrid = new QwtPlotGrid ();
1218
1295
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 ();
1233
1296
1234
1297
// make colors list
1235
1298
mHistoColors .clear ();
@@ -1327,7 +1390,7 @@ void QgsRasterLayerProperties::refreshHistogram()
1327
1390
++myIteratorInt )
1328
1391
{
1329
1392
QgsRasterBandStats myRasterBandStats = mRasterLayer ->bandStatistics ( myIteratorInt );
1330
- mRasterLayer ->populateHistogram ( myIteratorInt, BINCOUNT, myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
1393
+ // mRasterLayer->populateHistogram( myIteratorInt, BINCOUNT, myIgnoreOutOfRangeFlag, myThoroughBandScanFlag );
1331
1394
QwtPlotCurve * mypCurve = new QwtPlotCurve ( tr ( " Band %1" ).arg ( myIteratorInt ) );
1332
1395
mypCurve->setCurveAttribute ( QwtPlotCurve::Fitted );
1333
1396
mypCurve->setRenderHint ( QwtPlotItem::RenderAntialiased );
@@ -1938,6 +2001,10 @@ void QgsRasterLayerProperties::histoActionTriggered( QAction* action )
1938
2001
updateHistoMarkers ();
1939
2002
}
1940
2003
}
2004
+ else if ( actionName == " Compute Histogram" )
2005
+ {
2006
+ on_btnHistoCompute_clicked ();
2007
+ }
1941
2008
else
1942
2009
{
1943
2010
return ;
0 commit comments