ebe439f43cb3851091e59f3a775c3a69456a2c54
[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
33     if(mStartDate==mEndDate) // only one day conference
34     {
35         prevDayButton->setDisabled(true);
36         nextDayButton->setDisabled(true);
37         emit(dateChanged(mCurDate));
38     }
39     else
40     {
41         // at least 2-days conference
42         prevDayButton->setDisabled(true);
43         nextDayButton->setDisabled(false);
44         emit(dateChanged(mCurDate));
45     }
46 }
47
48 void DayNavigatorWidget::prevDayButtonClicked()
49 {
50     //qDebug() << mStartDate << ":" << mCurDate << ":" << mEndDate;
51     if(mCurDate>mStartDate)
52     {
53         mCurDate = mCurDate.addDays(-1);
54         // check start date
55         if(mCurDate==mStartDate || mStartDate==mEndDate)
56             prevDayButton->setDisabled(true);
57         else
58             prevDayButton->setDisabled(false);
59         // check end date
60         if(mCurDate==mEndDate || mStartDate==mEndDate)
61             nextDayButton->setDisabled(true);
62         else
63             nextDayButton->setDisabled(false);
64
65         emit(dateChanged(mCurDate));
66         selectedDate->update();
67     }
68 }
69
70 void DayNavigatorWidget::nextDayButtonClicked()
71 {
72     //qDebug() << mStartDate << ":" << mCurDate << ":" << mEndDate;
73     if(mCurDate<mEndDate)
74     {
75         mCurDate = mCurDate.addDays(1);
76         // check start date
77         if(mCurDate==mStartDate || mStartDate==mEndDate)
78             prevDayButton->setDisabled(true);
79         else
80             prevDayButton->setDisabled(false);
81         // check end date
82         if(mCurDate==mEndDate || mStartDate==mEndDate)
83             nextDayButton->setDisabled(true);
84         else
85             nextDayButton->setDisabled(false);
86
87         emit(dateChanged(mCurDate));
88         selectedDate->update();
89     }
90 }
91
92 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
93 {
94     Q_UNUSED(aEvent);
95
96     QString selectedDateStr = mCurDate.toString("MMM dd yyyy");
97
98     QPainter painter(this);
99     painter.save();
100     QRect r = selectedDate->geometry();
101     QRect s = mFontMetrics->boundingRect(selectedDateStr);
102     QPoint p = QPoint(
103             r.x() + r.width()/2 - s.height()/2 - mFontMetrics->descent(),
104             - 130
105             );
106
107     painter.translate(r.width()/2, r.height()/2);
108     painter.rotate(270);
109     painter.drawText(p.y(), p.x(), selectedDateStr); // y,x,string
110     painter.restore();
111 }
112