1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
45 class Digits: public QWidget
57 Digits(QWidget *parent)
62 setAttribute(Qt::WA_OpaquePaintEvent, true);
63 setAttribute(Qt::WA_NoSystemBackground, true);
64 connect(&m_animator, SIGNAL(frameChanged(int)), SLOT(update()));
65 m_animator.setFrameRange(0, 100);
66 m_animator.setDuration(600);
67 m_animator.setCurveShape(QTimeLine::EaseInOutCurve);
70 void setTransition(int tr) {
74 int transition() const {
78 void setNumber(int n) {
80 m_number = qBound(0, n, 99);
88 m_number = qBound(0, n, 99);
89 m_lastPixmap = m_pixmap;
98 void drawFrame(QPainter *p, const QRect &rect) {
100 QLinearGradient gradient(rect.topLeft(), rect.bottomLeft());
101 gradient.setColorAt(0.00, QColor(245, 245, 245));
102 gradient.setColorAt(0.49, QColor(192, 192, 192));
103 gradient.setColorAt(0.51, QColor(245, 245, 245));
104 gradient.setColorAt(1.00, QColor(192, 192, 192));
105 p->setBrush(gradient);
107 p->drawRoundedRect(r, 15, 15, Qt::RelativeSize);
108 r.adjust(1, 4, -1, -4);
109 p->setPen(QColor(181, 181, 181));
110 p->setBrush(Qt::NoBrush);
111 p->drawRoundedRect(r, 15, 15, Qt::RelativeSize);
112 p->setPen(QColor(159, 159, 159));
113 int y = rect.top() + rect.height() / 2 - 1;
114 p->drawLine(rect.left(), y, rect.right(), y);
117 QPixmap drawDigits(int n, const QRect &rect) {
120 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM)
121 if (rect.height() > 240)
125 QString str = QString::number(n);
126 if (str.length() == 1)
130 font.setFamily("Helvetica");
131 int fontHeight = scaleFactor * 0.55 * rect.height();
132 font.setPixelSize(fontHeight);
135 QPixmap pixmap(rect.size() * scaleFactor);
136 pixmap.fill(Qt::transparent);
138 QLinearGradient gradient(QPoint(0, 0), QPoint(0, pixmap.height()));
139 gradient.setColorAt(0.00, QColor(128, 128, 128));
140 gradient.setColorAt(0.49, QColor(64, 64, 64));
141 gradient.setColorAt(0.51, QColor(128, 128, 128));
142 gradient.setColorAt(1.00, QColor(16, 16, 16));
148 pen.setBrush(QBrush(gradient));
150 p.drawText(pixmap.rect(), Qt::AlignCenter, str);
153 return pixmap.scaledToWidth(width(), Qt::SmoothTransformation);
156 void preparePixmap() {
157 m_pixmap = QPixmap(size());
158 m_pixmap.fill(Qt::transparent);
161 p.drawPixmap(0, 0, drawDigits(m_number, rect()));
165 void resizeEvent(QResizeEvent*) {
172 p.fillRect(rect(), Qt::black);
174 int pad = width() / 10;
175 drawFrame(&p, rect().adjusted(pad, pad, -pad, -pad));
176 p.drawPixmap(0, 0, m_pixmap);
181 p.fillRect(rect(), Qt::black);
183 int pad = width() / 10;
184 QRect fr = rect().adjusted(pad, pad, -pad, -pad);
188 int y = height() * m_animator.currentFrame() / 100;
189 p.drawPixmap(0, y, m_lastPixmap);
190 p.drawPixmap(0, y - height(), m_pixmap);
195 #if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_WINCE_WM)
196 p.setRenderHint(QPainter::SmoothPixmapTransform, true);
197 p.setRenderHint(QPainter::Antialiasing, true);
199 p.fillRect(rect(), Qt::black);
201 int hw = width() / 2;
202 int hh = height() / 2;
204 // behind is the new pixmap
205 int pad = width() / 10;
206 QRect fr = rect().adjusted(pad, pad, -pad, -pad);
208 p.drawPixmap(0, 0, m_pixmap);
210 int index = m_animator.currentFrame();
214 // the top part of the old pixmap is flipping
215 int angle = -180 * index / 100;
216 QTransform transform;
217 transform.translate(hw, hh);
218 transform.rotate(angle, Qt::XAxis);
219 p.setTransform(transform);
220 drawFrame(&p, fr.adjusted(-hw, -hh, -hw, -hh));
221 p.drawPixmap(-hw, -hh, m_lastPixmap);
223 // the bottom part is still the old pixmap
225 p.setClipRect(0, hh, width(), hh);
227 p.drawPixmap(0, 0, m_lastPixmap);
230 p.setClipRect(0, hh, width(), hh);
232 // the bottom part is still the old pixmap
234 p.drawPixmap(0, 0, m_lastPixmap);
236 // the bottom part of the new pixmap is flipping
237 int angle = 180 - 180 * m_animator.currentFrame() / 100;
238 QTransform transform;
239 transform.translate(hw, hh);
240 transform.rotate(angle, Qt::XAxis);
241 p.setTransform(transform);
242 drawFrame(&p, fr.adjusted(-hw, -hh, -hw, -hh));
243 p.drawPixmap(-hw, -hh, m_pixmap);
252 int pad = width() / 10;
253 QRect fr = rect().adjusted(pad, pad, -pad, -pad);
257 int angle1 = -180 * m_animator.currentFrame() / 100;
258 int angle2 = 180 - 180 * m_animator.currentFrame() / 100;
259 int angle = (m_animator.currentFrame() <= 50) ? angle1 : angle2;
260 QPixmap pix = (m_animator.currentFrame() <= 50) ? m_lastPixmap : m_pixmap;
262 QTransform transform;
263 transform.translate(width() / 2, height() / 2);
264 transform.rotate(angle, Qt::XAxis);
266 p.setTransform(transform);
267 p.setRenderHint(QPainter::SmoothPixmapTransform, true);
268 p.drawPixmap(-width() / 2, -height() / 2, pix);
271 void paintEvent(QPaintEvent *event) {
273 if (m_animator.state() == QTimeLine::Running) {
274 if (m_transition == Slide)
276 if (m_transition == Flip)
278 if (m_transition == Rotate)
289 QPixmap m_lastPixmap;
290 QTimeLine m_animator;
293 class DigiFlip : public QMainWindow
298 DigiFlip(QWidget *parent = 0)
299 : QMainWindow(parent)
301 m_hour = new Digits(this);
303 m_minute = new Digits(this);
306 QPalette pal = palette();
307 pal.setColor(QPalette::Window, Qt::black);
310 m_ticker.start(1000, this);
311 QTime t = QTime::currentTime();
312 m_hour->setNumber(t.hour());
313 m_minute->setNumber(t.minute());
316 QAction *slideAction = new QAction("&Slide", this);
317 QAction *flipAction = new QAction("&Flip", this);
318 QAction *rotateAction = new QAction("&Rotate", this);
319 connect(slideAction, SIGNAL(triggered()), SLOT(chooseSlide()));
320 connect(flipAction, SIGNAL(triggered()), SLOT(chooseFlip()));
321 connect(rotateAction, SIGNAL(triggered()), SLOT(chooseRotate()));
322 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM)
323 menuBar()->addAction(slideAction);
324 menuBar()->addAction(flipAction);
325 menuBar()->addAction(rotateAction);
327 addAction(slideAction);
328 addAction(flipAction);
329 addAction(rotateAction);
330 setContextMenuPolicy(Qt::ActionsContextMenu);
335 QTime t = QTime::currentTime();
336 m_hour->flipTo(t.hour());
337 m_minute->flipTo(t.minute());
338 QString str = t.toString("hh:mm:ss");
340 if (m_hour->transition() == Digits::Slide)
341 str.prepend("Slide");
342 if (m_hour->transition() == Digits::Flip)
344 if (m_hour->transition() == Digits::Rotate)
345 str.prepend("Rotate");
349 void switchTransition(int delta) {
350 int i = (m_hour->transition() + delta + 3) % 3;
351 m_hour->setTransition(i);
352 m_minute->setTransition(i);
357 void resizeEvent(QResizeEvent*) {
358 int digitsWidth = width() / 2;
359 int digitsHeight = digitsWidth * 1.2;
361 int y = (height() - digitsHeight) / 3;
363 m_hour->resize(digitsWidth, digitsHeight);
366 m_minute->resize(digitsWidth, digitsHeight);
367 m_minute->move(width() / 2, y);
370 void timerEvent(QTimerEvent*) {
374 void keyPressEvent(QKeyEvent *event) {
375 if (event->key() == Qt::Key_Right) {
379 if (event->key() == Qt::Key_Left) {
380 switchTransition(-1);
387 m_hour->setTransition(0);
388 m_minute->setTransition(0);
393 m_hour->setTransition(1);
394 m_minute->setTransition(1);
398 void chooseRotate() {
399 m_hour->setTransition(2);
400 m_minute->setTransition(2);
405 QBasicTimer m_ticker;
410 #include "digiflip.moc"
412 int main(int argc, char *argv[])
414 QApplication app(argc, argv);
417 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM)
418 time.showMaximized();
420 time.resize(320, 240);