screensaver/snsrplugins/snsrbigclockscreensaverplugin/snsrclockwidgets/src/snsroledclockwidget.cpp
changeset 97 66b5fe3c07fd
child 98 e6f74eb7f69f
equal deleted inserted replaced
95:32e56106abf2 97:66b5fe3c07fd
       
     1 /*
       
     2 * Copyright (c) 2009 - 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: Oled Clock Widget.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "snsroledclockwidget.h"
       
    19 #include "snsrlabel.h"
       
    20 #include "snsrcolors.h"
       
    21 
       
    22 #include <QGraphicsSvgItem>
       
    23 #include <QTime>
       
    24 #include <QDebug>
       
    25 #include <QLocale>
       
    26 #include <HbExtendedLocale>
       
    27 #include <HbStyleLoader>
       
    28 #include <HbIconItem>
       
    29 #include <HbTextItem>
       
    30 
       
    31 
       
    32 const char *gOledClockCssFilePath = ":/style/snsroledclockwidget.css";
       
    33 const char *gOledClockWidgetMLFilePath = ":/style/snsroledclockwidget.widgetml";
       
    34 
       
    35 /*!
       
    36     \class SnsrOledClockWidget
       
    37     \ingroup group_snsrbigclockscreensaverprovider
       
    38     \brief Screensaver oled clock widget.
       
    39  */
       
    40 
       
    41 /*!
       
    42     Constructs a new SnsrOledClockWidget.
       
    43     \param parent Parent object.
       
    44  */
       
    45 SnsrOledClockWidget::SnsrOledClockWidget(QGraphicsItem* parent):
       
    46     HbWidget(parent),
       
    47     mClockBackground(0), mClockHourHand(0), mClockMinuteHand(0),
       
    48     mClockAmPmLabel(0)
       
    49 {
       
    50     HbStyleLoader::registerFilePath(gOledClockCssFilePath);
       
    51     HbStyleLoader::registerFilePath(gOledClockWidgetMLFilePath);
       
    52 
       
    53     createPrimitives();
       
    54     updatePrimitives();
       
    55 }
       
    56 
       
    57 void SnsrOledClockWidget::polish( HbStyleParameters& params )
       
    58 {
       
    59     // TODO: updatePrimitives might not be needed to be called from here anymore (hb changes)?
       
    60     HbWidget::polish(params);
       
    61     updatePrimitives();
       
    62 }
       
    63 
       
    64 /*!
       
    65     Destructs the class.
       
    66  */
       
    67 SnsrOledClockWidget::~SnsrOledClockWidget()
       
    68 {
       
    69     HbStyleLoader::unregisterFilePath( gOledClockCssFilePath );
       
    70     HbStyleLoader::unregisterFilePath( gOledClockWidgetMLFilePath );
       
    71 
       
    72 }
       
    73 
       
    74 /*!
       
    75     @copydoc HbWidget::resizeEvent()
       
    76  */
       
    77 void SnsrOledClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
    78 {
       
    79     HbWidget::resizeEvent(event);
       
    80     updatePrimitives();
       
    81 }
       
    82 
       
    83 /*!
       
    84     Creates all widget primitives.
       
    85  */
       
    86 void SnsrOledClockWidget::createPrimitives()
       
    87 {
       
    88     // TODO: final graphics will be named qtg_graf_screensaver_clock_oled_xxx.
       
    89     // not available in platform yet
       
    90 
       
    91     if (!mClockBackground) {
       
    92         mClockBackground = new HbIconItem(QLatin1String("qtg_graf_clock_night_bg"), this);
       
    93         HbStyle::setItemName(mClockBackground, QLatin1String("oled_clock_background"));
       
    94     }
       
    95     if (!mClockHourHand) {
       
    96         mClockHourHand = new HbIconItem(QLatin1String("qtg_graf_clock_night_hour"), this);
       
    97         HbStyle::setItemName(mClockHourHand, QLatin1String("oled_clock_hour_hand"));
       
    98     }
       
    99     if (!mClockMinuteHand) {
       
   100         mClockMinuteHand = new HbIconItem(QLatin1String("qtg_graf_clock_night_min"), this);
       
   101         HbStyle::setItemName(mClockMinuteHand, QLatin1String("oled_clock_minute_hand"));
       
   102     }
       
   103     if (!mClockAmPmLabel) {
       
   104         mClockAmPmLabel = new HbTextItem(this);
       
   105         HbStyle::setItemName(mClockAmPmLabel, QLatin1String("oled_clock_am_pm_label"));
       
   106         mClockAmPmLabel->setTextColor(SnsrColors::PowerSaveModeWidgetColor);
       
   107         mClockAmPmLabel->setMinimumLines(1);
       
   108         mClockAmPmLabel->setMaximumLines(1);
       
   109         mClockAmPmLabel->setTextWrapping(Hb::TextNoWrap);
       
   110         HbFontSpec textFont(HbFontSpec::Primary);
       
   111         qreal textHeight = 0;
       
   112         style()->parameter(QLatin1String("hb-param-text-height-tiny"), textHeight);
       
   113         textFont.setTextHeight(textHeight);
       
   114         mClockAmPmLabel->setFontSpec(textFont);
       
   115     }
       
   116 }
       
   117 
       
   118 /*!
       
   119     @copydoc HbWidget::updatePrimitives()
       
   120  */
       
   121 void SnsrOledClockWidget::updatePrimitives()
       
   122 {
       
   123     if (!mClockHourHand || !mClockMinuteHand || !mClockAmPmLabel) {
       
   124         createPrimitives();
       
   125     }
       
   126     Q_ASSERT( mClockHourHand && mClockMinuteHand && mClockAmPmLabel );
       
   127     
       
   128     // Calculate angles for clock hands.
       
   129     // Use granularity of one minute so that minute hand is always exactly
       
   130     // on some minute and not between minutes. OLED clock is not updated more
       
   131     // frequently than once per minute and using finer granularity would cause
       
   132     // the minute hand to be always between even minutes.
       
   133     QTime time = QTime::currentTime();
       
   134     qreal m = 6 * time.minute();
       
   135     qreal h = 30 * ((time.hour() % 12) + m/360);
       
   136     
       
   137     int x = mClockHourHand->preferredSize().width()/2;
       
   138     int y = mClockHourHand->preferredSize().height()/2;
       
   139     mClockHourHand->setTransform(QTransform().translate(x, y).rotate(h).translate(-x, -y));
       
   140 
       
   141     x = mClockMinuteHand->preferredSize().width()/2;
       
   142     y = mClockMinuteHand->preferredSize().height()/2;
       
   143     mClockMinuteHand->setTransform(QTransform().translate(x, y).rotate(m).translate(-x, -y));
       
   144 
       
   145     QString amPmString = (time.hour()<12) ? HbExtendedLocale().amText() : HbExtendedLocale().pmText();
       
   146     mClockAmPmLabel->setText( amPmString );
       
   147 }
       
   148 
       
   149 /*!
       
   150     Updates primitives when time change.
       
   151  */
       
   152 void SnsrOledClockWidget::tick()
       
   153 {
       
   154     updatePrimitives();
       
   155 }