diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-09-25 18:07:32 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-10-03 11:33:06 +0000 |
commit | 15693121bcb5214a30e823c59e9683fb8a536318 (patch) | |
tree | 60889ad41b2be181d75ecbe54563b617169da2fd /src/imports/calendar | |
parent | 3ac0236c501eefcb832da5e68b604ca59eec8b5d (diff) |
Calendar: validate month & year ranges
Fixes also all Calendar types to use 0-based month.
Change-Id: Iee3cafc00e0338b8076fbb3fe195b8491752cbc1
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
Diffstat (limited to 'src/imports/calendar')
-rw-r--r-- | src/imports/calendar/qquickcalendarview.cpp | 19 | ||||
-rw-r--r-- | src/imports/calendar/qquickmonthmodel.cpp | 2 | ||||
-rw-r--r-- | src/imports/calendar/qquickweeknumbercolumn.cpp | 19 | ||||
-rw-r--r-- | src/imports/calendar/qquickweeknumbermodel.cpp | 32 |
4 files changed, 50 insertions, 22 deletions
diff --git a/src/imports/calendar/qquickcalendarview.cpp b/src/imports/calendar/qquickcalendarview.cpp index 705ec8f7..7c7c54e9 100644 --- a/src/imports/calendar/qquickcalendarview.cpp +++ b/src/imports/calendar/qquickcalendarview.cpp @@ -40,6 +40,7 @@ #include <QtGui/qstylehints.h> #include <QtGui/qguiapplication.h> #include <QtLabsTemplates/private/qquickcontrol_p_p.h> +#include <QtQml/qqmlinfo.h> QT_BEGIN_NAMESPACE @@ -180,23 +181,33 @@ QQuickCalendarView::QQuickCalendarView(QQuickItem *parent) : \qmlproperty int Qt.labs.calendar::CalendarView::month This property holds the number of the month. + + The value must be in the range from \c 0 (January) to \c 11 (December). The default + value is the current month. */ int QQuickCalendarView::month() const { Q_D(const QQuickCalendarView); - return d->model->month(); + return d->model->month() - 1; } void QQuickCalendarView::setMonth(int month) { Q_D(QQuickCalendarView); - d->model->setMonth(month); + if (month < 0 || month > 11) { + qmlInfo(this) << "month " << month << " is out of range [0...11]"; + return; + } + d->model->setMonth(month + 1); } /*! \qmlproperty int Qt.labs.calendar::CalendarView::year This property holds the number of the year. + + The value must be in the range from \c -271820 to \c 275759. The default + value is the current year. */ int QQuickCalendarView::year() const { @@ -207,6 +218,10 @@ int QQuickCalendarView::year() const void QQuickCalendarView::setYear(int year) { Q_D(QQuickCalendarView); + if (year < -271820 || year > 275759) { + qmlInfo(this) << "year " << year << " is out of range [-271820...275759]"; + return; + } d->model->setYear(year); } diff --git a/src/imports/calendar/qquickmonthmodel.cpp b/src/imports/calendar/qquickmonthmodel.cpp index 7188bc00..cdf5656d 100644 --- a/src/imports/calendar/qquickmonthmodel.cpp +++ b/src/imports/calendar/qquickmonthmodel.cpp @@ -194,7 +194,7 @@ QVariant QQuickMonthModel::data(const QModelIndex &index, int role) const case WeekNumberRole: return date.weekNumber(); case MonthRole: - return date.month(); + return date.month() - 1; case YearRole: return date.year(); default: diff --git a/src/imports/calendar/qquickweeknumbercolumn.cpp b/src/imports/calendar/qquickweeknumbercolumn.cpp index 21540607..34e4bd52 100644 --- a/src/imports/calendar/qquickweeknumbercolumn.cpp +++ b/src/imports/calendar/qquickweeknumbercolumn.cpp @@ -38,6 +38,7 @@ #include "qquickweeknumbermodel_p.h" #include <QtLabsTemplates/private/qquickcontrol_p_p.h> +#include <QtQml/qqmlinfo.h> QT_BEGIN_NAMESPACE @@ -108,23 +109,33 @@ QQuickWeekNumberColumn::QQuickWeekNumberColumn(QQuickItem *parent) : \qmlproperty int Qt.labs.calendar::WeekNumberColumn::month This property holds the number of the month that the week numbers are calculated for. + + The value must be in the range from \c 0 (January) to \c 11 (December). The default + value is the current month. */ int QQuickWeekNumberColumn::month() const { Q_D(const QQuickWeekNumberColumn); - return d->model->month(); + return d->model->month() - 1; } void QQuickWeekNumberColumn::setMonth(int month) { Q_D(QQuickWeekNumberColumn); - d->model->setMonth(month); + if (month < 0 || month > 11) { + qmlInfo(this) << "month " << month << " is out of range [0...11]"; + return; + } + d->model->setMonth(month + 1); } /*! \qmlproperty int Qt.labs.calendar::WeekNumberColumn::year This property holds the number of the year that the week numbers are calculated for. + + The value must be in the range from \c -271820 to \c 275759. The default + value is the current year. */ int QQuickWeekNumberColumn::year() const { @@ -135,6 +146,10 @@ int QQuickWeekNumberColumn::year() const void QQuickWeekNumberColumn::setYear(int year) { Q_D(QQuickWeekNumberColumn); + if (year < -271820 || year > 275759) { + qmlInfo(this) << "year " << year << " is out of range [-271820...275759]"; + return; + } d->model->setYear(year); } diff --git a/src/imports/calendar/qquickweeknumbermodel.cpp b/src/imports/calendar/qquickweeknumbermodel.cpp index f6a0b014..4ff6674d 100644 --- a/src/imports/calendar/qquickweeknumbermodel.cpp +++ b/src/imports/calendar/qquickweeknumbermodel.cpp @@ -46,21 +46,21 @@ class QQuickWeekNumberModelPrivate : public QAbstractItemModelPrivate Q_DECLARE_PUBLIC(QQuickWeekNumberModel) public: - QQuickWeekNumberModelPrivate() + QQuickWeekNumberModelPrivate() : month(-1), year(-1) { QDate date = QDate::currentDate(); + init(date.month(), date.year(), locale); month = date.month(); year = date.year(); - first = calculateFirst(month, year, locale); } void init(int month, int year, const QLocale &locale = QLocale()); - static int calculateFirst(int month, int year, const QLocale &locale); + static QDate calculateFirst(int month, int year, const QLocale &locale); int month; int year; - int first; QLocale locale; + int weekNumbers[6]; }; void QQuickWeekNumberModelPrivate::init(int m, int y, const QLocale &l) @@ -69,21 +69,19 @@ void QQuickWeekNumberModelPrivate::init(int m, int y, const QLocale &l) if (m == month && y == year && l.firstDayOfWeek() == locale.firstDayOfWeek()) return; - first = calculateFirst(m, y, l); - - emit q->dataChanged(q->index(0, 0), q->index(5, 0)); -} - -int QQuickWeekNumberModelPrivate::calculateFirst(int month, int year, const QLocale &locale) -{ // The actual first (1st) day of the month. - QDate firstDayOfMonthDate(year, month, 1); - int difference = ((firstDayOfMonthDate.dayOfWeek() - locale.firstDayOfWeek()) + 7) % 7; + QDate firstDayOfMonthDate(y, m, 1); + int difference = ((firstDayOfMonthDate.dayOfWeek() - l.firstDayOfWeek()) + 7) % 7; // The first day to display should never be the 1st of the month, as we want some days from // the previous month to be visible. if (difference == 0) difference += 7; - return firstDayOfMonthDate.addDays(-difference).weekNumber(); + + for (int i = 0; i < 6; ++i) + weekNumbers[i] = firstDayOfMonthDate.addDays(i * 7 - difference).weekNumber(); + + if (q) // null at construction + emit q->dataChanged(q->index(0, 0), q->index(5, 0)); } QQuickWeekNumberModel::QQuickWeekNumberModel(QObject *parent) : @@ -144,15 +142,15 @@ int QQuickWeekNumberModel::weekNumberAt(int index) const Q_D(const QQuickWeekNumberModel); if (index < 0 || index > 5) return -1; - return d->first + index; + return d->weekNumbers[index]; } int QQuickWeekNumberModel::indexOf(int weekNumber) const { Q_D(const QQuickWeekNumberModel); - if (weekNumber < d->first || weekNumber > d->first + 6) + if (weekNumber < d->weekNumbers[0] || weekNumber > d->weekNumbers[5]) return -1; - return weekNumber - d->first; + return weekNumber - d->weekNumbers[0]; } QVariant QQuickWeekNumberModel::data(const QModelIndex &index, int role) const |