Changeset 701240f


Ignore:
Timestamp:
09/21/11 21:29:43 (11 years ago)
Author:
Philipp Spitzer <philipp@…>
Branches:
master, qt5
Children:
66ec7cb
Parents:
788182f
Message:

Cleanup daynavigatorwidget.

Location:
src/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/gui/daynavigatorwidget.cpp

    r788182f r701240f  
    2121
    2222#include <QPainter>
    23 #include <QFontMetrics>
    2423#include <QLabel>
    2524
    26 #include <QDebug>
    2725
    28 DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent)
    29     : QWidget(aParent)
    30     , mStartDate(QDate())
    31     , mEndDate(QDate())
    32     , mCurDate(QDate())
    33 {
    34     setupUi(this);
     26DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
     27    setupUi(this);   
     28
    3529    connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
    3630    connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
    3731    connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
    38 
    39     mFontMetrics = new QFontMetrics(QLabel().font());
    4032}
    4133
    42 void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
    43 {
    44     Q_ASSERT(aStartDate<=aEndDate);
     34
     35void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
     36    Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
    4537
    4638    mStartDate = aStartDate;
    4739    mEndDate = aEndDate;
     40
    4841    if (!mCurDate.isValid()) mCurDate = mStartDate;
    4942    // if mCurDate is out of range, set it to mstartDate
    5043    else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
    5144
    52     prevDayButton->setDisabled(mCurDate == mStartDate);
    53     nextDayButton->setDisabled(mCurDate == mEndDate);
     45    configureNavigation();
    5446    emit(dateChanged(mCurDate));
    5547    this->update();
    5648}
    5749
    58 void DayNavigatorWidget::configureNavigation()
    59 {
    60     // check start date
    61     if(mCurDate==mStartDate || mStartDate==mEndDate)
    62         prevDayButton->setDisabled(true);
    63     else
    64         prevDayButton->setDisabled(false);
    65     // check end date
    66     if(mCurDate==mEndDate || mStartDate==mEndDate)
    67         nextDayButton->setDisabled(true);
    68     else
    69         nextDayButton->setDisabled(false);
     50
     51void DayNavigatorWidget::setCurDate(const QDate& curDate) {
     52    Q_ASSERT(curDate.isValid());
     53    if (curDate == mCurDate) return;
     54
     55    if (!mStartDate.isValid()) {
     56        // the start and end date have not been set
     57        mStartDate = curDate;
     58        mEndDate = curDate;
     59        mCurDate = curDate;
     60    }
     61
     62    else if (curDate < mStartDate) mCurDate = mStartDate;
     63    else if (curDate > mEndDate) mCurDate = mEndDate;
     64    else mCurDate = curDate;
     65
     66    configureNavigation();
     67    emit(dateChanged(mCurDate));
     68    this->update();
    7069}
    7170
    72 void DayNavigatorWidget::prevDayButtonClicked()
    73 {
    74     if(mCurDate>mStartDate)
    75     {
    76         mCurDate = mCurDate.addDays(-1);
    77         configureNavigation();
    78         emit(dateChanged(mCurDate));
    79         this->update();
    80     }
     71
     72void DayNavigatorWidget::configureNavigation() {
     73    prevDayButton->setDisabled(mCurDate == mStartDate);
     74    nextDayButton->setDisabled(mCurDate == mEndDate);
    8175}
    8276
    83 void DayNavigatorWidget::nextDayButtonClicked()
    84 {
    85     if(mCurDate<mEndDate)
    86     {
    87         mCurDate = mCurDate.addDays(1);
    88         configureNavigation();
    89         emit(dateChanged(mCurDate));
    90         this->update();
    91     }
     77
     78void DayNavigatorWidget::prevDayButtonClicked() {
     79    if(mCurDate <= mStartDate) return;
     80    mCurDate = mCurDate.addDays(-1);
     81    configureNavigation();
     82    emit(dateChanged(mCurDate));
     83    this->update();
    9284}
    9385
    94 void DayNavigatorWidget::todayButtonClicked()
    95 {
    96     QDate targetDate = QDate::currentDate();
    97     if (targetDate>mStartDate && targetDate<mEndDate)
    98     {
    99         mCurDate = targetDate;
    100         configureNavigation();
    101         emit(dateChanged(mCurDate));
    102         this->update();
    103     }
     86
     87void DayNavigatorWidget::nextDayButtonClicked() {
     88    if(mCurDate >= mEndDate) return;
     89    mCurDate = mCurDate.addDays(1);
     90    configureNavigation();
     91    emit(dateChanged(mCurDate));
     92    this->update();
    10493}
     94
     95
     96void DayNavigatorWidget::todayButtonClicked() {
     97    setCurDate(QDate::currentDate());
     98}
     99
    105100
    106101void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
     
    135130    painter.restore();
    136131}
    137 
  • src/gui/daynavigatorwidget.h

    r788182f r701240f  
    2525#include <QDate>
    2626
    27 /*#include <QPainter>*/
    28 /*#include <QLabel>*/
    29 
    30 /*class QFontMetrics;*/
    31 
    32 /*class VerticalLabel : public QWidget*/
    33 /*{*/
    34 
    35 /*public:*/
    36 /*VerticalLabel(QWidget *aParent = NULL)*/
    37 /*: QWidget(aParent)*/
    38 /*, mText("")*/
    39 /*{*/
    40 /*mFont = QLabel().font();*/
    41 /*}   */
    42 
    43 /*void paintEvent(QPaintEvent *)*/
    44 /*{   */
    45 /*QPainter p(this);*/
    46 /*drawRotatedText(&p, 270, width()/2, height()/2, mText);*/
    47 /*}   */
    48 
    49 /*void drawRotatedText(QPainter *aPainter, qreal aDegrees, int x, int y, const QString &aText)*/
    50 /*{   */
    51 
    52 /*aPainter->save();*/
    53 /*aPainter->setFont(mFont);*/
    54 /*aPainter->translate(x, y); */
    55 /*aPainter->rotate(aDegrees);*/
    56 /*QFontMetrics fm(mFont);*/
    57 /*QRect r = fm.boundingRect(aText);*/
    58 /*aPainter->drawText(-r.width()/2, fm.descent()/2, aText);*/
    59 /*aPainter->restore();*/
    60 /*}   */
    61 
    62 /*void setText(const QString &aText)*/
    63 /*{*/
    64 /*mText = aText;*/
    65 /*update();*/
    66 /*}*/
    67 
    68 /*private:*/
    69 /*QString mText;*/
    70 /*QFont mFont;*/
    71 /*};*/
    72 
    73 class DayNavigatorWidget : public QWidget, private Ui::DayNavigatorWidget
    74 {
     27/** The DayNavigator widget manages three dates, the startDate, curDate and endDate.
     28Either startDate, curDate and endDate all have to be valid and startDate <= curDate <= endDate,
     29OR all three dates are invalid (representing "no date range", e.g. no conference). */
     30class DayNavigatorWidget : public QWidget, private Ui::DayNavigatorWidget {
    7531    Q_OBJECT
    7632    public:
     
    7834        ~DayNavigatorWidget() {}
    7935        void setDates(const QDate &aStartDate, const QDate &aEndDate);
     36        void setCurDate(const QDate& curDate);
    8037        QDate curDate() const {return mCurDate;}
    8138    protected:
     
    9249        QDate mEndDate;
    9350        QDate mCurDate;
    94         QFontMetrics *mFontMetrics;
    9551};
    9652
Note: See TracChangeset for help on using the changeset viewer.