src/hbplugins/devicedialogs/indicatormenuplugin/hbindicatormenuclock.cpp
changeset 2 06ff229162e9
parent 1 f7ac710697a9
child 3 11d3954df52a
child 4 ae1717029441
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbPlugins module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 #include <QTime>
       
    26 #include <hbtextitem.h>
       
    27 
       
    28 #include "hbindicatormenuclock_p.h"
       
    29 
       
    30 void HbIndicatorMenuClock::localeInfo(LocaleInfo *info)
       
    31 {
       
    32     QString timeFormat(QLocale().timeFormat(QLocale::LongFormat));
       
    33 #if defined(Q_OS_SYMBIAN)
       
    34     //use extendedlocale
       
    35     HbExtendedLocale loc;
       
    36     info->useSpace = loc.amPmSpace();
       
    37     info->symbolPos = loc.amPmSymbolPosition();
       
    38     
       
    39     //qt returns wrong timeformat in QLocale().timeFormat(), fix it using hbextendedlocale
       
    40     //todo: remove these, when it works
       
    41     timeFormat.clear();
       
    42     QChar sep(loc.timeSeparator(0));
       
    43     if (!sep.isNull()) {
       
    44         timeFormat.append(sep);
       
    45     }
       
    46     timeFormat.append('h').append(loc.timeSeparator(1))
       
    47               .append("mm").append(loc.timeSeparator(2))
       
    48               .append("ss");
       
    49     sep = loc.timeSeparator(3);
       
    50     if (!sep.isNull()) {
       
    51         timeFormat.append(sep);
       
    52     }
       
    53     
       
    54     if (loc.timeStyle() == HbExtendedLocale::Time12) {
       
    55         timeFormat.append(" ap");
       
    56     }
       
    57     //todo: remove end
       
    58 #endif
       
    59     QString strippedTimeFormat(timeFormat); //removes am/pm-symbol from format string.
       
    60     strippedTimeFormat.remove(QChar('a'), Qt::CaseInsensitive)
       
    61                       .remove(QChar('p'), Qt::CaseInsensitive);
       
    62 
       
    63     QString trimmedTimeFormat(timeFormat.trimmed());
       
    64     int index = trimmedTimeFormat.indexOf(QChar('A'));
       
    65     if (index >= 0) {
       
    66         info->amPmFormat = "AP";
       
    67         info->useAmPmSymbol = true;
       
    68     } else {
       
    69         index = trimmedTimeFormat.indexOf(QChar('a'));
       
    70         if (index >= 0) {
       
    71             info->amPmFormat = "ap";
       
    72             info->useAmPmSymbol = true;
       
    73         }
       
    74     }
       
    75 #if !defined(Q_OS_SYMBIAN)
       
    76     if (index == 0) {
       
    77         info->symbolPos = HbExtendedLocale::Before;
       
    78 
       
    79         //am/pm removed from strippedTimeFormat, only space is left at(0).
       
    80         info->useSpace = strippedTimeFormat.at(0).isSpace();
       
    81     } else {
       
    82         info->symbolPos = HbExtendedLocale::After;
       
    83         index--; //index to point to where space should be.
       
    84         if (index >= 0 && index < strippedTimeFormat.size()) {
       
    85             info->useSpace = strippedTimeFormat.at(index).isSpace();
       
    86         }
       
    87     }
       
    88 #endif
       
    89     if (info->useAmPmSymbol && info->useSpace) {
       
    90         if (info->symbolPos == HbExtendedLocale::Before) {
       
    91             info->amPmFormat.append(" ");
       
    92         } else {
       
    93             info->amPmFormat.prepend(" ");
       
    94         }
       
    95     }
       
    96     info->timeFormat = strippedTimeFormat.trimmed() + info->amPmFormat;
       
    97 }
       
    98 
       
    99 HbIndicatorMenuClock::HbIndicatorMenuClock(QGraphicsItem *parent) :
       
   100         HbWidget(parent), mTickTimerId(0),
       
   101         mTimeItem(0), mAmPmSymbol(0)
       
   102 {
       
   103     mTimeItem = new HbTextItem(this);
       
   104     HbStyle::setItemName(mTimeItem, "time");
       
   105     mTimeItem->setFontSpec(HbFontSpec(HbFontSpec::Primary));
       
   106     mTimeItem->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
       
   107     mTimeItem->setTextWrapping( Hb::TextNoWrap );
       
   108 
       
   109     localeInfo(&mInfo);
       
   110     if (mInfo.useAmPmSymbol) {
       
   111         mAmPmSymbol = new HbTextItem(this);
       
   112         mAmPmSymbol->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
       
   113         mTimeItem->setTextWrapping(Hb::TextNoWrap);
       
   114         mAmPmSymbol->setFontSpec(HbFontSpec(HbFontSpec::Primary));
       
   115         HbStyle::setItemName(mAmPmSymbol, "ampmSymbol");
       
   116     }
       
   117 }
       
   118 
       
   119 HbIndicatorMenuClock::~HbIndicatorMenuClock()
       
   120 {
       
   121     if (mTickTimerId != 0) {
       
   122         killTimer(mTickTimerId);
       
   123         mTickTimerId = 0;
       
   124     }
       
   125 }
       
   126 
       
   127 void HbIndicatorMenuClock::timerEvent(QTimerEvent *event)
       
   128 {
       
   129     if (event->timerId() == mTickTimerId) {
       
   130         updateClock();
       
   131     }
       
   132 }
       
   133 
       
   134 void HbIndicatorMenuClock::updateClock()
       
   135 {
       
   136     QTime current = QTime::currentTime();
       
   137     QString amPmSymbolText(current.toString(mInfo.amPmFormat));
       
   138     if (mAmPmSymbol) {
       
   139         mAmPmSymbol->setText(amPmSymbolText);
       
   140     }
       
   141 
       
   142     QString timeText(current.toString(mInfo.timeFormat));
       
   143     timeText = timeText.left(timeText.size()- amPmSymbolText.size());
       
   144     mTimeItem->setText(timeText);
       
   145 
       
   146     QDate date = QDate::currentDate();
       
   147     if (mDate != date) {
       
   148         mDate = date;
       
   149         emit dateChanged();
       
   150     }
       
   151 }
       
   152 
       
   153 void HbIndicatorMenuClock::polish(HbStyleParameters& params)
       
   154 {
       
   155     setProperty("amPmSymbolPos", (int) mInfo.symbolPos);
       
   156     HbWidget::polish(params);
       
   157 }
       
   158 
       
   159 void HbIndicatorMenuClock::showEvent(QShowEvent *event)
       
   160 {
       
   161     Q_UNUSED(event)
       
   162     updateClock();
       
   163     // Start a timer.
       
   164     if (mTickTimerId == 0) {
       
   165         mTickTimerId = startTimer(1000);
       
   166     }
       
   167 }
       
   168 
       
   169 void HbIndicatorMenuClock::hideEvent(QHideEvent *event)
       
   170 {
       
   171     Q_UNUSED(event)
       
   172     if (mTickTimerId != 0) {
       
   173         killTimer(mTickTimerId);
       
   174         mTickTimerId = 0;
       
   175     }
       
   176 }
       
   177