summaryrefslogtreecommitdiffstats
path: root/examples/fontdisplayer.py
blob: bcf9d18c218c1940c20d53643ca6f28b0bedbf33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python

"""**************************************************************************
** $Id: fontdisplayer.py,v 1.2 2004/07/19 18:41:53 phil Exp $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
***************************************************************************"""

import sys
from PyTQt.tqt import *

class FontRowTable( TQFrame ):
    def __init__( self, parent=None, name=None ):
        TQFrame.__init__( self, parent, name )

        self.setBackgroundMode(self.PaletteBase)
        self.setFrameStyle(self.Panel|self.Sunken)
        self.setMargin(8)
        self.setRow(0)
        self.row = 0
        self.tablefont = TQFont( TQApplication.font() )

    def sizeHint( self ) :
        width  = 16*self.cellSize().width()+TQSize(2,2).width()*(self.margin()+self.frameWidth())
        height = 16*self.cellSize().height()+TQSize(2,2).height()*(self.margin()+self.frameWidth())
        return TQSize(width,height)
        
    def cellSize( self ) :
        fm = self.fontMetrics()
        return TQSize( fm.maxWidth(), fm.lineSpacing() + 1 )
    
    def paintEvent( self, e ):
        TQFrame.paintEvent(self, e)
        p = TQPainter(self)
        p.setClipRegion(e.region())
        r = TQRect(e.rect())
        fm = self.fontMetrics()
        ml = self.frameWidth() + self.margin() + 1 + max(0,-fm.minLeftBearing())
        mt = self.frameWidth() + self.margin()
        cell = TQSize(int((self.width()-15-ml)/16),int((self.height()-15-mt)/16))

        if not cell.width() or not cell.height() :
            return

        mini = int(r.left() / cell.width())
        maxi = int((r.right()+cell.width()-1) / cell.width())
        minj = int(r.top() / cell.height())
        maxj = int((r.bottom()+cell.height()-1) / cell.height())

        h = fm.height()

        body      = TQColor(255,255,192);
        negative  = TQColor(255,192,192);
        positive  = TQColor(192,192,255);
        rnegative = TQColor(255,128,128);
        rpositive = TQColor(128,128,255);

        for j in range(minj, maxj+1, 1) :
            for i in range(mini, maxi+1, 1) :
                if i < 16 and j < 16 :
                    x = i*cell.width()
                    y = j*cell.height()

                    ch = TQChar(j*16+i) #,self.row) # just accept one argument!!!

                    if fm.inFont(ch) :
                        w = fm.width(ch)
                        l = fm.leftBearing(ch)
                        r = fm.rightBearing(ch)

                        x += ml
                        y += mt+h

                        p.fillRect(x,y,w,-h,TQBrush(body))
                        
                        if w :
                            if l :
                                if l < 0: sign = negative
                                else: sign = positive
                                if l > 0: lsign = 0
                                else: lsign = 1
                                p.fillRect(x+lsign, int(y-h/2), abs(l),int(-h/2), TQBrush(sign))
                            if r :
                                if r < 0: sign = rnegative
                                else: sign = rpositive
                                if r > 0: rsign = r
                                else: rsign = 0
                                p.fillRect(int(x+w-rsign),y+2, abs(r),int(-h/2), TQBrush(sign))
                        s = TQString( ch )
                        p.setPen(TQPen(TQt.black))
                        p.drawText(x,y,s)
    
    def setRow( self, r ) :
        self.row = r

        fm = self.fontMetrics()
        str = " minLB=%d minRB=%d maxW=%d" % (fm.minLeftBearing(), fm.minRightBearing(), fm.maxWidth())
        self.emit( PYSIGNAL("fontInformation"), ( TQString(str), ) )
        self.update()
        
    def chooseFont( self ) :
        ok = 0
        oldfont = TQFont( self.tablefont )
        self.tablefont, ok = TQFontDialog.getFont(oldfont, self)
        if ok:
            self.setFont(self.tablefont)
        else:
            self.tablefont = oldfont

class FontDisplayer( TQMainWindow ) :
    def __init__( self, parent=None, name=None ):
        TQMainWindow.__init__( self, parent, name )
        table = FontRowTable(self)
        controls = TQToolBar(self)
        
        TQLabel(self.tr("Row:"), controls)
        self.row = TQSpinBox(0,255,1,controls)
        controls.addSeparator()
        fontbutton = TQPushButton(self.tr("Font..."), controls)
        status = TQStatusBar(self) 
        
        self.connect( self.row, TQ_SIGNAL("valueChanged(int)"), table.setRow )
        self.connect( fontbutton, TQ_SIGNAL("clicked()"), table.chooseFont )
        self.connect( table, PYSIGNAL("fontInformation"),
                      status, TQ_SLOT("message(const TQString&)") )
        table.setRow(0)
        self.setCentralWidget(table)

def main( args ):
    # Use an interesting font
    TQApplication.setFont(TQFont("unifont",16))
    app = TQApplication(sys.argv)
    
    m = FontDisplayer()
    sh = TQSize( m.centralWidget().sizeHint() )
    m.resize(sh.width(), sh.height() + 3 * m.statusBar().height())
    app.setMainWidget(m);
    m.setCaption("TQt Example - TQFD");
    m.show()
    
    app.exec_loop()
    
if __name__=="__main__":
    main(sys.argv)