📄 xform-example.html
字号:
shearLCD-><a href="qlcdnumber.html#display">display</a>( tmp ); int rot = rotS-><a href="qslider.html#value">value</a>(); if ( rot < 0 ) rot = rot + 360; tmp.<a href="qstring.html#sprintf">sprintf</a>( "%3i'", rot ); rotLCD-><a href="qlcdnumber.html#display">display</a>( tmp ); return m;}void <a name="f444"></a>XFormControl::selectFont(){ bool ok;<a name="x1423"></a> <a href="qfont.html">QFont</a> f = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &ok, currentFont ); if ( ok ) { currentFont = f; fontSelected( f ); }}void <a name="f445"></a>XFormControl::fontSelected( const <a href="qfont.html">QFont</a> &font ){ emit newFont( font ); changeMode(Text);}/* Sets the mode - Text, Image, or Picture.*/void <a name="f446"></a>XFormControl::changeMode(int m){ mode = (Mode)m; emit newMode( m ); newMtx(); if ( mode == Text ) {<a name="x1478"></a> optionals-><a href="qwidgetstack.html#raiseWidget">raiseWidget</a>(0); rb_txt-><a href="qradiobutton.html#setChecked">setChecked</a>(TRUE); } else { optionals-><a href="qwidgetstack.html#raiseWidget">raiseWidget</a>(1); if ( mode == Image ) rb_img-><a href="qradiobutton.html#setChecked">setChecked</a>(TRUE); else rb_pic-><a href="qradiobutton.html#setChecked">setChecked</a>(TRUE); }<a name="x1416"></a> qApp-><a href="qapplication.html#flushX">flushX</a>();}<a name="f431"></a>ShowXForm::ShowXForm( const <a href="qfont.html">QFont</a> &initialFont, <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qwidget.html">QWidget</a>( parent, name, WResizeNoErase ){ <a href="qwidget.html#setFont">setFont</a>( initialFont ); <a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( white ); m = Text; eraseRect = QRect( 0, 0, 0, 0 );}<a name="x1476"></a>QSizePolicy ShowXForm::<a href="qwidget.html#sizePolicy">sizePolicy</a>() const{ return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );}<a name="x1475"></a>QSize ShowXForm::<a href="qwidget.html#sizeHint">sizeHint</a>() const{ return QSize(400,400);}void ShowXForm::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ){ showIt();}void ShowXForm::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * ){ eraseRect = QRect( <a href="qwidget.html#width">width</a>()/2, height()/2, 0, 0 ); <a href="qwidget.html#repaint">repaint</a>(<a href="qwidget.html#rect">rect</a>());}void <a name="f432"></a>ShowXForm::setText( const <a href="qstring.html">QString</a>& s ){ text = s; showIt();}void <a name="f433"></a>ShowXForm::setMatrix( <a href="qwmatrix.html">QWMatrix</a> w ){ mtx = w; showIt();}<a name="x1473"></a>void ShowXForm::<a href="qwidget.html#setFont">setFont</a>( const <a href="qfont.html">QFont</a> &f ){ m = Text; QWidget::<a href="qwidget.html#setFont">setFont</a>( f );}void <a name="f434"></a>ShowXForm::setPixmap( <a href="qpixmap.html">QPixmap</a> pm ){ pix = pm; m = Image; showIt();}void <a name="f435"></a>ShowXForm::setPicture( const <a href="qpicture.html">QPicture</a>& p ){ picture = p; m = Picture; showIt();}void <a name="f436"></a>ShowXForm::setMode( int mode ){ m = (Mode)mode;}void <a name="f437"></a>ShowXForm::showIt(){ <a href="qpainter.html">QPainter</a> p; <a href="qrect.html">QRect</a> r; // rectangle covering new text/pixmap in virtual coordinates <a href="qwmatrix.html">QWMatrix</a> um; // copy user specified transform int textYPos = 0; // distance from boundingRect y pos to baseline int textXPos = 0; // distance from boundingRect x pos to text start <a href="qrect.html">QRect</a> br; <a href="qfontmetrics.html">QFontMetrics</a> fm( <a href="qwidget.html#fontMetrics">fontMetrics</a>() ); // get widget font metrics switch ( mode() ) { case Text:<a name="x1424"></a> br = fm.<a href="qfontmetrics.html#boundingRect">boundingRect</a>( text ); // rectangle covering text r = br; textYPos = -r.<a href="qrect.html#y">y</a>(); textXPos = -r.<a href="qrect.html#x">x</a>();<a name="x1459"></a><a name="x1455"></a><a name="x1452"></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 ) ); break; case Image:<a name="x1447"></a> r = pix.<a href="qpixmap.html#rect">rect</a>(); break; case Picture: // ### need QPicture::boundingRect() r = QRect(0,0,1000,1000); break; } 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) ); // compute union of new and old rect // the resulting rectangle will cover what is already displayed // and have room for the new text/pixmap<a name="x1479"></a><a name="x1458"></a> eraseRect = eraseRect.<a href="qrect.html#unite">unite</a>( mtx.<a href="qwmatrix.html#map">map</a>(r) );<a name="x1454"></a> eraseRect.<a href="qrect.html#moveBy">moveBy</a>( -1, -1 ); // add border for matrix round off<a name="x1456"></a> eraseRect.<a href="qrect.html#setSize">setSize</a>( QSize( eraseRect.<a href="qrect.html#width">width</a>() + 2,eraseRect.<a href="qrect.html#height">height</a>() + 2 ) ); int pw = QMIN(eraseRect.<a href="qrect.html#width">width</a>(),width()); int ph = QMIN(eraseRect.<a href="qrect.html#height">height</a>(),height()); <a href="qpixmap.html">QPixmap</a> pm( pw, ph ); // off-screen drawing pixmap<a name="x1444"></a> pm.<a href="qpixmap.html#fill">fill</a>( <a href="qwidget.html#backgroundColor">backgroundColor</a>() );<a name="x1432"></a> p.<a href="qpainter.html#begin">begin</a>( &pm );<a name="x1483"></a> um.<a href="qwmatrix.html#translate">translate</a>( pw/2, ph/2 ); // 0,0 is center um = mtx * um;<a name="x1441"></a> p.<a href="qpainter.html#setWorldMatrix">setWorldMatrix</a>( um ); switch ( mode() ) { case Text:<a name="x1439"></a> p.<a href="qpainter.html#setFont">setFont</a>( <a href="qwidget.html#font">font</a>() ); // use widget font<a name="x1457"></a><a name="x1453"></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 );#if 0 p.<a href="qpainter.html#setPen">setPen</a>( red ); p.<a href="qpainter.html#drawRect">drawRect</a>( br );#endif break; case Image:<a name="x1448"></a><a name="x1445"></a><a name="x1434"></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 );<a name="x1449"></a> //QPixmap rotated = pix.<a href="qpixmap.html#xForm">xForm</a>(mtx); //bitBlt( &pm, pm.<a href="qpixmap.html#width">width</a>()/2 - rotated.width()/2, //pm.<a href="qpixmap.html#height">height</a>()/2 - rotated.height()/2, &rotated ); break; case Picture: // ### need QPicture::boundingRect()<a name="x1438"></a> p.<a href="qpainter.html#scale">scale</a>(0.25,0.25);<a name="x1442"></a> p.<a href="qpainter.html#translate">translate</a>(-230,-180);<a name="x1433"></a> p.<a href="qpainter.html#drawPicture">drawPicture</a>( picture ); }<a name="x1437"></a> p.<a href="qpainter.html#end">end</a>(); int xpos = <a href="qwidget.html#width">width</a>()/2 - pw/2; int ypos = <a href="qwidget.html#height">height</a>()/2 - ph/2; <a href="qpaintdevice.html#bitBlt-2">bitBlt</a>( this, xpos, ypos, // copy pixmap to widget &pm, 0, 0, -1, -1 ); eraseRect = mtx.<a href="qwmatrix.html#map">map</a>( r );}/* Grand unifying widget, putting ShowXForm and XFormControl together.*/class XFormCenter : public <a href="qhbox.html">QHBox</a>, public ModeNames{ Q_OBJECTpublic: XFormCenter( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );public slots: void setFont( const <a href="qfont.html">QFont</a> &f ) { sx-><a href="qwidget.html#setFont">setFont</a>( f ); } void newMode( int );private: ShowXForm *sx; XFormControl *xc;};void <a name="f438"></a>XFormCenter::newMode( int m ){ static bool first_i = TRUE; static bool first_p = TRUE; if ( sx->mode() == m ) return; if ( m == Image && first_i ) { first_i = FALSE; <a href="qpixmap.html">QPixmap</a> pm;<a name="x1446"></a> if ( pm.<a href="qpixmap.html#load">load</a>( "image.any" ) ) sx->setPixmap( pm ); return; } if ( m == Picture && first_p ) { first_p = FALSE; <a href="qpicture.html">QPicture</a> p;<a name="x1443"></a> if (p.<a href="qpicture.html#load">load</a>( "picture.any" )) sx->setPicture( p ); return; } sx->setMode(m);}<a name="f439"></a>XFormCenter::XFormCenter( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qhbox.html">QHBox</a>( parent, name ){ <a href="qfont.html">QFont</a> f( "Charter", 36, QFont::Bold ); xc = new XFormControl( f, this ); sx = new ShowXForm( f, this ); <a href="qhbox.html#setStretchFactor">setStretchFactor</a>(sx,1); xc-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Raised );<a name="x1426"></a> xc-><a href="qframe.html#setLineWidth">setLineWidth</a>( 2 ); <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newText(const <a href="qstring.html">QString</a>&)), sx, SLOT(setText(const <a href="qstring.html">QString</a>&)) ); <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newMatrix(QWMatrix)), sx, SLOT(setMatrix(QWMatrix)) ); <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newFont(const <a href="qfont.html">QFont</a>&)), sx, SLOT(<a href="qwidget.html#setFont">setFont</a>(const <a href="qfont.html">QFont</a>&)) ); <a href="qobject.html#connect">connect</a>( xc, SIGNAL(newMode(int)), SLOT(newMode(int)) ); sx->setText( "Troll" ); newMode( Image ); sx->setMatrix(xc->matrix());}int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); XFormCenter *xfc = new XFormCenter; a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( xfc ); xfc-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - XForm"); xfc-><a href="qwidget.html#show">show</a>(); return a.<a href="qapplication.html#exec">exec</a>();}#include "xform.moc" // include metadata generated by the <a href="moc.html#moc">moc</a></pre><p>See also <a href="examples.html">Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -