1 | /****************************************************************************
|
---|
2 | ** $Id: hello.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 "hello.h"
|
---|
12 | #include <qpushbutton.h>
|
---|
13 | #include <qtimer.h>
|
---|
14 | #include <qpainter.h>
|
---|
15 | #include <qpixmap.h>
|
---|
16 |
|
---|
17 |
|
---|
18 | /*
|
---|
19 | Constructs a Hello widget. Starts a 40 ms animation timer.
|
---|
20 | */
|
---|
21 |
|
---|
22 | Hello::Hello( const char *text, QWidget *parent, const char *name )
|
---|
23 | : QWidget(parent,name), t(text), b(0)
|
---|
24 | {
|
---|
25 | QTimer *timer = new QTimer(this);
|
---|
26 | connect( timer, SIGNAL(timeout()), SLOT(animate()) );
|
---|
27 | timer->start( 40 );
|
---|
28 |
|
---|
29 | resize( 260, 130 );
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | /*
|
---|
34 | This private slot is called each time the timer fires.
|
---|
35 | */
|
---|
36 |
|
---|
37 | void Hello::animate()
|
---|
38 | {
|
---|
39 | b = (b + 1) & 15;
|
---|
40 | repaint( FALSE );
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | /*
|
---|
45 | Handles mouse button release events for the Hello widget.
|
---|
46 |
|
---|
47 | We emit the clicked() signal when the mouse is released inside
|
---|
48 | the widget.
|
---|
49 | */
|
---|
50 |
|
---|
51 | void Hello::mouseReleaseEvent( QMouseEvent *e )
|
---|
52 | {
|
---|
53 | if ( rect().contains( e->pos() ) )
|
---|
54 | emit clicked();
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /*
|
---|
59 | Handles paint events for the Hello widget.
|
---|
60 |
|
---|
61 | Flicker-free update. The text is first drawn in the pixmap and the
|
---|
62 | pixmap is then blt'ed to the screen.
|
---|
63 | */
|
---|
64 |
|
---|
65 | void Hello::paintEvent( QPaintEvent * )
|
---|
66 | {
|
---|
67 | static int sin_tbl[16] = {
|
---|
68 | 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38};
|
---|
69 |
|
---|
70 | if ( t.isEmpty() )
|
---|
71 | return;
|
---|
72 |
|
---|
73 | // 1: Compute some sizes, positions etc.
|
---|
74 | QFontMetrics fm = fontMetrics();
|
---|
75 | int w = fm.width(t) + 20;
|
---|
76 | int h = fm.height() * 2;
|
---|
77 | int pmx = width()/2 - w/2;
|
---|
78 | int pmy = height()/2 - h/2;
|
---|
79 |
|
---|
80 | // 2: Create the pixmap and fill it with the widget's background
|
---|
81 | QPixmap pm( w, h );
|
---|
82 | pm.fill( this, pmx, pmy );
|
---|
83 |
|
---|
84 | // 3: Paint the pixmap. Cool wave effect
|
---|
85 | QPainter p;
|
---|
86 | int x = 10;
|
---|
87 | int y = h/2 + fm.descent();
|
---|
88 | int i = 0;
|
---|
89 | p.begin( &pm );
|
---|
90 | p.setFont( font() );
|
---|
91 | while ( !t[i].isNull() ) {
|
---|
92 | int i16 = (b+i) & 15;
|
---|
93 | p.setPen( QColor((15-i16)*16,255,255,QColor::Hsv) );
|
---|
94 | p.drawText( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 );
|
---|
95 | x += fm.width( t[i] );
|
---|
96 | i++;
|
---|
97 | }
|
---|
98 | p.end();
|
---|
99 |
|
---|
100 | // 4: Copy the pixmap to the Hello widget
|
---|
101 | bitBlt( this, pmx, pmy, &pm );
|
---|
102 | }
|
---|