⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qdatetimeedit.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <math.h>#include <private/qabstractspinbox_p.h>#include <private/qdatetime_p.h>#include <qabstractspinbox.h>#include <qapplication.h>#include <qdatetimeedit.h>#include <qdebug.h>#include <qevent.h>#include <qlineedit.h>#include <qlocale.h>#include <qset.h>#ifndef QT_NO_DATETIMEEDIT//#define QDATETIMEEDIT_QDTEDEBUG#ifdef QDATETIMEEDIT_QDTEDEBUG#  define QDTEDEBUG qDebug() << QString("%1:%2").arg(__FILE__).arg(__LINE__)#  define QDTEDEBUGN qDebug#else#  define QDTEDEBUG if (false) qDebug()#  define QDTEDEBUGN if (false) qDebug#endifclass QDateTimeEditPrivate : public QAbstractSpinBoxPrivate, public QDateTimeParser{    Q_DECLARE_PUBLIC(QDateTimeEdit)public:    QDateTimeEditPrivate();    void readLocaleSettings();    void emitSignals(EmitPolicy ep, const QVariant &old);    QString textFromValue(const QVariant &f) const;    QVariant valueFromText(const QString &f) const;    virtual void _q_editorCursorPositionChanged(int oldpos, int newpos);    QVariant validateAndInterpret(QString &input, int &, QValidator::State &state, bool fixup = false) const;    QVariant valueForPosition(int pos) const;    void clearSection(int index);    QString displayText() const { return edit->displayText(); }    int absoluteIndex(QDateTimeEdit::Section s, int index) const;    int absoluteIndex(const SectionNode &s) const;    void updateEdit();    QVariant stepBy(int index, int steps, bool test = false) const;    int sectionAt(int pos) const;    int closestSection(int index, bool forward) const;    int nextPrevSection(int index, bool forward) const;    void setSelected(int index, bool forward = false);    void updateCache(const QVariant &val, const QString &str) const;    QVariant getMinimum() const { return minimum; }    QVariant getMaximum() const { return maximum; }    QString valueToText(const QVariant &var) const { return textFromValue(var); }    QString getAmPmText(AmPm ap, Case cs) const;    bool isRightToLeft() const { return qApp->layoutDirection() == Qt::RightToLeft; }    static QDateTimeEdit::Sections convertSections(QDateTimeParser::Sections s);    static QDateTimeEdit::Section convertToPublic(QDateTimeParser::Section s);    QDateTimeEdit::Sections sections;    mutable bool cacheGuard;    QString defaultDateFormat, defaultTimeFormat;    Qt::LayoutDirection layoutDirection;    mutable QVariant conflictGuard;};// --- QDateTimeEdit ---/*!  \class QDateTimeEdit qdatetimeedit.h  \brief The QDateTimeEdit class provides a widget for editing dates and times.  \ingroup basic  \mainclass  QDateTimeEdit allows the user to edit dates by using the keyboard or  the arrow keys to increase and decrease date and time values. The  arrow keys can be used to move from section to section within the  QDateTimeEdit box. Dates and times appear in accordance with the  format set; see setDisplayFormat().  \code  QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate());  dateEdit->setMinimumDate(QDate::currentDate().addDays(-365));  dateEdit->setMaximumDate(QDate::currentDate().addDays(365));  dateEdit->setDisplayFormat("yyyy.MM.dd");  \endcode  Here we've created a new QDateTimeEdit object initialized with  today's date, and restricted the valid date range to today plus or  minus 365 days. We've set the order to month, day, year.  The maximum and minimum values for a date value in the date editor  default to the maximum and minimum values for a QDate. You can  change this by calling setMinimumDate(), setMaximumDate(),  setMinimumTime(), and setMaximumTime().  \table 100%  \row \o \inlineimage windowsxp-datetimeedit.png Screenshot of a Windows XP style date time editing widget       \o A date time editing widget shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}.  \row \o \inlineimage macintosh-datetimeedit.png Screenshot of a Macintosh style date time editing widget       \o A date time editing widget shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.  \row \o \inlineimage plastique-datetimeedit.png Screenshot of a Plastique style date time editing widget       \o A date time editing widget shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}.  \endtable  \sa QDateEdit, QTimeEdit, QDate, QTime*//*!  \enum QDateTimeEdit::Section  \value NoSection  \value AmPmSection  \value MSecSection  \value SecondSection  \value MinuteSection  \value HourSection  \value DaySection  \value MonthSection  \value YearSection  \omitvalue DateSections_Mask  \omitvalue TimeSections_Mask*//*!  \fn void QDateTimeEdit::dateTimeChanged(const QDateTime &datetime)  This signal is emitted whenever the date or time is changed. The  new date and time is passed in \a datetime.*//*!  \fn void QDateTimeEdit::timeChanged(const QTime &time)  This signal is emitted whenever the time is changed. The new time  is passed in \a time.*//*!  \fn void QDateTimeEdit::dateChanged(const QDate &date)  This signal is emitted whenever the date is changed. The new date  is passed in \a date.*//*!  Constructs an empty date time editor with a \a parent.*/QDateTimeEdit::QDateTimeEdit(QWidget *parent)    : QAbstractSpinBox(*new QDateTimeEditPrivate, parent){    Q_D(QDateTimeEdit);    d->minimum = QVariant(QDATETIME_MIN);    d->maximum = QVariant(QDATETIME_MAX);    d->value = QVariant(QDateTime(QDATE_INITIAL, QTIME_MIN));    setDisplayFormat(d->defaultDateFormat + QLatin1String(" ") + d->defaultTimeFormat);}/*!  Constructs an empty date time editor with a \a parent. The value  is set to \a datetime.*/QDateTimeEdit::QDateTimeEdit(const QDateTime &datetime, QWidget *parent)    : QAbstractSpinBox(*new QDateTimeEditPrivate, parent){    Q_D(QDateTimeEdit);    d->minimum = QVariant(QDATETIME_MIN);    d->maximum = QVariant(QDATETIME_MAX);    d->value = datetime.isValid() ? QVariant(datetime) : QVariant(QDateTime(QDATE_INITIAL, QTIME_MIN));    setDisplayFormat(d->defaultDateFormat + QLatin1String(" ") + d->defaultTimeFormat);}/*!  \fn QDateTimeEdit::QDateTimeEdit(const QDate &date, QWidget *parent)  Constructs an empty date time editor with a \a parent.  The value is set to \a date.*/QDateTimeEdit::QDateTimeEdit(const QDate &date, QWidget *parent)    : QAbstractSpinBox(*new QDateTimeEditPrivate, parent){    Q_D(QDateTimeEdit);    d->minimum = QVariant(QDATETIME_MIN);    d->maximum = QVariant(QDATETIME_MAX);    d->value = QVariant(QDateTime(date.isValid() ? date : QDATE_INITIAL, QTIME_MIN));    setDisplayFormat(d->defaultDateFormat);}/*!  \fn QDateTimeEdit::QDateTimeEdit(const QTime &time, QWidget *parent)  Constructs an empty date time editor with a \a parent.  The value is set to \a time.*/QDateTimeEdit::QDateTimeEdit(const QTime &time, QWidget *parent)    : QAbstractSpinBox(*new QDateTimeEditPrivate, parent){    Q_D(QDateTimeEdit);    d->minimum = QVariant(QDATETIME_MIN);    d->maximum = QVariant(QDATETIME_MAX);    d->value = QVariant(QDateTime(QDATE_INITIAL, time.isValid() ? time : QTIME_MIN));    setDisplayFormat(d->defaultTimeFormat);    if (d->displayFormat.isEmpty()) {        d->defaultDateFormat = QLatin1String("hh:mm:ss");        setDisplayFormat(d->defaultTimeFormat);    }}QDateTime QDateTimeEdit::dateTime() const{    Q_D(const QDateTimeEdit);    return d->value.toDateTime();}void QDateTimeEdit::setDateTime(const QDateTime &datetime){    Q_D(QDateTimeEdit);    if (datetime.isValid()) {        d->cachedDay = -1;        d->clearCache();        d->setValue(QVariant(datetime), EmitIfChanged);    }}/*!  \property QDateTimeEdit::date  \brief the QDate that is set in the QDateTimeEdit  \sa time*/QDate QDateTimeEdit::date() const{    Q_D(const QDateTimeEdit);    return d->value.toDate();}void QDateTimeEdit::setDate(const QDate &date){    Q_D(QDateTimeEdit);    if (date.isValid()) {        d->cachedDay = -1;        d->clearCache();        d->setValue(QVariant(QDateTime(date, d->value.toTime())), EmitIfChanged);    }}/*!  \property QDateTimeEdit::time  \brief the QTime that is set in the QDateTimeEdit  \sa date*/QTime QDateTimeEdit::time() const{    Q_D(const QDateTimeEdit);    return d->value.toTime();}void QDateTimeEdit::setTime(const QTime &time){    Q_D(QDateTimeEdit);    if (time.isValid()) {        d->clearCache();        d->cachedDay = -1;        d->setValue(QVariant(QDateTime(d->value.toDate(), time)), EmitIfChanged);    }}/*!  \property QDateTimeEdit::dateTime  \brief the QDateTime that is set in the QDateTimeEdit  \sa minimumDate, minimumTime, maximumDate, maximumTime*//*!  \property QDateTimeEdit::minimumDate  \brief the minimum date of the date time edit  When setting this property the \l maximumDate is adjusted if  necessary, to ensure that the range remains valid. If the date is  not a valid QDate object, this function does nothing.  \sa minimumTime, maximumTime, setDateRange()*/QDate QDateTimeEdit::minimumDate() const{    Q_D(const QDateTimeEdit);    return d->minimum.toDate();}void QDateTimeEdit::setMinimumDate(const QDate &min){    Q_D(QDateTimeEdit);    if (min.isValid()) {        const QVariant m(QDateTime(min, d->minimum.toTime()));        d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));    }}void QDateTimeEdit::clearMinimumDate(){    setMinimumDate(QDATE_MIN);}/*!  \property QDateTimeEdit::maximumDate  \brief the maximum date of the date time edit  When setting this property the \l minimumDate is adjusted if  necessary to ensure that the range remains valid. If the date is  not a valid QDate object, this function does nothing.  \sa minimumDate, minimumTime, maximumTime, setDateRange()*/QDate QDateTimeEdit::maximumDate() const{    Q_D(const QDateTimeEdit);    return d->maximum.toDate();}void QDateTimeEdit::setMaximumDate(const QDate &max){    Q_D(QDateTimeEdit);    if (max.isValid()) {        const QVariant m(QDateTime(max, d->maximum.toTime()));        d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m);    }}void QDateTimeEdit::clearMaximumDate(){    setMaximumDate(QDATE_MAX);}/*!  \property QDateTimeEdit::minimumTime  \brief the minimum time of the date time edit  When setting this property the \l maximumTime is adjusted if  necessary, to ensure that the range remains valid. If the time is  not a valid QTime object, this function does nothing.  \sa maximumTime, minimumDate, maximumDate, setTimeRange()*/QTime QDateTimeEdit::minimumTime() const{    Q_D(const QDateTimeEdit);    return d->minimum.toTime();}void QDateTimeEdit::setMinimumTime(const QTime &min){    Q_D(QDateTimeEdit);    if (min.isValid()) {        const QVariant m(QDateTime(d->minimum.toDate(), min));        d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));    }}void QDateTimeEdit::clearMinimumTime(){    setMinimumTime(QTIME_MIN);}/*!  \property QDateTimeEdit::maximumTime  \brief the maximum time of the date time edit  When setting this property, the \l minimumTime is adjusted if  necessary to ensure that the range remains valid. If the time is  not a valid QTime object, this function does nothing.  \sa minimumTime, minimumDate, maximumDate, setTimeRange()*/QTime QDateTimeEdit::maximumTime() const{    Q_D(const QDateTimeEdit);    return d->maximum.toTime();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -