| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "shapedclock.h"
|
|---|
| 45 |
|
|---|
| 46 | //! [0]
|
|---|
| 47 | ShapedClock::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]
|
|---|
| 67 | void 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]
|
|---|
| 77 | void 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]
|
|---|
| 87 | void 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]
|
|---|
| 145 | void 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]
|
|---|
| 155 | QSize ShapedClock::sizeHint() const
|
|---|
| 156 | {
|
|---|
| 157 | return QSize(100, 100);
|
|---|
| 158 | }
|
|---|
| 159 | //! [5]
|
|---|