source: trunk/examples/widgets/shapedclock/shapedclock.cpp@ 168

Last change on this file since 168 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "shapedclock.h"
45
46//! [0]
47ShapedClock::ShapedClock(QWidget *parent)
48 : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint)
49{
50 QTimer *timer = new QTimer(this);
51 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
52 timer->start(1000);
53
54 QAction *quitAction = new QAction(tr("E&xit"), this);
55 quitAction->setShortcut(tr("Ctrl+Q"));
56 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
57 addAction(quitAction);
58
59 setContextMenuPolicy(Qt::ActionsContextMenu);
60 setToolTip(tr("Drag the clock with the left mouse button.\n"
61 "Use the right mouse button to open a context menu."));
62 setWindowTitle(tr("Shaped Analog Clock"));
63}
64//! [0]
65
66//! [1]
67void ShapedClock::mousePressEvent(QMouseEvent *event)
68{
69 if (event->button() == Qt::LeftButton) {
70 dragPosition = event->globalPos() - frameGeometry().topLeft();
71 event->accept();
72 }
73}
74//! [1]
75
76//! [2]
77void ShapedClock::mouseMoveEvent(QMouseEvent *event)
78{
79 if (event->buttons() & Qt::LeftButton) {
80 move(event->globalPos() - dragPosition);
81 event->accept();
82 }
83}
84//! [2]
85
86//! [3]
87void ShapedClock::paintEvent(QPaintEvent *)
88{
89 static const QPoint hourHand[3] = {
90 QPoint(7, 8),
91 QPoint(-7, 8),
92 QPoint(0, -40)
93 };
94 static const QPoint minuteHand[3] = {
95 QPoint(7, 8),
96 QPoint(-7, 8),
97 QPoint(0, -70)
98 };
99
100 QColor hourColor(127, 0, 127);
101 QColor minuteColor(0, 127, 127, 191);
102
103 int side = qMin(width(), height());
104 QTime time = QTime::currentTime();
105
106 QPainter painter(this);
107 painter.setRenderHint(QPainter::Antialiasing);
108 painter.translate(width() / 2, height() / 2);
109 painter.scale(side / 200.0, side / 200.0);
110
111 painter.setPen(Qt::NoPen);
112 painter.setBrush(hourColor);
113
114 painter.save();
115 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
116 painter.drawConvexPolygon(hourHand, 3);
117 painter.restore();
118
119 painter.setPen(hourColor);
120
121 for (int i = 0; i < 12; ++i) {
122 painter.drawLine(88, 0, 96, 0);
123 painter.rotate(30.0);
124 }
125
126 painter.setPen(Qt::NoPen);
127 painter.setBrush(minuteColor);
128
129 painter.save();
130 painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
131 painter.drawConvexPolygon(minuteHand, 3);
132 painter.restore();
133
134 painter.setPen(minuteColor);
135
136 for (int j = 0; j < 60; ++j) {
137 if ((j % 5) != 0)
138 painter.drawLine(92, 0, 96, 0);
139 painter.rotate(6.0);
140 }
141}
142//! [3]
143
144//! [4]
145void ShapedClock::resizeEvent(QResizeEvent * /* event */)
146{
147 int side = qMin(width(), height());
148 QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side,
149 side, QRegion::Ellipse);
150 setMask(maskedRegion);
151}
152//! [4]
153
154//! [5]
155QSize ShapedClock::sizeHint() const
156{
157 return QSize(100, 100);
158}
159//! [5]
Note: See TracBrowser for help on using the repository browser.