File indexing completed on 2024-09-08 04:54:09
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "daysofmonthmodel.h" 0008 #include <QDebug> 0009 0010 void DaysOfMonthModel::update() 0011 { 0012 beginResetModel(); 0013 m_dayList = {}; 0014 0015 int totalDays = m_daysPerWeek * m_weeks; 0016 0017 int daysBeforeCurrentMonth = 0; 0018 int daysAfterCurrentMonth = 0; 0019 0020 QDate firstDay(m_year, m_month, 1); 0021 0022 if (m_firstDayOfWeek < firstDay.dayOfWeek()) { 0023 daysBeforeCurrentMonth = firstDay.dayOfWeek() - m_firstDayOfWeek; 0024 } else { 0025 daysBeforeCurrentMonth = 7 - (m_firstDayOfWeek - firstDay.dayOfWeek()); 0026 } 0027 0028 int daysThusFar = daysBeforeCurrentMonth + firstDay.daysInMonth(); 0029 if (daysThusFar < totalDays) { 0030 daysAfterCurrentMonth = totalDays - daysThusFar; 0031 } 0032 0033 if (daysBeforeCurrentMonth > 0) { 0034 QDate previousMonth = firstDay.addMonths(-1); 0035 for (int i = 0; i < daysBeforeCurrentMonth; i++) { 0036 DayData day; 0037 day.isCurrent = false; 0038 day.dayNumber = previousMonth.daysInMonth() - (daysBeforeCurrentMonth - (i + 1)); 0039 day.monthNumber = previousMonth.month(); 0040 day.yearNumber = previousMonth.year(); 0041 m_dayList << day; 0042 } 0043 } 0044 0045 for (int i = 0; i < firstDay.daysInMonth(); i++) { 0046 DayData day; 0047 day.isCurrent = true; 0048 day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates 0049 day.monthNumber = firstDay.month(); 0050 day.yearNumber = firstDay.year(); 0051 m_dayList << day; 0052 0053 } 0054 0055 if (daysAfterCurrentMonth > 0) { 0056 for (int i = 0; i < daysAfterCurrentMonth; i++) { 0057 DayData day; 0058 day.isCurrent = false; 0059 day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates 0060 day.monthNumber = firstDay.addMonths(1).month(); 0061 day.yearNumber = firstDay.addMonths(1).year(); 0062 m_dayList << day; 0063 } 0064 } 0065 0066 m_dayList[QDate::currentDate().day() + daysBeforeCurrentMonth - 1].isToday = QDate::currentDate().month() == m_month && QDate::currentDate().year() == m_year; 0067 0068 endResetModel(); 0069 } 0070 0071 QVariant DaysOfMonthModel::data(const QModelIndex &index, int role) const 0072 { 0073 int row = index.row(); 0074 0075 switch (role) { 0076 case CurrentMonthRole: 0077 return m_dayList[row].isCurrent; 0078 case DayNumberRole: 0079 return m_dayList[row].dayNumber; 0080 case MonthNumberRole: 0081 return m_dayList[row].monthNumber; 0082 case YearNumberRole: 0083 return m_dayList[row].yearNumber; 0084 case TodayRole: 0085 return m_dayList[row].isToday; 0086 default: 0087 return QStringLiteral("Deadbeef"); 0088 } 0089 } 0090 0091 QHash<int, QByteArray> DaysOfMonthModel::roleNames() const 0092 { 0093 return { 0094 {CurrentMonthRole, "isCurrentMonth"}, 0095 {DayNumberRole, "dayNumber"}, 0096 {MonthNumberRole, "monthNumber"}, 0097 {YearNumberRole, "yearNumber"}, 0098 {TodayRole, "isToday"} 0099 }; 0100 } 0101 0102 void DaysOfMonthModel::goNextMonth() 0103 { 0104 if (m_month == 12) { 0105 m_month = 1; 0106 m_year++; 0107 } else { 0108 m_month ++; 0109 } 0110 0111 Q_EMIT yearChanged(); 0112 Q_EMIT monthChanged(); 0113 update(); 0114 } 0115 0116 void DaysOfMonthModel::goPreviousMonth() 0117 { 0118 if (m_month == 1) { 0119 m_month = 12; 0120 m_year--; 0121 } else { 0122 m_month--; 0123 } 0124 0125 Q_EMIT yearChanged(); 0126 Q_EMIT monthChanged(); 0127 update(); 0128 } 0129 0130 void DaysOfMonthModel::goCurrentMonth() 0131 { 0132 m_year = QDate::currentDate().year(); 0133 m_month = QDate::currentDate().month(); 0134 Q_EMIT yearChanged(); 0135 Q_EMIT monthChanged(); 0136 update(); 0137 } 0138 0139 int DaysOfMonthModel::month() const 0140 { 0141 return m_month; 0142 } 0143 0144 void DaysOfMonthModel::setMonth(int month) 0145 { 0146 if (m_month != month) { 0147 m_month = month; 0148 Q_EMIT monthChanged(); 0149 update(); 0150 } 0151 } 0152 0153 void DaysOfMonthModel::setYear(int year) 0154 { 0155 if (m_year != year) { 0156 m_year = year; 0157 Q_EMIT yearChanged(); 0158 update(); 0159 } 0160 } 0161 0162 int DaysOfMonthModel::year() const 0163 { 0164 return m_year; 0165 } 0166 0167 int DaysOfMonthModel::daysPerWeek() const 0168 { 0169 return m_daysPerWeek; 0170 } 0171 0172 void DaysOfMonthModel::setDaysPerWeek(int daysPerWeek) 0173 { 0174 if (m_daysPerWeek != daysPerWeek) { 0175 m_daysPerWeek = daysPerWeek; 0176 Q_EMIT daysPerWeekChanged(); 0177 update(); 0178 } 0179 } 0180 0181 int DaysOfMonthModel::rowCount(const QModelIndex &parent) const 0182 { 0183 Q_UNUSED(parent) 0184 return m_weeks * m_daysPerWeek; 0185 } 0186 0187 int DaysOfMonthModel::weeks() const 0188 { 0189 return m_weeks; 0190 } 0191 0192 void DaysOfMonthModel::setWeeks(int weeks) 0193 { 0194 if (m_weeks != weeks) { 0195 m_weeks = weeks; 0196 Q_EMIT weeksChanged(); 0197 update(); 0198 } 0199 }