source: src/gui/daynavigatorwidget.cpp @ de5d0f1

qt5
Last change on this file since de5d0f1 was de5d0f1, checked in by gregor herrmann <gregoa@…>, 6 years ago

Bump copyright years.

  • Property mode set to 100644
File size: 3.7 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
25
26DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
27    setupUi(this);   
28
29    connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
30    connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
31
32    configureNavigation();
33}
34
35
36void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
37    Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
38
39    mStartDate = aStartDate;
40    mEndDate = aEndDate;
41
42    if (!mCurDate.isValid()) mCurDate = mStartDate;
43    // if mCurDate is out of range, set it to mstartDate
44    else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
45
46    configureNavigation();
47    emit(dateChanged(mCurDate));
48    this->update();
49}
50
51
52void DayNavigatorWidget::setCurDate(const QDate& curDate) {
53    Q_ASSERT(curDate.isValid());
54    if (curDate == mCurDate) return;
55
56    if (!mStartDate.isValid()) {
57        // the start and end date have not been set
58        mStartDate = curDate;
59        mEndDate = curDate;
60        mCurDate = curDate;
61    }
62
63    else if (curDate < mStartDate) mCurDate = mStartDate;
64    else if (curDate > mEndDate) mCurDate = mEndDate;
65    else mCurDate = curDate;
66
67    configureNavigation();
68    emit(dateChanged(mCurDate));
69    this->update();
70}
71
72
73void DayNavigatorWidget::unsetDates() {
74    mStartDate= QDate();
75    mEndDate = QDate();
76    mCurDate = QDate();
77
78    configureNavigation();
79    emit(dateChanged(mCurDate));
80    this->update();
81}
82
83
84void DayNavigatorWidget::configureNavigation() {
85    prevDayButton->setDisabled(!mStartDate.isValid() || mCurDate == mStartDate);
86    nextDayButton->setDisabled(!mEndDate.isValid() || mCurDate == mEndDate);
87}
88
89
90void DayNavigatorWidget::prevDayButtonClicked() {
91    if(mCurDate <= mStartDate) return;
92    mCurDate = mCurDate.addDays(-1);
93    configureNavigation();
94    emit(dateChanged(mCurDate));
95    this->update();
96}
97
98
99void DayNavigatorWidget::nextDayButtonClicked() {
100    if(mCurDate >= mEndDate) return;
101    mCurDate = mCurDate.addDays(1);
102    configureNavigation();
103    emit(dateChanged(mCurDate));
104    this->update();
105}
106
107
108void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent) {
109    Q_UNUSED(aEvent);
110
111    QString selectedDateStr = mCurDate.isValid() ? mCurDate.toString("dddd\nyyyy-MM-dd") : tr("No date");
112    QPainter painter(this);
113    painter.save();
114
115    // rectangle only for the text
116    QRect q(-selectedDate->height()-selectedDate->y(), selectedDate->x(), selectedDate->height(), selectedDate->width());
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}
Note: See TracBrowser for help on using the repository browser.