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