tests/qtp/qtp_analogclock/analogclock.cpp
changeset 37 758a864f9613
parent 36 ef0373b55136
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
     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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "analogclock.h"
       
    45 
       
    46 //! [0] //! [1]
       
    47 AnalogClock::AnalogClock(QWidget *parent)
       
    48 //! [0] //! [2]
       
    49     : QWidget(parent)
       
    50 //! [2] //! [3]
       
    51 {
       
    52 //! [3] //! [4]
       
    53     QTimer *timer = new QTimer(this);
       
    54 //! [4] //! [5]
       
    55     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
       
    56 //! [5] //! [6]
       
    57     timer->start(1000);
       
    58 //! [6]
       
    59 
       
    60     setWindowTitle(tr("Analog Clock"));
       
    61     resize(200, 200);
       
    62     
       
    63     // simple counter for testing purposes
       
    64     counter = 0;
       
    65 //! [7]
       
    66 }
       
    67 //! [1] //! [7]
       
    68 
       
    69 //! [8] //! [9]
       
    70 void AnalogClock::paintEvent(QPaintEvent *)
       
    71 //! [8] //! [10]
       
    72 {
       
    73     static const QPoint hourHand[3] = {
       
    74         QPoint(7, 8),
       
    75         QPoint(-7, 8),
       
    76         QPoint(0, -40)
       
    77     };
       
    78     static const QPoint minuteHand[3] = {
       
    79         QPoint(7, 8),
       
    80         QPoint(-7, 8),
       
    81         QPoint(0, -70)
       
    82     };
       
    83 
       
    84     QColor hourColor(127, 0, 127);
       
    85     QColor minuteColor(0, 127, 127, 191);
       
    86 
       
    87     int side = qMin(width(), height());
       
    88     QTime time = QTime::currentTime();
       
    89 //! [10]
       
    90 
       
    91 //! [11]
       
    92     QPainter painter(this);
       
    93 //! [11] //! [12]
       
    94     painter.setRenderHint(QPainter::Antialiasing);
       
    95 //! [12] //! [13]
       
    96     painter.translate(width() / 2, height() / 2);
       
    97 //! [13] //! [14]
       
    98     painter.scale(side / 200.0, side / 200.0);
       
    99 //! [9] //! [14]
       
   100 
       
   101 //! [15]
       
   102     painter.setPen(Qt::NoPen);
       
   103 //! [15] //! [16]
       
   104     painter.setBrush(hourColor);
       
   105 //! [16]
       
   106 
       
   107 //! [17] //! [18]
       
   108     painter.save();
       
   109 //! [17] //! [19]
       
   110     painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
       
   111     painter.drawConvexPolygon(hourHand, 3);
       
   112     painter.restore();
       
   113 //! [18] //! [19]
       
   114 
       
   115 //! [20]
       
   116     painter.setPen(hourColor);
       
   117 //! [20] //! [21]
       
   118 
       
   119     for (int i = 0; i < 12; ++i) {
       
   120         painter.drawLine(88, 0, 96, 0);
       
   121         painter.rotate(30.0);
       
   122     }
       
   123 //! [21]
       
   124 
       
   125 //! [22]
       
   126     painter.setPen(Qt::NoPen);
       
   127 //! [22] //! [23]
       
   128     painter.setBrush(minuteColor);
       
   129 
       
   130 //! [24]
       
   131     painter.save();
       
   132     painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
       
   133     painter.drawConvexPolygon(minuteHand, 3);
       
   134     painter.restore();
       
   135 //! [23] //! [24]
       
   136 
       
   137 //! [25]
       
   138     painter.setPen(minuteColor);
       
   139 //! [25] //! [26]
       
   140 
       
   141 //! [27]
       
   142     for (int j = 0; j < 60; ++j) {
       
   143         if ((j % 5) != 0)
       
   144             painter.drawLine(92, 0, 96, 0);
       
   145         painter.rotate(6.0);
       
   146     }
       
   147     counter++;
       
   148 //! [27]
       
   149 }
       
   150 //! [26]