screensaver/screensaverplugins/snsrbigclockscreensaverplugin/snsrclockwidgets/src/snsroledclockwidget.cpp
changeset 97 66b5fe3c07fd
parent 95 32e56106abf2
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 
       
    21 #include <QGraphicsSvgItem>
       
    22 #include <QTime>
       
    23 #include <QDebug>
       
    24 #include <QLocale>
       
    25 
       
    26 #include <HbStyleLoader>
       
    27 #include <HbIconItem>
       
    28 #include <HbTextItem>
       
    29 
       
    30 
       
    31 const char *gOledClockCssFilePath = ":/style/snsroledclockwidget.css";
       
    32 const char *gOledClockWidgetMLFilePath = ":/style/snsroledclockwidget.widgetml";
       
    33 const char *gDateFormatStr = "ddd d"; // this is Qt format string, not Hb format like the others
       
    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     //,mClockDateFrame(0), mClockDateLabel(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     // TODO: deleted by parent?
       
    70   /*  delete mClockBackground;
       
    71     delete mClockHourHand;
       
    72     delete mClockMinuteHand;
       
    73     delete mClockDateFrame;
       
    74     delete mClockDateLabel;*/
       
    75 
       
    76     HbStyleLoader::unregisterFilePath( gOledClockCssFilePath );
       
    77     HbStyleLoader::unregisterFilePath( gOledClockWidgetMLFilePath );
       
    78 
       
    79 }
       
    80 
       
    81 /*!
       
    82     @copydoc HbWidget::resizeEvent()
       
    83  */
       
    84 void SnsrOledClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
    85 {
       
    86     HbWidget::resizeEvent(event);
       
    87     updatePrimitives();
       
    88 }
       
    89 
       
    90 /*!
       
    91     Creates all widget primitives.
       
    92  */
       
    93 void SnsrOledClockWidget::createPrimitives()
       
    94 {
       
    95     // TODO: final graphics will be named qtg_graf_screensaver_clock_oled_xxx.
       
    96     // not available in platform yet
       
    97     // Also am/pm label should be added into the clock face and date label
       
    98     // be removed (=replace date with am/pm) ?
       
    99 
       
   100     if (!mClockBackground) {
       
   101         mClockBackground = new HbIconItem(QLatin1String("qtg_graf_clock_night_bg"), this);
       
   102         HbStyle::setItemName(mClockBackground, QLatin1String("oled_clock_background"));
       
   103     }
       
   104     if (!mClockHourHand) {
       
   105         mClockHourHand = new HbIconItem(QLatin1String("qtg_graf_clock_night_hour"), this);
       
   106         HbStyle::setItemName(mClockHourHand, QLatin1String("oled_clock_hour_hand"));
       
   107     }
       
   108     if (!mClockMinuteHand) {
       
   109         mClockMinuteHand = new HbIconItem(QLatin1String("qtg_graf_clock_night_min"), this);
       
   110         HbStyle::setItemName(mClockMinuteHand, QLatin1String("oled_clock_minute_hand"));
       
   111     }
       
   112   /*
       
   113     if (!mClockDateFrame) {
       
   114         mClockDateFrame = new HbIconItem(QLatin1String("oled-clock-date-frame"), this);
       
   115         HbStyle::setItemName(mClockDateFrame, QLatin1String("oled_clock_date_frame"));
       
   116     }
       
   117     if (!mClockDateLabel) {
       
   118         mClockDateLabel = new SnsrLabel(this);
       
   119         HbStyle::setItemName(mClockDateLabel, QLatin1String("oled_clock_date_label"));
       
   120     }*/
       
   121 }
       
   122 
       
   123 /*!
       
   124     @copydoc HbWidget::updatePrimitives()
       
   125  */
       
   126 void SnsrOledClockWidget::updatePrimitives()
       
   127 {
       
   128     if (!mClockHourHand || !mClockMinuteHand /*|| !mClockDateLabel*/) {
       
   129         createPrimitives();
       
   130     }
       
   131     Q_ASSERT( mClockHourHand && mClockMinuteHand /*&& mClockDateLabel */);
       
   132     
       
   133     // Calculate angles for clock hands.
       
   134     // Use granularity of one minute so that minute hand is always exactly
       
   135     // on some minute and not between minutes. OLED clock is not updated more
       
   136     // frequently than once per minute and using finer granularity would cause
       
   137     // the minute hand to be always between even minutes.
       
   138     QTime time = QTime::currentTime();
       
   139     qreal m = 6 * time.minute();
       
   140     qreal h = 30 * ((time.hour() % 12) + m/360);
       
   141     
       
   142     int x = mClockHourHand->preferredSize().width()/2;
       
   143     int y = mClockHourHand->preferredSize().height()/2;
       
   144     mClockHourHand->setTransform(QTransform().translate(x, y).rotate(h).translate(-x, -y));
       
   145 
       
   146     x = mClockMinuteHand->preferredSize().width()/2;
       
   147     y = mClockMinuteHand->preferredSize().height()/2;
       
   148     mClockMinuteHand->setTransform(QTransform().translate(x, y).rotate(m).translate(-x, -y));
       
   149 
       
   150    // QString dateString = QLocale().toString(QDate::currentDate(), gDateFormatStr);
       
   151    // mClockDateLabel->setPlainText(dateString);
       
   152 }
       
   153 
       
   154 /*!
       
   155     Updates primitives when time change.
       
   156  */
       
   157 void SnsrOledClockWidget::tick()
       
   158 {
       
   159     updatePrimitives();
       
   160 }