2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
5 * This file is part of ConfClerk.
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)
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
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/>.
20 #include "daynavigatorwidget.h"
23 #include <QFontMetrics>
28 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent)
35 connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
36 connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
37 connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
39 mFontMetrics = new QFontMetrics(QLabel().font());
42 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
44 Q_ASSERT(aStartDate<=aEndDate);
46 mStartDate = aStartDate;
48 if (!mCurDate.isValid()) mCurDate = mStartDate;
49 else if (mCurDate < mStartDate) mCurDate = mStartDate;
50 else if (mCurDate > mEndDate) mCurDate = mEndDate;
52 prevDayButton->setDisabled(mCurDate == mStartDate);
53 nextDayButton->setDisabled(mCurDate == mEndDate);
54 emit(dateChanged(mCurDate));
57 void DayNavigatorWidget::configureNavigation()
60 if(mCurDate==mStartDate || mStartDate==mEndDate)
61 prevDayButton->setDisabled(true);
63 prevDayButton->setDisabled(false);
65 if(mCurDate==mEndDate || mStartDate==mEndDate)
66 nextDayButton->setDisabled(true);
68 nextDayButton->setDisabled(false);
71 void DayNavigatorWidget::prevDayButtonClicked()
73 if(mCurDate>mStartDate)
75 mCurDate = mCurDate.addDays(-1);
76 configureNavigation();
77 emit(dateChanged(mCurDate));
82 void DayNavigatorWidget::nextDayButtonClicked()
86 mCurDate = mCurDate.addDays(1);
87 configureNavigation();
88 emit(dateChanged(mCurDate));
93 void DayNavigatorWidget::todayButtonClicked()
95 QDate targetDate = QDate::currentDate();
96 if (targetDate>mStartDate && targetDate<mEndDate)
98 mCurDate = targetDate;
99 configureNavigation();
100 emit(dateChanged(mCurDate));
105 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
109 QString selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
110 QPainter painter(this);
113 // rectangle only for the text
117 QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
119 QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
123 // font size adjustion, static on maemo, dynamically otherwise
124 QFont f = painter.font();
128 qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
130 if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
133 painter.drawText(q, Qt::AlignCenter, selectedDateStr);