screensaver/screensaverplugins/snsrbigclockscreensaverplugin/src/snsroleddigitalclockcontainer.cpp
changeset 62 341166945d65
child 69 87476091b3f5
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
       
     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:  Container for oled digital clock.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "snsroleddigitalclockcontainer.h"
       
    19 
       
    20 #include <QDebug>
       
    21 #include <QGraphicsLinearLayout>
       
    22 #include <QTime>
       
    23 #include <QTimer>
       
    24 
       
    25 #include <hblabel.h>
       
    26 #include <hbextendedlocale.h>
       
    27 
       
    28 #include "snsrindicatorwidget.h"
       
    29 
       
    30 /*!
       
    31     \class SnsrOledDigitalClockContainer
       
    32     \ingroup group_snsrbigclockscreensaverplugin
       
    33     \brief Container used for preparing layout for oled digital clock.
       
    34  */
       
    35 
       
    36 const char *gOledDigitalLayoutDocml = ":/xml/snsrbigclockscreensaveroleddigital.docml";
       
    37 extern const char *gPortraitSectionName;
       
    38 extern const char *gLandscapeSectionName;
       
    39 
       
    40 extern const char *gMainViewName;
       
    41 extern const char *gMainContainerName;
       
    42 extern const char *gClockContainerName;
       
    43 
       
    44 extern const char *gTimeLabelName;
       
    45 extern const char *gAmPmLabelName;
       
    46 extern const char *gDateLabelName;
       
    47 
       
    48 extern const char *gDateFormatVerticalStr;
       
    49 extern const char *gDateFormatHorizontalStr;
       
    50 
       
    51 extern const char *gTimeFormatStr;
       
    52 extern const char *gAmPmFormatStr;
       
    53 
       
    54 extern const char *gIndicatorWidgetName;
       
    55 
       
    56 
       
    57 /*!
       
    58     Constructs a new SnsrOledDigitalClockContainer.
       
    59  */
       
    60 SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer() :
       
    61     SnsrBigClockContainer(), mClockContainer(0), mTimeLabel(0),
       
    62     mDateLabel(0), mAmPmLabel(0), mDestPosition(QPointF()), mInitialize(false)
       
    63 {
       
    64     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer")
       
    65 
       
    66     bool ok(false);
       
    67     qDebug() << gOledDigitalLayoutDocml;
       
    68 
       
    69     // load digital clock
       
    70     mDocumentObjects = mDocumentLoader.load(gOledDigitalLayoutDocml, &ok);
       
    71     Q_ASSERT_X(ok, gOledDigitalLayoutDocml, "Invalid DocML file.");
       
    72 
       
    73     if (ok) {
       
    74         mMainView = mDocumentLoader.findWidget(gMainViewName);
       
    75         mMainContainer = mDocumentLoader.findWidget(gMainContainerName);
       
    76         mClockContainer = mDocumentLoader.findWidget(gClockContainerName);
       
    77         mDateLabel = qobject_cast<HbLabel *>(
       
    78                 mDocumentLoader.findWidget(gDateLabelName));
       
    79         mTimeLabel = qobject_cast<HbLabel *>(
       
    80                 mDocumentLoader.findWidget(gTimeLabelName));
       
    81         mAmPmLabel = qobject_cast<HbLabel *>(
       
    82                 mDocumentLoader.findWidget(gAmPmLabelName));
       
    83         mIndicatorWidget = qobject_cast<SnsrIndicatorWidget *>(
       
    84                 mDocumentLoader.findWidget(gIndicatorWidgetName));
       
    85         Q_ASSERT_X(
       
    86                 mMainView && mMainContainer && mClockContainer 
       
    87                 && mDateLabel && mTimeLabel && mAmPmLabel
       
    88                 && mIndicatorWidget,
       
    89                 gOledDigitalLayoutDocml, "Objects not found in DocML file."
       
    90                 );
       
    91 
       
    92         mIndicatorWidget->setLayoutType(SnsrIndicatorWidget::IndicatorsCentered);
       
    93         
       
    94         mBackgroundContainerLayout->addItem(mMainView);
       
    95     }
       
    96 
       
    97     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer")
       
    98 }
       
    99 
       
   100 /*!
       
   101     Destructs the class.
       
   102  */
       
   103 SnsrOledDigitalClockContainer::~SnsrOledDigitalClockContainer()
       
   104 {
       
   105     //mTimeLabel, mAmPmLabel, mDateLabel, mMoveTimer - deleted by the parent
       
   106 }
       
   107 
       
   108 /*!
       
   109     Updates OLED digital clock widget position.
       
   110  */
       
   111 void SnsrOledDigitalClockContainer::updatePosition()
       
   112 {
       
   113     QSizeF containerSize = mMainContainer->size();
       
   114     
       
   115     // Container must have a valid size to enable calculating the 
       
   116     // destination position for the clock.
       
   117     if ( containerSize.width() > 0 && containerSize.height() > 0 ) {
       
   118         containerSize -= mClockContainer->boundingRect().size();
       
   119         QRectF containerRect( mMainContainer->pos(), containerSize );
       
   120         if ( mInitialize ) {
       
   121             QPointF clockPos = nextRandomPosition( mClockContainer->pos(), mDestPosition, containerRect );
       
   122             mClockContainer->setPos( clockPos );
       
   123         }
       
   124         else {
       
   125             mDestPosition = randomPosition( containerRect );
       
   126             mInitialize = true;
       
   127         }
       
   128     }
       
   129 }
       
   130 
       
   131 /*!
       
   132     Updates displayed time and date.
       
   133  */
       
   134 void SnsrOledDigitalClockContainer::update()
       
   135 {
       
   136     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::update")
       
   137 
       
   138     // time
       
   139     mTimeLabel->setPlainText(
       
   140         HbExtendedLocale().format(QTime::currentTime(), gTimeFormatStr)
       
   141         );
       
   142     mAmPmLabel->setPlainText(
       
   143         HbExtendedLocale().format(QTime::currentTime(), gAmPmFormatStr)
       
   144         );
       
   145 
       
   146     // date
       
   147     if (mCurrentOrientation == Qt::Vertical) {
       
   148         mDateLabel->setPlainText(
       
   149             HbExtendedLocale().format(
       
   150                 QDate::currentDate(), gDateFormatVerticalStr)
       
   151             );
       
   152     } else {
       
   153         mDateLabel->setPlainText(
       
   154             HbExtendedLocale().format(
       
   155                 QDate::currentDate(), gDateFormatHorizontalStr)
       
   156             );
       
   157     }
       
   158 
       
   159     updatePosition();
       
   160 
       
   161     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::update")
       
   162 }
       
   163 
       
   164 /*!
       
   165     Changes screensaver layout basing on orientation changes.
       
   166     \param orientation Current orientation.
       
   167  */
       
   168 void SnsrOledDigitalClockContainer::changeLayout(Qt::Orientation orientation)
       
   169 {
       
   170     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::changeLayout")
       
   171 
       
   172     if (mCurrentOrientation != orientation) {
       
   173         mCurrentOrientation = orientation;
       
   174 
       
   175         // hide controls to avoid screen flickering
       
   176         mMainView->hide();
       
   177 
       
   178         QString sectionToLoad("");
       
   179         if (mCurrentOrientation == Qt::Horizontal) {
       
   180             sectionToLoad = gLandscapeSectionName;
       
   181         }
       
   182         qDebug() << "loading: " << gOledDigitalLayoutDocml << ", section: "
       
   183             << sectionToLoad;
       
   184         bool ok(false);
       
   185         mDocumentLoader.load(gOledDigitalLayoutDocml, sectionToLoad, &ok);
       
   186         Q_ASSERT_X(ok, gOledDigitalLayoutDocml, "Invalid section in DocML file.");
       
   187 
       
   188         // disconnect container from parent layout,
       
   189         // connected container resets its position to the one defined in docml
       
   190         // after label text updates
       
   191         mClockContainer->setParentLayoutItem(0);
       
   192 
       
   193         // update labels before showing the view
       
   194         update();
       
   195 
       
   196         // view is rebuilt and ready to show
       
   197         mMainView->show();
       
   198     }
       
   199 
       
   200     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::changeLayout")
       
   201 }
       
   202 
       
   203 int SnsrOledDigitalClockContainer::updateIntervalInMilliseconds()
       
   204 {
       
   205     return 60*1000;
       
   206 }
       
   207 
       
   208 // end of file