1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "shapedclock.h" |
52 | |
53 | #include <QAction> |
54 | #include <QCoreApplication> |
55 | #include <QMouseEvent> |
56 | #include <QPainter> |
57 | #include <QTime> |
58 | #include <QTimer> |
59 | |
60 | //! [0] |
61 | ShapedClock::ShapedClock(QWidget *parent) |
62 | : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint) |
63 | { |
64 | setAttribute(Qt::WA_TranslucentBackground); |
65 | QTimer *timer = new QTimer(this); |
66 | connect(sender: timer, signal: &QTimer::timeout, receiver: this, slot: QOverload<>::of(ptr: &ShapedClock::update)); |
67 | timer->start(msec: 1000); |
68 | |
69 | QAction *quitAction = new QAction(tr(s: "E&xit" ), this); |
70 | quitAction->setShortcut(tr(s: "Ctrl+Q" )); |
71 | connect(sender: quitAction, signal: &QAction::triggered, qApp, slot: &QCoreApplication::quit); |
72 | addAction(action: quitAction); |
73 | |
74 | setContextMenuPolicy(Qt::ActionsContextMenu); |
75 | setToolTip(tr(s: "Drag the clock with the left mouse button.\n" |
76 | "Use the right mouse button to open a context menu." )); |
77 | setWindowTitle(tr(s: "Shaped Analog Clock" )); |
78 | } |
79 | //! [0] |
80 | |
81 | //! [1] |
82 | void ShapedClock::mousePressEvent(QMouseEvent *event) |
83 | { |
84 | if (event->button() == Qt::LeftButton) { |
85 | dragPosition = event->globalPos() - frameGeometry().topLeft(); |
86 | event->accept(); |
87 | } |
88 | } |
89 | //! [1] |
90 | |
91 | //! [2] |
92 | void ShapedClock::mouseMoveEvent(QMouseEvent *event) |
93 | { |
94 | if (event->buttons() & Qt::LeftButton) { |
95 | move(event->globalPos() - dragPosition); |
96 | event->accept(); |
97 | } |
98 | } |
99 | //! [2] |
100 | |
101 | //! [3] |
102 | void ShapedClock::paintEvent(QPaintEvent *) |
103 | { |
104 | static const QPoint hourHand[3] = { |
105 | QPoint(7, 8), |
106 | QPoint(-7, 8), |
107 | QPoint(0, -40) |
108 | }; |
109 | static const QPoint minuteHand[3] = { |
110 | QPoint(7, 8), |
111 | QPoint(-7, 8), |
112 | QPoint(0, -70) |
113 | }; |
114 | |
115 | QColor hourColor(127, 0, 127); |
116 | QColor minuteColor(0, 127, 127, 191); |
117 | |
118 | int side = qMin(a: width(), b: height()); |
119 | QTime time = QTime::currentTime(); |
120 | |
121 | QPainter painter(this); |
122 | painter.setRenderHint(hint: QPainter::Antialiasing); |
123 | painter.translate(dx: width() / 2, dy: height() / 2); |
124 | painter.scale(sx: side / 200.0, sy: side / 200.0); |
125 | |
126 | painter.setPen(Qt::NoPen); |
127 | painter.setBrush(palette().window()); |
128 | painter.drawEllipse(center: QPoint(0, 0), rx: 98, ry: 98); |
129 | |
130 | painter.setPen(Qt::NoPen); |
131 | painter.setBrush(hourColor); |
132 | |
133 | painter.save(); |
134 | painter.rotate(a: 30.0 * ((time.hour() + time.minute() / 60.0))); |
135 | painter.drawConvexPolygon(points: hourHand, pointCount: 3); |
136 | painter.restore(); |
137 | |
138 | painter.setPen(hourColor); |
139 | |
140 | for (int i = 0; i < 12; ++i) { |
141 | painter.drawLine(x1: 88, y1: 0, x2: 96, y2: 0); |
142 | painter.rotate(a: 30.0); |
143 | } |
144 | |
145 | painter.setPen(Qt::NoPen); |
146 | painter.setBrush(minuteColor); |
147 | |
148 | painter.save(); |
149 | painter.rotate(a: 6.0 * (time.minute() + time.second() / 60.0)); |
150 | painter.drawConvexPolygon(points: minuteHand, pointCount: 3); |
151 | painter.restore(); |
152 | |
153 | painter.setPen(minuteColor); |
154 | |
155 | for (int j = 0; j < 60; ++j) { |
156 | if ((j % 5) != 0) |
157 | painter.drawLine(x1: 92, y1: 0, x2: 96, y2: 0); |
158 | painter.rotate(a: 6.0); |
159 | } |
160 | } |
161 | //! [3] |
162 | |
163 | //! [4] |
164 | void ShapedClock::resizeEvent(QResizeEvent * /* event */) |
165 | { |
166 | int side = qMin(a: width(), b: height()); |
167 | QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side, |
168 | side, QRegion::Ellipse); |
169 | setMask(maskedRegion); |
170 | } |
171 | //! [4] |
172 | |
173 | //! [5] |
174 | QSize ShapedClock::sizeHint() const |
175 | { |
176 | return QSize(200, 200); |
177 | } |
178 | //! [5] |
179 | |