0672f35b96ce8e4a74016b611448efa956073fe4
[toast/confclerk.git] / src / gui / daynavigatorwidget.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "daynavigatorwidget.h"
20
21 #include <QPainter>
22 #include <QFontMetrics>
23 #include <QLabel>
24
25 #include <QDebug>
26
27 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent)
28     : QWidget(aParent)
29     , mStartDate(QDate())
30     , mEndDate(QDate())
31     , mCurDate(QDate())
32 {
33     setupUi(this);
34     connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
35     connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
36
37     mFontMetrics = new QFontMetrics(QLabel().font());
38 }
39
40 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
41 {
42     Q_ASSERT(aStartDate<=aEndDate);
43
44     //qDebug() << "DayNavigatorWidget::setDates(): " << aStartDate << ", " << aEndDate;
45     mStartDate = aStartDate;
46     mEndDate = aEndDate;
47     mCurDate = aStartDate;
48
49     QRect rect = mFontMetrics->boundingRect(mCurDate.toString("MMM dd yyyy"));
50
51     if(mStartDate==mEndDate) // only one day conference
52     {
53         prevDayButton->setDisabled(true);
54         nextDayButton->setDisabled(true);
55         emit(dateChanged(mCurDate));
56     }
57     else
58     {
59         // at least 2-days conference
60         prevDayButton->setDisabled(true);
61         nextDayButton->setDisabled(false);
62         emit(dateChanged(mCurDate));
63     }
64 }
65
66 void DayNavigatorWidget::prevDayButtonClicked()
67 {
68     //qDebug() << mStartDate << ":" << mCurDate << ":" << mEndDate;
69     if(mCurDate>mStartDate)
70     {
71         mCurDate = mCurDate.addDays(-1);
72         // check start date
73         if(mCurDate==mStartDate || mStartDate==mEndDate)
74             prevDayButton->setDisabled(true);
75         else
76             prevDayButton->setDisabled(false);
77         // check end date
78         if(mCurDate==mEndDate || mStartDate==mEndDate)
79             nextDayButton->setDisabled(true);
80         else
81             nextDayButton->setDisabled(false);
82
83         emit(dateChanged(mCurDate));
84         selectedDate->update();
85     }
86 }
87
88 void DayNavigatorWidget::nextDayButtonClicked()
89 {
90     //qDebug() << mStartDate << ":" << mCurDate << ":" << mEndDate;
91     if(mCurDate<mEndDate)
92     {
93         mCurDate = mCurDate.addDays(1);
94         // check start date
95         if(mCurDate==mStartDate || mStartDate==mEndDate)
96             prevDayButton->setDisabled(true);
97         else
98             prevDayButton->setDisabled(false);
99         // check end date
100         if(mCurDate==mEndDate || mStartDate==mEndDate)
101             nextDayButton->setDisabled(true);
102         else
103             nextDayButton->setDisabled(false);
104
105         emit(dateChanged(mCurDate));
106         selectedDate->update();
107     }
108 }
109
110 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
111 {
112     Q_UNUSED(aEvent);
113
114     QString selectedDateStr = mCurDate.toString("MMM dd yyyy");
115
116     QPainter painter(this);
117     painter.save();
118     QRect r = selectedDate->geometry();
119     QRect s = mFontMetrics->boundingRect(selectedDateStr);
120     QPoint p = QPoint(
121             r.x() + r.width()/2 - s.height()/2 - mFontMetrics->descent(),
122             - 130
123             );
124
125     painter.translate(r.width()/2, r.height()/2);
126     painter.rotate(270);
127     painter.drawText(p.y(), p.x(), selectedDateStr); // y,x,string
128     painter.restore();
129 }
130