[plasma/kdeplasma-addons] plasmacalendarplugins/alternatecalendar/provider: [Vietnamese Lunar Calendar] Improve day sublabel display
Nate Graham <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 9a1852cef9a1577250ad94c249f58252c8085284 by Nate Graham, on behalf of Trần Nam Tuấn.
Committed on 27/07/2026 at 19:07.
Pushed by ngraham into branch 'master'.
[Vietnamese Lunar Calendar] Improve day sublabel display
Based on feedback sent to my personal email. This commit adds month info
(mouth number + leap month indication) to the day sublabel at the *first* day 1
of the Gregorian month and every day 1 of lunar month. This should help with
determining the month easily with just a quick glance without having to click
on a date.
M +54 -2 plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.cpp
M +1 -0 plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.h
https://invent.kde.org/plasma/kdeplasma-addons/-/commit/9a1852cef9a1577250ad94c249f58252c8085284
diff --git a/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.cpp b/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.cpp
index 61caca800..d21c67b64 100644
--- a/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.cpp
+++ b/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.cpp
@@ -368,19 +368,31 @@ QString getYearGanzhi(int year)
class VietnameseCalendarProviderPrivate
{
Q_DISABLE_COPY(VietnameseCalendarProviderPrivate)
+ VietnameseCalendarProvider *const q;
+
public:
- VietnameseCalendarProviderPrivate() = default;
+ explicit VietnameseCalendarProviderPrivate(VietnameseCalendarProvider *q);
QCalendar::YearMonthDay fromGregorian(const QDate &date);
CalendarEvents::CalendarEventsPlugin::SubLabel subLabel(const QDate &date);
private:
+ const int m_monthChangesBetweenStartAndEndDate;
QDate m_date;
LunarDate m_lunarDate{};
bool setDate(const QDate &date);
+ bool shouldAppendMonthToDayLabel();
+
+ static int monthBetween(const QDate &d1, const QDate &d2);
};
+VietnameseCalendarProviderPrivate::VietnameseCalendarProviderPrivate(VietnameseCalendarProvider *q)
+ : q{q}
+ , m_monthChangesBetweenStartAndEndDate{monthBetween(q->m_startDate.addDays(q->m_dateOffset), q->m_endDate.addDays(q->m_dateOffset))}
+{
+}
+
bool VietnameseCalendarProviderPrivate::setDate(const QDate &date)
{
if (!date.isValid()) {
@@ -391,6 +403,42 @@ bool VietnameseCalendarProviderPrivate::setDate(const QDate &date)
return true;
}
+int VietnameseCalendarProviderPrivate::monthBetween(const QDate &d1, const QDate &d2)
+{
+ if (!d1.isValid() || !d2.isValid()) {
+ return 0;
+ }
+
+ auto totalMonths1 = d1.year() * 12 + d1.month();
+ auto totalMonths2 = d2.year() * 12 + d2.month();
+
+ return std::abs(totalMonths2 - totalMonths1);
+}
+
+bool VietnameseCalendarProviderPrivate::shouldAppendMonthToDayLabel()
+{
+ // Always append month to day label on the first day of every lunar month.
+ if (m_lunarDate.day == 1) {
+ return true;
+ }
+
+ auto unoffsetDate = m_date.addDays(-q->m_dateOffset);
+
+ if (m_monthChangesBetweenStartAndEndDate >= 1) {
+ // If there are 1 or more month changes between end and start date,
+ // append month to day label of the *first* day 1 of a Gregorian month.
+ if (m_date.day() == 1 && monthBetween(unoffsetDate, q->m_startDate) == 1) {
+ return true;
+ }
+ } else if (m_monthChangesBetweenStartAndEndDate == 0 && unoffsetDate == q->m_startDate) {
+ // If there is 0 month change between end and start date,
+ // append month to day label on the start date.
+ return true;
+ }
+
+ return false;
+}
+
QCalendar::YearMonthDay VietnameseCalendarProviderPrivate::fromGregorian(const QDate &date)
{
if (!setDate(date)) {
@@ -416,12 +464,16 @@ CalendarEvents::CalendarEventsPlugin::SubLabel VietnameseCalendarProviderPrivate
sublabel.yearLabel = getYearGanzhi(m_lunarDate.year).append(' '_L1).append(QString::number(m_lunarDate.year));
sublabel.label = u"Ngày %1 tháng %2 năm %3"_s.arg(sublabel.dayLabel, sublabel.monthLabel, sublabel.yearLabel);
+ if (shouldAppendMonthToDayLabel()) {
+ sublabel.dayLabel.append('/'_L1).append(sublabel.monthLabel);
+ }
+
return sublabel;
}
VietnameseCalendarProvider::VietnameseCalendarProvider(QObject *parent, CalendarSystem::System calendarSystem, const QDate &startDate, const QDate &endDate)
: AbstractCalendarProvider{parent, calendarSystem, startDate, endDate}
- , d{std::make_unique<VietnameseCalendarProviderPrivate>()}
+ , d{std::make_unique<VietnameseCalendarProviderPrivate>(this)}
{
Q_ASSERT(calendarSystem == CalendarSystem::System::Vietnamese);
}
diff --git a/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.h b/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.h
index 165fd894b..9fe93ddbb 100644
--- a/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.h
+++ b/plasmacalendarplugins/alternatecalendar/provider/vietnamesecalendar.h
@@ -28,5 +28,6 @@ public:
CalendarEvents::CalendarEventsPlugin::SubLabel subLabel(const QDate &date) const override;
private:
+ friend class VietnameseCalendarProviderPrivate;
const std::unique_ptr<class VietnameseCalendarProviderPrivate> d;
};