]> ToastFreeware Gitweb - toast/confclerk.git/blobdiff - src/gui/daynavigatorwidget.cpp
Adjust shown event time when displayTimeShift is set.
[toast/confclerk.git] / src / gui / daynavigatorwidget.cpp
index 8db754df4df2df7d2923214ec00c84b6ba3622dc..82b34438156d8d869e4842228bd819f10dfc3531 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2010 Ixonos Plc.
- * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
+ * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
  *
  * This file is part of ConfClerk.
  *
 #include "daynavigatorwidget.h"
 
 #include <QPainter>
-#include <QFontMetrics>
 #include <QLabel>
+#include <conference.h>
+#include <application.h>
 
-#include <QDebug>
 
-DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent)
-    : QWidget(aParent)
-    , mStartDate(QDate())
-    , mEndDate(QDate())
-    , mCurDate(QDate())
-{
-    setupUi(this);
+DayNavigatorWidget::DayNavigatorWidget(QWidget *aParent): QWidget(aParent) {
+    setupUi(this);    
+
     connect(prevDayButton, SIGNAL(clicked()), SLOT(prevDayButtonClicked()));
     connect(nextDayButton, SIGNAL(clicked()), SLOT(nextDayButtonClicked()));
-    connect(todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
 
-    mFontMetrics = new QFontMetrics(QLabel().font());
+    configureNavigation();
 }
 
-void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate)
-{
-    Q_ASSERT(aStartDate<=aEndDate);
+
+void DayNavigatorWidget::setDates(const QDate &aStartDate, const QDate &aEndDate) {
+    Q_ASSERT(aStartDate.isValid() && aEndDate.isValid() && aStartDate<=aEndDate);
 
     mStartDate = aStartDate;
     mEndDate = aEndDate;
-    mCurDate = aStartDate;
 
-    // QRect rect = mFontMetrics->boundingRect(mCurDate.toString("MMM dd yyyy"));
+    if (!mCurDate.isValid()) mCurDate = mStartDate;
+    // if mCurDate is out of range, set it to mstartDate
+    else if (mCurDate < mStartDate || mCurDate > mEndDate) mCurDate = mStartDate;
 
-    if(mStartDate==mEndDate) // only one day conference
-    {
-        prevDayButton->setDisabled(true);
-        nextDayButton->setDisabled(true);
-        emit(dateChanged(mCurDate));
-    }
-    else
-    {
-        // at least 2-days conference
-        prevDayButton->setDisabled(true);
-        nextDayButton->setDisabled(false);
-        emit(dateChanged(mCurDate));
+    configureNavigation();
+    emit(dateChanged(mCurDate));
+    this->update();
+}
+
+
+void DayNavigatorWidget::setCurDate(const QDate& curDate) {
+    Q_ASSERT(curDate.isValid());
+    if (curDate == mCurDate) return;
+
+    if (!mStartDate.isValid()) {
+        // the start and end date have not been set
+        mStartDate = curDate;
+        mEndDate = curDate;
+        mCurDate = curDate;
     }
+
+    else if (curDate < mStartDate) mCurDate = mStartDate;
+    else if (curDate > mEndDate) mCurDate = mEndDate;
+    else mCurDate = curDate;
+
+    configureNavigation();
+    emit(dateChanged(mCurDate));
+    this->update();
 }
 
-void DayNavigatorWidget::configureNavigation()
-{
-    // check start date
-    if(mCurDate==mStartDate || mStartDate==mEndDate)
-        prevDayButton->setDisabled(true);
-    else
-        prevDayButton->setDisabled(false);
-    // check end date
-    if(mCurDate==mEndDate || mStartDate==mEndDate)
-        nextDayButton->setDisabled(true);
-    else
-        nextDayButton->setDisabled(false);
+
+void DayNavigatorWidget::unsetDates() {
+    mStartDate= QDate();
+    mEndDate = QDate();
+    mCurDate = QDate();
+
+    configureNavigation();
+    emit(dateChanged(mCurDate));
+    this->update();
 }
 
-void DayNavigatorWidget::prevDayButtonClicked()
-{
-    if(mCurDate>mStartDate)
-    {
-        mCurDate = mCurDate.addDays(-1);
-        configureNavigation();
-        emit(dateChanged(mCurDate));
-        this->update();
-    }
+
+void DayNavigatorWidget::configureNavigation() {
+    prevDayButton->setDisabled(!mStartDate.isValid() || mCurDate == mStartDate);
+    nextDayButton->setDisabled(!mEndDate.isValid() || mCurDate == mEndDate);
 }
 
-void DayNavigatorWidget::nextDayButtonClicked()
-{
-    if(mCurDate<mEndDate)
-    {
-        mCurDate = mCurDate.addDays(1);
-        configureNavigation();
-        emit(dateChanged(mCurDate));
-        this->update();
-    }
+
+void DayNavigatorWidget::prevDayButtonClicked() {
+    if(mCurDate <= mStartDate) return;
+    mCurDate = mCurDate.addDays(-1);
+    configureNavigation();
+    emit(dateChanged(mCurDate));
+    this->update();
 }
 
-void DayNavigatorWidget::todayButtonClicked()
-{
-    QDate targetDate = QDate::currentDate();
-    if (targetDate>mStartDate && targetDate<mEndDate)
-    {
-        mCurDate = targetDate;
-        configureNavigation();
-        emit(dateChanged(mCurDate));
-        this->update();
-    }
+
+void DayNavigatorWidget::nextDayButtonClicked() {
+    if(mCurDate >= mEndDate) return;
+    mCurDate = mCurDate.addDays(1);
+    configureNavigation();
+    emit(dateChanged(mCurDate));
+    this->update();
 }
 
-void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
-{
-    Q_UNUSED(aEvent);
 
-    QString selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
+void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent) {
+    Q_UNUSED(aEvent);
+    QString selectedDateStr;
+    if (mCurDate.isValid()) {
+        selectedDateStr = mCurDate.toString("dddd\nyyyy-MM-dd");
+        bool hasConference = ((Application*) qApp)->hasActiveConference();
+        if (hasConference) {
+            Conference& conference = ((Application*) qApp)->activeConference();
+            if (conference.hasDisplayTimeShift() && conference.displayTimeShift() != 0) {
+                QTime shift(0, 0);
+                bool minus = conference.displayTimeShift() < 0;
+                shift = shift.addSecs(conference.displayTimeShift() * 60 * (minus ? -1 : 1));
+                selectedDateStr += " (" + (minus ? QString("-") : "+") + shift.toString("HH:mm") + ")";
+            }
+        }
+    } else {
+        selectedDateStr = tr("No date");
+    }
     QPainter painter(this);
     painter.save();
 
     // rectangle only for the text
-    int marginSize = 9;
-    int buttonSize = 32;
-#ifdef MAEMO
-    QRect q(y()-height()+1*marginSize+2.5*buttonSize, x(), height()-2*marginSize-2.5*buttonSize, width());
-#else
-    QRect q(y()-height()+1*marginSize+2*buttonSize, x(), height()-2*marginSize-3*buttonSize, width());
-#endif
+    QRect q(-selectedDate->height()-selectedDate->y(), selectedDate->x(), selectedDate->height(), selectedDate->width());
     painter.rotate(270);
 
     // font size adjustion, static on maemo, dynamically otherwise
@@ -135,7 +137,12 @@ void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
 #ifdef MAEMO
     qreal factor = 0.8;
 #else
-    qreal factor = (qreal) 2 * q.width() / painter.fontMetrics().width(selectedDateStr);
+#if QT_VERSION >= 0x050b00 // QT 5.11
+    auto dateStrWidth = painter.fontMetrics().horizontalAdvance(selectedDateStr);
+#else
+    auto dateStrWidth = painter.fontMetrics().width(selectedDateStr);
+#endif
+    qreal factor = (qreal) 2 * q.width() / dateStrWidth;
 #endif
     if (factor < 1) f.setPointSizeF(f.pointSizeF() * factor);
     painter.setFont(f);
@@ -143,4 +150,3 @@ void DayNavigatorWidget::paintEvent(QPaintEvent *aEvent)
     painter.drawText(q, Qt::AlignCenter, selectedDateStr);
     painter.restore();
 }
-