clock/clockui/clockwidget/clockwidgetimpl/src/analogclockwidget.cpp
changeset 26 a949c2543c15
child 50 579cc610882e
equal deleted inserted replaced
23:fd30d51f876b 26:a949c2543c15
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  AnalogClockWidget
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <HbIconItem>
       
    20 #include <HbStyleLoader>
       
    21 #include <QTime>
       
    22 #include <QTimer>
       
    23 
       
    24 // User includes
       
    25 #include "analogclockwidget.h"
       
    26 
       
    27 // Constants
       
    28 const int clockUpdateInterval  (1000); // msec
       
    29 
       
    30 /*!
       
    31     \class AnalogClockWidget
       
    32 
       
    33     This class implements the analogclock widget which gets displayed
       
    34     in the clockmainview when the clocktype is set to analog type.
       
    35 */
       
    36 
       
    37 /*!
       
    38     Constructor.
       
    39     \param parent The parent of type QGraphicsItem.
       
    40 */
       
    41 AnalogClockWidget::AnalogClockWidget(QGraphicsItem *parent)
       
    42     : HbWidget(parent)
       
    43 {
       
    44     HbStyleLoader::registerFilePath(":/resource");
       
    45     updatePrimitives();
       
    46     mTimer = new QTimer(this);
       
    47     connect(mTimer, SIGNAL(timeout()), SLOT(tick()));
       
    48     mTimer->start(clockUpdateInterval);
       
    49 }
       
    50 
       
    51 /*!
       
    52     Destructor.
       
    53  */
       
    54 AnalogClockWidget::~AnalogClockWidget()
       
    55 {    
       
    56     mTimer->stop(); 
       
    57     HbStyleLoader::unregisterFilePath(":/resource");
       
    58 }
       
    59 
       
    60 /*!
       
    61     Handles resize event from HbWidget
       
    62  */
       
    63 void AnalogClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
    64 {
       
    65     QGraphicsWidget::resizeEvent(event);
       
    66     updatePrimitives();   
       
    67 }
       
    68 
       
    69 /*!
       
    70     @copydoc HbWidget::updatePrimitives()
       
    71  */
       
    72 void AnalogClockWidget::updatePrimitives()
       
    73 {
       
    74     if (!mClockBackground) {
       
    75         mClockBackground = new HbIconItem(QLatin1String("qtg_graf_clock_day_bg"), this);
       
    76         HbStyle::setItemName(mClockBackground, QLatin1String("clock_background"));
       
    77     }
       
    78 
       
    79     // Calculate angles for clock hands.
       
    80     QTime time = QTime::currentTime();
       
    81     qreal s = 6 * time.second();
       
    82     qreal m = 6 * (time.minute() + s/360);
       
    83     qreal h = 30 * ((time.hour() % 12) + m/360);
       
    84 
       
    85 	if (!mClockHourHand) {
       
    86         mClockHourHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_hour"), this);
       
    87         HbStyle::setItemName(mClockHourHand, QLatin1String("clock_hour_hand"));
       
    88     }
       
    89 
       
    90     int x = mClockHourHand->geometry().width()/2;
       
    91     int y = mClockHourHand->geometry().height()/2;
       
    92     mClockHourHand->setTransform(QTransform().translate(x, y).rotate(h).translate(-x, -y));
       
    93 
       
    94 	if (!mClockMinuteHand) {
       
    95         mClockMinuteHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_min"), this);
       
    96         HbStyle::setItemName(mClockMinuteHand, QLatin1String("clock_minute_hand"));
       
    97     }
       
    98 
       
    99     x = mClockMinuteHand->geometry().width()/2;
       
   100     y = mClockMinuteHand->geometry().height()/2;
       
   101     mClockMinuteHand->setTransform(QTransform().translate(x, y).rotate(m).translate(-x, -y));
       
   102     
       
   103       
       
   104     if (!mClockSecondHand) {
       
   105          mClockSecondHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_sec"), this);
       
   106         HbStyle::setItemName(mClockSecondHand, QLatin1String("clock_second_hand"));
       
   107         }
       
   108 
       
   109     x = mClockSecondHand->geometry().width()/2;
       
   110     y = mClockSecondHand->geometry().height()/2;
       
   111     mClockSecondHand->setTransform(QTransform().translate(x, y).rotate(s).translate(-x, -y));
       
   112 
       
   113 }
       
   114 
       
   115 /*!
       
   116     Updates clock visualization according to current time
       
   117  */
       
   118 void AnalogClockWidget::tick()
       
   119 {
       
   120     updatePrimitives();
       
   121     update();
       
   122 }
       
   123 
       
   124 /*!
       
   125     Handles polish event
       
   126  */
       
   127 void AnalogClockWidget::polish( HbStyleParameters& params ) 
       
   128 {  
       
   129     HbWidget::polish(params); 
       
   130     updatePrimitives();
       
   131 } 
       
   132 
       
   133 // End of file  --Don't remove this.