PDA

View Full Version : QwtPlotSpectrogram NearestNeighbor with borders or grid?



alketi
22nd May 2014, 17:27
I'm using the QwtPlotSpectrogram in NearestNeighbor mode.

The issue is that neighboring squares of the same color have no border or divider between them, so the colors blend into each other.

http://i.imgur.com/kku23eW.png

OPTIONS

1. I figured one possible solution would be to use a QwtPlotGrid for the divisions. Uwe, you might have also tried this as the "rasterview" example has some QwtPlotGrid code commented out with "#if 0".

The problems with a QwtPlotGrid were that I had trouble aligning the grid's default state with my divisions (which I'm thinking is solvable), and the fact that the grid is didn't track the square divisions correctly when zooming.

2. Another option (what would seem to be a perfect solution) would be to by default create a "border" around each square. The border could be black or white, or another specified color. And, a border would correctly track at all zoom levels.
------------

A few questions:

1. Within the API, is there a way to get borders or divisions between the squares? <I didn't see one>

2. Am I looking at an enhancement request? An option to add an x-pixel border to the squares in Nearest Neighbor mode doesn't seem like an impossible task. I'd be willing to poke around the source code if needed.

3. Any other options?

Thank you,
Alketi

Uwe
23rd May 2014, 07:04
The issue is that neighboring squares of the same color have no border or divider between them, so the colors blend into each other.i
By squares you are probably talking about data pixels and you want to have a frame around each pixel.
As your data seems to be organized in equidistant pixels your QwtRasterData object returns a valid pixelHint() ( when using QwtMatrixRasterData this is always the case ).

So you could try something like this ( completely untested ):


class YourSpectrogram: public QwtPlotSpectrogram
{
public:
virtual void draw( QPainter *painter,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRectF &canvasRect ) const
{
QwtPlotSpectrgram::draw( painter, xMap, yMap, canvasRect );
drawGrid( painter, xMap, yMap, canvasRect() );
}

private:
void drawGrid( QPainter *painter,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRectF &canvasRect ) const
{
painter->setPen( ... );

const QRectF hint = data()->pixelHint();

// vertical lines

const double x1 = xMap.invTransform( canvasRect.left() );
const double x2 = xMap.invTransform( canvasRect.right() ),

// aligning x to the beginning of a pixel
const double x = hint.right() + ( qFloor( ( x1 - hint.right() ) / hint.width() ) + 1 ) * hint.width();

for ( ; x < x2, x += hint.width() )
{
const double pos = xMap.transform( x );
painter->drawLine( pos, canvasRect.top(), pos, canvasRect.bottom() );
}

// horizontal lines

...
}
};HTH Uwe