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 import tango.io.Stdout;
|
|
51
|
|
52 class AnalogClock : public QWidget
|
|
53 {
|
|
54 public:
|
|
55 this(QWidget parent = null)
|
|
56 {
|
|
57 super(parent);
|
|
58 auto timer = new QTimer(this);
|
|
59 timer.timeout.connect(&this.update);
|
|
60 timer.start(1000);
|
|
61 setWindowTitle("Analog Clock");
|
|
62 resize(200, 200);
|
|
63 }
|
|
64
|
|
65 void paintEvent(QPaintEvent event)
|
|
66 {
|
4
|
67 /* static const QPoint hourHand[3] = {
|
1
|
68 QPoint(7, 8),
|
|
69 QPoint(-7, 8),
|
|
70 QPoint(0, -40)
|
4
|
71 };
|
|
72 static const QPoint minuteHand[3] = {
|
1
|
73 QPoint(7, 8),
|
|
74 QPoint(-7, 8),
|
|
75 QPoint(0, -70)
|
4
|
76 };
|
|
77 */
|
|
78 scope hourHand = new QPolygon;
|
|
79 hourHand.append(QPoint(7, 8));
|
|
80 hourHand.append(QPoint(-7, 8));
|
|
81 hourHand.append(QPoint(0, -40));
|
|
82
|
|
83 scope minuteHand = new QPolygon;
|
|
84 minuteHand.append(QPoint(7, 8));
|
|
85 minuteHand.append(QPoint(-7, 8));
|
|
86 minuteHand.append(QPoint(0, -70));
|
1
|
87
|
|
88 scope hourColor = new QColor(127, 0, 127);
|
|
89 scope minuteColor = new QColor(0, 127, 127, 191);
|
|
90
|
|
91 int side = qMin(width(), height());
|
|
92 QTime time = QTime.currentTime();
|
|
93
|
|
94 // !! it is important that QPainter object should be scoped
|
|
95 // !! in order to be destroyed after painting
|
|
96 scope painter = new QPainter(this);
|
|
97
|
|
98 painter.setRenderHint(QPainter.Antialiasing);
|
|
99 painter.translate(width() / 2, height() / 2);
|
|
100 painter.scale(side / 200.0, side / 200.0);
|
|
101
|
|
102 painter.setPen(Qt.NoPen);
|
|
103 painter.setBrush(hourColor);
|
|
104
|
|
105 painter.save();
|
|
106 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
|
107 painter.drawConvexPolygon(hourHand);
|
|
108 painter.restore();
|
|
109
|
|
110 painter.setPen(hourColor);
|
|
111
|
|
112 for (int i = 0; i < 12; ++i) {
|
|
113 painter.drawLine(88, 0, 96, 0);
|
|
114 painter.rotate(30.0);
|
|
115 }
|
|
116
|
|
117 painter.setPen(Qt.NoPen);
|
|
118 painter.setBrush(minuteColor);
|
|
119
|
|
120 painter.save();
|
|
121 painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
|
|
122 painter.drawConvexPolygon(minuteHand);
|
|
123 painter.restore();
|
|
124
|
|
125 painter.setPen(minuteColor);
|
|
126
|
|
127 for (int j = 0; j < 60; ++j) {
|
|
128 if ((j % 5) != 0)
|
|
129 painter.drawLine(92, 0, 96, 0);
|
|
130 painter.rotate(6.0);
|
|
131 }
|
|
132 }
|
|
133 } |