📄 q3datetimeedit.cpp
字号:
if (max.isValid()) d->max = max;}/*! Sets the separator to \a s. Note that currently only the first character of \a s is used.*/void Q3DateEdit::setSeparator(const QString& s){ d->ed->setSeparator(s);}/*! Returns the editor's separator.*/QString Q3DateEdit::separator() const{ return d->ed->separator();}/*! Enables/disables the push buttons according to the min/max date for this widget.*/void Q3DateEdit::updateButtons(){ if (!isEnabled()) return; bool upEnabled = date() < maxValue(); bool downEnabled = date() > minValue(); d->controls->setUpEnabled(upEnabled); d->controls->setDownEnabled(downEnabled);}/*! \reimp */void Q3DateEdit::resizeEvent(QResizeEvent *){ d->controls->resize(width(), height());}/*! \reimp*/QSize Q3DateEdit::sizeHint() const{ ensurePolished(); QFontMetrics fm(font()); int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this); int h = qMax(fm.lineSpacing(), 14) + 2; int w = 2 + fm.width(QLatin1Char('9')) * 8 + fm.width(d->ed->separator()) * 2 + d->controls->upRect().width() + fw * 4; return QSize(w, qMax(h + fw * 2,20)).expandedTo(QApplication::globalStrut());}/*! \reimp*/QSize Q3DateEdit::minimumSizeHint() const{ return sizeHint();}/*! Returns the formatted number for section \a sec. This will correspond to either the year, month or day section, depending on the current display order. \sa setOrder()*/QString Q3DateEdit::sectionFormattedText(int sec){ QString txt; txt = sectionText(sec); if (d->typing && sec == d->ed->focusSection()) d->ed->setSectionSelection(sec, sectionOffsetEnd(sec) - txt.length(), sectionOffsetEnd(sec)); else d->ed->setSectionSelection(sec, sectionOffsetEnd(sec) - sectionLength(sec), sectionOffsetEnd(sec)); txt = txt.rightJustified(sectionLength(sec), QDATETIMEEDIT_HIDDEN_CHAR); return txt;}/*! Returns the desired length (number of digits) of section \a sec. This will correspond to either the year, month or day section, depending on the current display order. \sa setOrder()*/int Q3DateEdit::sectionLength(int sec) const{ int val = 0; if (sec == d->yearSection) { val = 4; } else if (sec == d->monthSection) { val = 2; } else if (sec == d->daySection) { val = 2; } return val;}/*! Returns the text of section \a sec. This will correspond to either the year, month or day section, depending on the current display order. \sa setOrder()*/QString Q3DateEdit::sectionText(int sec) const{ int val = 0; if (sec == d->yearSection) { val = d->y; } else if (sec == d->monthSection) { val = d->m; } else if (sec == d->daySection) { val = d->d; } return QString::number(val);}/*! \internal Returns the end of the section offset \a sec.*/int Q3DateEdit::sectionOffsetEnd(int sec) const{ if (sec == d->yearSection) { switch(d->ord) { case DMY: case MDY: return sectionOffsetEnd(sec-1) + separator().length() + sectionLength(sec); case YMD: case YDM: return sectionLength(sec); } } else if (sec == d->monthSection) { switch(d->ord) { case DMY: case YDM: case YMD: return sectionOffsetEnd(sec-1) + separator().length() + sectionLength(sec); case MDY: return sectionLength(sec); } } else if (sec == d->daySection) { switch(d->ord) { case DMY: return sectionLength(sec); case YMD: case MDY: case YDM: return sectionOffsetEnd(sec-1) + separator().length() + sectionLength(sec); } } return 0;}/*! \property Q3DateEdit::order \brief the order in which the year, month and day appear The default order is locale dependent. \sa Order*/void Q3DateEdit::setOrder(Q3DateEdit::Order order){ d->ord = order; switch(d->ord) { case DMY: d->yearSection = 2; d->monthSection = 1; d->daySection = 0; break; case MDY: d->yearSection = 2; d->monthSection = 0; d->daySection = 1; break; case YMD: d->yearSection = 0; d->monthSection = 1; d->daySection = 2; break; case YDM: d->yearSection = 0; d->monthSection = 2; d->daySection = 1; break; } if (isVisible()) d->ed->repaint(d->ed->rect());}Q3DateEdit::Order Q3DateEdit::order() const{ return d->ord;}/*! \reimp*/void Q3DateEdit::stepUp(){ int sec = d->ed->focusSection(); bool accepted = false; if (sec == d->yearSection) { if (!outOfRange(d->y+1, d->m, d->d)) { accepted = true; setYear(d->y+1); } } else if (sec == d->monthSection) { if (!outOfRange(d->y, d->m+1, d->d)) { accepted = true; setMonth(d->m+1); } } else if (sec == d->daySection) { if (!outOfRange(d->y, d->m, d->d+1)) { accepted = true; setDay(d->d+1); } } if (accepted) { d->changed = false; emit valueChanged(date()); } d->ed->repaint(d->ed->rect());}/*! \reimp*/void Q3DateEdit::stepDown(){ int sec = d->ed->focusSection(); bool accepted = false; if (sec == d->yearSection) { if (!outOfRange(d->y-1, d->m, d->d)) { accepted = true; setYear(d->y-1); } } else if (sec == d->monthSection) { if (!outOfRange(d->y, d->m-1, d->d)) { accepted = true; setMonth(d->m-1); } } else if (sec == d->daySection) { if (!outOfRange(d->y, d->m, d->d-1)) { accepted = true; setDay(d->d-1); } } if (accepted) { d->changed = false; emit valueChanged(date()); } d->ed->repaint(d->ed->rect());}/*! Sets the year to \a year, which must be a valid year. The range currently supported is from 1752 to 8000. \sa QDate*/void Q3DateEdit::setYear(int year){ if (year < 1752) year = 1752; if (year > 8000) year = 8000; if (!outOfRange(year, d->m, d->d)) { d->y = year; setMonth(d->m); int tmp = d->dayCache; setDay(d->dayCache); d->dayCache = tmp; }}/*! Sets the month to \a month, which must be a valid month, i.e. between 1 and 12.*/void Q3DateEdit::setMonth(int month){ if (month < 1) month = 1; if (month > 12) month = 12; if (!outOfRange(d->y, month, d->d)) { d->m = month; int tmp = d->dayCache; setDay(d->dayCache); d->dayCache = tmp; }}/*! Sets the day to \a day, which must be a valid day. The function will ensure that the \a day set is valid for the month and year.*/void Q3DateEdit::setDay(int day){ if (day < 1) day = 1; if (day > 31) day = 31; if (d->m > 0 && d->y > 1752) { while (!QDate::isValid(d->y, d->m, day)) --day; if (!outOfRange(d->y, d->m, day)) d->d = day; } else if (d->m > 0) { if (day > 0 && day < 32) { if (!outOfRange(d->y, d->m, day)) d->d = day; } } d->dayCache = d->d;}/*! \property Q3DateEdit::date \brief the editor's date value. If the date property is not valid, the editor displays all zeroes and Q3DateEdit::date() will return an invalid date. It is strongly recommended that the editor is given a default date value (e.g. currentDate()). That way, attempts to set the date property to an invalid date will fail. When changing the date property, if the date is less than minValue(), or is greater than maxValue(), nothing happens.*/void Q3DateEdit::setDate(const QDate& date){ if (!date.isValid()) { d->y = 0; d->m = 0; d->d = 0; d->dayCache = 0; } else { if (date > maxValue() || date < minValue()) return; d->y = date.year(); d->m = date.month(); d->d = date.day(); d->dayCache = d->d; emit valueChanged(date); } d->changed = false; d->ed->repaint(d->ed->rect());}QDate Q3DateEdit::date() const{ if (QDate::isValid(d->y, d->m, d->d)) return QDate(d->y, d->m, d->d); return QDate();}/*! \internal Returns true if \a y, \a m, \a d is out of range, otherwise returns false. \sa setRange()*/bool Q3DateEdit::outOfRange(int y, int m, int d) const{ if (QDate::isValid(y, m, d)) { QDate currentDate(y, m, d); if (currentDate > maxValue() || currentDate < minValue()) { //## outOfRange should set overwrite? return true; } return false; } return false; /* assume ok */}/*! \reimp*/void Q3DateEdit::addNumber(int sec, int num){ if (sec == -1) return; if (d->timerId) killTimer(d->timerId); d->timerId = 0; bool overwrite = false; bool accepted = false; d->typing = true; QString txt; if (sec == d->yearSection) { txt = QString::number(d->y); if (d->overwrite || txt.length() == 4) { accepted = true; d->y = num; } else { txt += QString::number(num); if (txt.length() == 4 ) { const int val = qBound(1792, txt.toInt(), 8000); if (outOfRange(val, d->m, d->d)) { txt = QString::number(d->y); } else { accepted = true; d->y = val; } } else { accepted = true; d->y = txt.toInt(); } if (d->adv && txt.length() == 4) { d->ed->setFocusSection(d->ed->focusSection()+1); overwrite = true; } } } else if (sec == d->monthSection) { txt = QString::number(d->m); if (d->overwrite || txt.length() == 2) { accepted = true; d->m = num; } else { txt += QString::number(num); int temp = txt.toInt(); if (temp > 12) temp = num; if (outOfRange(d->y, temp, d->d)) txt = QString::number(d->m); else { accepted = true; d->m = temp; } if (d->adv && txt.length() == 2) { d->ed->setFocusSection(d->ed->focusSection()+1); overwrite = true; } } } else if (sec == d->daySection) { txt = QString::number(d->d); if (d->overwrite || txt.length() == 2) { accepted = true; d->d = num; d->dayCache = d->d; } else { txt += QString::number(num); int temp = txt.toInt(); if (temp > 31) temp = num; if (outOfRange(d->y, d->m, temp)) txt = QString::number(d->d); else { accepted = true; d->d = temp; d->dayCache = d->d; } if (d->adv && txt.length() == 2) { d->ed->setFocusSection(d->ed->focusSection()+1); overwrite = true; } } } if (accepted) { d->changed = false; emit valueChanged(date()); } d->overwrite = overwrite; d->timerId = startTimer(qApp->doubleClickInterval()*4); d->ed->repaint(d->ed->rect());}/*! \reimp*/bool Q3DateEdit::setFocusSection(int s){ if (s != d->ed->focusSection()) { if (d->timerId) killTimer(d->timerId); d->timerId = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -