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"
26 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
29 connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
30 connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
31 connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
33 configureNavigation();
37 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
38 Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
40 mStartDate = aStartDate;
43 if (!mCurDate.isValid()) mCurDate = mStartDate;
44 // if mCurDate is out of range, set it to mstartDate
45 else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
47 configureNavigation();
48 emit(dateChanged(mCurDate));
53 void DayNavigatorWidget::setCurDate(const QDate& curDate) {
54 Q_ASSERT(curDate.isValid());
55 if (curDate == mCurDate) return;
57 if (!mStartDate.isValid()) {
58 // the start and end date have not been set
64 else if (curDate < mStartDate) mCurDate = mStartDate;
65 else if (curDate > mEndDate) mCurDate = mEndDate;
66 else mCurDate = curDate;
68 configureNavigation();
69 emit(dateChanged(mCurDate));
74 void DayNavigatorWidget::configureNavigation() {
75 prevDayButton->setDisabled(!mStartDate.isValid() || mCurDate == mStartDate);
76 nextDayButton->setDisabled(!mEndDate.isValid() || mCurDate == mEndDate);
80 void DayNavigatorWidget::prevDayButtonClicked() {
81 if(mCurDate <= mStartDate) return;
82 mCurDate = mCurDate.addDays(-1);
83 configureNavigation();
84 emit(dateChanged(mCurDate));
89 void DayNavigatorWidget::nextDayButtonClicked() {
90 if(mCurDate >= mEndDate) return;
91 mCurDate = mCurDate.addDays(1);
92 configureNavigation();
93 emit(dateChanged(mCurDate));
98 void DayNavigatorWidget::todayButtonClicked() {
99 setCurDate(QDate::currentDate());
103 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
107 QString selectedDateStr = mCurDate.isValid() ? mCurDate.toString("dddd\nyyyy-MM-dd") : tr("No date");
108 QPainter painter(this);
111 // rectangle only for the text
115 QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
117 QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
121 // font size adjustion, static on maemo, dynamically otherwise
122 QFont f = painter.font();
126 qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
128 if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
131 painter.drawText(q, Qt::AlignCenter, selectedDateStr);