📄 qdatetimeedit.cpp
字号:
const char space = ' '; int cursorPos = edit->cursorPosition(); bool blocked = edit->blockSignals(true); QString t = edit->text(); const int pos = sectionPos(index); if (pos == -1) { qWarning("%s:%d this is unexpected", __FILE__, __LINE__); return; } const int size = sectionSize(index); t.replace(pos, size, QString().fill(space, size)); edit->setText(t); edit->setCursorPosition(cursorPos); QDTEDEBUG << cursorPos; edit->blockSignals(blocked);}/*! \internal updates the cached values*/void QDateTimeEditPrivate::updateCache(const QVariant &val, const QString &str) const{ if (val != cachedValue || str != cachedText || cacheGuard) { cacheGuard = true; QString copy = str; int unused = edit->cursorPosition(); QValidator::State unusedState; validateAndInterpret(copy, unused, unusedState); cacheGuard = false; }}/*! \internal parses and validates \a input*/QVariant QDateTimeEditPrivate::validateAndInterpret(QString &input, int &/*position*/, QValidator::State &state, bool fixup) const{ if (input.isEmpty()) { state = QValidator::Invalid; return getZeroVariant(); } else if (cachedText == input && !fixup) { state = cachedState; return cachedValue; } if (!specialValueText.isEmpty() && input == specialValueText) { state = QValidator::Acceptable; return minimum; } StateNode tmp = parse(input, value, fixup); input = tmp.input; state = *reinterpret_cast<QValidator::State *>(&tmp.state); if (state == QValidator::Acceptable) { if (tmp.conflicts && conflictGuard != tmp.value) { conflictGuard = tmp.value; clearCache(); input = textFromValue(tmp.value); updateCache(tmp.value, input); conflictGuard.clear(); } else { cachedText = input; cachedState = state; cachedValue = tmp.value; } } else { clearCache(); } return (tmp.value.isNull() ? getZeroVariant() : tmp.value);}/*! \internal \reimp*/QString QDateTimeEditPrivate::textFromValue(const QVariant &f) const{ Q_Q(const QDateTimeEdit); return q->textFromDateTime(f.toDateTime());}/*! \internal \reimp*/QVariant QDateTimeEditPrivate::valueFromText(const QString &f) const{ Q_Q(const QDateTimeEdit); return QVariant(q->dateTimeFromText(f));}/*! \internal Internal function called by QDateTimeEdit::stepBy(). Also takes a Section for which section to step on and a bool \a test for whether or not to modify the internal cachedDay variable. This is necessary because the function is called from the const function QDateTimeEdit::stepEnabled() as well as QDateTimeEdit::stepBy().*/QVariant QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) const{ Q_Q(const QDateTimeEdit); QVariant v = value; QString str = displayText(); int pos = edit->cursorPosition(); const SectionNode sn = sectionNode(sectionIndex); int val; // to make sure it behaves reasonably when typing something and then stepping in non-tracking mode if (!test && pendingEmit) { if (q->validate(str, pos) != QValidator::Acceptable) { v = value; } else { v = valueFromText(str); } val = getDigit(v, sn.type); } else { val = getDigit(value, sn.type); } val += steps; const int min = absoluteMin(sectionIndex); const int max = absoluteMax(sectionIndex); if (val < min) { val = (wrapping ? max - (min - val) + 1 : min); } else if (val > max) { val = (wrapping ? min + val - max - 1 : max); } const int tmp = v.toDate().day(); setDigit(v, sn.type, val); // if this sets year or month it will make // sure that days are lowered if needed. // changing one section should only modify that section, if possible if (sn.type != AmPmSection && (variantCompare(v, minimum) < 0) || (variantCompare(v, maximum) > 0)) { const int localmin = getDigit(minimum, sn.type); const int localmax = getDigit(maximum, sn.type); if (wrapping) { // just because we hit the roof in one direction, it // doesn't mean that we hit the floor in the other if (steps > 0) { setDigit(v, sn.type, min); if (sn.type != DaySection && sections & DateSectionMask) { int daysInMonth = v.toDate().daysInMonth(); if (v.toDate().day() < tmp && v.toDate().day() < daysInMonth) setDigit(v, DaySection, qMin(tmp, daysInMonth)); } if (variantCompare(v, minimum) < 0) { setDigit(v, sn.type, localmin); if (variantCompare(v, minimum) < 0) setDigit(v, sn.type, localmin + 1); } } else { setDigit(v, sn.type, max); if (sn.type != DaySection && sections & DateSectionMask) { int daysInMonth = v.toDate().daysInMonth(); if (v.toDate().day() < tmp && v.toDate().day() < daysInMonth) setDigit(v, DaySection, qMin(tmp, daysInMonth)); } if (variantCompare(v, maximum) > 0) { setDigit(v, sn.type, localmax); if (variantCompare(v, maximum) > 0) setDigit(v, sn.type, localmax - 1); } } } else { setDigit(v, sn.type, (steps > 0 ? localmax : localmin)); } } if (!test && tmp != v.toDate().day() && sn.type != DaySection) { // this should not happen when called from stepEnabled cachedDay = qMax<int>(tmp, cachedDay); } if (variantCompare(v, minimum) < 0) { if (wrapping) { QVariant t = v; setDigit(t, sn.type, steps < 0 ? max : min); int mincmp = variantCompare(t, minimum); int maxcmp = variantCompare(t, maximum); if (mincmp >= 0 && maxcmp <= 0) { v = t; } else { setDigit(t, sn.type, getDigit(steps < 0 ? maximum : minimum, sn.type)); mincmp = variantCompare(t, minimum); maxcmp = variantCompare(t, maximum); if (mincmp >= 0 && maxcmp <= 0) { v = t; } } } else { v = value; } } else if (variantCompare(v, maximum) > 0) { if (wrapping) { QVariant t = v; setDigit(t, sn.type, steps > 0 ? min : max); int mincmp = variantCompare(t, minimum); int maxcmp = variantCompare(t, maximum); if (mincmp >= 0 && maxcmp <= 0) { v = t; } else { setDigit(t, sn.type, getDigit(steps > 0 ? minimum : maximum, sn.type)); mincmp = variantCompare(t, minimum); maxcmp = variantCompare(t, maximum); if (mincmp >= 0 && maxcmp <= 0) { v = t; } } } else { v = value; } } const QVariant ret = bound(v, value, steps);// if (!test) {// clearCache();// updateCache(ret, textFromValue(ret));// } return ret;}/*! \internal \reimp*/void QDateTimeEditPrivate::emitSignals(EmitPolicy ep, const QVariant &old){ Q_Q(QDateTimeEdit); if (ep == NeverEmit) { return; } pendingEmit = false; const bool dodate = value.toDate().isValid() && (sections & DateSectionMask); const bool datechanged = (ep == AlwaysEmit || old.toDate() != value.toDate()); const bool dotime = value.toTime().isValid() && (sections & TimeSectionMask); const bool timechanged = (ep == AlwaysEmit || old.toTime() != value.toTime()); updateCache(value, displayText()); if (dodate && dotime && (datechanged || timechanged)) emit q->dateTimeChanged(value.toDateTime()); if (dodate && datechanged) emit q->dateChanged(value.toDate()); if (dotime && timechanged) emit q->timeChanged(value.toTime());}/*! \internal \reimp*/void QDateTimeEditPrivate::_q_editorCursorPositionChanged(int oldpos, int newpos){ Q_Q(QDateTimeEdit); if (ignoreCursorPositionChanged || specialValue()) return; updateCache(value, displayText()); const bool allowChange = !edit->hasSelectedText(); const bool forward = oldpos <= newpos; ignoreCursorPositionChanged = true; int s = sectionAt(newpos); if (s == NoSectionIndex && forward && newpos > 0) { s = sectionAt(newpos - 1); } int c = newpos; const int selstart = edit->selectionStart(); const int selSection = sectionAt(selstart); const int l = selSection != -1 ? sectionSize(selSection) : 0; if (s == NoSectionIndex) { if (l > 0 && selstart == sectionPos(selSection) && edit->selectedText().size() == l) { s = selSection; if (allowChange) setSelected(selSection, true); c = -1; } else { int closest = closestSection(newpos, forward); c = sectionPos(closest) + (forward ? 0 : qMax<int>(0, sectionSize(closest))); if (allowChange) { edit->setCursorPosition(c); QDTEDEBUG << c; } s = closest; } } if (allowChange && currentSectionIndex != s) { QString tmp = displayText(); int pos = edit->cursorPosition(); if (q->validate(tmp, pos) != QValidator::Acceptable) { interpret(EmitIfChanged); if (c == -1) { setSelected(s, true); } else { edit->setCursorPosition(pos); } } updateButtons(); } QDTEDEBUG << "currentSectionIndex is set to" << sectionName(sectionType(s)) << oldpos << newpos << "was" << sectionName(sectionType(currentSectionIndex)); currentSectionIndex = s; if (currentSectionIndex >= sectionNodes.size()) qFatal("%d currentSectionIndex >= sectionNodes.size()) %d %d", __LINE__, currentSectionIndex, sectionNodes.size()); ignoreCursorPositionChanged = false;}/*! \internal Try to get the format from the local settings*/void QDateTimeEditPrivate::readLocaleSettings(){ const QLocale loc; defaultTimeFormat = loc.timeFormat(QLocale::ShortFormat); defaultDateFormat = loc.dateFormat(QLocale::ShortFormat);}QDateTimeEdit::Section QDateTimeEditPrivate::convertToPublic(QDateTimeParser::Section s){ switch (s & ~Internal) { case AmPmSection: return QDateTimeEdit::AmPmSection; case MSecSection: return QDateTimeEdit::MSecSection; case SecondSection: return QDateTimeEdit::SecondSection; case MinuteSection: return QDateTimeEdit::MinuteSection; case DaySection: return QDateTimeEdit::DaySection; case MonthSection: return QDateTimeEdit::MonthSection; case YearSection: return QDateTimeEdit::YearSection; case Hour12Section: case Hour24Section: return QDateTimeEdit::HourSection; case FirstSection: case NoSection: case LastSection: break; } return QDateTimeEdit::NoSection;}QDateTimeEdit::Sections QDateTimeEditPrivate::convertSections(QDateTimeParser::Sections s){ QDateTimeEdit::Sections ret = 0; if (s & QDateTimeParser::MSecSection) ret |= QDateTimeEdit::MSecSection; if (s & QDateTimeParser::SecondSection) ret |= QDateTimeEdit::SecondSection; if (s & QDateTimeParser::MinuteSection) ret |= QDateTimeEdit::MinuteSection; if (s & (QDateTimeParser::Hour24Section|QDateTimeParser::Hour12Section)) ret |= QDateTimeEdit::HourSection; if (s & QDateTimeParser::AmPmSection) ret |= QDateTimeEdit::AmPmSection; if (s & QDateTimeParser::DaySection) ret |= QDateTimeEdit::DaySection; if (s & QDateTimeParser::MonthSection) ret |= QDateTimeEdit::MonthSection; if (s & QDateTimeParser::YearSection) ret |= QDateTimeEdit::YearSection; return ret;}QString QDateTimeEditPrivate::getAmPmText(AmPm ap, Case cs) const{ if (ap == AmText) { return (cs == UpperCase ? QDateTimeEdit::tr("AM") : QDateTimeEdit::tr("am")); } else { return (cs == UpperCase ? QDateTimeEdit::tr("PM") : QDateTimeEdit::tr("pm")); }}int QDateTimeEditPrivate::absoluteIndex(QDateTimeEdit::Section s, int index) const{ for (int i=0; i<sectionNodes.size(); ++i) { if (convertToPublic(sectionNodes.at(i).type) == s && index-- == 0) { return i; } } return NoSectionIndex;}int QDateTimeEditPrivate::absoluteIndex(const SectionNode &s) const{ return sectionNodes.indexOf(s);}#include "moc_qdatetimeedit.cpp"#endif // QT_NO_DATETIMEEDIT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -