summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@nokia.com>2012-08-21 14:46:42 +0200
committerQt by Nokia <qt-info@nokia.com>2012-09-05 21:06:39 +0200
commit2b562b756401da02017ec937fd2716dd6b9863d3 (patch)
tree3578f0a9ead68655887b7b2617ca1aa0695671fc /src/corelib
parentaba8de1bee8bbb9bc6b92e761a3dda0d829a211e (diff)
Fix 3 digit day being displayed by QDateEdit.
When a QDateEdit has its display format set to "yyyy/MM/dd", its day set to 31 and its month set to 2, it will display 291 as the day until the cursor is moved or the focus changed. This is because QDateTimeParser::parse calls sectionSize() for the day section, which will sometimes return an incorrect size. There are also other display formats affected by this bug (e.g. long day names). For example, (in the context of sectionSize()) when text is "2000/01/31" and displayText() is "2000/2/31", there is a difference between displayText() and text - text is the previous value and displayText() is the new value. The size difference is always due to leading zeroes. This patch makes QDateTimeParser keep track of the quantity of zeroes added to each section and then factors this value into the result of sectionSize() if there is a size difference between text and displayText(). Task-number: QTBUG-26847 Change-Id: I3823cc41167ec920f742cb6a20d39fc5f433c915 Reviewed-by: Mitch Curtis <mitch.curtis@nokia.com> Reviewed-by: Gabriel de Dietrich <gabriel.dietrich-de@nokia.com> Reviewed-by: aavit <qt_aavit@ovi.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qdatetime.cpp37
-rw-r--r--src/corelib/tools/qdatetime_p.h4
2 files changed, 32 insertions, 9 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index f0e2b3735c..421e6db81e 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -4492,7 +4492,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
case 'h':
if (parserType != QVariant::Date) {
const Section hour = (sect == 'h') ? Hour12Section : Hour24Section;
- const SectionNode sn = { hour, i - add, countRepeat(newFormat, i, 2) };
+ const SectionNode sn = { hour, i - add, countRepeat(newFormat, i, 2), 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;
@@ -4502,7 +4502,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
break;
case 'm':
if (parserType != QVariant::Date) {
- const SectionNode sn = { MinuteSection, i - add, countRepeat(newFormat, i, 2) };
+ const SectionNode sn = { MinuteSection, i - add, countRepeat(newFormat, i, 2), 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;
@@ -4512,7 +4512,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
break;
case 's':
if (parserType != QVariant::Date) {
- const SectionNode sn = { SecondSection, i - add, countRepeat(newFormat, i, 2) };
+ const SectionNode sn = { SecondSection, i - add, countRepeat(newFormat, i, 2), 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;
@@ -4523,7 +4523,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
case 'z':
if (parserType != QVariant::Date) {
- const SectionNode sn = { MSecSection, i - add, countRepeat(newFormat, i, 3) < 3 ? 1 : 3 };
+ const SectionNode sn = { MSecSection, i - add, countRepeat(newFormat, i, 3) < 3 ? 1 : 3, 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;
@@ -4535,7 +4535,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
case 'a':
if (parserType != QVariant::Date) {
const bool cap = (sect == 'A');
- const SectionNode sn = { AmPmSection, i - add, (cap ? 1 : 0) };
+ const SectionNode sn = { AmPmSection, i - add, (cap ? 1 : 0), 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
newDisplay |= AmPmSection;
@@ -4551,7 +4551,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
const int repeat = countRepeat(newFormat, i, 4);
if (repeat >= 2) {
const SectionNode sn = { repeat == 4 ? YearSection : YearSection2Digits,
- i - add, repeat == 4 ? 4 : 2 };
+ i - add, repeat == 4 ? 4 : 2, 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;
@@ -4562,7 +4562,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
break;
case 'M':
if (parserType != QVariant::Time) {
- const SectionNode sn = { MonthSection, i - add, countRepeat(newFormat, i, 4) };
+ const SectionNode sn = { MonthSection, i - add, countRepeat(newFormat, i, 4), 0 };
newSectionNodes.append(sn);
newSeparators.append(unquote(newFormat.mid(index, i - index)));
i += sn.count - 1;
@@ -4573,7 +4573,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
case 'd':
if (parserType != QVariant::Time) {
const int repeat = countRepeat(newFormat, i, 4);
- const SectionNode sn = { repeat >= 3 ? DayOfWeekSection : DaySection, i - add, repeat };
+ const SectionNode sn = { repeat >= 3 ? DayOfWeekSection : DaySection, i - add, repeat, 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;
@@ -4637,8 +4637,26 @@ int QDateTimeParser::sectionSize(int sectionIndex) const
qWarning("QDateTimeParser::sectionSize Internal error (%d)", sectionIndex);
return -1;
}
+
if (sectionIndex == sectionNodes.size() - 1) {
- return displayText().size() - sectionPos(sectionIndex) - separators.last().size();
+ // In some cases there is a difference between displayText() and text.
+ // e.g. when text is 2000/01/31 and displayText() is "2000/2/31" - text
+ // is the previous value and displayText() is the new value.
+ // The size difference is always due to leading zeroes.
+ int sizeAdjustment = 0;
+ if (displayText().size() != text.size()) {
+ // Any zeroes added before this section will affect our size.
+ int preceedingZeroesAdded = 0;
+ if (sectionNodes.size() > 1 && context == DateTimeEdit) {
+ for (QVector<SectionNode>::ConstIterator sectionIt = sectionNodes.constBegin();
+ sectionIt != sectionNodes.constBegin() + sectionIndex; ++sectionIt) {
+ preceedingZeroesAdded += sectionIt->zeroesAdded;
+ }
+ }
+ sizeAdjustment = preceedingZeroesAdded;
+ }
+
+ return displayText().size() + sizeAdjustment - sectionPos(sectionIndex) - separators.last().size();
} else {
return sectionPos(sectionIndex + 1) - sectionPos(sectionIndex)
- separators.at(sectionIndex + 1).size();
@@ -4926,6 +4944,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
text.insert(index, QString().fill(QLatin1Char('0'), missingZeroes));
used = sectionmaxsize;
cursorPosition += missingZeroes;
+ ++(const_cast<QDateTimeParser*>(this)->sectionNodes[sectionIndex].zeroesAdded);
} else {
state = Intermediate;;
}
diff --git a/src/corelib/tools/qdatetime_p.h b/src/corelib/tools/qdatetime_p.h
index 3861a731d9..ce8be86b09 100644
--- a/src/corelib/tools/qdatetime_p.h
+++ b/src/corelib/tools/qdatetime_p.h
@@ -120,12 +120,15 @@ public:
first.type = FirstSection;
first.pos = -1;
first.count = -1;
+ first.zeroesAdded = 0;
last.type = FirstSection;
last.pos = -1;
last.count = -1;
+ last.zeroesAdded = 0;
none.type = NoSection;
none.pos = -1;
none.count = -1;
+ none.zeroesAdded = 0;
}
virtual ~QDateTimeParser() {}
enum {
@@ -168,6 +171,7 @@ public:
Section type;
mutable int pos;
int count;
+ int zeroesAdded;
};
enum State { // duplicated from QValidator