5#include <QRandomGenerator>
7class MonthModel::Private {
15MonthModel::MonthModel(
QObject *parent)
23MonthModel::~MonthModel()
32void MonthModel::setYear(
int year)
34 if (d->year ==
year) {
40 setSelected(
QDate(
year, d->selected.month(), qMin(d->selected.day(), d->calendar.daysInMonth(d->selected.month(),
year))));
48void MonthModel::setMonth(
int month)
50 if (d->month ==
month) {
56 setSelected(
QDate(d->selected.year(), d->month, qMin(d->selected.day(), d->calendar.daysInMonth(d->month, d->selected.year()))));
64void MonthModel::setSelected(
const QDate &selected)
78 for (
int i = 0; i < 7; i++) {
95 setMonth(d->calendar.monthsInYear(d->year) - 1);
97 setMonth(d->month - 1);
103 if (d->calendar.monthsInYear(d->year) == d->month) {
105 setYear(d->year + 1);
107 setMonth(d->month + 1);
114 setMonth(today.month());
115 setYear(today.year());
128 int prefix = d->calendar.dayOfWeek(
QDate(d->year, d->month, 1)) - m_locale.
firstDayOfWeek();
132 }
else if (prefix > 7) {
143 int month = d->month;
145 const int daysInMonth = d->calendar.daysInMonth(d->month, d->year);
146 if (row >= prefix && row - prefix < daysInMonth) {
148 day = row - prefix + 1;
149 }
else if (row - prefix >= daysInMonth) {
151 month = d->calendar.monthsInYear(d->year) == d->month ? 1 : d->month + 1;
152 year = d->calendar.monthsInYear(d->year) == d->month ? d->year + 1 : d->year;
153 day = row - daysInMonth - prefix + 1;
156 year = d->month > 1 ? d->year : d->year - 1;
157 month = d->month > 1 ? d->month - 1 : d->calendar.monthsInYear(
year);
158 int daysInPreviousMonth = d->calendar.daysInMonth(
month,
year);
159 day = daysInPreviousMonth - prefix + row + 1;
167 return date.startOfDay();
172 return d->selected == date;
180 const int daysInMonth = d->calendar.daysInMonth(d->month, d->year);
181 return row >= prefix && row - prefix < daysInMonth;
188int MonthModel::rowCount(
const QModelIndex &parent)
const
206#include "moc_monthmodel.cpp"
Month model exposing month days and events to a QML view.
Q_INVOKABLE void next()
Go to the next month.
@ IsSelected
Date is equal the selected date.
@ DayNumber
Day numbers, usually from 1 to 31.
@ SameMonth
True iff this day is in the same month as the one displayed.
Q_INVOKABLE void previous()
Go to the previous month.
QDate selected
Set the selected date.
int month
The month number of the month.
QStringList weekDays
The translated week days.
Q_INVOKABLE void goToday()
Go to the currentDate.
QML_ELEMENTint year
The year number of the month.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void append(QList< T > &&value)
Qt::DayOfWeek firstDayOfWeek() const const
QString standaloneDayName(int day, FormatType type) const const
bool isValid() const const
QModelIndex parent() const const
QObject * parent() const const