1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the example classes of the Qt Toolkit.
|
|
7 **
|
|
8 ** Commercial Usage
|
|
9 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
10 ** accordance with the Qt Commercial License Agreement provided with the
|
|
11 ** Software or, alternatively, in accordance with the terms contained in
|
|
12 ** a written agreement between you and Nokia.
|
|
13 **
|
|
14 **
|
|
15 ** GNU General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU
|
|
17 ** General Public License versions 2.0 or 3.0 as published by the Free
|
|
18 ** Software Foundation and appearing in the file LICENSE.GPL included in
|
|
19 ** the packaging of this file. Please review the following information
|
|
20 ** to ensure GNU General Public Licensing requirements will be met:
|
|
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
|
23 ** exception, Nokia gives you certain additional rights. These rights
|
|
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in
|
|
25 ** the file GPL_EXCEPTION.txt in this package.
|
|
26 **
|
|
27 ** Qt for Windows(R) Licensees
|
|
28 ** As a special exception, Nokia, as the sole copyright holder for Qt
|
|
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
|
30 ** right for the Qt/Eclipse Integration to link to functionality
|
|
31 ** provided by Qt Designer and its related libraries.
|
|
32 **
|
|
33 ** If you are unsure which license is appropriate for your use, please
|
|
34 ** contact the sales department at qt-sales@nokia.com.
|
|
35 **
|
|
36 ****************************************************************************/
|
|
37
|
|
38 module AnalogClock;
|
|
39
|
|
40 import qt.core.QPoint;
|
|
41 import qt.core.QTime;
|
|
42 import qt.core.QTimer;
|
|
43
|
|
44 import qt.gui.QWidget;
|
|
45 import qt.gui.QPolygon;
|
|
46 import qt.gui.QColor;
|
|
47 import qt.gui.QPaintEvent;
|
|
48 import qt.gui.QPainter;
|
|
49
|
|
50
|
|
51 class AnalogClock : public QWidget
|
|
52 {
|
|
53 public:
|
|
54 this(QWidget parent = null)
|
|
55 {
|
|
56 super(parent);
|
|
57 auto timer = new QTimer(this);
|
|
58 timer.timeout.connect(&this.update);
|
|
59 timer.start(1000);
|
|
60 setWindowTitle("Analog Clock");
|
|
61 resize(200, 200);
|
|
62 }
|
|
63
|
|
64 void paintEvent(QPaintEvent event)
|
|
65 {
|
192
|
66 const QPoint[3] hourHand = [
|
1
|
67 QPoint(7, 8),
|
|
68 QPoint(-7, 8),
|
|
69 QPoint(0, -40)
|
5
|
70 ];
|
192
|
71 const QPoint[3] minuteHand = [
|
1
|
72 QPoint(7, 8),
|
|
73 QPoint(-7, 8),
|
|
74 QPoint(0, -70)
|
5
|
75 ];
|
1
|
76
|
|
77 scope hourColor = new QColor(127, 0, 127);
|
|
78 scope minuteColor = new QColor(0, 127, 127, 191);
|
|
79
|
|
80 int side = qMin(width(), height());
|
|
81 QTime time = QTime.currentTime();
|
|
82
|
|
83 // !! it is important that QPainter object should be scoped
|
|
84 // !! in order to be destroyed after painting
|
|
85 scope painter = new QPainter(this);
|
|
86
|
|
87 painter.setRenderHint(QPainter.Antialiasing);
|
|
88 painter.translate(width() / 2, height() / 2);
|
|
89 painter.scale(side / 200.0, side / 200.0);
|
|
90
|
|
91 painter.setPen(Qt.NoPen);
|
|
92 painter.setBrush(hourColor);
|
|
93
|
|
94 painter.save();
|
|
95 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
|
96 painter.drawConvexPolygon(hourHand);
|
|
97 painter.restore();
|
|
98
|
|
99 painter.setPen(hourColor);
|
|
100
|
|
101 for (int i = 0; i < 12; ++i) {
|
|
102 painter.drawLine(88, 0, 96, 0);
|
|
103 painter.rotate(30.0);
|
|
104 }
|
|
105
|
|
106 painter.setPen(Qt.NoPen);
|
|
107 painter.setBrush(minuteColor);
|
|
108
|
|
109 painter.save();
|
|
110 painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
|
|
111 painter.drawConvexPolygon(minuteHand);
|
|
112 painter.restore();
|
|
113
|
|
114 painter.setPen(minuteColor);
|
|
115
|
|
116 for (int j = 0; j < 60; ++j) {
|
|
117 if ((j % 5) != 0)
|
|
118 painter.drawLine(92, 0, 96, 0);
|
|
119 painter.rotate(6.0);
|
|
120 }
|
|
121 }
|
|
122 } |