Fixed ticket #26 (empty tabs after some actions).
[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     connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
38
39     mFontMetrics = new QFontMetrics(QLabel().font());
40 }
41
42 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
43 {
44     Q_ASSERT(aStartDate<=aEndDate);
45
46     mStartDate = aStartDate;
47     mEndDate = aEndDate;
48     if (!mCurDate.isValid()) mCurDate = mStartDate;
49     else if (mCurDate < mStartDate) mCurDate = mStartDate;
50     else if (mCurDate > mEndDate) mCurDate = mEndDate;
51
52     prevDayButton->setDisabled(mCurDate == mStartDate);
53     nextDayButton->setDisabled(mCurDate == mEndDate);
54     emit(dateChanged(mCurDate));
55 }
56
57 void DayNavigatorWidget::configureNavigation()
58 {
59     // check start date
60     if(mCurDate==mStartDate || mStartDate==mEndDate)
61         prevDayButton->setDisabled(true);
62     else
63         prevDayButton->setDisabled(false);
64     // check end date
65     if(mCurDate==mEndDate || mStartDate==mEndDate)
66         nextDayButton->setDisabled(true);
67     else
68         nextDayButton->setDisabled(false);
69 }
70
71 void DayNavigatorWidget::prevDayButtonClicked()
72 {
73     if(mCurDate>mStartDate)
74     {
75         mCurDate = mCurDate.addDays(-1);
76         configureNavigation();
77         emit(dateChanged(mCurDate));
78         this->update();
79     }
80 }
81
82 void DayNavigatorWidget::nextDayButtonClicked()
83 {
84     if(mCurDate<mEndDate)
85     {
86         mCurDate = mCurDate.addDays(1);
87         configureNavigation();
88         emit(dateChanged(mCurDate));
89         this->update();
90     }
91 }
92
93 void DayNavigatorWidget::todayButtonClicked()
94 {
95     QDate targetDate = QDate::currentDate();
96     if (targetDate>mStartDate && targetDate<mEndDate)
97     {
98         mCurDate = targetDate;
99         configureNavigation();
100         emit(dateChanged(mCurDate));
101         this->update();
102     }
103 }
104
105 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
106 {
107     Q_UNUSED(aEvent);
108
109     QString selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
110     QPainter painter(this);
111     painter.save();
112
113     // rectangle only for the text
114     int marginSize = 9;
115     int buttonSize = 32;
116 #ifdef MAEMO
117     QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
118 #else
119     QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
120 #endif
121     painter.rotate(270);
122
123     // font size adjustion, static on maemo, dynamically otherwise
124     QFont f = painter.font();
125 #ifdef MAEMO
126     qreal factor = 0.8;
127 #else
128     qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
129 #endif
130     if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
131     painter.setFont(f);
132
133     painter.drawText(q, Qt::AlignCenter, selectedDateStr);
134     painter.restore();
135 }
136