]> ToastFreeware Gitweb - toast/confclerk.git/blob - src/gui/daynavigatorwidget.cpp
Cleanup daynavigatorwidget.
[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 <QLabel>
24
25
26 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
27     setupUi(this);    
28
29     connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
30     connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
31     connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
32 }
33
34
35 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
36     Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
37
38     mStartDate = aStartDate;
39     mEndDate = aEndDate;
40
41     if (!mCurDate.isValid()) mCurDate = mStartDate;
42     // if mCurDate is out of range, set it to mstartDate
43     else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
44
45     configureNavigation();
46     emit(dateChanged(mCurDate));
47     this->update();
48 }
49
50
51 void DayNavigatorWidget::setCurDate(const QDate& curDate) {
52     Q_ASSERT(curDate.isValid());
53     if (curDate == mCurDate) return;
54
55     if (!mStartDate.isValid()) {
56         // the start and end date have not been set
57         mStartDate = curDate;
58         mEndDate = curDate;
59         mCurDate = curDate;
60     }
61
62     else if (curDate < mStartDate) mCurDate = mStartDate;
63     else if (curDate > mEndDate) mCurDate = mEndDate;
64     else mCurDate = curDate;
65
66     configureNavigation();
67     emit(dateChanged(mCurDate));
68     this->update();
69 }
70
71
72 void DayNavigatorWidget::configureNavigation() {
73     prevDayButton->setDisabled(mCurDate == mStartDate);
74     nextDayButton->setDisabled(mCurDate == mEndDate);
75 }
76
77
78 void DayNavigatorWidget::prevDayButtonClicked() {
79     if(mCurDate <= mStartDate) return;
80     mCurDate = mCurDate.addDays(-1);
81     configureNavigation();
82     emit(dateChanged(mCurDate));
83     this->update();
84 }
85
86
87 void DayNavigatorWidget::nextDayButtonClicked() {
88     if(mCurDate >= mEndDate) return;
89     mCurDate = mCurDate.addDays(1);
90     configureNavigation();
91     emit(dateChanged(mCurDate));
92     this->update();
93 }
94
95
96 void DayNavigatorWidget::todayButtonClicked() {
97     setCurDate(QDate::currentDate());
98 }
99
100
101 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
102 {
103     Q_UNUSED(aEvent);
104
105     QString selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
106     QPainter painter(this);
107     painter.save();
108
109     // rectangle only for the text
110     int marginSize = 9;
111     int buttonSize = 32;
112 #ifdef MAEMO
113     QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
114 #else
115     QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
116 #endif
117     painter.rotate(270);
118
119     // font size adjustion, static on maemo, dynamically otherwise
120     QFont f = painter.font();
121 #ifdef MAEMO
122     qreal factor = 0.8;
123 #else
124     qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
125 #endif
126     if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
127     painter.setFont(f);
128
129     painter.drawText(q, Qt::AlignCenter, selectedDateStr);
130     painter.restore();
131 }