homescreenapp/widgetplugins/hsclockwidgetplugin/src/hsclockwidget.cpp
changeset 35 f9ce957a272c
child 36 cdae8c6c3876
equal deleted inserted replaced
5:c743ef5928ba 35:f9ce957a272c
       
     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:  Clock widget
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QtGui>
       
    19 #include <QGraphicsWidget>
       
    20 #include <QSizePolicy>
       
    21 #include <QGraphicsLinearLayout>
       
    22 #include <hblabel.h>
       
    23 #include <hbextendedlocale.h>
       
    24 
       
    25 #include "hsclockwidget.h"
       
    26 #include "hsanalogclockwidget.h"
       
    27 #include "hsdigitalclockwidget.h"
       
    28 #include "hsclockwidgetdocumentloader.h"
       
    29 
       
    30 #ifdef Q_OS_SYMBIAN    
       
    31 #include "hsclockwidgettype_symbian.h"
       
    32 #endif
       
    33 
       
    34 namespace
       
    35 {
       
    36     const char ANALOG[] = "analog";
       
    37     const char DIGITAL[] = "digital";
       
    38 
       
    39     const char DIGITAL_CLOCK_DOCML[] = ":/xml/hsdigitalclockwidget.docml";
       
    40     const char ANALOG_CLOCK_DOCML[] = ":/xml/hsanalogclockwidget.docml";
       
    41 
       
    42     const char DIGITAL_CLOCK_WIDGET[] = "timeLabel";
       
    43     const char ANALOG_CLOCK_WIDGET[] = "analogClockWidget";
       
    44 
       
    45     const int clockUpdateInterval = 1000; // msec
       
    46 }
       
    47 
       
    48 /*!
       
    49     \class HsClockWidget
       
    50     \ingroup group_hsclockwidgetplugin
       
    51     \brief Implementation for the homescreen clock widget.
       
    52 
       
    53 */
       
    54 
       
    55 
       
    56 /*!
       
    57     \fn HsClockWidget::HsClockWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
       
    58 
       
    59     Constructs widget.
       
    60 */
       
    61 HsClockWidget::HsClockWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
       
    62     : HbWidget(parent, flags),
       
    63       mTimer(0),
       
    64       mClockType(ANALOG),
       
    65       mWidget(0),
       
    66       mLayout(0),
       
    67       mWidgetShown(false)
       
    68 {
       
    69 #if 0
       
    70 #ifdef Q_OS_SYMBIAN    
       
    71     HsClockWidgetType *clockType = new HsClockWidgetType(this);
       
    72     mClockType=clockType->type();
       
    73     connect(clockType, SIGNAL(typeChanged(QString)), this, SLOT(onTypeChanged(QString)));
       
    74 #endif    
       
    75 #endif
       
    76 }
       
    77 
       
    78 /*!
       
    79     \fn HsClockWidget::~HsClockWidget()
       
    80 
       
    81     Destructor.
       
    82 */
       
    83 HsClockWidget::~HsClockWidget()
       
    84 {
       
    85 }
       
    86 
       
    87 /*!
       
    88     Returns the clock type.
       
    89 */
       
    90 QString HsClockWidget::clockType() const
       
    91 {
       
    92     return mClockType;
       
    93 }
       
    94   
       
    95 /*!
       
    96     Sets the clock type;
       
    97 */
       
    98 void HsClockWidget::setClockType(const QString &type)
       
    99 {
       
   100     if (type == DIGITAL) {
       
   101         mClockType = DIGITAL;
       
   102     } else {
       
   103         mClockType = ANALOG;
       
   104     }
       
   105 }
       
   106 
       
   107 /*!
       
   108     \fn void HsClockWidget::onInitialize()
       
   109 
       
   110     Initializes clock widget
       
   111 */
       
   112 void HsClockWidget::onInitialize()
       
   113 {
       
   114     mLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   115     mLayout->setContentsMargins(0,0,0,0);
       
   116 
       
   117     hide();
       
   118 
       
   119     mWidget = loadClockWidget();
       
   120     mLayout->addItem(mWidget);
       
   121     setPreferredSize(mWidget->preferredSize());
       
   122     parentWidget()->resize(preferredSize()); // workaround for layouting
       
   123     
       
   124     mTimer = new QTimer(this);
       
   125     connect(mTimer, SIGNAL(timeout()), SLOT(updateTime()));
       
   126     setLayout(mLayout);
       
   127 
       
   128 }
       
   129 
       
   130 /*!
       
   131     \fn void HsClockWidget::show()
       
   132 
       
   133     Shows the widget
       
   134 */
       
   135 void HsClockWidget::onShow()
       
   136 {
       
   137     mWidgetShown = true;
       
   138     mTimer->start(clockUpdateInterval);
       
   139     show();
       
   140 }
       
   141 
       
   142 
       
   143 /*!
       
   144     \fn void HsClockWidget::show()
       
   145 
       
   146     Hides the widget
       
   147 */
       
   148 void HsClockWidget::onHide()
       
   149 {
       
   150     mWidgetShown = false;
       
   151     mTimer->stop();
       
   152     hide();
       
   153 }
       
   154 
       
   155 /*!
       
   156     Uninitializes the widget.
       
   157 */
       
   158 void HsClockWidget::onUninitialize()
       
   159 {
       
   160     mTimer->stop();
       
   161     hide();
       
   162 }
       
   163 
       
   164 /*!
       
   165     \fn void HsClockWidget::updateTime()
       
   166 
       
   167     Draws the clock with every second
       
   168 */
       
   169 void HsClockWidget::updateTime()
       
   170 {
       
   171     if (mClockType == DIGITAL) {
       
   172         hide(); // workaround for clock not updating
       
   173         static_cast<HbLabel*>(mWidget)->setPlainText(
       
   174             HbExtendedLocale().format(QTime::currentTime(), r_qtn_time_usual_with_zero));
       
   175         show(); // workaround for clock not updating 
       
   176     } else {
       
   177         static_cast<HsAnalogClockWidget*>(mWidget)->tick();
       
   178     }
       
   179 }
       
   180 
       
   181 /*!
       
   182     Toggles the clock type.
       
   183 */
       
   184 void HsClockWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   185 {
       
   186     Q_UNUSED(event);    
       
   187 #ifndef Q_OS_SYMBIAN    
       
   188     mTimer->stop();
       
   189     toggleClockType();
       
   190     emit setPreferences(QStringList() << "clockType");
       
   191 
       
   192     hide();
       
   193 
       
   194     mLayout->removeItem(mWidget);
       
   195     delete mWidget;
       
   196     mWidget = 0;
       
   197     mWidget = loadClockWidget();
       
   198     mLayout->addItem(mWidget);
       
   199     setPreferredSize(mWidget->preferredSize());
       
   200     parentWidget()->resize(preferredSize());
       
   201     
       
   202     show();
       
   203     updateTime();
       
   204     update();
       
   205 
       
   206 
       
   207     mTimer->start(clockUpdateInterval);
       
   208 #endif    
       
   209 }
       
   210 
       
   211 /*!
       
   212     Toggles the clock type.
       
   213 */
       
   214 void HsClockWidget::toggleClockType()
       
   215 {
       
   216     if (mClockType == ANALOG) {
       
   217         mClockType = DIGITAL;
       
   218     } else {
       
   219         mClockType = ANALOG;
       
   220     }
       
   221 }
       
   222 
       
   223 /*!
       
   224     Loads the digital or analog clock widget.
       
   225 */
       
   226 HbWidget *HsClockWidget::loadClockWidget()
       
   227 {
       
   228     HbWidget *clockWidget = 0;
       
   229 
       
   230     QString docmlFile;
       
   231     if (mClockType == DIGITAL) {
       
   232         docmlFile = DIGITAL_CLOCK_DOCML;
       
   233     } else {
       
   234         docmlFile = ANALOG_CLOCK_DOCML;
       
   235     }
       
   236 
       
   237     HsClockWidgetDocumentLoader loader;
       
   238     bool loaded = false;
       
   239     loader.load(docmlFile, &loaded);
       
   240 
       
   241     if (loaded) {
       
   242         if (mClockType == DIGITAL) {
       
   243             clockWidget = qobject_cast<HsDigitalClockWidget *>(loader.findWidget(DIGITAL_CLOCK_WIDGET));
       
   244             clockWidget->setBackgroundItem(HbStyle::P_Fade_background);
       
   245         } else {
       
   246             clockWidget = qobject_cast<HsAnalogClockWidget *>(loader.findWidget(ANALOG_CLOCK_WIDGET));
       
   247         }
       
   248 
       
   249     } else {
       
   250         qWarning() << "Unable to load clock widget from docml: " << docmlFile;
       
   251         // TODO: We must handle this error situation once error handling strategy is clear!
       
   252     }
       
   253 
       
   254     return clockWidget;
       
   255 }
       
   256 
       
   257 void HsClockWidget::onTypeChanged(QString type)
       
   258 {
       
   259 #if 0
       
   260     if (mClockType != type) {
       
   261         mTimer->stop();
       
   262         mClockType = type;
       
   263         emit setPreferences(QStringList() << "clockType");
       
   264         hide();
       
   265         mLayout->removeItem(mWidget);
       
   266         delete mWidget;
       
   267         mWidget = 0;
       
   268         mWidget = loadClockWidget();
       
   269         mLayout->addItem(mWidget);
       
   270         setPreferredSize(mWidget->preferredSize());
       
   271         parentWidget()->resize(preferredSize());
       
   272         if ( mWidgetShown ) {
       
   273             mTimer->start(clockUpdateInterval);
       
   274             show();
       
   275             }
       
   276     }    
       
   277 #endif    
       
   278 }