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

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

Improve day navigator widget.

(Still black magic, now even with #ifdefs :/)

  • 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 <QFontMetrics>
24#include <QLabel>
25
26#include <QDebug>
27
28DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent)
29    : QWidget(aParent)
30    , mStartDate(QDate())
31    , mEndDate(QDate())
32    , mCurDate(QDate())
33{
34    setupUi(this);
35    connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
36    connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
37    connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
38
39    mFontMetrics = new QFontMetrics(QLabel().font());
40}
41
42void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
43{
44    Q_ASSERT(aStartDate<=aEndDate);
45
46    mStartDate = aStartDate;
47    mEndDate = aEndDate;
48    mCurDate = aStartDate;
49
50    // QRect rect = mFontMetrics->boundingRect(mCurDate.toString("MMM dd yyyy"));
51
52    if(mStartDate==mEndDate) // only one day conference
53    {
54        prevDayButton->setDisabled(true);
55        nextDayButton->setDisabled(true);
56        emit(dateChanged(mCurDate));
57    }
58    else
59    {
60        // at least 2-days conference
61        prevDayButton->setDisabled(true);
62        nextDayButton->setDisabled(false);
63        emit(dateChanged(mCurDate));
64    }
65}
66
67void DayNavigatorWidget::configureNavigation()
68{
69    // check start date
70    if(mCurDate==mStartDate || mStartDate==mEndDate)
71        prevDayButton->setDisabled(true);
72    else
73        prevDayButton->setDisabled(false);
74    // check end date
75    if(mCurDate==mEndDate || mStartDate==mEndDate)
76        nextDayButton->setDisabled(true);
77    else
78        nextDayButton->setDisabled(false);
79}
80
81void DayNavigatorWidget::prevDayButtonClicked()
82{
83    if(mCurDate>mStartDate)
84    {
85        mCurDate = mCurDate.addDays(-1);
86        configureNavigation();
87        emit(dateChanged(mCurDate));
88        this->update();
89    }
90}
91
92void DayNavigatorWidget::nextDayButtonClicked()
93{
94    if(mCurDate<mEndDate)
95    {
96        mCurDate = mCurDate.addDays(1);
97        configureNavigation();
98        emit(dateChanged(mCurDate));
99        this->update();
100    }
101}
102
103void DayNavigatorWidget::todayButtonClicked()
104{
105    QDate targetDate = QDate::currentDate();
106    if (targetDate>mStartDate && targetDate<mEndDate)
107    {
108        mCurDate = targetDate;
109        configureNavigation();
110        emit(dateChanged(mCurDate));
111        this->update();
112    }
113}
114
115void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
116{
117    Q_UNUSED(aEvent);
118
119    QString selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
120    QPainter painter(this);
121    painter.save();
122
123    // rectangle only for the text
124    int marginSize = 9;
125    int buttonSize = 32;
126#ifdef MAEMO
127    QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
128#else
129    QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
130#endif
131    painter.rotate(270);
132
133    // font size adjustion, static on maemo, dynamically otherwise
134    QFont f = painter.font();
135#ifdef MAEMO
136    qreal factor = 0.8;
137#else
138    qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
139#endif
140    if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
141    painter.setFont(f);
142
143    painter.drawText(q, Qt::AlignCenter, selectedDateStr);
144    painter.restore();
145}
146
Note: See TracBrowser for help on using the repository browser.