[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "analogclock.h"
|
---|
| 44 |
|
---|
| 45 | //! [0] //! [1]
|
---|
| 46 | AnalogClock::AnalogClock(QWidget *parent)
|
---|
| 47 | //! [0] //! [2]
|
---|
| 48 | : QWidget(parent)
|
---|
| 49 | //! [2] //! [3]
|
---|
| 50 | {
|
---|
| 51 | //! [3] //! [4]
|
---|
| 52 | QTimer *timer = new QTimer(this);
|
---|
| 53 | //! [4] //! [5]
|
---|
| 54 | connect(timer, SIGNAL(timeout()), this, SLOT(update()));
|
---|
| 55 | //! [5] //! [6]
|
---|
| 56 | timer->start(1000);
|
---|
| 57 | //! [6]
|
---|
| 58 |
|
---|
| 59 | setWindowTitle(tr("Analog Clock"));
|
---|
| 60 | resize(200, 200);
|
---|
| 61 | //! [7]
|
---|
| 62 | }
|
---|
| 63 | //! [1] //! [7]
|
---|
| 64 |
|
---|
| 65 | //! [8] //! [9]
|
---|
| 66 | void AnalogClock::paintEvent(QPaintEvent *)
|
---|
| 67 | //! [8] //! [10]
|
---|
| 68 | {
|
---|
| 69 | static const QPoint hourHand[3] = {
|
---|
| 70 | QPoint(7, 8),
|
---|
| 71 | QPoint(-7, 8),
|
---|
| 72 | QPoint(0, -40)
|
---|
| 73 | };
|
---|
| 74 | static const QPoint minuteHand[3] = {
|
---|
| 75 | QPoint(7, 8),
|
---|
| 76 | QPoint(-7, 8),
|
---|
| 77 | QPoint(0, -70)
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | QColor hourColor(127, 0, 127);
|
---|
| 81 | QColor minuteColor(0, 127, 127, 191);
|
---|
| 82 |
|
---|
| 83 | int side = qMin(width(), height());
|
---|
| 84 | QTime time = QTime::currentTime();
|
---|
| 85 | //! [10]
|
---|
| 86 |
|
---|
| 87 | //! [11]
|
---|
| 88 | QPainter painter(this);
|
---|
| 89 | //! [11] //! [12]
|
---|
| 90 | painter.setRenderHint(QPainter::Antialiasing);
|
---|
| 91 | //! [12] //! [13]
|
---|
| 92 | painter.translate(width() / 2, height() / 2);
|
---|
| 93 | //! [13] //! [14]
|
---|
| 94 | painter.scale(side / 200.0, side / 200.0);
|
---|
| 95 | //! [9] //! [14]
|
---|
| 96 |
|
---|
| 97 | //! [15]
|
---|
| 98 | painter.setPen(Qt::NoPen);
|
---|
| 99 | //! [15] //! [16]
|
---|
| 100 | painter.setBrush(hourColor);
|
---|
| 101 | //! [16]
|
---|
| 102 |
|
---|
| 103 | //! [17] //! [18]
|
---|
| 104 | painter.save();
|
---|
| 105 | //! [17] //! [19]
|
---|
| 106 | painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
---|
| 107 | painter.drawConvexPolygon(hourHand, 3);
|
---|
| 108 | painter.restore();
|
---|
| 109 | //! [18] //! [19]
|
---|
| 110 |
|
---|
| 111 | //! [20]
|
---|
| 112 | painter.setPen(hourColor);
|
---|
| 113 | //! [20] //! [21]
|
---|
| 114 |
|
---|
| 115 | for (int i = 0; i < 12; ++i) {
|
---|
| 116 | painter.drawLine(88, 0, 96, 0);
|
---|
| 117 | painter.rotate(30.0);
|
---|
| 118 | }
|
---|
| 119 | //! [21]
|
---|
| 120 |
|
---|
| 121 | //! [22]
|
---|
| 122 | painter.setPen(Qt::NoPen);
|
---|
| 123 | //! [22] //! [23]
|
---|
| 124 | painter.setBrush(minuteColor);
|
---|
| 125 |
|
---|
| 126 | //! [24]
|
---|
| 127 | painter.save();
|
---|
| 128 | painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
|
---|
| 129 | painter.drawConvexPolygon(minuteHand, 3);
|
---|
| 130 | painter.restore();
|
---|
| 131 | //! [23] //! [24]
|
---|
| 132 |
|
---|
| 133 | //! [25]
|
---|
| 134 | painter.setPen(minuteColor);
|
---|
| 135 | //! [25] //! [26]
|
---|
| 136 |
|
---|
| 137 | //! [27]
|
---|
| 138 | for (int j = 0; j < 60; ++j) {
|
---|
| 139 | if ((j % 5) != 0)
|
---|
| 140 | painter.drawLine(92, 0, 96, 0);
|
---|
| 141 | painter.rotate(6.0);
|
---|
| 142 | }
|
---|
| 143 | //! [27]
|
---|
| 144 | }
|
---|
| 145 | //! [26]
|
---|