source: src/gui/daynavigatorwidget.cpp @ 5b7fa79

Last change on this file since 5b7fa79 was 5b7fa79, checked in by Philipp Spitzer <philipp@…>, 10 months ago

Adjust shown event time when displayTimeShift is set.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
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#include <conference.h>
25#include <application.h>
26
27
28DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
29    setupUi(this);   
30
31    connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
32    connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
33
34    configureNavigation();
35}
36
37
38void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
39    Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
40
41    mStartDate = aStartDate;
42    mEndDate = aEndDate;
43
44    if (!mCurDate.isValid()) mCurDate = mStartDate;
45    // if mCurDate is out of range, set it to mstartDate
46    else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
47
48    configureNavigation();
49    emit(dateChanged(mCurDate));
50    this->update();
51}
52
53
54void DayNavigatorWidget::setCurDate(const QDate& curDate) {
55    Q_ASSERT(curDate.isValid());
56    if (curDate == mCurDate) return;
57
58    if (!mStartDate.isValid()) {
59        // the start and end date have not been set
60        mStartDate = curDate;
61        mEndDate = curDate;
62        mCurDate = curDate;
63    }
64
65    else if (curDate < mStartDate) mCurDate = mStartDate;
66    else if (curDate > mEndDate) mCurDate = mEndDate;
67    else mCurDate = curDate;
68
69    configureNavigation();
70    emit(dateChanged(mCurDate));
71    this->update();
72}
73
74
75void DayNavigatorWidget::unsetDates() {
76    mStartDate= QDate();
77    mEndDate = QDate();
78    mCurDate = QDate();
79
80    configureNavigation();
81    emit(dateChanged(mCurDate));
82    this->update();
83}
84
85
86void DayNavigatorWidget::configureNavigation() {
87    prevDayButton->setDisabled(!mStartDate.isValid() || mCurDate == mStartDate);
88    nextDayButton->setDisabled(!mEndDate.isValid() || mCurDate == mEndDate);
89}
90
91
92void DayNavigatorWidget::prevDayButtonClicked() {
93    if(mCurDate <= mStartDate) return;
94    mCurDate = mCurDate.addDays(-1);
95    configureNavigation();
96    emit(dateChanged(mCurDate));
97    this->update();
98}
99
100
101void DayNavigatorWidget::nextDayButtonClicked() {
102    if(mCurDate >= mEndDate) return;
103    mCurDate = mCurDate.addDays(1);
104    configureNavigation();
105    emit(dateChanged(mCurDate));
106    this->update();
107}
108
109
110void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent) {
111    Q_UNUSED(aEvent);
112    QString selectedDateStr;
113    if (mCurDate.isValid()) {
114        selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
115        bool hasConference = ((Application*) qApp)->hasActiveConference();
116        if (hasConference) {
117            Conference& conference = ((Application*) qApp)->activeConference();
118            if (conference.hasDisplayTimeShift() && conference.displayTimeShift() != 0) {
119                QTime shift(0, 0);
120                bool minus = conference.displayTimeShift() < 0;
121                shift = shift.addSecs(conference.displayTimeShift() * 60 * (minus ? -1 : 1));
122                selectedDateStr += " (" + (minus ? QString("-") : "+") + shift.toString("HH:mm") + ")";
123            }
124        }
125    } else {
126        selectedDateStr = tr("No date");
127    }
128    QPainter painter(this);
129    painter.save();
130
131    // rectangle only for the text
132    QRect q(-selectedDate->height()-selectedDate->y(), selectedDate->x(), selectedDate->height(), selectedDate->width());
133    painter.rotate(270);
134
135    // font size adjustion, static on maemo, dynamically otherwise
136    QFont f = painter.font();
137#ifdef MAEMO
138    qreal factor = 0.8;
139#else
140#if QT_VERSION >= 0x050b00 // QT 5.11
141    auto dateStrWidth = painter.fontMetrics().horizontalAdvance(selectedDateStr);
142#else
143    auto dateStrWidth = painter.fontMetrics().width(selectedDateStr);
144#endif
145    qreal factor = (qreal) 2 * q.width() / dateStrWidth;
146#endif
147    if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
148    painter.setFont(f);
149
150    painter.drawText(q, Qt::AlignCenter, selectedDateStr);
151    painter.restore();
152}
Note: See TracBrowser for help on using the repository browser.