summaryrefslogtreecommitdiffstats
path: root/python/pyqt/examples3/fontdisplayer.py
blob: cb8531a4f89e503abea12d021774eaa7ee049b48 (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$
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
***************************************************************************"""

import sys
from qt import *

class FontRowTable( QFrame ):
    def __init__( self, parent=None, name=None ):
        QFrame.__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 = QFont( QApplication.font() )

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

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

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

        h = fm.height()

        body      = QColor(255,255,192);
        negative  = QColor(255,192,192);
        positive  = QColor(192,192,255);
        rnegative = QColor(255,128,128);
        rpositive = QColor(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 = QChar(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,QBrush(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, y-h/2, abs(l),-h/2, QBrush(sign))
                            if r :
                                if r < 0: sign = rnegative
                                else: sign = rpositive
                                if r > 0: rsign = r
                                else: rsign = 0
                                p.fillRect(x+w-rsign,y+2, abs(r),-h/2, QBrush(sign))
                        s = QString( ch )
                        p.setPen(QPen(Qt.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"), ( QString(str), ) )
        self.update()
        
    def chooseFont( self ) :
        ok = 0
        oldfont = QFont( self.tablefont )
        self.tablefont, ok = QFontDialog.getFont(oldfont, self)
        if ok:
            self.setFont(self.tablefont)
        else:
            self.tablefont = oldfont

class FontDisplayer( QMainWindow ) :
    def __init__( self, parent=None, name=None ):
        QMainWindow.__init__( self, parent, name )
        table = FontRowTable(self)
        controls = QToolBar(self)
        
        QLabel(self.tr("Row:"), controls)
        self.row = QSpinBox(0,255,1,controls)
        controls.addSeparator()
        fontbutton = QPushButton(self.tr("Font..."), controls)
        status = QStatusBar(self) 
        
        self.connect( self.row, SIGNAL("valueChanged(int)"), table.setRow )
        self.connect( fontbutton, SIGNAL("clicked()"), table.chooseFont )
        self.connect( table, PYSIGNAL("fontInformation"),
                      status, SLOT("message(const QString&)") )
        table.setRow(0)
        self.setCentralWidget(table)

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