File indexing completed on 2025-04-20 06:34:37
0001 /* 0002 SPDX-FileCopyrightText: 2010 Akarsh Simha <akarshsimha@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "genericcalendarwidget.h" 0008 0009 #include <KNotification> 0010 #include <KCalendarSystem> 0011 0012 #include <QDebug> 0013 0014 GenericCalendarWidget::GenericCalendarWidget(KDateTable &datetable, QWidget *parent) 0015 : QWidget(parent), m_DateTable(datetable) 0016 { 0017 setupUi(this); 0018 0019 m_DateTable.setParent(DateTableFrame); // Put the date table in the QFrame that's meant to hold it 0020 0021 // Set icons for the front / back buttons 0022 0023 previousYear->setAutoRaise(true); 0024 nextYear->setAutoRaise(true); 0025 previousMonth->setAutoRaise(true); 0026 nextMonth->setAutoRaise(true); 0027 if (QApplication::isRightToLeft()) 0028 { 0029 nextYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-left-double"))); 0030 previousYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-right-double"))); 0031 nextMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-left"))); 0032 previousMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-right"))); 0033 } 0034 else 0035 { 0036 nextYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-right-double"))); 0037 previousYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-left-double"))); 0038 nextMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-right"))); 0039 previousMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-left"))); 0040 } 0041 0042 // Connects 0043 connect(&m_DateTable, SIGNAL(dateChanged(QDate)), SLOT(dateChangedSlot(QDate))); 0044 connect(nextMonth, SIGNAL(clicked()), SLOT(nextMonthClicked())); 0045 connect(previousMonth, SIGNAL(clicked()), SLOT(previousMonthClicked())); 0046 connect(nextYear, SIGNAL(clicked()), SLOT(nextYearClicked())); 0047 connect(previousYear, SIGNAL(clicked()), SLOT(previousYearClicked())); 0048 connect(selectMonth, SIGNAL(activated(int)), SLOT(monthChanged(int))); 0049 connect(selectYear, SIGNAL(valueChanged(int)), SLOT(yearChanged(int))); 0050 0051 m_DateTable.setCalendar(); // Set global calendar 0052 0053 populateMonthNames(); 0054 0055 // qDebug() << calendar()->monthName( date(), KCalendarSystem::LongName ); 0056 0057 selectMonth->setCurrentIndex(date().month() - 1); 0058 selectYear->setValue(date().year()); 0059 m_Date = date(); 0060 0061 show(); 0062 } 0063 0064 const QDate &GenericCalendarWidget::date() const 0065 { 0066 return m_DateTable.date(); 0067 } 0068 0069 const KCalendarSystem *GenericCalendarWidget::calendar() const 0070 { 0071 return m_DateTable.calendar(); 0072 } 0073 0074 void GenericCalendarWidget::populateMonthNames() 0075 { 0076 // Populate the combobox with month names -- can change by year / calendar type 0077 selectMonth->clear(); 0078 for (int m = 1; m <= calendar()->monthsInYear(date()); m++) 0079 { 0080 selectMonth->addItem(calendar()->monthName(m, calendar()->year(date()))); 0081 } 0082 } 0083 0084 void GenericCalendarWidget::dateChangedSlot(const QDate &date_) 0085 { 0086 // populateMonthNames(); // Not required for global calendar 0087 0088 if (m_Date != date_) 0089 { 0090 // To avoid an infinite loop, we update the year spin box / month combo box only when the date has actually changed. 0091 selectMonth->setCurrentIndex(date().month() - 1); 0092 selectYear->setValue(date().year()); 0093 m_Date = date_; 0094 } 0095 0096 qDebug() << "Date = " << m_Date; 0097 0098 emit(dateChanged(date_)); 0099 } 0100 0101 void GenericCalendarWidget::nextMonthClicked() 0102 { 0103 if (!setDate(calendar()->addMonths(date(), 1))) 0104 { 0105 KNotification::beep(); 0106 } 0107 m_DateTable.setFocus(); 0108 } 0109 0110 void GenericCalendarWidget::previousMonthClicked() 0111 { 0112 qDebug() << "Previous month clicked!"; 0113 if (!setDate(calendar()->addMonths(date(), -1))) 0114 { 0115 KNotification::beep(); 0116 } 0117 m_DateTable.setFocus(); 0118 } 0119 0120 void GenericCalendarWidget::nextYearClicked() 0121 { 0122 if (!setDate(calendar()->addYears(date(), 1))) 0123 { 0124 KNotification::beep(); 0125 } 0126 m_DateTable.setFocus(); 0127 } 0128 0129 void GenericCalendarWidget::previousYearClicked() 0130 { 0131 if (!setDate(calendar()->addYears(date(), -1))) 0132 { 0133 KNotification::beep(); 0134 } 0135 m_DateTable.setFocus(); 0136 } 0137 0138 void GenericCalendarWidget::yearChanged(int year) 0139 { 0140 if (!setYear(year)) 0141 { 0142 KNotification::beep(); 0143 } 0144 m_DateTable.setFocus(); 0145 } 0146 0147 void GenericCalendarWidget::monthChanged(int month) 0148 { 0149 qDebug() << "Month = " << month; 0150 if (!setMonth(month + 1)) 0151 { 0152 KNotification::beep(); 0153 } 0154 m_DateTable.setFocus(); 0155 } 0156 0157 bool GenericCalendarWidget::setYear(int year) 0158 { 0159 return setDate(QDate(year, date().month(), date().day())); 0160 } 0161 0162 bool GenericCalendarWidget::setMonth(int month) 0163 { 0164 return setDate(QDate(date().year(), month, date().day())); 0165 } 0166 0167 bool GenericCalendarWidget::setDate(int date_) 0168 { 0169 return setDate(QDate(date().year(), date().month(), date_)); 0170 } 0171 0172 bool GenericCalendarWidget::setDate(const QDate &date_) 0173 { 0174 if (date_ == date()) 0175 return true; 0176 return m_DateTable.setDate(date_); 0177 }