diff options
Diffstat (limited to 'src/widgets/widgets/qdatetimeedit.cpp')
-rw-r--r-- | src/widgets/widgets/qdatetimeedit.cpp | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 96a37197e9..54094de765 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -1,31 +1,37 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -46,6 +52,8 @@ #include <qset.h> #include <qstyle.h> +#include <algorithm> + #ifndef QT_NO_DATETIMEEDIT //#define QDATETIMEEDIT_QDTEDEBUG @@ -240,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); } } @@ -760,17 +769,17 @@ QCalendarWidget *QDateTimeEdit::calendarWidget() const void QDateTimeEdit::setCalendarWidget(QCalendarWidget *calendarWidget) { Q_D(QDateTimeEdit); - if (!calendarWidget) { + if (Q_UNLIKELY(!calendarWidget)) { qWarning("QDateTimeEdit::setCalendarWidget: Cannot set a null calendar widget"); return; } - if (!d->calendarPopup) { + if (Q_UNLIKELY(!d->calendarPopup)) { qWarning("QDateTimeEdit::setCalendarWidget: calendarPopup is set to false"); return; } - if (!(d->display & QDateTimeParser::DateSectionMask)) { + if (Q_UNLIKELY(!(d->display & QDateTimeParser::DateSectionMask))) { qWarning("QDateTimeEdit::setCalendarWidget: no date sections specified"); return; } @@ -855,14 +864,6 @@ QString QDateTimeEdit::displayFormat() const return isRightToLeft() ? d->unreversedFormat : d->displayFormat; } -template<typename C> static inline C reverse(const C &l) -{ - C ret; - for (int i=l.size() - 1; i>=0; --i) - ret.append(l.at(i)); - return ret; -} - void QDateTimeEdit::setDisplayFormat(const QString &format) { Q_D(QDateTimeEdit); @@ -876,8 +877,8 @@ void QDateTimeEdit::setDisplayFormat(const QString &format) d->displayFormat += d->sectionNode(i).format(); } d->displayFormat += d->separators.at(0); - d->separators = reverse(d->separators); - d->sectionNodes = reverse(d->sectionNodes); + std::reverse(d->separators.begin(), d->separators.end()); + std::reverse(d->sectionNodes.begin(), d->sectionNodes.end()); } d->formatExplicitlySet = true; @@ -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<sectionNodes.size(); ++i) { const int tmp = sectionPos(i); @@ -1799,12 +1803,14 @@ int QDateTimeEditPrivate::sectionAt(int pos) const int QDateTimeEditPrivate::closestSection(int pos, bool forward) const { Q_ASSERT(pos >= 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<sectionNodes.size(); ++i) { const int tmp = sectionPos(sectionNodes.at(i)); if (pos < tmp + sectionSize(i)) { @@ -1863,7 +1869,7 @@ void QDateTimeEditPrivate::clearSection(int index) const QSignalBlocker blocker(edit); QString t = edit->text(); const int pos = sectionPos(index); - if (pos == -1) { + if (Q_UNLIKELY(pos == -1)) { qWarning("QDateTimeEdit: Internal error (%s:%d)", __FILE__, __LINE__); return; } |