📄 q3datetimeedit.cpp
字号:
d->overwrite = true; d->typing = false; fix(); // will emit valueChanged if necessary } return d->ed->setFocusSection(s);}/*! Attempts to fix any invalid date entries. The rules applied are as follows: \list \i If the year has four digits it is left unchanged. \i If the year has two digits, the year will be changed to four digits in the range current year - 70 to current year + 29. \i If the year has three digits in the range 100..999, the current millennium, i.e. 2000, will be added giving a year in the range 2100..2999. \i If the day or month is 0 then it will be set to 1 or the minimum valid day/month in the range. \endlist*/void Q3DateEdit::fix(){ bool changed = false; int currentYear = QDate::currentDate().year(); int year = d->y; if (year < 100) { int currentCentury = currentYear / 100; year += currentCentury * 100; if (currentYear > year) { if (currentYear > year + 70) year += 100; } else { if (year >= currentYear + 30) year -= 100; } changed = true; } else if (year < 1000) { int currentMillennium = currentYear / 10; year += currentMillennium * 10; changed = true; } else if (d->d == 0) { d->d = 1; changed = true; } else if (d->m == 0) { d->m = 1; changed = true; } if (outOfRange(year, d->m, d->d)) { if (minValue().isValid() && date() < minValue()) { d->d = minValue().day(); d->dayCache = d->d; d->m = minValue().month(); d->y = minValue().year(); } if (date() > maxValue()) { d->d = maxValue().day(); d->dayCache = d->d; d->m = maxValue().month(); d->y = maxValue().year(); } changed = true; } else if (changed) setYear(year); if (changed) { emit valueChanged(date()); d->changed = false; }}/*! \reimp*/bool Q3DateEdit::event(QEvent *e){ if(e->type() == QEvent::FocusOut) { d->typing = false; fix(); // the following can't be done in fix() because fix() called // from all over the place and it will break the old behaviour if (!QDate::isValid(d->y, d->m, d->d)) { d->dayCache = d->d; int i = d->d; for (; i > 0; i--) { d->d = i; if (QDate::isValid(d->y, d->m, d->d)) break; } d->changed = true; } if (d->changed) { emit valueChanged(date()); d->changed = false; } } else if (e->type() == QEvent::LocaleChange) { readLocaleSettings(); d->ed->setSeparator(localDateSep()); setOrder(localOrder()); } return Q3DateTimeEditBase::event(e);}/*! \internal Function which is called whenever the user tries to remove the first number from \a sec by pressing the backspace key.*/void Q3DateEdit::removeFirstNumber(int sec){ if (sec == -1) return; QString txt; if (sec == d->yearSection) { txt = QString::number(d->y); txt = txt.mid(1, txt.length()) + QLatin1Char('0'); d->y = txt.toInt(); } else if (sec == d->monthSection) { txt = QString::number(d->m); txt = txt.mid(1, txt.length()) + QLatin1Char('0'); d->m = txt.toInt(); } else if (sec == d->daySection) { txt = QString::number(d->d); txt = txt.mid(1, txt.length()) + QLatin1Char('0'); d->d = txt.toInt(); d->dayCache = d->d; } d->ed->repaint(d->ed->rect());}/*! \reimp*/void Q3DateEdit::removeLastNumber(int sec){ if (sec == -1) return; QString txt; if (sec == d->yearSection) { txt = QString::number(d->y); txt = txt.mid(0, txt.length()-1); d->y = txt.toInt(); } else if (sec == d->monthSection) { txt = QString::number(d->m); txt = txt.mid(0, txt.length()-1); d->m = txt.toInt(); } else if (sec == d->daySection) { txt = QString::number(d->d); txt = txt.mid(0, txt.length()-1); d->d = txt.toInt(); d->dayCache = d->d; } d->ed->repaint(d->ed->rect());}/*! \property Q3DateEdit::autoAdvance \brief whether the editor automatically advances to the next section If autoAdvance is true, the editor will automatically advance focus to the next date section if a user has completed a section. The default is false.*/void Q3DateEdit::setAutoAdvance(bool advance){ d->adv = advance;}bool Q3DateEdit::autoAdvance() const{ return d->adv;}/*! \reimp*/void Q3DateEdit::timerEvent(QTimerEvent *){ d->overwrite = true;}/*! \fn void Q3DateEdit::valueChanged(const QDate& date) This signal is emitted whenever the editor's value changes. The \a date parameter is the new value.*////////////class Q3TimeEditPrivate{public: int h; int m; int s; uint display; bool adv; bool overwrite; int timerId; bool typing; QTime min; QTime max; bool changed; Q3DateTimeEditor *ed; Q3SpinWidget *controls;};/*! \class Q3TimeEdit q3datetimeedit.h \brief The Q3TimeEdit class provides a time editor. \compat Q3TimeEdit allows the user to edit times by using the keyboard or the arrow keys to increase/decrease time values. The arrow keys can be used to move from section to section within the Q3TimeEdit box. The user can automatically be moved to the next section once they complete a section using setAutoAdvance(). Times appear in hour, minute, second order. It is recommended that the Q3TimeEdit is initialised with a time, e.g. \code QTime timeNow = QTime::currentTime(); Q3TimeEdit *timeEdit = new Q3TimeEdit(timeNow, this); timeEdit->setRange(timeNow, timeNow.addSecs(60 * 60)); \endcode Here we've created a Q3TimeEdit widget set to the current time. We've also set the minimum value to the current time and the maximum time to one hour from now. The maximum and minimum values for a time value in the time editor default to the maximum and minimum values for a QTime. You can change this by calling setMinValue(), setMaxValue() or setRange(). Terminology: A QTimeWidget consists of three sections, one each for the hour, minute and second. You can change the separator character using setSeparator(), by default the separator is read from the system's settings. \img datetimewidgets.png Date Time Widgets \sa QTime Q3DateEdit Q3DateTimeEdit*//*! Constructs an empty time edit with parent \a parent and called \a name.*/Q3TimeEdit::Q3TimeEdit(QWidget * parent, const char * name) : Q3DateTimeEditBase(parent, name){ init();}/*! \overload Constructs a time edit with the initial time value, \a time, parent \a parent and called \a name.*/Q3TimeEdit::Q3TimeEdit(const QTime& time, QWidget * parent, const char * name) : Q3DateTimeEditBase(parent, name){ init(); setTime(time);}/*! \internal */void Q3TimeEdit::init(){ d = new Q3TimeEditPrivate(); d->controls = new QDateTimeSpinWidget(this, 0); d->ed = new Q3DateTimeEditor(this, d->controls, "time edit base"); d->controls->setEditWidget(d->ed); setFocusProxy(d->ed); connect(d->controls, SIGNAL(stepUpPressed()), SLOT(stepUp())); connect(d->controls, SIGNAL(stepDownPressed()), SLOT(stepDown())); d->ed->appendSection(QNumberSection(0,0, true, 0)); d->ed->appendSection(QNumberSection(0,0, true, 1)); d->ed->appendSection(QNumberSection(0,0, true, 2)); d->ed->setSeparator(localTimeSep()); d->h = 0; d->m = 0; d->s = 0; d->display = Hours | Minutes | Seconds; if (lAMPM) { d->display |= AMPM; d->ed->appendSection(QNumberSection(0,0, false, 3)); } d->adv = false; d->overwrite = true; d->timerId = 0; d->typing = false; d->min = QTime(0, 0, 0); d->max = QTime(23, 59, 59); d->changed = false; setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); refcount++;}/*! Destroys the object and frees any allocated resources.*/Q3TimeEdit::~Q3TimeEdit(){ delete d; if (!--refcount) cleanup();}/*! \property Q3TimeEdit::minValue \brief the minimum time value Setting the minimum time value is equivalent to calling Q3TimeEdit::setRange(\e t, maxValue()), where \e t is the minimum time. The default minimum time is 00:00:00. \sa maxValue setRange()*/QTime Q3TimeEdit::minValue() const{ return d->min;}/*! \property Q3TimeEdit::maxValue \brief the maximum time value Setting the maximum time value is equivalent to calling Q3TimeEdit::setRange(minValue(), \e t), where \e t is the maximum time. The default maximum time is 23:59:59. \sa minValue setRange()*/QTime Q3TimeEdit::maxValue() const{ return d->max;}/*! Sets the valid input range for the editor to be from \a min to \a max inclusive. If \a min is invalid no minimum time is set. Similarly, if \a max is invalid no maximum time is set.*/void Q3TimeEdit::setRange(const QTime& min, const QTime& max){ if (min.isValid()) d->min = min; if (max.isValid()) d->max = max;}/*! \property Q3TimeEdit::display \brief the sections that are displayed in the time edit The value can be any combination of the values in the Display enum. By default, the widget displays hours, minutes and seconds.*/void Q3TimeEdit::setDisplay(uint display){ if (d->display == display) return; d->ed->clearSections(); d->display = display; if (d->display & Hours) d->ed->appendSection(QNumberSection(0,0, true, 0)); if (d->display & Minutes) d->ed->appendSection(QNumberSection(0,0, true, 1)); if (d->display & Seconds) d->ed->appendSection(QNumberSection(0,0, true, 2)); if (d->display & AMPM) d->ed->appendSection(QNumberSection(0,0, false, 3)); d->ed->setFocusSection(0); d->ed->update();}uint Q3TimeEdit::display() const{ return d->display;}/*! \property Q3TimeEdit::time \brief the editor's time value. When changing the time property, if the time is less than minValue(), or is greater than maxValue(), nothing happens.*/void Q3TimeEdit::setTime(const QTime& time){ if (!time.isValid()) { d->h = 0; d->m = 0; d->s = 0; } else { if (time > maxValue() || time < minValue()) return; d->h = time.hour(); d->m = time.minute(); d->s = time.second(); emit valueChanged(time); } d->changed = false; d->ed->repaint(d->ed->rect());}QTime Q3TimeEdit::time() const{ if (QTime::isValid(d->h, d->m, d->s)) return QTime(d->h, d->m, d->s); return QTime();}/*! \property Q3TimeEdit::autoAdvance \brief whether the editor automatically advances to the next section If autoAdvance is true, the editor will automatically advance focus to the next time section if a user has completed a section. The default is false.*/void Q3TimeEdit::setAutoAdvance(bool advance){ d->adv = advance;}bool Q3TimeEdit::autoAdvance() const{ return d->adv;}/*! Sets the separator to \a s. Note that currently only the first character of \a s is used.*/void Q3TimeEdit::setSeparator(const QString& s){ d->ed->setSeparator(s);}/*! Returns the editor's separator.*/QString Q3TimeEdit::separator() const{ return d->ed->separator();}/*! \fn void Q3TimeEdit::valueChanged(const QTime& time) This signal is emitted whenever the editor's value changes. The \a time parameter is the new value.*//*! \reimp*/bool Q3TimeEdit::event(QEvent *e){ if (e->type() == QEvent::FocusOut) { d->typing = false; if (d->changed) { emit valueChanged(time()); d->changed = false; } } else if (e->type() == QEvent::LocaleChange) { readLocaleSettings(); d->ed->setSeparator(localTimeSep()); } return Q3DateTimeEditBase::event(e);}/*! \reimp*/void Q3TimeEdit::timerEvent(QTimerEvent *){ d->overwrite = true;}/*! \reimp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -