Move "List/Edit" button to menu bar and add min/max weight stats
[weightgraph] / weightgraph / weightstatsview.cpp
index 0ab0362..cb3505f 100644 (file)
@@ -1,7 +1,8 @@
-#include "weightstatsview.h"
 #include "settings.h"
-#include <QVBoxLayout>
+#include "weightstatsview.h"
+#include <QApplication>
 #include <QGroupBox>
+#include <QVBoxLayout>
 
 #include <QDebug>
 
@@ -17,65 +18,97 @@ WeightStatsView::WeightStatsView(WeightDataModel *wdm, QWidget *parent) :
   connect(Settings::self(), SIGNAL(settingChanged()),
           this, SLOT(updateStats()));
 
-  QVBoxLayout *lo = new QVBoxLayout(this);
+  QWidget *container = new QWidget(this);
+  container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
+  QVBoxLayout *lo = new QVBoxLayout(container);
+
+  QFont smallerFont = QApplication::font();
+  smallerFont.setPointSize(smallerFont.pointSize() - 4);
+
+  QWidget *lastContainer = new QWidget(container);
+  QHBoxLayout *lastLayout = new QHBoxLayout(lastContainer);
+  lastLayout->setMargin(0);
+  last = new QLabel(lastContainer);
+  lastLayout->addWidget(last, 0, Qt::AlignVCenter);
+  lastNote = new QLabel(lastContainer);
+  lastNote->setFont(smallerFont);
+  lastLayout->addWidget(lastNote, 0, Qt::AlignVCenter);
+  lo->addWidget(lastContainer);
+
+  QWidget *changeContainer = new QWidget(container);
+  QHBoxLayout *changeLayout = new QHBoxLayout(changeContainer);
+  changeLayout->setMargin(0);
+  change = new QLabel(changeContainer);
+  changeLayout->addWidget(change, 0, Qt::AlignVCenter);
+  changeNote = new QLabel(changeContainer);
+  changeNote->setFont(smallerFont);
+  changeLayout->addWidget(changeNote, 0, Qt::AlignVCenter);
+  lo->addWidget(changeContainer);
 
-  last = new QLabel(this);
-  lo->addWidget(last);
+  min = new QLabel(container);
+  lo->addWidget(min);
 
-  change = new QLabel(this);
-  lo->addWidget(change);
+  max = new QLabel(container);
+  lo->addWidget(max);
 
   updateStats();
 }
 
-static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
-                              "Friday", "Saturday", "Sunday"};
-QString dateString(QDate date)
-{
-  int days = date.daysTo(QDate::currentDate());
-  if (days < 0)
-    return date.toString(Qt::ISODate);
-  else if (days == 0)
-    return "Today";
-  else if (days == 1)
-    return "Yesterday";
-  else if (days < 7)
-    // return date.toString("dddd"); (Use this with l10n!)
-    // The following is used because the system locale might not be English
-    return wdays[date.dayOfWeek()-1];
-  else
-    return date.toString(Qt::ISODate);
-}
+namespace {
+  static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
+                                "Friday", "Saturday", "Sunday"};
+  QString dateString(QDate date)
+  {
+    int days = date.daysTo(QDate::currentDate());
+    if (days < 0)
+      return date.toString(Qt::ISODate);
+    else if (days == 0)
+      return "Today";
+    else if (days == 1)
+      return "Yesterday";
+    else if (days < 7)
+      return wdays[date.dayOfWeek()-1]; // Because locale might not be English
+    else
+      return date.toString(Qt::ISODate);
+  }
 
-QString dateIntervalString(int days)
-{
-  if (days < 30)
-    return QString("%1 days").arg(days);
-  else if (days < 360)
-    return QString("%1m %2d").arg(days/30).arg(days%30);
-  else if (days < 365)
-    return QString("1y 0m");
-  else
-    return QString("%1y %2m").arg(days/365).arg((days%365)/30);
+  QString dateIntervalString(int days)
+  {
+    if (days < 30)
+      return QString("%1 days").arg(days);
+    else if (days < 360)
+      return QString("%1m %2d").arg(days/30).arg(days%30);
+    else if (days < 365)
+      return QString("1y 0m");
+    else
+      return QString("%1y %2m").arg(days/365).arg((days%365)/30);
+  }
 }
 
 void WeightStatsView::updateStats()
 {
   if (wdm->size() == 0) {
     last->setText("Last: No data");
+    lastNote->setText("");
     change->setText("Change: No data");
+    changeNote->setText("");
+    min->setText("Min: No data");
+    max->setText("Max: No data");
     return;
   }
   QString unit = Settings::weightUnit();
   const DW &f = wdm->getWeights().first();
   const DW &l = wdm->getWeights().last();
-  last->setText(tr("Last: %1 %2\n     (%3)")
+  last->setText(tr("Last: %1 %2")
                 .arg(l.weight,0,'f',1)
-                .arg(unit)
-                .arg(dateString(l.date)));
-  change->setText(tr("Change: %1 %2\n"
-                     "     (in %3)")
+                .arg(unit));
+  lastNote->setText(tr("(%1)").arg(dateString(l.date)));
+
+  change->setText(tr("Change: %1 %2")
                   .arg(l.weight-f.weight,0,'f',1)
-                  .arg(unit)
-                  .arg(dateIntervalString(f.date.daysTo(l.date))));
+                  .arg(unit));
+  changeNote->setText(tr("(%1)").arg(dateIntervalString(f.date.daysTo(l.date))));
+
+  min->setText(tr("Min: %1 %2").arg(wdm->minWeight()).arg(unit));
+  max->setText(tr("Max: %1 %2").arg(wdm->maxWeight()).arg(unit));
 }