source: trunk/doc/html/xform-example.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 25.7 KB
Line 
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/xform/xform.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Transformed Graphics Demo</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>Transformed Graphics Demo</h1>
33
34
35<p>
36This example lets the user rotate, shear and scale text and graphics
37arbitrarily.
38<p> <hr>
39<p> Implementation:
40<p> <pre>/****************************************************************************
41** $Id: xform-example.html 2051 2007-02-21 10:04:20Z chehrlic $
42**
43** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
44**
45** This file is part of an example program for Qt. This example
46** program may be used, distributed and modified without limitation.
47**
48*****************************************************************************/
49
50#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
51
52#include &lt;<a href="qdialog-h.html">qdialog.h</a>&gt;
53#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
54#include &lt;<a href="qlineedit-h.html">qlineedit.h</a>&gt;
55#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
56#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;
57#include &lt;<a href="qradiobutton-h.html">qradiobutton.h</a>&gt;
58#include &lt;<a href="qbuttongroup-h.html">qbuttongroup.h</a>&gt;
59#include &lt;<a href="qlcdnumber-h.html">qlcdnumber.h</a>&gt;
60#include &lt;<a href="qslider-h.html">qslider.h</a>&gt;
61#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
62#include &lt;<a href="qfontdialog-h.html">qfontdialog.h</a>&gt;
63#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
64#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
65#include &lt;<a href="qwidgetstack-h.html">qwidgetstack.h</a>&gt;
66
67#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
68#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
69#include &lt;<a href="qpicture-h.html">qpicture.h</a>&gt;
70
71#include &lt;stdlib.h&gt;
72
73
74class ModeNames {
75public:
76 enum Mode { Text, Image, Picture };
77};
78
79
80class XFormControl : public <a href="qvbox.html">QVBox</a>, public ModeNames
81{
82 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
83public:
84 XFormControl( const <a href="qfont.html">QFont</a> &amp;initialFont, QWidget *parent=0, const char *name=0 );
85 ~XFormControl() {}
86
87 <a href="qwmatrix.html">QWMatrix</a> matrix();
88
89signals:
90 void newMatrix( <a href="qwmatrix.html">QWMatrix</a> );
91 void newText( const <a href="qstring.html">QString</a>&amp; );
92 void newFont( const <a href="qfont.html">QFont</a> &amp; );
93 void newMode( int );
94private slots:
95 void newMtx();
96 void newTxt(const <a href="qstring.html">QString</a>&amp;);
97 void selectFont();
98 void fontSelected( const <a href="qfont.html">QFont</a> &amp; );
99 void changeMode(int);
100 void timerEvent(QTimerEvent*);
101private:
102 Mode mode;
103 <a href="qslider.html">QSlider</a> *rotS; // Rotation angle scroll bar
104 <a href="qslider.html">QSlider</a> *shearS; // Shear value scroll bar
105 <a href="qslider.html">QSlider</a> *magS; // Magnification value scroll bar
106 <a href="qlcdnumber.html">QLCDNumber</a> *rotLCD; // Rotation angle LCD display
107 <a href="qlcdnumber.html">QLCDNumber</a> *shearLCD; // Shear value LCD display
108 <a href="qlcdnumber.html">QLCDNumber</a> *magLCD; // Magnification value LCD display
109 <a href="qcheckbox.html">QCheckBox</a> *mirror; // Checkbox for mirror image on/of
110 <a href="qwidgetstack.html">QWidgetStack</a>* optionals;
111 <a href="qlineedit.html">QLineEdit</a> *textEd; // Inp[ut field for xForm text
112 <a href="qpushbutton.html">QPushButton</a> *fpb; // Select font push button
113 <a href="qradiobutton.html">QRadioButton</a> *rb_txt; // Radio button for text
114 <a href="qradiobutton.html">QRadioButton</a> *rb_img; // Radio button for image
115 <a href="qradiobutton.html">QRadioButton</a> *rb_pic; // Radio button for picture
116 <a href="qfont.html">QFont</a> currentFont;
117};
118
119/*
120 ShowXForm displays a text or a pixmap (QPixmap) using a coordinate
121 <a href="qwmatrix.html#TransformationMode">transformation matrix</a> (QWMatrix)
122*/
123
124class ShowXForm : public <a href="qwidget.html">QWidget</a>, public ModeNames
125{
126 Q_OBJECT
127public:
128 ShowXForm( const <a href="qfont.html">QFont</a> &amp;f, QWidget *parent=0, const char *name=0 );
129 ~ShowXForm() {}
130 void showIt(); // (Re)displays text or pixmap
131
132 Mode mode() const { return m; }
133public slots:
134 void setText( const <a href="qstring.html">QString</a>&amp; );
135 void setMatrix( <a href="qwmatrix.html">QWMatrix</a> );
136 void setFont( const <a href="qfont.html">QFont</a> &amp;f );
137 void setPixmap( <a href="qpixmap.html">QPixmap</a> );
138 void setPicture( const <a href="qpicture.html">QPicture</a>&amp; );
139 void setMode( int );
140private:
141 <a href="qsizepolicy.html">QSizePolicy</a> sizePolicy() const;
142 <a href="qsize.html">QSize</a> sizeHint() const;
143 void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
144 void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
145 <a href="qwmatrix.html">QWMatrix</a> mtx; // coordinate transform matrix
146 <a href="qstring.html">QString</a> text; // text to be displayed
147 <a href="qpixmap.html">QPixmap</a> pix; // pixmap to be displayed
148 <a href="qpicture.html">QPicture</a> picture; // text to be displayed
149 <a href="qrect.html">QRect</a> eraseRect; // covers last displayed text/pixmap
150 Mode m;
151};
152
153<a name="f397"></a>XFormControl::XFormControl( const <a href="qfont.html">QFont</a> &amp;initialFont,
154 <a href="qwidget.html">QWidget</a> *parent, const char *name )
155 : <a href="qvbox.html">QVBox</a>( parent, name )
156{
157 <a href="qhbox.html#setSpacing">setSpacing</a>(6);
158 <a href="qframe.html#setMargin">setMargin</a>(6);
159 currentFont = initialFont;
160 mode = Image;
161
162 rotLCD = new <a href="qlcdnumber.html">QLCDNumber</a>( 4, this, "rotateLCD" );
163 rotS = new <a href="qslider.html">QSlider</a>( QSlider::Horizontal, this,
164 "rotateSlider" );
165 shearLCD = new <a href="qlcdnumber.html">QLCDNumber</a>( 5,this, "shearLCD" );
166 shearS = new <a href="qslider.html">QSlider</a>( QSlider::Horizontal, this,
167 "shearSlider" );
168 mirror = new <a href="qcheckbox.html">QCheckBox</a>( this, "mirrorCheckBox" );
169 rb_txt = new <a href="qradiobutton.html">QRadioButton</a>( this, "text" );
170 rb_img = new <a href="qradiobutton.html">QRadioButton</a>( this, "image" );
171 rb_pic = new <a href="qradiobutton.html">QRadioButton</a>( this, "picture" );
172 optionals = new <a href="qwidgetstack.html">QWidgetStack</a>(this);
173 <a href="qvbox.html">QVBox</a>* optionals_text = new <a href="qvbox.html">QVBox</a>(optionals);
174<a name="x1228"></a> optionals_text-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>(6);
175 <a href="qvbox.html">QVBox</a>* optionals_other = new <a href="qvbox.html">QVBox</a>(optionals);
176 optionals_other-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>(6);
177<a name="x1276"></a> optionals-&gt;<a href="qwidgetstack.html#addWidget">addWidget</a>(optionals_text,0);
178 optionals-&gt;<a href="qwidgetstack.html#addWidget">addWidget</a>(optionals_other,1);
179 fpb = new <a href="qpushbutton.html">QPushButton</a>( optionals_text, "text" );
180 textEd = new <a href="qlineedit.html">QLineEdit</a>( optionals_text, "text" );
181 textEd-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
182
183<a name="x1229"></a> rotLCD-&gt;<a href="qlcdnumber.html#display">display</a>( " 0'" );
184
185<a name="x1250"></a> rotS-&gt;<a href="qrangecontrol.html#setRange">setRange</a>( -180, 180 );
186<a name="x1261"></a> rotS-&gt;<a href="qslider.html#setValue">setValue</a>( 0 );
187<a name="x1263"></a> <a href="qobject.html#connect">connect</a>( rotS, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), SLOT(newMtx()) );
188
189 shearLCD-&gt;<a href="qlcdnumber.html#display">display</a>( "0.00" );
190
191 shearS-&gt;<a href="qrangecontrol.html#setRange">setRange</a>( -25, 25 );
192 shearS-&gt;<a href="qslider.html#setValue">setValue</a>( 0 );
193 <a href="qobject.html#connect">connect</a>( shearS, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), SLOT(newMtx()) );
194
195<a name="x1220"></a> mirror-&gt;<a href="qbutton.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>("Mirror") );
196 <a href="qobject.html#connect">connect</a>( mirror, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(newMtx()) );
197
198 <a href="qbuttongroup.html">QButtonGroup</a> *bg = new <a href="qbuttongroup.html">QButtonGroup</a>(this);
199<a name="x1267"></a> bg-&gt;<a href="qwidget.html#hide">hide</a>();
200<a name="x1222"></a> bg-&gt;<a href="qbuttongroup.html#insert">insert</a>(rb_txt,0);
201 bg-&gt;<a href="qbuttongroup.html#insert">insert</a>(rb_img,1);
202 bg-&gt;<a href="qbuttongroup.html#insert">insert</a>(rb_pic,2);
203 rb_txt-&gt;<a href="qbutton.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>("Text") );
204 rb_img-&gt;<a href="qbutton.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>("Image") );
205<a name="x1249"></a> rb_img-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>(TRUE);
206 rb_pic-&gt;<a href="qbutton.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>("Picture") );
207<a name="x1221"></a> <a href="qobject.html#connect">connect</a>( bg, SIGNAL(<a href="qbuttongroup.html#clicked">clicked</a>(int)), SLOT(changeMode(int)) );
208
209 fpb-&gt;<a href="qbutton.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>("Select font...") );
210 <a href="qobject.html#connect">connect</a>( fpb, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(selectFont()) );
211
212<a name="x1230"></a> textEd-&gt;<a href="qlineedit.html#setText">setText</a>( "Troll" );
213<a name="x1231"></a> <a href="qobject.html#connect">connect</a>( textEd, SIGNAL(<a href="qlineedit.html#textChanged">textChanged</a>(const <a href="qstring.html">QString</a>&amp;)),
214 SLOT(newTxt(const <a href="qstring.html">QString</a>&amp;)) );
215
216 magLCD = new <a href="qlcdnumber.html">QLCDNumber</a>( 4,optionals_other, "magLCD" );
217 magLCD-&gt;<a href="qlcdnumber.html#display">display</a>( "100" );
218 magS = new <a href="qslider.html">QSlider</a>( QSlider::Horizontal, optionals_other,
219 "magnifySlider" );
220 magS-&gt;<a href="qrangecontrol.html#setRange">setRange</a>( 0, 800 );
221 <a href="qobject.html#connect">connect</a>( magS, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), SLOT(newMtx()) );
222 magS-&gt;<a href="qslider.html#setValue">setValue</a>( 0 );
223 <a href="qobject.html#connect">connect</a>( magS, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), magLCD, SLOT(<a href="qlcdnumber.html#display">display</a>(int)));
224
225<a name="x1266"></a> optionals_text-&gt;<a href="qwidget.html#adjustSize">adjustSize</a>();
226 optionals_other-&gt;<a href="qwidget.html#adjustSize">adjustSize</a>();
227 changeMode(Image);
228
229 <a href="qobject.html#startTimer">startTimer</a>(20); // start an initial animation
230}
231
232<a name="x1232"></a>void XFormControl::<a href="qobject.html#timerEvent">timerEvent</a>(QTimerEvent*)
233{
234<a name="x1262"></a> int v = magS-&gt;<a href="qslider.html#value">value</a>();
235 v = (v+2)+v/10;
236 if ( v &gt;= 200 ) {
237 v = 200;
238 <a href="qobject.html#killTimers">killTimers</a>();
239 }
240 magS-&gt;<a href="qslider.html#setValue">setValue</a>(v);
241}
242
243
244
245/*
246 Called whenever the user has changed one of the matrix parameters
247 (i.e. rotate, shear or magnification)
248*/
249void <a name="f398"></a>XFormControl::newMtx()
250{
251 emit newMatrix( matrix() );
252}
253
254void <a name="f399"></a>XFormControl::newTxt(const <a href="qstring.html">QString</a>&amp; s)
255{
256 emit newText(s);
257 changeMode(Text);
258}
259
260/*
261 Calculates the matrix appropriate for the current controls,
262 and updates the displays.
263*/
264QWMatrix <a name="f400"></a>XFormControl::matrix()
265{
266 <a href="qwmatrix.html">QWMatrix</a> m;
267 if (mode != Text) {
268 double magVal = 1.0*magS-&gt;<a href="qslider.html#value">value</a>()/100;
269<a name="x1281"></a> m.<a href="qwmatrix.html#scale">scale</a>( magVal, magVal );
270 }
271 double shearVal = 1.0*shearS-&gt;<a href="qslider.html#value">value</a>()/25;
272<a name="x1282"></a> m.<a href="qwmatrix.html#shear">shear</a>( shearVal, shearVal );
273<a name="x1280"></a> m.<a href="qwmatrix.html#rotate">rotate</a>( rotS-&gt;<a href="qslider.html#value">value</a>() );
274<a name="x1223"></a> if ( mirror-&gt;<a href="qcheckbox.html#isChecked">isChecked</a>() ) {
275 m.<a href="qwmatrix.html#scale">scale</a>( 1, -1 );
276 m.<a href="qwmatrix.html#rotate">rotate</a>( 180 );
277 }
278
279 <a href="qstring.html">QString</a> tmp;
280<a name="x1265"></a> tmp.<a href="qstring.html#sprintf">sprintf</a>( "%1.2f", shearVal );
281 if ( shearVal &gt;= 0 )
282<a name="x1264"></a> tmp.<a href="qstring.html#insert">insert</a>( 0, " " );
283 shearLCD-&gt;<a href="qlcdnumber.html#display">display</a>( tmp );
284
285 int rot = rotS-&gt;<a href="qslider.html#value">value</a>();
286 if ( rot &lt; 0 )
287 rot = rot + 360;
288 tmp.<a href="qstring.html#sprintf">sprintf</a>( "%3i'", rot );
289 rotLCD-&gt;<a href="qlcdnumber.html#display">display</a>( tmp );
290 return m;
291}
292
293
294void <a name="f401"></a>XFormControl::selectFont()
295{
296 bool ok;
297<a name="x1224"></a> <a href="qfont.html">QFont</a> f = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &amp;ok, currentFont );
298 if ( ok ) {
299 currentFont = f;
300 fontSelected( f );
301 }
302}
303
304void <a name="f402"></a>XFormControl::fontSelected( const <a href="qfont.html">QFont</a> &amp;font )
305{
306 emit newFont( font );
307 changeMode(Text);
308}
309
310/*
311 Sets the mode - Text, Image, or Picture.
312*/
313
314void <a name="f403"></a>XFormControl::changeMode(int m)
315{
316 mode = (Mode)m;
317
318 emit newMode( m );
319 newMtx();
320 if ( mode == Text ) {
321<a name="x1277"></a> optionals-&gt;<a href="qwidgetstack.html#raiseWidget">raiseWidget</a>(0);
322 rb_txt-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>(TRUE);
323 } else {
324 optionals-&gt;<a href="qwidgetstack.html#raiseWidget">raiseWidget</a>(1);
325 if ( mode == Image )
326 rb_img-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>(TRUE);
327 else
328 rb_pic-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>(TRUE);
329 }
330<a name="x1217"></a> qApp-&gt;<a href="qapplication.html#flushX">flushX</a>();
331}
332
333<a name="f388"></a>ShowXForm::ShowXForm( const <a href="qfont.html">QFont</a> &amp;initialFont,
334 <a href="qwidget.html">QWidget</a> *parent, const char *name )
335 : <a href="qwidget.html">QWidget</a>( parent, name, WResizeNoErase )
336{
337 <a href="qwidget.html#setFont">setFont</a>( initialFont );
338 <a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( white );
339 m = Text;
340 eraseRect = QRect( 0, 0, 0, 0 );
341}
342
343<a name="x1275"></a>QSizePolicy ShowXForm::<a href="qwidget.html#sizePolicy">sizePolicy</a>() const
344{
345 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
346}
347
348<a name="x1274"></a>QSize ShowXForm::<a href="qwidget.html#sizeHint">sizeHint</a>() const
349{
350 return QSize(400,400);
351}
352
353void ShowXForm::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
354{
355 showIt();
356}
357
358<a name="x1269"></a>void ShowXForm::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
359{
360 eraseRect = QRect( <a href="qwidget.html#width">width</a>()/2, height()/2, 0, 0 );
361 <a href="qwidget.html#repaint">repaint</a>(<a href="qwidget.html#rect">rect</a>());
362}
363
364void <a name="f389"></a>ShowXForm::setText( const <a href="qstring.html">QString</a>&amp; s )
365{
366 text = s;
367 showIt();
368}
369
370void <a name="f390"></a>ShowXForm::setMatrix( <a href="qwmatrix.html">QWMatrix</a> w )
371{
372 mtx = w;
373 showIt();
374}
375
376<a name="x1272"></a>void ShowXForm::<a href="qwidget.html#setFont">setFont</a>( const <a href="qfont.html">QFont</a> &amp;f )
377{
378 m = Text;
379 QWidget::<a href="qwidget.html#setFont">setFont</a>( f );
380}
381
382void <a name="f391"></a>ShowXForm::setPixmap( <a href="qpixmap.html">QPixmap</a> pm )
383{
384 pix = pm;
385 m = Image;
386 showIt();
387}
388
389void <a name="f392"></a>ShowXForm::setPicture( const <a href="qpicture.html">QPicture</a>&amp; p )
390{
391 picture = p;
392 m = Picture;
393 showIt();
394}
395
396void <a name="f393"></a>ShowXForm::setMode( int mode )
397{
398 m = (Mode)mode;
399}
400
401void <a name="f394"></a>ShowXForm::showIt()
402{
403 <a href="qpainter.html">QPainter</a> p;
404 <a href="qrect.html">QRect</a> r; // rectangle covering new text/pixmap in virtual coordinates
405 <a href="qwmatrix.html">QWMatrix</a> um; // copy user specified transform
406 int textYPos = 0; // distance from boundingRect y pos to baseline
407 int textXPos = 0; // distance from boundingRect x pos to text start
408 <a href="qrect.html">QRect</a> br;
409 <a href="qfontmetrics.html">QFontMetrics</a> fm( <a href="qwidget.html#fontMetrics">fontMetrics</a>() ); // get widget font metrics
410 switch ( mode() ) {
411 case Text:
412<a name="x1225"></a> br = fm.<a href="qfontmetrics.html#boundingRect">boundingRect</a>( text ); // rectangle covering text
413 r = br;
414 textYPos = -r.<a href="qrect.html#y">y</a>();
415 textXPos = -r.<a href="qrect.html#x">x</a>();
416<a name="x1258"></a><a name="x1254"></a><a name="x1251"></a> br.<a href="qrect.html#moveTopLeft">moveTopLeft</a>( QPoint( -br.<a href="qrect.html#width">width</a>()/2, -br.<a href="qrect.html#height">height</a>()/2 ) );
417 break;
418 case Image:
419<a name="x1248"></a><a name="x1246"></a> r = QRect(0, 0, pix.<a href="qpixmap.html#width">width</a>()+1, pix.<a href="qpixmap.html#height">height</a>()+1);
420 break;
421 case Picture:
422 // ### need QPicture::boundingRect()
423 r = QRect(0,0,1000,1000);
424 break;
425 }
426 r.<a href="qrect.html#moveTopLeft">moveTopLeft</a>( QPoint(-r.<a href="qrect.html#width">width</a>()/2, -r.<a href="qrect.html#height">height</a>()/2) );
427<a name="x1253"></a> r.<a href="qrect.html#moveBy">moveBy</a>( -1, -1 ); // add border for matrix round off
428<a name="x1255"></a> r.<a href="qrect.html#setSize">setSize</a>( QSize( r.<a href="qrect.html#width">width</a>() + 2,r.<a href="qrect.html#height">height</a>() + 2 ) );
429 // compute union of new and old rect
430 // the resulting rectangle will cover what is already displayed
431 // and have room for the new text/pixmap
432<a name="x1279"></a><a name="x1257"></a> eraseRect = eraseRect.<a href="qrect.html#unite">unite</a>( mtx.<a href="qwmatrix.html#mapRect">mapRect</a>(r) );
433 int pw = QMIN(eraseRect.<a href="qrect.html#width">width</a>(),width());
434 int ph = QMIN(eraseRect.<a href="qrect.html#height">height</a>(),height());
435 <a href="qpixmap.html">QPixmap</a> pm( pw, ph ); // off-screen drawing pixmap
436<a name="x1245"></a> pm.<a href="qpixmap.html#fill">fill</a>( <a href="qwidget.html#backgroundColor">backgroundColor</a>() );
437
438<a name="x1233"></a> p.<a href="qpainter.html#begin">begin</a>( &amp;pm );
439<a name="x1283"></a> um.<a href="qwmatrix.html#translate">translate</a>( pw/2, ph/2 ); // 0,0 is center
440 um = mtx * um;
441<a name="x1242"></a> p.<a href="qpainter.html#setWorldMatrix">setWorldMatrix</a>( um );
442 switch ( mode() ) {
443 case Text:
444<a name="x1240"></a> p.<a href="qpainter.html#setFont">setFont</a>( <a href="qwidget.html#font">font</a>() ); // use widget font
445<a name="x1256"></a><a name="x1252"></a> p.<a href="qpainter.html#drawText">drawText</a>( r.<a href="qrect.html#left">left</a>() + textXPos, r.<a href="qrect.html#top">top</a>() + textYPos, text );
446#if 0
447 p.<a href="qpainter.html#setPen">setPen</a>( red );
448 p.<a href="qpainter.html#drawRect">drawRect</a>( br );
449#endif
450 break;
451 case Image:
452<a name="x1235"></a> p.<a href="qpainter.html#drawPixmap">drawPixmap</a>( -pix.<a href="qpixmap.html#width">width</a>()/2, -pix.<a href="qpixmap.html#height">height</a>()/2, pix );
453 break;
454 case Picture:
455 // ### need QPicture::boundingRect()
456<a name="x1239"></a> p.<a href="qpainter.html#scale">scale</a>(0.25,0.25);
457<a name="x1243"></a> p.<a href="qpainter.html#translate">translate</a>(-230,-180);
458<a name="x1234"></a> p.<a href="qpainter.html#drawPicture">drawPicture</a>( picture );
459 }
460<a name="x1238"></a> p.<a href="qpainter.html#end">end</a>();
461
462 int xpos = <a href="qwidget.html#width">width</a>()/2 - pw/2;
463 int ypos = <a href="qwidget.html#height">height</a>()/2 - ph/2;
464 <a href="qimage.html#bitBlt">bitBlt</a>( this, xpos, ypos, // copy pixmap to widget
465 &amp;pm, 0, 0, -1, -1 );
466<a name="x1278"></a> eraseRect = mtx.<a href="qwmatrix.html#map">map</a>( r );
467}
468
469
470/*
471 Grand unifying widget, putting ShowXForm and XFormControl
472 together.
473*/
474
475class XFormCenter : public <a href="qhbox.html">QHBox</a>, public ModeNames
476{
477 Q_OBJECT
478public:
479 XFormCenter( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
480public slots:
481 void setFont( const <a href="qfont.html">QFont</a> &amp;f ) { sx-&gt;<a href="qwidget.html#setFont">setFont</a>( f ); }
482 void newMode( int );
483private:
484 ShowXForm *sx;
485 XFormControl *xc;
486};
487
488void <a name="f395"></a>XFormCenter::newMode( int m )
489{
490 static bool first_i = TRUE;
491 static bool first_p = TRUE;
492
493 if ( sx-&gt;mode() == m )
494 return;
495 if ( m == Image &amp;&amp; first_i ) {
496 first_i = FALSE;
497 <a href="qpixmap.html">QPixmap</a> pm;
498<a name="x1247"></a> if ( pm.<a href="qpixmap.html#load">load</a>( "image.any" ) )
499 sx-&gt;setPixmap( pm );
500 return;
501 }
502 if ( m == Picture &amp;&amp; first_p ) {
503 first_p = FALSE;
504 <a href="qpicture.html">QPicture</a> p;
505<a name="x1244"></a> if (p.<a href="qpicture.html#load">load</a>( "picture.any" ))
506 sx-&gt;setPicture( p );
507 return;
508 }
509 sx-&gt;setMode(m);
510}
511
512<a name="f396"></a>XFormCenter::XFormCenter( <a href="qwidget.html">QWidget</a> *parent, const char *name )
513 : <a href="qhbox.html">QHBox</a>( parent, name )
514{
515 <a href="qfont.html">QFont</a> f( "Charter", 36, QFont::Bold );
516
517 xc = new XFormControl( f, this );
518 sx = new ShowXForm( f, this );
519 <a href="qhbox.html#setStretchFactor">setStretchFactor</a>(sx,1);
520 xc-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Raised );
521<a name="x1227"></a> xc-&gt;<a href="qframe.html#setLineWidth">setLineWidth</a>( 2 );
522 <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newText(const <a href="qstring.html">QString</a>&amp;)), sx,
523 SLOT(setText(const <a href="qstring.html">QString</a>&amp;)) );
524 <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newMatrix(QWMatrix)),
525 sx, SLOT(setMatrix(QWMatrix)) );
526 <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newFont(const <a href="qfont.html">QFont</a>&amp;)), sx,
527 SLOT(<a href="qwidget.html#setFont">setFont</a>(const <a href="qfont.html">QFont</a>&amp;)) );
528 <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newMode(int)), SLOT(newMode(int)) );
529 sx-&gt;setText( "Troll" );
530 newMode( Image );
531 sx-&gt;setMatrix(xc-&gt;matrix());
532}
533
534
535int main( int argc, char **argv )
536{
537 <a href="qapplication.html">QApplication</a> a( argc, argv );
538
539 XFormCenter *xfc = new XFormCenter;
540
541 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( xfc );
542 xfc-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - XForm");
543 xfc-&gt;<a href="qwidget.html#show">show</a>();
544 return a.<a href="qapplication.html#exec">exec</a>();
545}
546
547#include "xform.moc" // include metadata generated by the <a href="moc.html#moc">moc</a>
548</pre>
549
550<p>See also <a href="examples.html">Examples</a>.
551
552<!-- eof -->
553<p><address><hr><div align=center>
554<table width=100% cellspacing=0 border=0><tr>
555<td>Copyright &copy; 2007
556<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
557<td align=right><div align=right>Qt 3.3.8</div>
558</table></div></address></body>
559</html>
Note: See TracBrowser for help on using the repository browser.