plot canvas coodinates

First I want to state, that I am using qwt 6.0.1 and cannot upgrade to 6.1.2 at present time.

To be sure we have the same understanding of the definition of the canvas, for me the canvas is the area where the grid and the data to plot are drawn, nothing more.

Now to my problem.
I want to draw a vertical (horizontal) color band to mark an area of interest into the plot - see picture attached -. To do that, I draw a line centered at the arithmetic middle of the band in case of a linear plot, centered at the geometric middle in case of a logarithmic plot. I also calculate the width of the pen to spread over the whole area of the band, then I draw the vertical (horizontal) line with the attribute described before.

Since the width of the pen has to be given in pixels, I must convert the width of the band from physical coordinates into pixel (paint) coordinates.
To do that I have tried to use both methods
Qt Code:
  1. canvas()->contentsRect(), plotLayout()->canvasMargin()
  2. canvasMap().
To copy to clipboard, switch view to plain text mode 
- see code below -
Both methods lead almost to the same result - there is a difference of one pixel between the two! - see debugger output below -, but neither of the 2 methods are giving the right width in pixels as can be seen in the 2 plots.
It seems that the legend and also another offset is integrated in the result of the methods
canvas()->contentsRect() and canvasMap().

Any idea how to get around this issue?

Thank you for your valuable time
Alain

debugger output:

contentsRect.width() = 527 margin left = 4 margin right = 4 xSpanPixels = 519
xMap.p1()= 6 xMap.p2()= 524 xMap.pDist()= 518
LIN: xMin = 0 xMax = 10 lowerValue = 5 upperValue = 8 x = 6.5 widthPixels = 155.4
contentsRect.width() = 527 margin left = 4 margin right = 4 xSpanPixels = 519
xMap.p1()= 6 xMap.p2()= 524 xMap.pDist()= 518
LIN: xMin = 0 xMax = 10 lowerValue = 5 upperValue = 8 x = 6.5 widthPixels = 155.4
The program '[584] SigLibGraph.exe: Native' has exited with code 0 (0x0).

source code:

Qt Code:
  1. void Plot2D::setVerticalColorBand( double lowerValue, double upperValue
  2. , QColor color, QString bandLabel, double transparency )
  3. {
  4. // check parameters
  5. if ( lowerValue > upperValue )
  6. qSwap( lowerValue, upperValue );
  7.  
  8. // get axis extend in pixels
  9. updateAxes();
  10.  
  11. // just for test purpose
  12. QRect rect = this->canvas()->contentsRect();
  13. double marginLeft = double( this->plotLayout()->canvasMargin( QwtPlot::yLeft ));
  14. double marginRight = double( this->plotLayout()->canvasMargin( QwtPlot::yRight ));
  15. double xSpanPixels = rect.width() - marginLeft - marginRight;
  16. qDebug() << "contentsRect.width() =" << rect.width() << " margin left ="
  17. << marginLeft << " margin right =" << marginRight << " xSpanPixels =" << xSpanPixels;
  18. // end of test
  19.  
  20. QwtScaleMap xMap = canvasMap( QwtPlot::xBottom );
  21. /*double*/xSpanPixels = xMap.pDist();
  22. qDebug() << "xMap.p1()=" << xMap.p1() << " xMap.p2()=" << xMap.p2() << " xMap.pDist()=" << xMap.pDist();
  23.  
  24. // get axis extend in physical coordinates
  25. double xMin = axisScaleDiv( QwtPlot::xBottom )->lowerBound();
  26. double xMax = axisScaleDiv( QwtPlot::xBottom )->upperBound();
  27. double yMin = axisScaleDiv( QwtPlot::yLeft )->lowerBound();
  28. double yMax = axisScaleDiv( QwtPlot::yLeft )->upperBound();
  29.  
  30. // get color band line attributes (abcissa, width)
  31. double x, widthPixels;
  32. if ( axisScaleEngine(xBottom)->transformation()->type() == QwtScaleTransformation::Log10 ) { // log axis
  33. double xLowerPixels = xSpanPixels * (log10(lowerValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
  34. double xUpperPixels = xSpanPixels * (log10(upperValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
  35. widthPixels = xUpperPixels - xLowerPixels;
  36. double xPixels = (xUpperPixels + xLowerPixels) * 0.5;
  37. x = pow( 10., (log10(xMax) - log10(xMin)) * xPixels / xSpanPixels + log10(xMin) );
  38. qDebug() << "LOG: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
  39. }
  40. else { // lin axis, other
  41. x = ( lowerValue + upperValue ) * 0.5;
  42. widthPixels = xSpanPixels / ( xMax - xMin ) * ( upperValue - lowerValue );
  43. qDebug() << "LIN: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
  44. }
  45.  
  46. // draw color band
  47. double xData[2] = { x, x };
  48. double yData[2] = { yMin, yMax };
  49.  
  50. QwtPlotCurve* curve = new QwtPlotCurve;
  51. curve->setSamples( xData, yData, 2 ); //vertical line from (xMin,y) to (xMax,y)
  52. curve->setTitle( bandLabel );
  53.  
  54. QPen curvePen;
  55. color.setAlphaF( transparency ); // we want to see the underlying grid
  56. curvePen.setColor( color ); // must be located after setAlphaF()!
  57. curvePen.setWidthF( widthPixels );
  58. //curvePen.setCapStyle( Qt::FlatCap );
  59. curve->setPen( curvePen );
  60.  
  61. if ( bandLabel.isEmpty() )
  62. curve->setItemAttribute( QwtPlotItem::Legend, false );
  63.  
  64. curve->attach( this );
  65. }
To copy to clipboard, switch view to plain text mode 

setVerticalColor.jpg