1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/qfd/qfd.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Font Displayer</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Font Displayer</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example program displays all characters of a font.
|
---|
37 | <p> <hr>
|
---|
38 | <p> Header file:
|
---|
39 | <p> <pre>/****************************************************************************
|
---|
40 | ** $Id: qfd-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
41 | **
|
---|
42 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
43 | **
|
---|
44 | ** This file is part of an example program for Qt. This example
|
---|
45 | ** program may be used, distributed and modified without limitation.
|
---|
46 | **
|
---|
47 | *****************************************************************************/
|
---|
48 |
|
---|
49 | #ifndef FontDisplayer_H
|
---|
50 | #define FontDisplayer_H
|
---|
51 |
|
---|
52 | #include <<a href="qframe-h.html">qframe.h</a>>
|
---|
53 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
54 |
|
---|
55 | class QSlider;
|
---|
56 |
|
---|
57 | class FontRowTable : public <a href="qframe.html">QFrame</a> {
|
---|
58 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
59 | public:
|
---|
60 | FontRowTable( <a href="qwidget.html">QWidget</a>* parent=0, const char* name=0 );
|
---|
61 |
|
---|
62 | <a href="qsize.html">QSize</a> sizeHint() const;
|
---|
63 |
|
---|
64 | signals:
|
---|
65 | void fontInformation(const <a href="qstring.html">QString</a>&);
|
---|
66 |
|
---|
67 | public slots:
|
---|
68 | void setRow(int);
|
---|
69 | void chooseFont();
|
---|
70 |
|
---|
71 |
|
---|
72 | protected:
|
---|
73 | <a href="qsize.html">QSize</a> cellSize() const;
|
---|
74 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a>* );
|
---|
75 | private:
|
---|
76 | <a href="qfont.html">QFont</a> tablefont;
|
---|
77 | int row;
|
---|
78 | };
|
---|
79 |
|
---|
80 | class FontDisplayer : public <a href="qmainwindow.html">QMainWindow</a> {
|
---|
81 | Q_OBJECT
|
---|
82 | public:
|
---|
83 | FontDisplayer( <a href="qwidget.html">QWidget</a>* parent=0, const char* name=0 );
|
---|
84 | };
|
---|
85 |
|
---|
86 | #endif
|
---|
87 | </pre>
|
---|
88 |
|
---|
89 | <p> <hr>
|
---|
90 | <p> Implementation:
|
---|
91 | <p> <pre>/****************************************************************************
|
---|
92 | ** $Id: qfd-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
93 | **
|
---|
94 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
95 | **
|
---|
96 | ** This file is part of an example program for Qt. This example
|
---|
97 | ** program may be used, distributed and modified without limitation.
|
---|
98 | **
|
---|
99 | *****************************************************************************/
|
---|
100 |
|
---|
101 | #include "fontdisplayer.h"
|
---|
102 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
103 | #include <<a href="qslider-h.html">qslider.h</a>>
|
---|
104 | #include <<a href="qspinbox-h.html">qspinbox.h</a>>
|
---|
105 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
106 | #include <<a href="qtoolbar-h.html">qtoolbar.h</a>>
|
---|
107 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
108 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
109 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
110 | #include <<a href="qfontdialog-h.html">qfontdialog.h</a>>
|
---|
111 | #include <stdlib.h>
|
---|
112 |
|
---|
113 |
|
---|
114 | <a name="f531"></a>FontRowTable::FontRowTable( <a href="qwidget.html">QWidget</a>* parent, const char* name ) :
|
---|
115 | <a href="qframe.html">QFrame</a>(parent,name)
|
---|
116 | {
|
---|
117 | <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>(PaletteBase);
|
---|
118 | <a href="qframe.html#setFrameStyle">setFrameStyle</a>(Panel|Sunken);
|
---|
119 | <a href="qframe.html#setMargin">setMargin</a>(8);
|
---|
120 | setRow(0);
|
---|
121 | <a name="x1979"></a> tablefont = QApplication::<a href="qapplication.html#font">font</a>();
|
---|
122 | }
|
---|
123 |
|
---|
124 | <a name="x1996"></a>QSize FontRowTable::<a href="qwidget.html#sizeHint">sizeHint</a>() const
|
---|
125 | {
|
---|
126 | return 24*cellSize()+QSize(2,2)*(<a href="qframe.html#margin">margin</a>()+frameWidth());
|
---|
127 | }
|
---|
128 |
|
---|
129 | QSize <a name="f532"></a>FontRowTable::cellSize() const
|
---|
130 | {
|
---|
131 | <a href="qfontmetrics.html">QFontMetrics</a> fm = <a href="qwidget.html#fontMetrics">fontMetrics</a>();
|
---|
132 | <a name="x1990"></a><a name="x1989"></a> return QSize( fm.<a href="qfontmetrics.html#maxWidth">maxWidth</a>(), fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>()+1 );
|
---|
133 | }
|
---|
134 |
|
---|
135 | <a name="x1995"></a>void FontRowTable::<a href="qframe.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a>* e )
|
---|
136 | {
|
---|
137 | QFrame::<a href="qframe.html#paintEvent">paintEvent</a>(e);
|
---|
138 | <a href="qpainter.html">QPainter</a> p(this);
|
---|
139 | <a name="x2002"></a><a name="x1999"></a> p.<a href="qpainter.html#setClipRegion">setClipRegion</a>(e-><a href="qpaintevent.html#region">region</a>());
|
---|
140 | <a name="x2001"></a> <a href="qrect.html">QRect</a> r = e-><a href="qpaintevent.html#rect">rect</a>();
|
---|
141 | <a href="qfontmetrics.html">QFontMetrics</a> fm = <a href="qwidget.html#fontMetrics">fontMetrics</a>();
|
---|
142 | <a name="x1991"></a> int ml = <a href="qframe.html#frameWidth">frameWidth</a>()+margin() + 1 + QMAX(0,-fm.<a href="qfontmetrics.html#minLeftBearing">minLeftBearing</a>());
|
---|
143 | int mt = <a href="qframe.html#frameWidth">frameWidth</a>()+margin();
|
---|
144 | <a href="qsize.html">QSize</a> cell((<a href="qwidget.html#width">width</a>()-15-ml)/16,(<a href="qwidget.html#height">height</a>()-15-mt)/16);
|
---|
145 |
|
---|
146 | <a name="x2008"></a><a name="x2007"></a> if ( !cell.<a href="qsize.html#width">width</a>() || !cell.<a href="qsize.html#height">height</a>() )
|
---|
147 | return;
|
---|
148 |
|
---|
149 | int mini = r.<a href="qrect.html#left">left</a>() / cell.<a href="qsize.html#width">width</a>();
|
---|
150 | int maxi = (r.<a href="qrect.html#right">right</a>()+cell.<a href="qsize.html#width">width</a>()-1) / cell.<a href="qsize.html#width">width</a>();
|
---|
151 | int minj = r.<a href="qrect.html#top">top</a>() / cell.<a href="qsize.html#height">height</a>();
|
---|
152 | <a name="x2003"></a> int maxj = (r.<a href="qrect.html#bottom">bottom</a>()+cell.<a href="qsize.html#height">height</a>()-1) / cell.<a href="qsize.html#height">height</a>();
|
---|
153 |
|
---|
154 | <a name="x1986"></a> int h = fm.<a href="qfontmetrics.html#height">height</a>();
|
---|
155 |
|
---|
156 | <a href="qcolor.html">QColor</a> body(255,255,192);
|
---|
157 | <a href="qcolor.html">QColor</a> negative(255,192,192);
|
---|
158 | <a href="qcolor.html">QColor</a> positive(192,192,255);
|
---|
159 | <a href="qcolor.html">QColor</a> rnegative(255,128,128);
|
---|
160 | <a href="qcolor.html">QColor</a> rpositive(128,128,255);
|
---|
161 |
|
---|
162 | for (int j = minj; j<=maxj; j++) {
|
---|
163 | for (int i = mini; i<=maxi; i++) {
|
---|
164 | if ( i < 16 && j < 16 ) {
|
---|
165 | int x = i*cell.<a href="qsize.html#width">width</a>();
|
---|
166 | int y = j*cell.<a href="qsize.html#height">height</a>();
|
---|
167 |
|
---|
168 | <a href="qchar.html">QChar</a> ch = QChar(j*16+i,row);
|
---|
169 |
|
---|
170 | <a name="x1987"></a> if ( fm.<a href="qfontmetrics.html#inFont">inFont</a>(ch) ) {
|
---|
171 | <a name="x1994"></a> int w = fm.<a href="qfontmetrics.html#width">width</a>(ch);
|
---|
172 | <a name="x1988"></a> int l = fm.<a href="qfontmetrics.html#leftBearing">leftBearing</a>(ch);
|
---|
173 | <a name="x1993"></a> int r = fm.<a href="qfontmetrics.html#rightBearing">rightBearing</a>(ch);
|
---|
174 |
|
---|
175 | x += ml;
|
---|
176 | y += mt+h;
|
---|
177 |
|
---|
178 | <a name="x1998"></a> p.<a href="qpainter.html#fillRect">fillRect</a>(x,y,w,-h,body);
|
---|
179 | if ( w ) {
|
---|
180 | if ( l ) {
|
---|
181 | p.<a href="qpainter.html#fillRect">fillRect</a>(x+(l>0?0:l), y-h/2, abs(l),-h/2,
|
---|
182 | l < 0 ? negative : positive);
|
---|
183 | }
|
---|
184 | if ( r ) {
|
---|
185 | p.<a href="qpainter.html#fillRect">fillRect</a>(x+w-(r>0?r:0),y+2, abs(r),-h/2,
|
---|
186 | r < 0 ? rnegative : rpositive);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | <a href="qstring.html">QString</a> s;
|
---|
190 | s += ch;
|
---|
191 | p.<a href="qpainter.html#setPen">setPen</a>(QPen(Qt::black));
|
---|
192 | p.<a href="qpainter.html#drawText">drawText</a>(x,y,s);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | void <a name="f533"></a>FontRowTable::setRow(int r)
|
---|
200 | {
|
---|
201 | row = r;
|
---|
202 |
|
---|
203 | <a href="qfontmetrics.html">QFontMetrics</a> fm = <a href="qwidget.html#fontMetrics">fontMetrics</a>();
|
---|
204 | <a href="qfontinfo.html">QFontInfo</a> fi = <a href="qwidget.html#fontInfo">fontInfo</a>();
|
---|
205 | <a href="qstring.html">QString</a> str = QString("%1 %2pt%3%4 mLB=%5 mRB=%6 mW=%7")
|
---|
206 | <a name="x1983"></a> .arg(fi.<a href="qfontinfo.html#family">family</a>())
|
---|
207 | <a name="x1985"></a> .arg(fi.<a href="qfontinfo.html#pointSize">pointSize</a>())
|
---|
208 | <a name="x1982"></a> .arg(fi.<a href="qfontinfo.html#bold">bold</a>() ? " bold" : "")
|
---|
209 | <a name="x1984"></a> .arg(fi.<a href="qfontinfo.html#italic">italic</a>() ? " italic" : "")
|
---|
210 | .arg(fm.<a href="qfontmetrics.html#minLeftBearing">minLeftBearing</a>())
|
---|
211 | <a name="x1992"></a> .arg(fm.<a href="qfontmetrics.html#minRightBearing">minRightBearing</a>())
|
---|
212 | .arg(fm.<a href="qfontmetrics.html#maxWidth">maxWidth</a>());
|
---|
213 |
|
---|
214 | emit fontInformation(str);
|
---|
215 | <a href="qwidget.html#update">update</a>();
|
---|
216 | }
|
---|
217 |
|
---|
218 | void <a name="f534"></a>FontRowTable::chooseFont()
|
---|
219 | {
|
---|
220 | bool ok;
|
---|
221 | <a href="qfont.html">QFont</a> oldfont = tablefont;
|
---|
222 | <a name="x1981"></a> tablefont = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>(&ok, oldfont, this);
|
---|
223 |
|
---|
224 | if (ok)
|
---|
225 | <a href="qwidget.html#setFont">setFont</a>(tablefont);
|
---|
226 | else
|
---|
227 | tablefont = oldfont;
|
---|
228 | }
|
---|
229 |
|
---|
230 | <a name="f530"></a>FontDisplayer::FontDisplayer( <a href="qwidget.html">QWidget</a>* parent, const char* name ) :
|
---|
231 | <a href="qmainwindow.html">QMainWindow</a>(parent,name)
|
---|
232 | {
|
---|
233 | FontRowTable* table = new FontRowTable(this);
|
---|
234 | <a href="qtoolbar.html">QToolBar</a>* controls = new <a href="qtoolbar.html">QToolBar</a>(this);
|
---|
235 | (void) new <a href="qlabel.html">QLabel</a>(<a href="qobject.html#tr">tr</a>("Row:"), controls);
|
---|
236 | <a href="qspinbox.html">QSpinBox</a> *row = new <a href="qspinbox.html">QSpinBox</a>(0,255,1,controls);
|
---|
237 | <a name="x2010"></a> controls-><a href="qtoolbar.html#addSeparator">addSeparator</a>();
|
---|
238 | <a href="qpushbutton.html">QPushButton</a> *fontbutton = new <a href="qpushbutton.html">QPushButton</a>(<a href="qobject.html#tr">tr</a>("Font..."), controls);
|
---|
239 |
|
---|
240 | <a name="x2009"></a> <a href="qobject.html#connect">connect</a>(row,SIGNAL(<a href="qspinbox.html#valueChanged">valueChanged</a>(int)),table,SLOT(setRow(int)));
|
---|
241 | <a href="qobject.html#connect">connect</a>(fontbutton, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), table, SLOT(chooseFont()));
|
---|
242 | <a href="qobject.html#connect">connect</a>(table,SIGNAL(fontInformation(const <a href="qstring.html">QString</a>&)),
|
---|
243 | <a href="qmainwindow.html#statusBar">statusBar</a>(),SLOT(message(const <a href="qstring.html">QString</a>&)));
|
---|
244 | table->setRow(0);
|
---|
245 | <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>(table);
|
---|
246 | }
|
---|
247 | </pre>
|
---|
248 |
|
---|
249 | <p> <hr>
|
---|
250 | <p> Main:
|
---|
251 | <p> <pre>/****************************************************************************
|
---|
252 | ** $Id: qfd-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
253 | **
|
---|
254 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
255 | **
|
---|
256 | ** This file is part of an example program for Qt. This example
|
---|
257 | ** program may be used, distributed and modified without limitation.
|
---|
258 | **
|
---|
259 | *****************************************************************************/
|
---|
260 |
|
---|
261 | #include "fontdisplayer.h"
|
---|
262 |
|
---|
263 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
264 | #include <<a href="qslider-h.html">qslider.h</a>>
|
---|
265 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
266 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
267 |
|
---|
268 |
|
---|
269 |
|
---|
270 | int main(int argc, char** argv)
|
---|
271 | {
|
---|
272 | <a href="qapplication.html">QApplication</a> app(argc,argv);
|
---|
273 |
|
---|
274 | FontDisplayer m;
|
---|
275 | <a name="x2013"></a> <a href="qsize.html">QSize</a> sh = m.<a href="qmainwindow.html#centralWidget">centralWidget</a>()->sizeHint();
|
---|
276 | <a name="x2017"></a> m.<a href="qwidget.html#resize">resize</a>(sh.<a href="qsize.html#width">width</a>(),
|
---|
277 | <a name="x2016"></a><a name="x2015"></a> sh.<a href="qsize.html#height">height</a>()+3*m.<a href="qmainwindow.html#statusBar">statusBar</a>()->height());
|
---|
278 | app.<a href="qapplication.html#setMainWidget">setMainWidget</a>(&m);
|
---|
279 | m.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - QFD");
|
---|
280 | <a name="x2014"></a> m.<a href="qwidget.html#show">show</a>();
|
---|
281 |
|
---|
282 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
283 | }
|
---|
284 | </pre>
|
---|
285 |
|
---|
286 | <p>See also <a href="examples.html">Examples</a>.
|
---|
287 |
|
---|
288 | <!-- eof -->
|
---|
289 | <p><address><hr><div align=center>
|
---|
290 | <table width=100% cellspacing=0 border=0><tr>
|
---|
291 | <td>Copyright © 2007
|
---|
292 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
293 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
294 | </table></div></address></body>
|
---|
295 | </html>
|
---|