diff options
Diffstat (limited to 'src/widgets/widgets/qcalendarwidget.cpp')
-rw-r--r-- | src/widgets/widgets/qcalendarwidget.cpp | 210 |
1 files changed, 92 insertions, 118 deletions
diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index fa3dbc1f32..2150fc7a50 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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. ** ** $QT_END_LICENSE$ @@ -65,6 +65,11 @@ enum { namespace { +static QString formatNumber(int number, int fieldWidth) +{ + return QString::number(number).rightJustified(fieldWidth, QLatin1Char('0')); +} + class QCalendarDateSectionValidator { public: @@ -86,16 +91,16 @@ public: QLocale m_locale; protected: - QString highlightString(const QString &str, int pos) const; + static QString highlightString(const QString &str, int pos); private: }; -QString QCalendarDateSectionValidator::highlightString(const QString &str, int pos) const +QString QCalendarDateSectionValidator::highlightString(const QString &str, int pos) { if (pos == 0) return QLatin1String("<b>") + str + QLatin1String("</b>"); int startPos = str.length() - pos; - return str.mid(0, startPos) + QLatin1String("<b>") + str.mid(startPos, pos) + QLatin1String("</b>"); + return str.midRef(0, startPos) + QLatin1String("<b>") + str.midRef(startPos, pos) + QLatin1String("</b>"); } @@ -104,11 +109,11 @@ class QCalendarDayValidator : public QCalendarDateSectionValidator public: QCalendarDayValidator(); - virtual Section handleKey(int key); - virtual QDate applyToDate(const QDate &date) const; - virtual void setDate(const QDate &date); - virtual QString text() const; - virtual QString text(const QDate &date, int repeat) const; + virtual Section handleKey(int key) Q_DECL_OVERRIDE; + virtual QDate applyToDate(const QDate &date) const Q_DECL_OVERRIDE; + virtual void setDate(const QDate &date) Q_DECL_OVERRIDE; + virtual QString text() const Q_DECL_OVERRIDE; + virtual QString text(const QDate &date, int repeat) const Q_DECL_OVERRIDE; private: int m_pos; int m_day; @@ -189,11 +194,7 @@ void QCalendarDayValidator::setDate(const QDate &date) QString QCalendarDayValidator::text() const { - QString str; - if (m_day / 10 == 0) - str += QLatin1Char('0'); - str += QString::number(m_day); - return highlightString(str, m_pos); + return highlightString(formatNumber(m_day, 2), m_pos); } QString QCalendarDayValidator::text(const QDate &date, int repeat) const @@ -201,10 +202,7 @@ QString QCalendarDayValidator::text(const QDate &date, int repeat) const if (repeat <= 1) { return QString::number(date.day()); } else if (repeat == 2) { - QString str; - if (date.day() / 10 == 0) - str += QLatin1Char('0'); - return str + QString::number(date.day()); + return formatNumber(date.day(), 2); } else if (repeat == 3) { return m_locale.dayName(date.dayOfWeek(), QLocale::ShortFormat); } else if (repeat >= 4) { @@ -220,11 +218,11 @@ class QCalendarMonthValidator : public QCalendarDateSectionValidator public: QCalendarMonthValidator(); - virtual Section handleKey(int key); - virtual QDate applyToDate(const QDate &date) const; - virtual void setDate(const QDate &date); - virtual QString text() const; - virtual QString text(const QDate &date, int repeat) const; + virtual Section handleKey(int key) Q_DECL_OVERRIDE; + virtual QDate applyToDate(const QDate &date) const Q_DECL_OVERRIDE; + virtual void setDate(const QDate &date) Q_DECL_OVERRIDE; + virtual QString text() const Q_DECL_OVERRIDE; + virtual QString text(const QDate &date, int repeat) const Q_DECL_OVERRIDE; private: int m_pos; int m_month; @@ -307,11 +305,7 @@ void QCalendarMonthValidator::setDate(const QDate &date) QString QCalendarMonthValidator::text() const { - QString str; - if (m_month / 10 == 0) - str += QLatin1Char('0'); - str += QString::number(m_month); - return highlightString(str, m_pos); + return highlightString(formatNumber(m_month, 2), m_pos); } QString QCalendarMonthValidator::text(const QDate &date, int repeat) const @@ -319,10 +313,7 @@ QString QCalendarMonthValidator::text(const QDate &date, int repeat) const if (repeat <= 1) { return QString::number(date.month()); } else if (repeat == 2) { - QString str; - if (date.month() / 10 == 0) - str += QLatin1Char('0'); - return str + QString::number(date.month()); + return formatNumber(date.month(), 2); } else if (repeat == 3) { return m_locale.standaloneMonthName(date.month(), QLocale::ShortFormat); } else /*if (repeat >= 4)*/ { @@ -337,11 +328,11 @@ class QCalendarYearValidator : public QCalendarDateSectionValidator public: QCalendarYearValidator(); - virtual Section handleKey(int key); - virtual QDate applyToDate(const QDate &date) const; - virtual void setDate(const QDate &date); - virtual QString text() const; - virtual QString text(const QDate &date, int repeat) const; + virtual Section handleKey(int key) Q_DECL_OVERRIDE; + virtual QDate applyToDate(const QDate &date) const Q_DECL_OVERRIDE; + virtual void setDate(const QDate &date) Q_DECL_OVERRIDE; + virtual QString text() const Q_DECL_OVERRIDE; + virtual QString text(const QDate &date, int repeat) const Q_DECL_OVERRIDE; private: int pow10(int n); int m_pos; @@ -420,26 +411,13 @@ void QCalendarYearValidator::setDate(const QDate &date) QString QCalendarYearValidator::text() const { - QString str; - int pow = 10; - for (int i = 0; i < 3; i++) { - if (m_year / pow == 0) - str += QLatin1Char('0'); - pow *= 10; - } - str += QString::number(m_year); - return highlightString(str, m_pos); + return highlightString(formatNumber(m_year, 4), m_pos); } QString QCalendarYearValidator::text(const QDate &date, int repeat) const { - if (repeat < 4) { - QString str; - int year = date.year() % 100; - if (year / 10 == 0) - str = QLatin1Char('0'); - return str + QString::number(year); - } + if (repeat < 4) + return formatNumber(date.year() % 100, 2); return QString::number(date.year()); } @@ -476,9 +454,9 @@ private: QStringList m_separators; QList<SectionToken *> m_tokens; - QCalendarDateSectionValidator *m_yearValidator; - QCalendarDateSectionValidator *m_monthValidator; - QCalendarDateSectionValidator *m_dayValidator; + QCalendarYearValidator m_yearValidator; + QCalendarMonthValidator m_monthValidator; + QCalendarDayValidator m_dayValidator; SectionToken *m_currentToken; @@ -489,26 +467,22 @@ private: }; QCalendarDateValidator::QCalendarDateValidator() - : m_currentToken(0), m_lastSectionMove(QCalendarDateSectionValidator::ThisSection) + : m_currentToken(Q_NULLPTR), + m_initialDate(QDate::currentDate()), + m_currentDate(m_initialDate), + m_lastSectionMove(QCalendarDateSectionValidator::ThisSection) { - m_initialDate = m_currentDate = QDate::currentDate(); - m_yearValidator = new QCalendarYearValidator(); - m_monthValidator = new QCalendarMonthValidator(); - m_dayValidator = new QCalendarDayValidator(); } void QCalendarDateValidator::setLocale(const QLocale &locale) { - m_yearValidator->m_locale = locale; - m_monthValidator->m_locale = locale; - m_dayValidator->m_locale = locale; + m_yearValidator.m_locale = locale; + m_monthValidator.m_locale = locale; + m_dayValidator.m_locale = locale; } QCalendarDateValidator::~QCalendarDateValidator() { - delete m_yearValidator; - delete m_monthValidator; - delete m_dayValidator; clear(); } @@ -525,9 +499,9 @@ int QCalendarDateValidator::countRepeat(const QString &str, int index) const void QCalendarDateValidator::setInitialDate(const QDate &date) { - m_yearValidator->setDate(date); - m_monthValidator->setDate(date); - m_dayValidator->setDate(date); + m_yearValidator.setDate(date); + m_monthValidator.setDate(date); + m_dayValidator.setDate(date); m_initialDate = date; m_currentDate = date; m_lastSectionMove = QCalendarDateSectionValidator::ThisSection; @@ -587,13 +561,13 @@ void QCalendarDateValidator::setFormat(const QString &format) SectionToken *token = 0; if (nextChar == QLatin1Char('d')) { offset = qMin(4, countRepeat(format, pos)); - token = new SectionToken(m_dayValidator, offset); + token = new SectionToken(&m_dayValidator, offset); } else if (nextChar == QLatin1Char('M')) { offset = qMin(4, countRepeat(format, pos)); - token = new SectionToken(m_monthValidator, offset); + token = new SectionToken(&m_monthValidator, offset); } else if (nextChar == QLatin1Char('y')) { offset = qMin(4, countRepeat(format, pos)); - token = new SectionToken(m_yearValidator, offset); + token = new SectionToken(&m_yearValidator, offset); } else { separator += nextChar; } @@ -614,9 +588,9 @@ void QCalendarDateValidator::setFormat(const QString &format) void QCalendarDateValidator::applyToDate() { - m_currentDate = m_yearValidator->applyToDate(m_currentDate); - m_currentDate = m_monthValidator->applyToDate(m_currentDate); - m_currentDate = m_dayValidator->applyToDate(m_currentDate); + m_currentDate = m_yearValidator.applyToDate(m_currentDate); + m_currentDate = m_monthValidator.applyToDate(m_currentDate); + m_currentDate = m_dayValidator.applyToDate(m_currentDate); } void QCalendarDateValidator::toNextToken() @@ -683,8 +657,8 @@ public: QDate date() const; void setDate(const QDate &date); - bool eventFilter(QObject *o, QEvent *e); - void timerEvent(QTimerEvent *e); + bool eventFilter(QObject *o, QEvent *e) Q_DECL_OVERRIDE; + void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE; signals: void dateChanged(const QDate &date); @@ -853,32 +827,32 @@ class QCalendarModel : public QAbstractTableModel public: QCalendarModel(QObject *parent = 0); - int rowCount(const QModelIndex &) const + int rowCount(const QModelIndex &) const Q_DECL_OVERRIDE { return RowCount + m_firstRow; } - int columnCount(const QModelIndex &) const + int columnCount(const QModelIndex &) const Q_DECL_OVERRIDE { return ColumnCount + m_firstColumn; } - QVariant data(const QModelIndex &index, int role) const; - Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; + Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) + bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE { beginInsertRows(parent, row, row + count - 1); endInsertRows(); return true; } - bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) + bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE { beginInsertColumns(parent, column, column + count - 1); endInsertColumns(); return true; } - bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) + bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE { beginRemoveRows(parent, row, row + count - 1); endRemoveRows(); return true; } - bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) + bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE { beginRemoveColumns(parent, column, column + count - 1); endRemoveColumns(); @@ -939,7 +913,7 @@ public: void internalUpdate() { updateGeometries(); } void setReadOnly(bool enable); - virtual void keyboardSearch(const QString & search) { Q_UNUSED(search) } + virtual void keyboardSearch(const QString & search) Q_DECL_OVERRIDE { Q_UNUSED(search) } signals: void showDate(const QDate &date); @@ -947,16 +921,16 @@ signals: void clicked(const QDate &date); void editingFinished(); protected: - QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers); - void mouseDoubleClickEvent(QMouseEvent *event); - void mousePressEvent(QMouseEvent *event); - void mouseMoveEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); + QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE; + void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; #ifndef QT_NO_WHEELEVENT - void wheelEvent(QWheelEvent *event); + void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE; #endif - void keyPressEvent(QKeyEvent *event); - bool event(QEvent *event); + void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; + bool event(QEvent *event) Q_DECL_OVERRIDE; QDate handleMouseEvent(QMouseEvent *event); public: @@ -969,19 +943,19 @@ private: }; QCalendarModel::QCalendarModel(QObject *parent) - : QAbstractTableModel(parent) -{ - m_date = QDate::currentDate(); - m_minimumDate = QDate::fromJulianDay(1); - m_maximumDate = QDate(7999, 12, 31); - m_shownYear = m_date.year(); - m_shownMonth = m_date.month(); - m_firstDay = QLocale().firstDayOfWeek(); - m_horizontalHeaderFormat = QCalendarWidget::ShortDayNames; - m_weekNumbersShown = true; - m_firstColumn = 1; - m_firstRow = 1; - m_view = 0; + : QAbstractTableModel(parent), + m_firstColumn(1), + m_firstRow(1), + m_date(QDate::currentDate()), + m_minimumDate(QDate::fromJulianDay(1)), + m_maximumDate(7999, 12, 31), + m_shownYear(m_date.year()), + m_shownMonth(m_date.month()), + m_firstDay(QLocale().firstDayOfWeek()), + m_horizontalHeaderFormat(QCalendarWidget::ShortDayNames), + m_weekNumbersShown(true), + m_view(Q_NULLPTR) +{ } Qt::DayOfWeek QCalendarModel::dayOfWeekForColumn(int column) const @@ -1555,7 +1529,7 @@ public: : QItemDelegate(parent), calendarWidgetPrivate(w) { } virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, - const QModelIndex &index) const; + const QModelIndex &index) const Q_DECL_OVERRIDE; void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const; private: @@ -1571,11 +1545,11 @@ public: : QToolButton(parent) { } protected: - void paintEvent(QPaintEvent *e) + void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE { Q_UNUSED(e) -#ifndef Q_WS_MAC +#ifndef Q_DEAD_CODE_FROM_QT4_MAC QStyleOptionToolButton opt; initStyleOption(&opt); @@ -1599,7 +1573,7 @@ class QPrevNextCalButton : public QToolButton public: QPrevNextCalButton(QWidget *parent) : QToolButton(parent) {} protected: - void paintEvent(QPaintEvent *) { + void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE { QStylePainter painter(this); QStyleOptionToolButton opt; initStyleOption(&opt); |