not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / plasmaclock / calendar.cpp
blob64290ff319a008ad888cbd7d586fc311d9c1087b
1 /*
2 * Copyright 2008 Davide Bettio <davide.bettio@kdemail.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "calendar.h"
22 //Qt
23 #include <QtCore/QDate>
24 #include <QtGui/QGraphicsSceneWheelEvent>
25 #include <QtGui/QGraphicsLinearLayout>
26 #include <QtGui/QGraphicsProxyWidget>
27 #include <QtGui/QLabel>
28 #include <QtGui/QMenu>
29 #include <QtGui/QSpinBox>
30 #include <QtGui/QToolButton>
32 //KDECore
33 #include <KCalendarSystem>
34 #include <KDebug>
35 #include <KGlobal>
36 #include <KIcon>
37 #include <KLineEdit>
38 #include <KLocale>
39 #include <KIntSpinBox>
41 //Plasma
42 #include <Plasma/Label>
43 #include <Plasma/LineEdit>
44 #include <Plasma/SpinBox>
45 #include <Plasma/ToolButton>
47 namespace Plasma
50 class CalendarPrivate
52 public:
53 ToolButton *back;
54 Plasma::Label *spacer0;
55 Plasma::ToolButton *month;
56 Plasma::SpinBox *yearSpinBox;
57 Plasma::ToolButton *year;
58 Plasma::Label *spacer1;
59 Plasma::ToolButton *forward;
60 Plasma::CalendarTable *calendarTable;
61 Plasma::LineEdit *dateText;
62 ToolButton *jumpToday;
63 QMenu *monthMenu;
64 Plasma::SpinBox *weekSpinBox;
67 //TODO
68 Calendar::Calendar(const QDate &, QGraphicsWidget *parent)
69 : QGraphicsWidget(parent), d(new CalendarPrivate())
74 Calendar::Calendar(QGraphicsWidget *parent)
75 : QGraphicsWidget(parent), d(new CalendarPrivate())
77 QGraphicsLinearLayout *m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);
78 QGraphicsLinearLayout *m_hLayout = new QGraphicsLinearLayout(m_layout);
79 QGraphicsLinearLayout *m_layoutTools = new QGraphicsLinearLayout(m_layout);
81 d->calendarTable = new Plasma::CalendarTable(this);
82 d->calendarTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
84 d->back = new Plasma::ToolButton(this);
85 d->back->setText("<");
86 d->back->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
87 connect(d->back, SIGNAL(clicked()), this, SLOT(prevMonth()));
88 m_hLayout->addItem(d->back);
90 m_hLayout->addStretch();
92 d->month = new Plasma::ToolButton(this);
93 d->month->setText(d->calendarTable->calendar()->monthName(d->calendarTable->calendar()->month(d->calendarTable->date()), d->calendarTable->calendar()->year(d->calendarTable->date())));
94 d->month->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
95 connect(d->month, SIGNAL(clicked()), this, SLOT(monthsPopup()));
96 m_hLayout->addItem(d->month);
98 d->year = new Plasma::ToolButton(this);
99 d->year->setText(QString::number(d->calendarTable->calendar()->year(d->calendarTable->date())));
100 d->year->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
101 connect(d->year, SIGNAL(clicked()), this, SLOT(showYearSpinBox()));
102 m_hLayout->addItem(d->year);
104 d->yearSpinBox = new Plasma::SpinBox(this);
105 d->yearSpinBox->setRange(d->calendarTable->calendar()->year(d->calendarTable->calendar()->earliestValidDate()), d->calendarTable->calendar()->year(d->calendarTable->calendar()->latestValidDate()));
106 d->yearSpinBox->setValue(d->calendarTable->calendar()->year(d->calendarTable->date()));
107 d->yearSpinBox->hide();
108 connect(d->yearSpinBox->nativeWidget(), SIGNAL(editingFinished()), this, SLOT(hideYearSpinBox()));
110 m_hLayout->addStretch();
112 d->forward = new Plasma::ToolButton(this);
113 d->forward->setText(">");
114 d->forward->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
115 connect(d->forward, SIGNAL(clicked()), this, SLOT(nextMonth()));
116 m_hLayout->addItem(d->forward);
118 m_layout->addItem(m_hLayout);
120 m_layout->addItem(d->calendarTable);
122 d->jumpToday = new Plasma::ToolButton(this);
123 d->jumpToday->nativeWidget()->setIcon(KIcon("go-jump-today"));
124 d->jumpToday->nativeWidget()->setMinimumWidth(25);
125 connect(d->jumpToday, SIGNAL(clicked()), this, SLOT(goToToday()));
126 m_layoutTools->addItem(d->jumpToday);
127 m_layoutTools->addStretch();
129 d->dateText = new Plasma::LineEdit(this);
130 connect(d->calendarTable, SIGNAL(dateChanged(const QDate &)), this, SLOT(dateUpdated(const QDate &)));
131 connect(d->calendarTable, SIGNAL(displayedMonthChanged(int, int)), this, SLOT(goToMonth(int, int)));
132 connect(d->dateText->nativeWidget(), SIGNAL(returnPressed()), this, SLOT(manualDateChange()));
133 m_layoutTools->addItem(d->dateText);
134 m_layoutTools->addStretch();
136 d->weekSpinBox = new Plasma::SpinBox(this);
137 d->weekSpinBox->setMinimum(1);
138 d->weekSpinBox->setMaximum(d->calendarTable->calendar()->weeksInYear(d->calendarTable->date()));
139 connect(d->weekSpinBox, SIGNAL(valueChanged(int)), this, SLOT(goToWeek(int)));
140 m_layoutTools->addItem(d->weekSpinBox);
142 m_layout->addItem(m_layoutTools);
144 d->monthMenu = 0;
146 dateUpdated(d->calendarTable->date());
149 Calendar::~Calendar()
151 delete d->monthMenu;
152 delete d;
155 void Calendar::manualDateChange()
157 QDate date = KGlobal::locale()->readDate(((QLineEdit*)sender())->text());
158 if(date.isValid()) {
159 setDate(date);
163 void Calendar::goToToday()
165 setDate(QDate::currentDate());
168 bool Calendar::setCalendar(KCalendarSystem *calendar)
170 return d->calendarTable->setCalendar(calendar);
173 bool Calendar::setDate(const QDate &date)
175 bool r = d->calendarTable->setDate(date);
176 dateUpdated(date);
177 return r;
180 const QDate& Calendar::date() const
182 return d->calendarTable->date();
185 void Calendar::dateUpdated(const QDate &date)
187 QString formatted = KGlobal::locale()->formatDate( date, KLocale::ShortDate );
188 d->month->setText(d->calendarTable->calendar()->monthName(date));
189 d->year->setText(QString::number(d->calendarTable->calendar()->year(date)));
190 d->dateText->setText(formatted);
191 d->weekSpinBox->setValue(d->calendarTable->calendar()->weekNumber(date));
194 void Calendar::prevMonth()
196 const KCalendarSystem *calendar = d->calendarTable->calendar();
198 QDate tmpDate = d->calendarTable->date();
199 QDate newDate;
201 int month = calendar->month(tmpDate);
202 int year = calendar->year(tmpDate);
204 if (month == 1){
205 month = 12;
206 year--;
207 }else{
208 month--;
211 if (calendar->setYMD(newDate, year, month, calendar->day(tmpDate))){
212 setDate(newDate);
213 }else if (calendar->setYMD(newDate, year, month, 1)){
214 setDate(newDate);
218 void Calendar::nextMonth()
220 const KCalendarSystem *calendar = d->calendarTable->calendar();
222 QDate tmpDate = d->calendarTable->date();
223 QDate newDate;
225 int month = calendar->month(tmpDate);
226 int year = calendar->year(tmpDate);
228 if (month == 12){
229 month = 1;
230 year++;
231 }else{
232 month++;
235 if (calendar->setYMD(newDate, year, month, calendar->day(tmpDate))){
236 setDate(newDate);
237 }else if (calendar->setYMD(newDate, year, month, 1)){
238 setDate(newDate);
242 void Calendar::monthsPopup()
244 delete d->monthMenu;
245 d->monthMenu = new QMenu();
247 int year = d->calendarTable->calendar()->year(d->calendarTable->date());
249 for (int i = 1; i <= 12; i++){
250 QAction *tmpAction = new QAction(d->calendarTable->calendar()->monthName(i, year), d->monthMenu);
251 tmpAction->setProperty("month", i);
252 connect(tmpAction, SIGNAL(triggered()), this, SLOT(monthTriggered()));
253 d->monthMenu->addAction(tmpAction);
256 d->monthMenu->popup(QCursor::pos());
259 void Calendar::monthTriggered()
261 QAction *action = dynamic_cast<QAction*> (sender());
262 if (!action || action->property("month").type() != QVariant::Int) return;
263 int month = action->property("month").toInt();
265 const KCalendarSystem *calendar = d->calendarTable->calendar();
266 QDate tmpDate = d->calendarTable->date();
267 QDate newDate;
269 int year = calendar->year(tmpDate);
271 if (calendar->setYMD(newDate, year, month, calendar->day(tmpDate))){
272 setDate(newDate);
273 }else if (calendar->setYMD(newDate, year, month, 1)){
274 setDate(newDate);
278 void Calendar::goToWeek(int week)
280 const KCalendarSystem *calendar = d->calendarTable->calendar();
282 if (calendar->weekNumber(d->calendarTable->date()) != week){
283 QDate firstDayOfWeek;
284 calendar->setYMD(firstDayOfWeek, calendar->year(d->calendarTable->date()), 1, 1);
285 int weeksInYear = calendar->weeksInYear(d->calendarTable->date());
287 for (int i = 1; i < weeksInYear; i++){
288 if (week == calendar->weekNumber(firstDayOfWeek)) //TODO: Check year
289 break;
291 firstDayOfWeek= calendar->addDays(firstDayOfWeek, calendar->daysInWeek(firstDayOfWeek));
294 setDate(firstDayOfWeek);
298 void Calendar::goToMonth(int year, int month)
300 setDate(QDate(year, month, 1));
303 void Calendar::showYearSpinBox()
305 d->yearSpinBox->setValue(d->calendarTable->calendar()->year(d->calendarTable->date()));
306 d->yearSpinBox->setGeometry(d->year->geometry());
307 d->year->hide();
308 d->yearSpinBox->show();
309 d->yearSpinBox->nativeWidget()->setFocus(Qt::MouseFocusReason);
312 void Calendar::hideYearSpinBox()
314 d->yearSpinBox->hide();
316 const KCalendarSystem *calendar = d->calendarTable->calendar();
317 QDate newDate;
318 if (calendar->setYMD(newDate, d->yearSpinBox->value(), calendar->month(d->calendarTable->date()), calendar->day(d->calendarTable->date()))){
319 setDate(newDate);
322 d->year->show();
325 CalendarTable *Calendar::calendarTable() const
327 return d->calendarTable;
331 #include "calendar.moc"