]> ToastFreeware Gitweb - toast/confclerk.git/blob - src/gui/daynavigatorwidget.cpp
Day navigator widget: setDates()
[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     // if mCurDate is out of range, set it to mstartDate
50     else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
51
52     prevDayButton->setDisabled(mCurDate == mStartDate);
53     nextDayButton->setDisabled(mCurDate == mEndDate);
54     emit(dateChanged(mCurDate));
55     this->update();
56 }
57
58 void DayNavigatorWidget::configureNavigation()
59 {
60     // check start date
61     if(mCurDate==mStartDate || mStartDate==mEndDate)
62         prevDayButton->setDisabled(true);
63     else
64         prevDayButton->setDisabled(false);
65     // check end date
66     if(mCurDate==mEndDate || mStartDate==mEndDate)
67         nextDayButton->setDisabled(true);
68     else
69         nextDayButton->setDisabled(false);
70 }
71
72 void DayNavigatorWidget::prevDayButtonClicked()
73 {
74     if(mCurDate>mStartDate)
75     {
76         mCurDate = mCurDate.addDays(-1);
77         configureNavigation();
78         emit(dateChanged(mCurDate));
79         this->update();
80     }
81 }
82
83 void DayNavigatorWidget::nextDayButtonClicked()
84 {
85     if(mCurDate<mEndDate)
86     {
87         mCurDate = mCurDate.addDays(1);
88         configureNavigation();
89         emit(dateChanged(mCurDate));
90         this->update();
91     }
92 }
93
94 void DayNavigatorWidget::todayButtonClicked()
95 {
96     QDate targetDate = QDate::currentDate();
97     if (targetDate>mStartDate && targetDate<mEndDate)
98     {
99         mCurDate = targetDate;
100         configureNavigation();
101         emit(dateChanged(mCurDate));
102         this->update();
103     }
104 }
105
106 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
107 {
108     Q_UNUSED(aEvent);
109
110     QString selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
111     QPainter painter(this);
112     painter.save();
113
114     // rectangle only for the text
115     int marginSize = 9;
116     int buttonSize = 32;
117 #ifdef MAEMO
118     QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
119 #else
120     QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
121 #endif
122     painter.rotate(270);
123
124     // font size adjustion, static on maemo, dynamically otherwise
125     QFont f = painter.font();
126 #ifdef MAEMO
127     qreal factor = 0.8;
128 #else
129     qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
130 #endif
131     if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
132     painter.setFont(f);
133
134     painter.drawText(q, Qt::AlignCenter, selectedDateStr);
135     painter.restore();
136 }
137