From 802c31bdbd4cd8d9a94bbd02ad1955d648682be7 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Tue, 5 Jul 2016 12:36:58 +0300 Subject: [PATCH] QDateTimeEdit: de-duplicate calls and cache results Change-Id: I32162846f5f412c7563e66015ea371f9a1af7748 Reviewed-by: Edward Welbourne --- src/widgets/widgets/qdatetimeedit.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 795f2cedd9..b168c98410 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -248,9 +248,10 @@ void QDateTimeEdit::setDateTime(const QDateTime &datetime) Q_D(QDateTimeEdit); if (datetime.isValid()) { d->clearCache(); + const QDate date = datetime.date(); if (!(d->sections & DateSections_Mask)) - setDateRange(datetime.date(), datetime.date()); - d->setValue(QDateTime(datetime.date(), datetime.time(), d->spec), EmitIfChanged); + setDateRange(date, date); + d->setValue(QDateTime(date, datetime.time(), d->spec), EmitIfChanged); } } @@ -1770,15 +1771,18 @@ void QDateTimeEditPrivate::setSelected(int sectionIndex, bool forward) int QDateTimeEditPrivate::sectionAt(int pos) const { - if (pos < separators.first().size()) { + if (pos < separators.first().size()) return (pos == 0 ? FirstSectionIndex : NoSectionIndex); - } else if (displayText().size() - pos < separators.last().size() + 1) { + + const QString text = displayText(); + const int textSize = text.size(); + if (textSize - pos < separators.last().size() + 1) { if (separators.last().size() == 0) { return sectionNodes.count() - 1; } - return (pos == displayText().size() ? LastSectionIndex : NoSectionIndex); + return (pos == textSize ? LastSectionIndex : NoSectionIndex); } - updateCache(value, displayText()); + updateCache(value, text); for (int i=0; i= 0); - if (pos < separators.first().size()) { + if (pos < separators.first().size()) return forward ? 0 : FirstSectionIndex; - } else if (displayText().size() - pos < separators.last().size() + 1) { + + const QString text = displayText(); + if (text.size() - pos < separators.last().size() + 1) return forward ? LastSectionIndex : sectionNodes.size() - 1; - } - updateCache(value, displayText()); + + updateCache(value, text); for (int i=0; i