c554c1dc33e1ccbaf8f64a4b28db79a40019aa00
[toast/confclerk.git] / src / gui / daynavigatorwidget.cpp
1 #include "daynavigatorwidget.h"
2
3 #include <QPainter>
4 #include <QFontMetrics>
5 #include <QLabel>
6
7 #include <QDebug>
8
9 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent)
10     : QWidget(aParent)
11     , mStartDate(QDate())
12     , mEndDate(QDate())
13     , mCurDate(QDate())
14 {
15     setupUi(this);
16     connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
17     connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
18
19     mFontMetrics = new QFontMetrics(QLabel().font());
20 }
21
22 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
23 {
24     Q_ASSERT(aStartDate<=aEndDate);
25
26     //qDebug() << "DayNavigatorWidget::setDates(): " << aStartDate << ", " << aEndDate;
27     mStartDate = aStartDate;
28     mEndDate = aEndDate;
29     mCurDate = aStartDate;
30
31     QRect rect = mFontMetrics->boundingRect(mCurDate.toString("MMM dd yyyy"));
32     qDebug() << mCurDate.toString();
33
34     if(mStartDate==mEndDate) // only one day conference
35     {
36         prevDayButton->setDisabled(true);
37         nextDayButton->setDisabled(true);
38         emit(dateChanged(mCurDate));
39     }
40     else
41     {
42         // at least 2-days conference
43         prevDayButton->setDisabled(true);
44         nextDayButton->setDisabled(false);
45         emit(dateChanged(mCurDate));
46     }
47 }
48
49 void DayNavigatorWidget::prevDayButtonClicked()
50 {
51     //qDebug() << mStartDate << ":" << mCurDate << ":" << mEndDate;
52     if(mCurDate>mStartDate)
53     {
54         mCurDate = mCurDate.addDays(-1);
55         // check start date
56         if(mCurDate==mStartDate || mStartDate==mEndDate)
57             prevDayButton->setDisabled(true);
58         else
59             prevDayButton->setDisabled(false);
60         // check end date
61         if(mCurDate==mEndDate || mStartDate==mEndDate)
62             nextDayButton->setDisabled(true);
63         else
64             nextDayButton->setDisabled(false);
65
66         emit(dateChanged(mCurDate));
67         selectedDate->update();
68     }
69 }
70
71 void DayNavigatorWidget::nextDayButtonClicked()
72 {
73     //qDebug() << mStartDate << ":" << mCurDate << ":" << mEndDate;
74     if(mCurDate<mEndDate)
75     {
76         mCurDate = mCurDate.addDays(1);
77         // check start date
78         if(mCurDate==mStartDate || mStartDate==mEndDate)
79             prevDayButton->setDisabled(true);
80         else
81             prevDayButton->setDisabled(false);
82         // check end date
83         if(mCurDate==mEndDate || mStartDate==mEndDate)
84             nextDayButton->setDisabled(true);
85         else
86             nextDayButton->setDisabled(false);
87
88         emit(dateChanged(mCurDate));
89         selectedDate->update();
90     }
91 }
92
93 QDate DayNavigatorWidget::getCurrentDate()
94 {
95     return mCurDate;
96 }
97
98 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
99 {
100     QString selectedDateStr = mCurDate.toString("MMM dd yyyy");
101
102     QPainter painter(this);
103     painter.save();
104     QRect r = selectedDate->geometry();
105     QRect s = mFontMetrics->boundingRect(selectedDateStr);
106     QPoint p = QPoint(
107             r.x() + r.width()/2 - s.height()/2 - mFontMetrics->descent(),
108             - 130
109             );
110
111     painter.translate(r.width()/2, r.height()/2);
112     painter.rotate(270);
113     painter.drawText(p.y(), p.x(), selectedDateStr); // y,x,string
114     painter.restore();
115 }
116