summaryrefslogtreecommitdiff
path: root/noncore/tools/clock/analogclock.cpp
blob: 16ecc5cc4d1a2b08dbb44c04259d9bf88378c9a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "analogclock.h"

#include <qtopia/global.h>

#include <qlayout.h>
#include <qpainter.h>
#include <qpixmap.h>


#include <math.h>

const double deg2rad = 0.017453292519943295769;	// pi/180

AnalogClock::AnalogClock( QWidget *parent, const char *name )
    : QFrame( parent, name )
{
    setMinimumSize(50,50);

    /* initial the buffer pixmap */
    QRect r = contentsRect();
    _pixmap = new QPixmap( r.width(), r.height() );
}

AnalogClock::~AnalogClock() {
    delete _pixmap;
}

QSizePolicy AnalogClock::sizePolicy() const
{
    return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
}

void AnalogClock::drawContents( QPainter * )
{
    /* no need to draw... */
    if ( !isVisible() )
        return;


    QRect r = contentsRect();

    if ( r.width() < r.height() ) {
	r.setY( (r.height() - r.width())/2 );
	r.setHeight( r.width() );
    }

    /* resize the buffer */
    if ( r.width()  != _pixmap->width() ||
         r.height() != _pixmap->height() )
        _pixmap->resize( r.width(), r.height() );

    QPainter p;
    p.begin( _pixmap );


    QPoint center( r.x() + r.width() / 2, r.y() + r.height() / 2 );

    const int w_tick = r.width()/300+1;
    const int w_sec = r.width()/400+1;
    const int w_hour = r.width()/80+1;

    QPoint l1( r.x() + r.width() / 2, r.y() + 2 );
    QPoint l2( r.x() + r.width() / 2, r.y() + 8 );

    QPoint h1( r.x() + r.width() / 2, r.y() + r.height() / 4 );
    QPoint h2( r.x() + r.width() / 2, r.y() + r.height() / 2 );

    QPoint m1( r.x() + r.width() / 2, r.y() + r.height() / 9 );
    QPoint m2( r.x() + r.width() / 2, r.y() + r.height() / 2 );

    QPoint s1( r.x() + r.width() / 2, r.y() + 8 );
    QPoint s2( r.x() + r.width() / 2, r.y() + r.height() / 2 );

    /* fill the pixmap */
    _pixmap->fill( this, 0, 0 );


    // draw ticks
    QColor color = black;
    p.setPen( QPen( color, w_tick ) );
    for ( int i = 0; i < 12; i++ )
        p.drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) );


    // draw hour pointer
    h1 = rotate( center, h1, 30 * ( currTime.hour() % 12 ) + currTime.minute() / 2 );
    h2 = rotate( center, h2, 30 * ( currTime.hour() % 12 ) + currTime.minute() / 2 );
    p.setPen( color );
    p.setBrush( color  );
    drawHand( &p, h1, h2 );


    // draw minute pointer
    m1 = rotate( center, m1, currTime.minute() * 6 );
    m2 = rotate( center, m2, currTime.minute() * 6 );
    p.setPen( color );
    p.setBrush( color );
    drawHand( &p, m1, m2 );

    // draw second pointer
    s1 = rotate( center, s1, currTime.second() * 6 );
    s2 = rotate( center, s2, currTime.second() * 6 );
    p.setPen( QPen( color, w_sec ) );
    p.drawLine( s1, s2 );

    // cap
    p.setBrush(color);
    p.drawEllipse( center.x()-w_hour/2, center.y()-w_hour/2, w_hour, w_hour );

    prevTime = currTime;

    p.end();

    p.begin( this );
    p.drawPixmap( 0, 0, *_pixmap );

    /* leave */
}

// Dijkstra's bisection algorithm to find the square root as an integer.

static uint int_sqrt(uint n)
{
    if ( n >= UINT_MAX>>2 ) // n must be in the range 0...UINT_MAX/2-1
	return 2*int_sqrt( n/4 );
    uint h, p= 0, q= 1, r= n;
    while ( q <= n )
	q <<= 2;
    while ( q != 1 ) {
	q >>= 2;
	h= p + q;
	p >>= 1;
	if ( r >= h ) {
	    p += q;
	    r -= h;
	}
    }
    return p;
}

void AnalogClock::drawHand( QPainter *p, QPoint p1, QPoint p2 )
{
    int hw = 7;
    if ( contentsRect().height() < 100 )
	hw = 5;

    int dx = p2.x() - p1.x();
    int dy = p2.y() - p1.y();
    int w = dx*dx+dy*dy;
    int ix,iy;
    w = int_sqrt(w*256);
    iy = w ? (hw * dy * 16)/ w : dy ? 0 : hw;
    ix = w ? (hw * dx * 16)/ w : dx ? 0 : hw;

    // rounding dependent on sign
    int nix, niy;
    if ( ix < 0 ) {
	nix = ix/2;
	ix = (ix-1)/2;
    } else {
	nix = (ix+1)/2;
	ix = ix/2;
    }
    if ( iy < 0 ) {
	niy = iy/2;
	iy = (iy-1)/2;
    } else {
	niy = (iy+1)/2;
	iy = iy/2;
    }

    QPointArray pa(4);
    pa[0] = p1;
    pa[1] = QPoint( p2.x()+iy, p2.y()-nix );
    pa[2] = QPoint( p2.x()-niy, p2.y()+ix );
    pa[3] = p1;

    p->drawPolygon( pa );
}

void AnalogClock::display( const QTime& t )
{
    currTime = t;
    repaint( false );
}

QPoint AnalogClock::rotate( QPoint c, QPoint p, int a )
{
    double angle = deg2rad * ( - a + 180 );
    double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) -
		( p.y() - c.y() ) * sin( angle );
    double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) +
		( p.x() - c.x() ) * sin( angle );
    return QPoint( int(nx), int(ny) );
}