| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 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.
|
|---|
| 25 | **
|
|---|
| 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."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include <QtGui>
|
|---|
| 42 |
|
|---|
| 43 | #include "shapedclock.h"
|
|---|
| 44 |
|
|---|
| 45 | //! [0]
|
|---|
| 46 | ShapedClock::ShapedClock(QWidget *parent)
|
|---|
| 47 | : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint)
|
|---|
| 48 | {
|
|---|
| 49 | QTimer *timer = new QTimer(this);
|
|---|
| 50 | connect(timer, SIGNAL(timeout()), this, SLOT(update()));
|
|---|
| 51 | timer->start(1000);
|
|---|
| 52 |
|
|---|
| 53 | QAction *quitAction = new QAction(tr("E&xit"), this);
|
|---|
| 54 | quitAction->setShortcut(tr("Ctrl+Q"));
|
|---|
| 55 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
|---|
| 56 | addAction(quitAction);
|
|---|
| 57 |
|
|---|
| 58 | setContextMenuPolicy(Qt::ActionsContextMenu);
|
|---|
| 59 | setToolTip(tr("Drag the clock with the left mouse button.\n"
|
|---|
| 60 | "Use the right mouse button to open a context menu."));
|
|---|
| 61 | setWindowTitle(tr("Shaped Analog Clock"));
|
|---|
| 62 | }
|
|---|
| 63 | //! [0]
|
|---|
| 64 |
|
|---|
| 65 | //! [1]
|
|---|
| 66 | void ShapedClock::mousePressEvent(QMouseEvent *event)
|
|---|
| 67 | {
|
|---|
| 68 | if (event->button() == Qt::LeftButton) {
|
|---|
| 69 | dragPosition = event->globalPos() - frameGeometry().topLeft();
|
|---|
| 70 | event->accept();
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | //! [1]
|
|---|
| 74 |
|
|---|
| 75 | //! [2]
|
|---|
| 76 | void ShapedClock::mouseMoveEvent(QMouseEvent *event)
|
|---|
| 77 | {
|
|---|
| 78 | if (event->buttons() & Qt::LeftButton) {
|
|---|
| 79 | move(event->globalPos() - dragPosition);
|
|---|
| 80 | event->accept();
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | //! [2]
|
|---|
| 84 |
|
|---|
| 85 | //! [3]
|
|---|
| 86 | void ShapedClock::paintEvent(QPaintEvent *)
|
|---|
| 87 | {
|
|---|
| 88 | static const QPoint hourHand[3] = {
|
|---|
| 89 | QPoint(7, 8),
|
|---|
| 90 | QPoint(-7, 8),
|
|---|
| 91 | QPoint(0, -40)
|
|---|
| 92 | };
|
|---|
| 93 | static const QPoint minuteHand[3] = {
|
|---|
| 94 | QPoint(7, 8),
|
|---|
| 95 | QPoint(-7, 8),
|
|---|
| 96 | QPoint(0, -70)
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | QColor hourColor(127, 0, 127);
|
|---|
| 100 | QColor minuteColor(0, 127, 127, 191);
|
|---|
| 101 |
|
|---|
| 102 | int side = qMin(width(), height());
|
|---|
| 103 | QTime time = QTime::currentTime();
|
|---|
| 104 |
|
|---|
| 105 | QPainter painter(this);
|
|---|
| 106 | painter.setRenderHint(QPainter::Antialiasing);
|
|---|
| 107 | painter.translate(width() / 2, height() / 2);
|
|---|
| 108 | painter.scale(side / 200.0, side / 200.0);
|
|---|
| 109 |
|
|---|
| 110 | painter.setPen(Qt::NoPen);
|
|---|
| 111 | painter.setBrush(hourColor);
|
|---|
| 112 |
|
|---|
| 113 | painter.save();
|
|---|
| 114 | painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
|---|
| 115 | painter.drawConvexPolygon(hourHand, 3);
|
|---|
| 116 | painter.restore();
|
|---|
| 117 |
|
|---|
| 118 | painter.setPen(hourColor);
|
|---|
| 119 |
|
|---|
| 120 | for (int i = 0; i < 12; ++i) {
|
|---|
| 121 | painter.drawLine(88, 0, 96, 0);
|
|---|
| 122 | painter.rotate(30.0);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | painter.setPen(Qt::NoPen);
|
|---|
| 126 | painter.setBrush(minuteColor);
|
|---|
| 127 |
|
|---|
| 128 | painter.save();
|
|---|
| 129 | painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
|
|---|
| 130 | painter.drawConvexPolygon(minuteHand, 3);
|
|---|
| 131 | painter.restore();
|
|---|
| 132 |
|
|---|
| 133 | painter.setPen(minuteColor);
|
|---|
| 134 |
|
|---|
| 135 | for (int j = 0; j < 60; ++j) {
|
|---|
| 136 | if ((j % 5) != 0)
|
|---|
| 137 | painter.drawLine(92, 0, 96, 0);
|
|---|
| 138 | painter.rotate(6.0);
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | //! [3]
|
|---|
| 142 |
|
|---|
| 143 | //! [4]
|
|---|
| 144 | void ShapedClock::resizeEvent(QResizeEvent * /* event */)
|
|---|
| 145 | {
|
|---|
| 146 | int side = qMin(width(), height());
|
|---|
| 147 | QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side,
|
|---|
| 148 | side, QRegion::Ellipse);
|
|---|
| 149 | setMask(maskedRegion);
|
|---|
| 150 | }
|
|---|
| 151 | //! [4]
|
|---|
| 152 |
|
|---|
| 153 | //! [5]
|
|---|
| 154 | QSize ShapedClock::sizeHint() const
|
|---|
| 155 | {
|
|---|
| 156 | return QSize(100, 100);
|
|---|
| 157 | }
|
|---|
| 158 | //! [5]
|
|---|