[patch] cosmetic fix for KPlotWidget

Jason Harris kstars at 30doradus.org
Sun Dec 2 13:44:32 GMT 2007

On 12/2/07, Thomas Zander <zander at kde.org> wrote:
> On Sunday 02 December 2007 06:30:43 Jason Harris wrote:
> > Ok, I am fine with temporarily shutting off antialiasing when drawing
> > the plot axes rather than offsetting by 0.5 pixels. Patch is below.
> > Ok to commit?
> I tend to use save() restore() instead of hard coding the antialiassing.
> So;
>   p->save();
>   p->setRenderHint( QPainter::Antialiasing, false );
>   // do something that requires AA to be off
>   p->restore();
> I think thats cleaner then a bool.
Thanks for the further advice.  Of course I should have used save()/restore().

> At the same time, I expect the the caller of drawAxis() to be the one to
> do save()/restore() so the drawAxis can manipulate the painter as much as
> it wants without interfering with other people's code.
> In KOffice I aim for this kind of contract-coding which makes things a lot
> easier.   So, always call save before you pass the painter to some method
> and call restore directly after.
I would prefer to keep it internal to drawAxis(), because subclasses
may override the paintEvent(), and I'd prefer it if they don't have to
worry about wrapping save/restore around calls to drawAxes().  If they
were to forget, then AA would be killed for all subsequent drawing.
It seems safer to encapsulate the save/restore inside drawAxes(),
exactly for the reason that I don't want to mess with other code.

Index: kplotwidget.cpp
--- kplotwidget.cpp     (revision 743854)
+++ kplotwidget.cpp     (working copy)
@@ -548,7 +548,7 @@
     p.begin( this );
     p.setRenderHint( QPainter::Antialiasing, d->useAntialias );
     p.fillRect( rect(), backgroundColor() );
-    p.translate( leftPadding() + 0.5, topPadding() + 0.5 );
+    p.translate( leftPadding(), topPadding() );

     p.setClipRect( d->pixRect );
@@ -582,6 +582,15 @@

 void KPlotWidget::drawAxes( QPainter *p ) {
+    //Turn off antialiasing inside this function, so that the axis lines are
+    //rendered as sharp, thin lines.  Lines that are perfectly
+    //and have integer-value coordinates are rendered as
semi-transparent and two
+    //pixels wide when antialiasing is on, because the line width is
+    //equally between the bracketing pixels.  We turn off antialiasing in this
+    //function to avoid that.
+    p->save();
+    p->setRenderHint( QPainter::Antialiasing, false );
     if ( d->showGrid ) {
         p->setPen( gridColor() );

@@ -780,6 +789,8 @@
             p->restore();  //restore translation/rotation state
     }  //End of RightAxis
+    p->restore();

 int KPlotWidget::leftPadding() const

> Anyway, thats just what I prefer, take it as you want :)  I don't object
> to the patch, I don't know enough of this code to do.
> --
> Thomas Zander

More information about the kde-core-devel mailing list