source: vendor/trolltech/current/examples/qfd/fontdisplayer.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/****************************************************************************
2** $Id: fontdisplayer.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "fontdisplayer.h"
12#include <qapplication.h>
13#include <qslider.h>
14#include <qspinbox.h>
15#include <qpainter.h>
16#include <qtoolbar.h>
17#include <qstatusbar.h>
18#include <qlabel.h>
19#include <qpushbutton.h>
20#include <qfontdialog.h>
21#include <stdlib.h>
22
23
24FontRowTable::FontRowTable( QWidget* parent, const char* name ) :
25 QFrame(parent,name)
26{
27 setBackgroundMode(PaletteBase);
28 setFrameStyle(Panel|Sunken);
29 setMargin(8);
30 setRow(0);
31 tablefont = QApplication::font();
32}
33
34QSize FontRowTable::sizeHint() const
35{
36 return 24*cellSize()+QSize(2,2)*(margin()+frameWidth());
37}
38
39QSize FontRowTable::cellSize() const
40{
41 QFontMetrics fm = fontMetrics();
42 return QSize( fm.maxWidth(), fm.lineSpacing()+1 );
43}
44
45void FontRowTable::paintEvent( QPaintEvent* e )
46{
47 QFrame::paintEvent(e);
48 QPainter p(this);
49 p.setClipRegion(e->region());
50 QRect r = e->rect();
51 QFontMetrics fm = fontMetrics();
52 int ml = frameWidth()+margin() + 1 + QMAX(0,-fm.minLeftBearing());
53 int mt = frameWidth()+margin();
54 QSize cell((width()-15-ml)/16,(height()-15-mt)/16);
55
56 if ( !cell.width() || !cell.height() )
57 return;
58
59 int mini = r.left() / cell.width();
60 int maxi = (r.right()+cell.width()-1) / cell.width();
61 int minj = r.top() / cell.height();
62 int maxj = (r.bottom()+cell.height()-1) / cell.height();
63
64 int h = fm.height();
65
66 QColor body(255,255,192);
67 QColor negative(255,192,192);
68 QColor positive(192,192,255);
69 QColor rnegative(255,128,128);
70 QColor rpositive(128,128,255);
71
72 for (int j = minj; j<=maxj; j++) {
73 for (int i = mini; i<=maxi; i++) {
74 if ( i < 16 && j < 16 ) {
75 int x = i*cell.width();
76 int y = j*cell.height();
77
78 QChar ch = QChar(j*16+i,row);
79
80 if ( fm.inFont(ch) ) {
81 int w = fm.width(ch);
82 int l = fm.leftBearing(ch);
83 int r = fm.rightBearing(ch);
84
85 x += ml;
86 y += mt+h;
87
88 p.fillRect(x,y,w,-h,body);
89 if ( w ) {
90 if ( l ) {
91 p.fillRect(x+(l>0?0:l), y-h/2, abs(l),-h/2,
92 l < 0 ? negative : positive);
93 }
94 if ( r ) {
95 p.fillRect(x+w-(r>0?r:0),y+2, abs(r),-h/2,
96 r < 0 ? rnegative : rpositive);
97 }
98 }
99 QString s;
100 s += ch;
101 p.setPen(QPen(Qt::black));
102 p.drawText(x,y,s);
103 }
104 }
105 }
106 }
107}
108
109void FontRowTable::setRow(int r)
110{
111 row = r;
112
113 QFontMetrics fm = fontMetrics();
114 QFontInfo fi = fontInfo();
115 QString str = QString("%1 %2pt%3%4 mLB=%5 mRB=%6 mW=%7")
116 .arg(fi.family())
117 .arg(fi.pointSize())
118 .arg(fi.bold() ? " bold" : "")
119 .arg(fi.italic() ? " italic" : "")
120 .arg(fm.minLeftBearing())
121 .arg(fm.minRightBearing())
122 .arg(fm.maxWidth());
123
124 emit fontInformation(str);
125 update();
126}
127
128void FontRowTable::chooseFont()
129{
130 bool ok;
131 QFont oldfont = tablefont;
132 tablefont = QFontDialog::getFont(&ok, oldfont, this);
133
134 if (ok)
135 setFont(tablefont);
136 else
137 tablefont = oldfont;
138}
139
140FontDisplayer::FontDisplayer( QWidget* parent, const char* name ) :
141 QMainWindow(parent,name)
142{
143 FontRowTable* table = new FontRowTable(this);
144 QToolBar* controls = new QToolBar(this);
145 (void) new QLabel(tr("Row:"), controls);
146 QSpinBox *row = new QSpinBox(0,255,1,controls);
147 controls->addSeparator();
148 QPushButton *fontbutton = new QPushButton(tr("Font..."), controls);
149
150 connect(row,SIGNAL(valueChanged(int)),table,SLOT(setRow(int)));
151 connect(fontbutton, SIGNAL(clicked()), table, SLOT(chooseFont()));
152 connect(table,SIGNAL(fontInformation(const QString&)),
153 statusBar(),SLOT(message(const QString&)));
154 table->setRow(0);
155 setCentralWidget(table);
156}
Note: See TracBrowser for help on using the repository browser.