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