Kstars

genericcalendarwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Akarsh Simha <akarshsimha@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "genericcalendarwidget.h"
8
9#include <KNotification>
10#include <KCalendarSystem>
11
12#include <QDebug>
13
15 : QWidget(parent), m_DateTable(datetable)
16{
17 setupUi(this);
18
19 m_DateTable.setParent(DateTableFrame); // Put the date table in the QFrame that's meant to hold it
20
21 // Set icons for the front / back buttons
22
23 previousYear->setAutoRaise(true);
24 nextYear->setAutoRaise(true);
25 previousMonth->setAutoRaise(true);
26 nextMonth->setAutoRaise(true);
28 {
29 nextYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-left-double")));
30 previousYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-right-double")));
31 nextMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-left")));
32 previousMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-right")));
33 }
34 else
35 {
36 nextYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-right-double")));
37 previousYear->setIcon(QIcon::fromTheme(QLatin1String("arrow-left-double")));
38 nextMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-right")));
39 previousMonth->setIcon(QIcon::fromTheme(QLatin1String("arrow-left")));
40 }
41
42 // Connects
43 connect(&m_DateTable, SIGNAL(dateChanged(QDate)), SLOT(dateChangedSlot(QDate)));
44 connect(nextMonth, SIGNAL(clicked()), SLOT(nextMonthClicked()));
45 connect(previousMonth, SIGNAL(clicked()), SLOT(previousMonthClicked()));
46 connect(nextYear, SIGNAL(clicked()), SLOT(nextYearClicked()));
47 connect(previousYear, SIGNAL(clicked()), SLOT(previousYearClicked()));
48 connect(selectMonth, SIGNAL(activated(int)), SLOT(monthChanged(int)));
49 connect(selectYear, SIGNAL(valueChanged(int)), SLOT(yearChanged(int)));
50
51 m_DateTable.setCalendar(); // Set global calendar
52
53 populateMonthNames();
54
55 // qDebug() << calendar()->monthName( date(), KCalendarSystem::LongName );
56
57 selectMonth->setCurrentIndex(date().month() - 1);
58 selectYear->setValue(date().year());
59 m_Date = date();
60
61 show();
62}
63
65{
66 return m_DateTable.date();
67}
68
69const KCalendarSystem *GenericCalendarWidget::calendar() const
70{
71 return m_DateTable.calendar();
72}
73
74void GenericCalendarWidget::populateMonthNames()
75{
76 // Populate the combobox with month names -- can change by year / calendar type
77 selectMonth->clear();
78 for (int m = 1; m <= calendar()->monthsInYear(date()); m++)
79 {
80 selectMonth->addItem(calendar()->monthName(m, calendar()->year(date())));
81 }
82}
83
84void GenericCalendarWidget::dateChangedSlot(const QDate &date_)
85{
86 // populateMonthNames(); // Not required for global calendar
87
88 if (m_Date != date_)
89 {
90 // To avoid an infinite loop, we update the year spin box / month combo box only when the date has actually changed.
91 selectMonth->setCurrentIndex(date().month() - 1);
92 selectYear->setValue(date().year());
93 m_Date = date_;
94 }
95
96 qDebug() << "Date = " << m_Date;
97
99}
100
101void GenericCalendarWidget::nextMonthClicked()
102{
103 if (!setDate(calendar()->addMonths(date(), 1)))
104 {
106 }
107 m_DateTable.setFocus();
108}
109
110void GenericCalendarWidget::previousMonthClicked()
111{
112 qDebug() << "Previous month clicked!";
113 if (!setDate(calendar()->addMonths(date(), -1)))
114 {
116 }
117 m_DateTable.setFocus();
118}
119
120void GenericCalendarWidget::nextYearClicked()
121{
122 if (!setDate(calendar()->addYears(date(), 1)))
123 {
125 }
126 m_DateTable.setFocus();
127}
128
129void GenericCalendarWidget::previousYearClicked()
130{
131 if (!setDate(calendar()->addYears(date(), -1)))
132 {
134 }
135 m_DateTable.setFocus();
136}
137
138void GenericCalendarWidget::yearChanged(int year)
139{
140 if (!setYear(year))
141 {
143 }
144 m_DateTable.setFocus();
145}
146
147void GenericCalendarWidget::monthChanged(int month)
148{
149 qDebug() << "Month = " << month;
150 if (!setMonth(month + 1))
151 {
153 }
154 m_DateTable.setFocus();
155}
156
158{
159 return setDate(QDate(year, date().month(), date().day()));
160}
161
163{
164 return setDate(QDate(date().year(), month, date().day()));
165}
166
168{
169 return setDate(QDate(date().year(), date().month(), date_));
170}
171
173{
174 if (date_ == date())
175 return true;
176 return m_DateTable.setDate(date_);
177}
bool setYear(int year)
Set the year.
GenericCalendarWidget(KDateTable &dateTable, QWidget *parent=nullptr)
Constructor.
bool setMonth(int month)
Set the month.
const QDate & date() const
Returns the selected date.
void dateChanged(const QDate &date_)
Emitted when the date has been changed.
bool setDate(int date_)
Set the selected day of month.
static void beep(const QString &reason=QString())
bool isRightToLeft()
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setupUi(QWidget *widget)
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:04 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.