clock/clockui/clockwidget/clockwidgetimpl/src/digitalclockwidget.cpp
changeset 45 b6db4fd4947b
child 57 bb2d3e476f29
equal deleted inserted replaced
23:fd30d51f876b 45:b6db4fd4947b
       
     1 /*
       
     2 * Copyright (c) 2010 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:  DigitalClockWidget
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <HbStyleLoader>
       
    20 #include <HbLabel>
       
    21 #include <HbTextItem>
       
    22 #include <hbextendedlocale>
       
    23 #include <QGraphicsSceneMouseEvent>
       
    24 #include <QSizePolicy>
       
    25 #include <QTimer>
       
    26 #include <QTime>
       
    27 
       
    28 // User includes
       
    29 #include "digitalclockwidget.h"
       
    30 
       
    31 // Constants
       
    32 const int clockUpdateInterval  (60000); // msec
       
    33 
       
    34 /*!
       
    35     \class DigitalClockWidget
       
    36 
       
    37     This class implements the digitalclock widget which gets displayed
       
    38     in the clockmainview when the clocktype is set to digital type.
       
    39 */
       
    40 
       
    41 /*!
       
    42     Constructor.
       
    43     \param useAmPm bool for setting 12 hour format
       
    44     \param parent The parent of type QGraphicsItem.
       
    45 */
       
    46 DigitalClockWidget::DigitalClockWidget(bool useAmPm, QGraphicsItem *parent)
       
    47   : HbWidget(parent),
       
    48     mUseAmPm(useAmPm)
       
    49 {
       
    50     bool result = HbStyleLoader::registerFilePath(":/resource/digitalclockwidget.widgetml");
       
    51     result = HbStyleLoader::registerFilePath(":/resource/digitalclockwidget.css");
       
    52     result = HbStyleLoader::registerFilePath(":/resource/digitalclockwidget_color.css");
       
    53 
       
    54     createPrimitives();
       
    55     
       
    56     mTimer = new QTimer(this);
       
    57     connect(mTimer, SIGNAL(timeout()), SLOT(tick()));
       
    58     
       
    59     QTime time = QTime::currentTime();
       
    60     int initialIntervalTime = (clockUpdateInterval - ( time.msec() + (time.second() * 1000)));
       
    61     mTimer->start(initialIntervalTime);
       
    62     
       
    63 }
       
    64 
       
    65 /*!
       
    66     Destructor.
       
    67 */
       
    68 DigitalClockWidget::~DigitalClockWidget()
       
    69 {   
       
    70     mTimer->stop();
       
    71     HbStyleLoader::unregisterFilePath(":/resource/digitalclockwidget.widgetml");
       
    72     HbStyleLoader::unregisterFilePath(":/resource/digitalclockwidget.css");
       
    73     HbStyleLoader::unregisterFilePath(":/resource/digitalclockwidget_color.css");
       
    74 
       
    75 }
       
    76 
       
    77 /*!
       
    78     Updates clock visualization according to current time
       
    79  */
       
    80 void DigitalClockWidget::tick()
       
    81 {
       
    82     mTimer->setInterval(clockUpdateInterval);
       
    83     updatePrimitives();
       
    84     update();
       
    85 }
       
    86 
       
    87 /*!
       
    88     Toggles time format
       
    89  */
       
    90 void DigitalClockWidget::setAmPm(bool useAmPm)
       
    91 {
       
    92     
       
    93     bool old = mUseAmPm;
       
    94     mUseAmPm = useAmPm;
       
    95     
       
    96     if (mUseAmPm != old) { // change am/pm label
       
    97 	    QTime time = QTime::currentTime();
       
    98 	    QString timeString;
       
    99 	    if (mUseAmPm) {
       
   100 	    	mAmPmLabel->setText(time.toString("ap")); // set am/pm label
       
   101 	    } else {
       
   102 	    	mAmPmLabel->setText(""); // set am/pm label as null
       
   103 	    }
       
   104     }
       
   105     
       
   106 }
       
   107 
       
   108 /*!
       
   109     @copydoc HbWidget::resizeEvent()
       
   110  */
       
   111 void DigitalClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   112 {
       
   113     HbWidget::resizeEvent(event);
       
   114 }
       
   115 
       
   116 /*!
       
   117     Creates all widget primitives.
       
   118  */
       
   119 void DigitalClockWidget::createPrimitives()
       
   120 {
       
   121     mClockLabel = new HbTextItem(this);    
       
   122     mClockLabel->setTextWrapping( Hb::TextNoWrap );
       
   123     HbStyle::setItemName(mClockLabel, QLatin1String("clockLabel"));
       
   124     
       
   125     mAmPmLabel = new HbTextItem(this);
       
   126     mClockLabel->setTextWrapping( Hb::TextNoWrap );
       
   127     HbStyle::setItemName(mAmPmLabel, QLatin1String("amPmLabel"));
       
   128 }
       
   129 
       
   130 /*!
       
   131     @copydoc HbWidget::updatePrimitives()
       
   132     updates all widget primitives
       
   133  */
       
   134 void DigitalClockWidget::updatePrimitives()
       
   135 {
       
   136 
       
   137     QTime time = QTime::currentTime();
       
   138     QString timeString;
       
   139     
       
   140     if (mUseAmPm) {
       
   141 	    timeString = time.toString("hh:ap"); // covert time in 12 hours format
       
   142 	    timeString = timeString.section(":", 0, 0); // it would remove :ap section from timeString
       
   143 	    mAmPmLabel->setText(time.toString("ap")); // set am/pm label
       
   144 	} else {
       
   145 	    mAmPmLabel->setText(""); // set am/pm label as null
       
   146 	    timeString = time.toString("hh"); // append hour part
       
   147     }
       
   148 
       
   149     int index(1);
       
   150     HbExtendedLocale locale;
       
   151     QChar timeSeparator(locale.timeSeparator(index)) ;
       
   152     timeString.append(timeSeparator); // append time separator
       
   153     timeString.append(time.toString("mm")); //append minute part
       
   154 
       
   155     mClockLabel->setText(timeString);
       
   156 
       
   157 }