6#include "infinitecalendarviewmodel.h"
8InfiniteCalendarViewModel::InfiniteCalendarViewModel(
QObject *parent)
13void InfiniteCalendarViewModel::classBegin()
18void InfiniteCalendarViewModel::componentComplete()
24void InfiniteCalendarViewModel::setup()
30 if (!m_currentDate.isValid()) {
36 QDateTime firstDay = m_currentDate.addDays(-m_currentDate.date().dayOfWeek() + m_locale.firstDayOfWeek());
38 firstDay = firstDay.
addDays((-m_datesToAdd * 7) / 2);
40 addWeekDates(
true, firstDay);
44 QDateTime firstDay(QDate(m_currentDate.date().year(), m_currentDate.date().month(), 1), {});
45 firstDay = firstDay.
addMonths(-m_datesToAdd / 2);
47 addMonthDates(
true, firstDay);
51 QDateTime firstDay(QDate(m_currentDate.date().year(), m_currentDate.date().month(), 1), {});
52 firstDay = firstDay.
addYears(-m_datesToAdd / 2);
54 addYearDates(
true, firstDay);
58 const int firstYear = ((floor(m_currentDate.date().year() / 10)) * 10) - 1;
59 QDateTime firstDay(QDate(firstYear, m_currentDate.date().month(), 1), {});
60 firstDay = firstDay.
addYears(((-m_datesToAdd * 12) / 2) + 10);
62 addDecadeDates(
true, firstDay);
74 if (m_scale == MonthScale && role != StartDateRole) {
75 const auto firstDay = m_firstDayOfMonthDates[idx.
row()].
date();
78 case FirstDayOfMonthRole:
79 return firstDay.startOfDay();
80 case SelectedMonthRole:
81 return firstDay.month();
82 case SelectedYearRole:
83 return firstDay.year();
90 const auto startDate = m_startDates[idx.
row()].date();
93 case FirstDayOfMonthRole:
94 return QDate(startDate.year(), startDate.month(), 1).startOfDay();
96 return startDate.startOfDay();
97 case SelectedMonthRole:
98 return startDate.month();
99 case SelectedYearRole:
100 return startDate.year();
107int InfiniteCalendarViewModel::rowCount(
const QModelIndex &parent)
const
110 return m_startDates.length();
116 {StartDateRole, QByteArrayLiteral(
"startDate")},
117 {FirstDayOfMonthRole, QByteArrayLiteral(
"firstDay")},
118 {SelectedMonthRole, QByteArrayLiteral(
"selectedMonth")},
119 {SelectedYearRole, QByteArrayLiteral(
"selectedYear")},
123QDateTime InfiniteCalendarViewModel::currentDate()
const
125 return m_currentDate;
128void InfiniteCalendarViewModel::setCurrentDate(
const QDateTime ¤tDate)
130 m_currentDate = currentDate;
133QDateTime InfiniteCalendarViewModel::minimumDate()
const
135 return m_minimumDate;
138void InfiniteCalendarViewModel::setMinimumDate(
const QDateTime &minimumDate)
140 if (m_minimumDate == minimumDate) {
143 m_minimumDate = minimumDate;
144 Q_EMIT minimumDateChanged();
147QDateTime InfiniteCalendarViewModel::maximumDate()
const
149 return m_maximumDate;
152void InfiniteCalendarViewModel::setMaximumDate(
const QDateTime &maximumDate)
154 if (m_maximumDate == maximumDate) {
157 m_maximumDate = maximumDate;
158 Q_EMIT maximumDateChanged();
161void InfiniteCalendarViewModel::addDates(
bool atEnd,
const QDateTime &startFrom)
165 addWeekDates(atEnd, startFrom);
168 addMonthDates(atEnd, startFrom);
171 addYearDates(atEnd, startFrom);
174 addDecadeDates(atEnd, startFrom);
179void InfiniteCalendarViewModel::addWeekDates(
bool atEnd,
const QDateTime &startFrom)
181 const int newRow = atEnd ? rowCount() : 0;
185 for (
int i = 0; i < m_datesToAdd; i++) {
186 QDateTime startDate = startFrom.
isValid() && i == 0 ? startFrom : atEnd ? m_startDates[rowCount() - 1].addDays(7) : m_startDates[0].addDays(-7);
188 if (startDate.
date().
dayOfWeek() != m_locale.firstDayOfWeek()) {
193 m_startDates.append(startDate);
195 m_startDates.insert(0, startDate);
202void InfiniteCalendarViewModel::addMonthDates(
bool atEnd,
const QDateTime &startFrom)
204 QVector<QDateTime> startDates;
206 const int newRow = atEnd ? rowCount() : 0;
208 for (
int i = 0; i < m_datesToAdd; i++) {
211 if (startFrom.
isValid() && i == 0) {
212 firstDay = startFrom;
216 firstDay = m_firstDayOfMonthDates[0].
addMonths(-1);
219 QDateTime startDate = firstDay;
222 if (startDate >= firstDay) {
223 startDate = startDate.
addDays(-7);
227 if (m_maximumDate.isValid() && startDate > m_maximumDate) {
230 m_firstDayOfMonthDates.append(firstDay);
231 startDates.
append(startDate);
233 m_firstDayOfMonthDates.insert(0, firstDay);
234 startDates.
insert(0, startDate);
241 m_startDates = m_startDates + startDates;
243 m_startDates = startDates + m_startDates;
249void InfiniteCalendarViewModel::addYearDates(
bool atEnd,
const QDateTime &startFrom)
251 const int newRow = atEnd ? rowCount() : 0;
255 for (
int i = 0; i < m_datesToAdd; i++) {
256 QDateTime startDate = startFrom.
isValid() && i == 0 ? startFrom : atEnd ? m_startDates[rowCount() - 1].addYears(1) : m_startDates[0].addYears(-1);
259 m_startDates.append(startDate);
261 m_startDates.insert(0, startDate);
268void InfiniteCalendarViewModel::addDecadeDates(
bool atEnd,
const QDateTime &startFrom)
270 const int newRow = atEnd ? rowCount() : 0;
274 for (
int i = 0; i < m_datesToAdd; i++) {
275 QDateTime startDate = startFrom.
isValid() && i == 0 ? startFrom : atEnd ? m_startDates[rowCount() - 1].addYears(10) : m_startDates[0].addYears(-10);
278 m_startDates.append(startDate);
280 m_startDates.insert(0, startDate);
287int InfiniteCalendarViewModel::datesToAdd()
const
292void InfiniteCalendarViewModel::setDatesToAdd(
int datesToAdd)
294 m_datesToAdd = datesToAdd;
295 Q_EMIT datesToAddChanged();
298int InfiniteCalendarViewModel::scale()
303void InfiniteCalendarViewModel::setScale(
int scale)
307 m_startDates.clear();
308 m_firstDayOfMonthDates.clear();
316#include "moc_infinitecalendarviewmodel.cpp"
void beginInsertRows(const QModelIndex &parent, int first, int last)
bool hasIndex(int row, int column, const QModelIndex &parent) const const
virtual QModelIndex parent(const QModelIndex &index) const const=0
int dayOfWeek() const const
QDateTime addDays(qint64 ndays) const const
QDateTime addMonths(int nmonths) const const
QDateTime addYears(int nyears) const const
bool isValid() const const
void append(QList< T > &&value)
iterator insert(const_iterator before, parameter_type value)
qsizetype length() const const
QTestData & newRow(const char *dataTag)