src/hbwidgets/editors/hbdatetimeedit.cpp
changeset 34 ed14f46c0e55
parent 31 7516d6d86cf5
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
     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 HbWidgets 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 
       
    26 #include <QDateTime>
       
    27 #include <QMetaMethod>
       
    28 
       
    29 #include "hblineedit.h"
       
    30 #include "hbdatetimevalidator_p.h"
       
    31 #include "hbdatetimeedit.h"
       
    32 #include "hbdatetimeedit_p.h"
       
    33 #include "hbevent.h"
       
    34 
       
    35 /*!
       
    36     @alpha
       
    37     @hbwidgets
       
    38     \brief The HbDateTimeEdit class provides a widget for editing dates and times.
       
    39 
       
    40 
       
    41 
       
    42     \sa QDateTime
       
    43 
       
    44  */
       
    45 
       
    46 /*!
       
    47     Constructs datetime edit widget.
       
    48  */
       
    49 HbDateTimeEdit::HbDateTimeEdit (QGraphicsItem *parent) :
       
    50     HbLineEdit(*new HbDateTimeEditPrivate, parent)
       
    51 {
       
    52     Q_D(HbDateTimeEdit);
       
    53     d->q_ptr = this;
       
    54 
       
    55     d->init();
       
    56 }
       
    57 
       
    58 /*!
       
    59     Constructs date time editor to display \a date using default locale.
       
    60 
       
    61  */
       
    62 HbDateTimeEdit::HbDateTimeEdit (const QDate &date, QGraphicsItem *parent) :
       
    63     HbLineEdit(*new HbDateTimeEditPrivate, parent)
       
    64 {
       
    65     Q_D(HbDateTimeEdit);
       
    66     d->q_ptr = this;
       
    67 
       
    68     d->init(date);
       
    69 }
       
    70 
       
    71 /*!
       
    72     Constructs date time editor to display \a time using default locale.
       
    73  */
       
    74 HbDateTimeEdit::HbDateTimeEdit (const QTime &time, QGraphicsItem *parent) :
       
    75     HbLineEdit(*new HbDateTimeEditPrivate, parent)
       
    76 {
       
    77     Q_D(HbDateTimeEdit);
       
    78     d->q_ptr = this;
       
    79 
       
    80     d->init(time);
       
    81 }
       
    82 
       
    83 /*!
       
    84     Constructs date time editor to display \a dateTime using default locale.
       
    85  */
       
    86 HbDateTimeEdit::HbDateTimeEdit (const QDateTime &dateTime, QGraphicsItem *parent) :
       
    87     HbLineEdit(*new HbDateTimeEditPrivate, parent)
       
    88 {
       
    89     Q_D(HbDateTimeEdit);
       
    90     d->q_ptr = this;
       
    91 
       
    92     d->init(dateTime);
       
    93 }
       
    94 
       
    95 /*
       
    96     for internal use only
       
    97  */
       
    98 HbDateTimeEdit::HbDateTimeEdit (HbDateTimeEditPrivate &dd, QGraphicsItem *parent) :
       
    99     HbLineEdit(dd, parent)
       
   100 {
       
   101     Q_D(HbDateTimeEdit);
       
   102     d->q_ptr = this;
       
   103 
       
   104     d->init();
       
   105 }
       
   106 
       
   107 /*!
       
   108     Destructor.
       
   109  */
       
   110 HbDateTimeEdit::~HbDateTimeEdit ()
       
   111 {
       
   112 }
       
   113 
       
   114 /*!
       
   115     \reimp
       
   116  */
       
   117 int HbDateTimeEdit::type () const
       
   118 {
       
   119     return Type;
       
   120 }
       
   121 
       
   122 /*!
       
   123     Sets the current value.
       
   124 
       
   125     \sa dateTime()
       
   126  */
       
   127 void HbDateTimeEdit::setDateTime (const QDateTime& dateTime)
       
   128 {
       
   129     Q_D(HbDateTimeEdit);
       
   130     d->dateTime = dateTime;
       
   131     d->isSetDateTimeActive = true;
       
   132     setPlainText(dateTime.toString(d->validator->displayFormat()));
       
   133     d->isSetDateTimeActive = false;
       
   134 }
       
   135 
       
   136 /*!
       
   137     Returns the current value as a QDateTime.
       
   138 
       
   139     /sa setDateTime()
       
   140  */
       
   141 QDateTime HbDateTimeEdit::dateTime () const
       
   142 {
       
   143     Q_D(const HbDateTimeEdit);
       
   144     return d->dateTime;
       
   145 }
       
   146 
       
   147 /*!
       
   148     Returns the current display format.
       
   149 
       
   150     /sa setDisplayFormat() and QDateTime::toString
       
   151  */
       
   152 QString HbDateTimeEdit::displayFormat () const
       
   153 {
       
   154     Q_D(const HbDateTimeEdit);
       
   155     return d->validator->displayFormat();
       
   156 }
       
   157 
       
   158 /*!
       
   159     Sets editor to display date and/or time in custom \a format.
       
   160 
       
   161     \sa displayFormat()
       
   162     \sa QDateTime::toString
       
   163  */
       
   164 void HbDateTimeEdit::setDisplayFormat (const QString& format)
       
   165 {
       
   166     Q_D(HbDateTimeEdit);
       
   167 
       
   168     QString suportedFormat = format;
       
   169 
       
   170     static const char KNotSupportedDayLongNameFormat[] = "dddd";
       
   171     static const char KNotSupportedDayShortNameFormat[] = "ddd";
       
   172     static const char KNotSupportedMonthLongNameFormat[] = "MMMM";
       
   173     static const char KNotSupportedMonthShortNameFormat[] = "MMM";
       
   174     static const char KSupportedMonthFormat[] = "MM";
       
   175 
       
   176     suportedFormat.remove(KNotSupportedDayLongNameFormat);
       
   177     suportedFormat.remove(KNotSupportedDayShortNameFormat);
       
   178 
       
   179     suportedFormat.replace(KNotSupportedMonthLongNameFormat, KSupportedMonthFormat);
       
   180     suportedFormat.replace(KNotSupportedMonthShortNameFormat, KSupportedMonthFormat);
       
   181 
       
   182     if(suportedFormat!=format) {
       
   183         qWarning() << "Names of days and months are not suported. Replacing format string: "
       
   184                 << format << " by a format string: "
       
   185                 << suportedFormat;
       
   186     } else {
       
   187         // to save memory
       
   188         suportedFormat = format;
       
   189     }
       
   190     d->validator->setDisplayFormat(suportedFormat);
       
   191     setPlainText(d->dateTime.toString(d->validator->displayFormat()));
       
   192 }
       
   193 
       
   194 /*!
       
   195     Sets editor to display date in format described by \a locale
       
   196 
       
   197     \sa QLocale::dateFormat
       
   198  */
       
   199 void HbDateTimeEdit::setDateFormat(const QLocale& locale)
       
   200 {
       
   201     setDisplayFormat(locale.dateFormat(QLocale::ShortFormat));
       
   202 }
       
   203 
       
   204 /*!
       
   205     Sets editor to display time in format described by \a locale
       
   206 
       
   207     \sa QLocale::timeFormat
       
   208  */
       
   209 void HbDateTimeEdit::setTimeFormat(const QLocale& locale)
       
   210 {
       
   211     setDisplayFormat(locale.timeFormat(QLocale::ShortFormat));
       
   212 }
       
   213 
       
   214 /*!
       
   215     Sets editor to display date and time in format described by \a locale
       
   216 
       
   217     \sa QLocale::dateTimeFormat
       
   218  */
       
   219 void HbDateTimeEdit::setDateTimeFormat(const QLocale& locale)
       
   220 {
       
   221     setDisplayFormat(locale.dateTimeFormat(QLocale::ShortFormat));
       
   222 }
       
   223 
       
   224 
       
   225 void HbDateTimeEdit::setMaxLength (int length)
       
   226 {
       
   227     HbLineEdit::setMaxLength(length);
       
   228 }
       
   229 
       
   230 void HbDateTimeEdit::setText (const QString &text)
       
   231 {
       
   232     HbLineEdit::setText(text);
       
   233 }
       
   234 
       
   235 /*!
       
   236     \reimp
       
   237 */
       
   238 bool HbDateTimeEdit::event(QEvent* event)
       
   239 {
       
   240     Q_D(HbDateTimeEdit);
       
   241 
       
   242     if (event->type() == HbEvent::InputMethodFocusOut) {
       
   243         d->validator->fixDate(&d->cursor, true);
       
   244     }
       
   245 
       
   246     return HbLineEdit::event(event);
       
   247 }
       
   248