2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2021 Philipp Spitzer, gregor herrmann, Stefan Stahl
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/>.
24 #include "daynavigatorwidget.h"
25 #include "conference.h"
26 #include "application.h"
29 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
32 connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
33 connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
35 configureNavigation();
39 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
40 Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
42 mStartDate = aStartDate;
45 if (!mCurDate.isValid()) mCurDate = mStartDate;
46 // if mCurDate is out of range, set it to mstartDate
47 else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
49 configureNavigation();
50 emit(dateChanged(mCurDate));
55 void DayNavigatorWidget::setCurDate(const QDate& curDate) {
56 Q_ASSERT(curDate.isValid());
57 if (curDate == mCurDate) return;
59 if (!mStartDate.isValid()) {
60 // the start and end date have not been set
66 else if (curDate < mStartDate) mCurDate = mStartDate;
67 else if (curDate > mEndDate) mCurDate = mEndDate;
68 else mCurDate = curDate;
70 configureNavigation();
71 emit(dateChanged(mCurDate));
76 void DayNavigatorWidget::unsetDates() {
81 configureNavigation();
82 emit(dateChanged(mCurDate));
87 void DayNavigatorWidget::configureNavigation() {
88 prevDayButton->setDisabled(!mStartDate.isValid() || mCurDate == mStartDate);
89 nextDayButton->setDisabled(!mEndDate.isValid() || mCurDate == mEndDate);
93 void DayNavigatorWidget::prevDayButtonClicked() {
94 if(mCurDate <= mStartDate) return;
95 mCurDate = mCurDate.addDays(-1);
96 configureNavigation();
97 emit(dateChanged(mCurDate));
102 void DayNavigatorWidget::nextDayButtonClicked() {
103 if(mCurDate >= mEndDate) return;
104 mCurDate = mCurDate.addDays(1);
105 configureNavigation();
106 emit(dateChanged(mCurDate));
111 void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent) {
113 QString selectedDateStr;
114 if (mCurDate.isValid()) {
115 QString selectedDateFormat =
121 selectedDateStr = mCurDate.toString(selectedDateFormat);
122 bool hasConference = ((Application*) qApp)->hasActiveConference();
124 Conference& conference = ((Application*) qApp)->activeConference();
125 if (conference.hasDisplayTimeShift() && conference.displayTimeShift() != 0) {
127 bool minus = conference.displayTimeShift() < 0;
128 shift = shift.addSecs(conference.displayTimeShift() * 60 * (minus ? -1 : 1));
129 selectedDateStr += " • " + (minus ? QString("-") : "+") + shift.toString("HH:mm");
133 selectedDateStr = tr("No date");
135 QPainter painter(this);
138 // rectangle only for the text
139 QRect q(-selectedDate->height()-selectedDate->y(), selectedDate->x(), selectedDate->height(), selectedDate->width());
142 // font size adjustion, static on maemo, dynamically otherwise
143 QFont f = painter.font();
147 #if QT_VERSION >= 0x050b00 // QT 5.11
148 auto dateStrWidth = painter.fontMetrics().horizontalAdvance(selectedDateStr);
150 auto dateStrWidth = painter.fontMetrics().width(selectedDateStr);
152 qreal factor = (qreal) 2 * q.width() / dateStrWidth;
154 if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
157 painter.drawText(q, Qt::AlignCenter, selectedDateStr);