[PyKDE] rotated CanvasText

Florian Remek remek at web.de
Thu Oct 20 04:21:45 BST 2005


Helo dear python-hackers,

im quite new to python and pyqt and stumbled across a problem
which seems unsolvable for me. I wrote a little test application
using the canvas painting facility and wanted to rotate a canvasText.
But i cant figure how to get it properly repainted after i moved
the view or the whole window.
The rotated Text always looks dirty or doesnt get repainted at all.

Here's the code:

from qt import *
from qtcanvas import *
import sys

## let pyqt say which name an event has!
## borrowed this class from:
##
http://mats.imk.fraunhofer.de/pipermail/pykde/2003-October/006212.html
class _qtEvent:
    _qtEventDict = {
        0: "None",        
        12: "Paint",
        13: "Move",
        14: "Resize",
        15: "Create",
        16: "Destroy",
        17: "Show",
        18: "Hide"
    }

    def types(self):
        return self._qtEventDict.keys()

    def which(self, t):
        try:
            if t in self._qtEventDict.keys():
                return self._qtEventDict[t]
        except:
            return "Unknown";

qte = _qtEvent()

####################################################
class TestCanvas(QCanvas):
   def __init__(self, parent, *args):
        QCanvas.__init__(self, *args)
        self.parent=parent
        self.setDoubleBuffering(0)

####################################################
## ROTATED TEXT CLASS

class TestTextItem(QCanvasText):
    def __init__(self, text, x, y, degree, parent,*args):
      QCanvasText.__init__(self,parent,*args)
      self.degree=degree
      self.parent=parent
      self.text=QString(text)
      self.x=x
      self.y=y 
      #font debug info
      print "Using font-family: ",self.font().family()      
      qfm = QFontMetrics(self.font())
      print "font specs: ",qfm.height(),qfm.width(self.text)
      
      #getting text-bounding-box from fontmetrics
      self._bndrect=QRect(0,0,qfm.width(self.text),qfm.height())  
      #do the rotation and translation
      wm = QWMatrix()
      wm.rotate(self.degree)
      wm.translate(self.x,self.y)
      self._bndrect=wm.mapRect(self._bndrect)      
      print "Inital bounding: ",self._bndrect.left()\
      ,self._bndrect.top(),self._bndrect.right(),self._bndrect.bottom()

    def boundingRect(self):        
        return self._bndrect
        
    def draw(self, qp):
        qp.save()        
        wm = QWMatrix()
        qp.setWorldMatrix(wm.rotate(self.degree))
        qp.setWorldMatrix(wm.translate(self.x,self.y))                
        self.setText(self.text)
        QCanvasText.draw(self,qp)
        qp.restore()
        qp.drawRect(self._bndrect)

####################################################
class TestCanvasView(QCanvasView):
    def __init__(self, *args):
        QCanvasView.__init__(self,*args)
        
    def paintEvent(selv,ev):
        QFrame.paintEvent(ev)
        p=QPainter(self)
        p.setClipRegion(ev.region())
        for i in self.canvas().canvItems:
            canvItems.show()

    #just for tracing the event handling        
    def eventFilter(self, qobj, qev):
        if qev.type()==QEvent.Paint:
            print "TestCanvasView(QCanvasView) Event:"\
	    ,qte.which(qev.type())
            print "repaintRect(x1,y1,x2,y2) ="\
	    ,qev.rect().x(),qev.rect().y()\
            ,qev.rect().x()+qev.rect().width(),qev.rect().y()+qev.rect().height()
        else:
            print "TestCanvasView(QCanvasView) Event: "\
	    ,qte.which(qev.type())            
        return QCanvasView.eventFilter(self,qobj,qev)

####################################################
class TestClass(QMainWindow):
    def __init__(self) :
        self.canvItems=[]
        QMainWindow.__init__(self)
        self.canv=TestCanvas(self,640,480)
        self.canvView=TestCanvasView(self.canv,self)        

    def show(self):
        QMainWindow.show(self)

        self.canvView.resize(self.width(),self.height())
        self.setCentralWidget(self.canvView)
        for i in range(5):
            item=QCanvasRectangle(0,0,100+5*i,100+5*i,self.canv)
            item.show()
            self.canvItems.append(item)

            item=TestTextItem("Text "+str(i), 100+5*i,5*i, 12*i\
	    ,self.canv)
            item.show()
            print "TestClass: bdBox:"\
	    ,item.boundingRect().left(),item.boundingRect().bottom()\
	    ,item.boundingRect().right(),item.boundingRect().top()
            self.canvItems.append(item)

        self.canv.update()
        self.canvView.show()
        

def main(args):
    app=QApplication(args)
    testObj=TestClass()
    app.setMainWidget(testObj)
    testObj.show()
    app.exec_loop()

if __name__ == "__main__":
    main(sys.argv)





More information about the PyQt mailing list