source: src/gui/daynavigatorwidget.cpp @ 1825f82

qt5
Last change on this file since 1825f82 was 1825f82, checked in by Philipp Spitzer <philipp@…>, 11 years ago

Implemented "unset dates" in the date navigator.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
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    connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
32
33    configureNavigation();
34}
35
36
37void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
38    Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
39
40    mStartDate = aStartDate;
41    mEndDate = aEndDate;
42
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;
46
47    configureNavigation();
48    emit(dateChanged(mCurDate));
49    this->update();
50}
51
52
53void DayNavigatorWidget::setCurDate(const QDate& curDate) {
54    Q_ASSERT(curDate.isValid());
55    if (curDate == mCurDate) return;
56
57    if (!mStartDate.isValid()) {
58        // the start and end date have not been set
59        mStartDate = curDate;
60        mEndDate = curDate;
61        mCurDate = curDate;
62    }
63
64    else if (curDate < mStartDate) mCurDate = mStartDate;
65    else if (curDate > mEndDate) mCurDate = mEndDate;
66    else mCurDate = curDate;
67
68    configureNavigation();
69    emit(dateChanged(mCurDate));
70    this->update();
71}
72
73
74void DayNavigatorWidget::unsetDates() {
75    mStartDate= QDate();
76    mEndDate = QDate();
77    mCurDate = QDate();
78
79    configureNavigation();
80    emit(dateChanged(mCurDate));
81    this->update();
82}
83
84
85void DayNavigatorWidget::configureNavigation() {
86    prevDayButton->setDisabled(!mStartDate.isValid() || mCurDate == mStartDate);
87    nextDayButton->setDisabled(!mEndDate.isValid() || mCurDate == mEndDate);
88}
89
90
91void DayNavigatorWidget::prevDayButtonClicked() {
92    if(mCurDate <= mStartDate) return;
93    mCurDate = mCurDate.addDays(-1);
94    configureNavigation();
95    emit(dateChanged(mCurDate));
96    this->update();
97}
98
99
100void DayNavigatorWidget::nextDayButtonClicked() {
101    if(mCurDate >= mEndDate) return;
102    mCurDate = mCurDate.addDays(1);
103    configureNavigation();
104    emit(dateChanged(mCurDate));
105    this->update();
106}
107
108
109void DayNavigatorWidget::todayButtonClicked() {
110    setCurDate(QDate::currentDate());
111}
112
113
114void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
115{
116    Q_UNUSED(aEvent);
117
118    QString selectedDateStr = mCurDate.isValid() ? mCurDate.toString("dddd\nyyyy-MM-dd") : tr("No date");
119    QPainter painter(this);
120    painter.save();
121
122    // rectangle only for the text
123    int marginSize = 9;
124    int buttonSize = 32;
125#ifdef MAEMO
126    QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
127#else
128    QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
129#endif
130    painter.rotate(270);
131
132    // font size adjustion, static on maemo, dynamically otherwise
133    QFont f = painter.font();
134#ifdef MAEMO
135    qreal factor = 0.8;
136#else
137    qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
138#endif
139    if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
140    painter.setFont(f);
141
142    painter.drawText(q, Qt::AlignCenter, selectedDateStr);
143    painter.restore();
144}
Note: See TracBrowser for help on using the repository browser.