SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fontdisplayer.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. """**************************************************************************
  3. ** $Id: fontdisplayer.py,v 1.2 2004/07/19 18:41:53 phil Exp $
  4. **
  5. ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
  6. **
  7. ** This file is part of an example program for TQt. This example
  8. ** program may be used, distributed and modified without limitation.
  9. **
  10. ***************************************************************************"""
  11. import sys
  12. from python_tqt.qt import *
  13. class FontRowTable( TQFrame ):
  14. def __init__( self, parent=None, name=None ):
  15. TQFrame.__init__( self, parent, name )
  16. self.setBackgroundMode(self.PaletteBase)
  17. self.setFrameStyle(self.Panel|self.Sunken)
  18. self.setMargin(8)
  19. self.setRow(0)
  20. self.row = 0
  21. self.tablefont = TQFont( TQApplication.font() )
  22. def sizeHint( self ) :
  23. width = 16*self.cellSize().width()+TQSize(2,2).width()*(self.margin()+self.frameWidth())
  24. height = 16*self.cellSize().height()+TQSize(2,2).height()*(self.margin()+self.frameWidth())
  25. return TQSize(width,height)
  26. def cellSize( self ) :
  27. fm = self.fontMetrics()
  28. return TQSize( fm.maxWidth(), fm.lineSpacing() + 1 )
  29. def paintEvent( self, e ):
  30. TQFrame.paintEvent(self, e)
  31. p = TQPainter(self)
  32. p.setClipRegion(e.region())
  33. r = TQRect(e.rect())
  34. fm = self.fontMetrics()
  35. ml = self.frameWidth() + self.margin() + 1 + max(0,-fm.minLeftBearing())
  36. mt = self.frameWidth() + self.margin()
  37. cell = TQSize((self.width()-15-ml)/16,(self.height()-15-mt)/16)
  38. if not cell.width() or not cell.height() :
  39. return
  40. mini = r.left() / cell.width()
  41. maxi = (r.right()+cell.width()-1) / cell.width()
  42. minj = r.top() / cell.height()
  43. maxj = (r.bottom()+cell.height()-1) / cell.height()
  44. h = fm.height()
  45. body = TQColor(255,255,192);
  46. negative = TQColor(255,192,192);
  47. positive = TQColor(192,192,255);
  48. rnegative = TQColor(255,128,128);
  49. rpositive = TQColor(128,128,255);
  50. for j in range(minj, maxj+1, 1) :
  51. for i in range(mini, maxi+1, 1) :
  52. if i < 16 and j < 16 :
  53. x = i*cell.width()
  54. y = j*cell.height()
  55. ch = TQChar(j*16+i) #,self.row) # just accept one argument!!!
  56. if fm.inFont(ch) :
  57. w = fm.width(ch)
  58. l = fm.leftBearing(ch)
  59. r = fm.rightBearing(ch)
  60. x += ml
  61. y += mt+h
  62. p.fillRect(x,y,w,-h,TQBrush(body))
  63. if w :
  64. if l :
  65. if l < 0: sign = negative
  66. else: sign = positive
  67. if l > 0: lsign = 0
  68. else: lsign = 1
  69. p.fillRect(x+lsign, y-h/2, abs(l),-h/2, TQBrush(sign))
  70. if r :
  71. if r < 0: sign = rnegative
  72. else: sign = rpositive
  73. if r > 0: rsign = r
  74. else: rsign = 0
  75. p.fillRect(x+w-rsign,y+2, abs(r),-h/2, TQBrush(sign))
  76. s = TQString( ch )
  77. p.setPen(TQPen(TQt.black))
  78. p.drawText(x,y,s)
  79. def setRow( self, r ) :
  80. self.row = r
  81. fm = self.fontMetrics()
  82. str = " minLB=%d minRB=%d maxW=%d" % (fm.minLeftBearing(), fm.minRightBearing(), fm.maxWidth())
  83. self.emit( PYSIGNAL("fontInformation"), ( TQString(str), ) )
  84. self.update()
  85. def chooseFont( self ) :
  86. ok = 0
  87. oldfont = TQFont( self.tablefont )
  88. self.tablefont, ok = TQFontDialog.getFont(oldfont, self)
  89. if ok:
  90. self.setFont(self.tablefont)
  91. else:
  92. self.tablefont = oldfont
  93. class FontDisplayer( TQMainWindow ) :
  94. def __init__( self, parent=None, name=None ):
  95. TQMainWindow.__init__( self, parent, name )
  96. table = FontRowTable(self)
  97. controls = TQToolBar(self)
  98. TQLabel(self.tr("Row:"), controls)
  99. self.row = TQSpinBox(0,255,1,controls)
  100. controls.addSeparator()
  101. fontbutton = TQPushButton(self.tr("Font..."), controls)
  102. status = TQStatusBar(self)
  103. self.connect( self.row, SIGNAL("valueChanged(int)"), table.setRow )
  104. self.connect( fontbutton, SIGNAL("clicked()"), table.chooseFont )
  105. self.connect( table, PYSIGNAL("fontInformation"),
  106. status, SLOT("message(const TQString&)") )
  107. table.setRow(0)
  108. self.setCentralWidget(table)
  109. def main( args ):
  110. # Use an interesting font
  111. TQApplication.setFont(TQFont("unifont",16))
  112. app = TQApplication(sys.argv)
  113. m = FontDisplayer()
  114. sh = TQSize( m.centralWidget().sizeHint() )
  115. m.resize(sh.width(), sh.height() + 3 * m.statusBar().height())
  116. app.setMainWidget(m);
  117. m.setCaption("TQt Example - TQFD");
  118. m.show()
  119. app.exec_loop()
  120. if __name__=="__main__":
  121. main(sys.argv)