Release 1.0.4
[weightgraph] / weightgraph / weightstatsview.cpp
1 #include "settings.h"
2 #include "weightstatsview.h"
3 #include <QApplication>
4 #include <QGroupBox>
5 #include <QVBoxLayout>
6
7 #include <QDebug>
8
9 WeightStatsView::WeightStatsView(WeightDataModel *wdm, QWidget *parent) :
10   QWidget(parent), wdm(wdm)
11 {
12   connect(wdm, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
13           this, SLOT(updateStats()));
14   connect(wdm, SIGNAL(rowsInserted(const QModelIndex&,int,int)),
15           this, SLOT(updateStats()));
16   connect(wdm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
17           this, SLOT(updateStats()));
18   connect(Settings::self(), SIGNAL(settingChanged()),
19           this, SLOT(updateStats()));
20
21   QWidget *container = new QWidget(this);
22   container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
23   QVBoxLayout *lo = new QVBoxLayout(container);
24
25   QFont smallerFont = QApplication::font();
26   smallerFont.setPointSize(smallerFont.pointSize() - 4);
27
28   QWidget *lastContainer = new QWidget(container);
29   QHBoxLayout *lastLayout = new QHBoxLayout(lastContainer);
30   lastLayout->setMargin(0);
31   last = new QLabel(lastContainer);
32   lastLayout->addWidget(last, 0, Qt::AlignVCenter);
33   lastNote = new QLabel(lastContainer);
34   lastNote->setFont(smallerFont);
35   lastLayout->addWidget(lastNote, 0, Qt::AlignVCenter);
36   lo->addWidget(lastContainer);
37
38   QWidget *changeContainer = new QWidget(container);
39   QHBoxLayout *changeLayout = new QHBoxLayout(changeContainer);
40   changeLayout->setMargin(0);
41   change = new QLabel(changeContainer);
42   changeLayout->addWidget(change, 0, Qt::AlignVCenter);
43   changeNote = new QLabel(changeContainer);
44   changeNote->setFont(smallerFont);
45   changeLayout->addWidget(changeNote, 0, Qt::AlignVCenter);
46   lo->addWidget(changeContainer);
47
48   max = new QLabel(container);
49   lo->addWidget(max);
50
51   min = new QLabel(container);
52   lo->addWidget(min);
53
54   updateStats();
55 }
56
57 namespace {
58   static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
59                                 "Friday", "Saturday", "Sunday"};
60   QString dateString(QDate date)
61   {
62     int days = date.daysTo(QDate::currentDate());
63     if (days < 0)
64       return date.toString(Qt::ISODate);
65     else if (days == 0)
66       return "Today";
67     else if (days == 1)
68       return "Yesterday";
69     else if (days < 7)
70       return wdays[date.dayOfWeek()-1]; // Because locale might not be English
71     else
72       return date.toString(Qt::ISODate);
73   }
74
75   QString dateIntervalString(int days)
76   {
77     if (days < 30)
78       return QString("%1 days").arg(days);
79     else if (days < 360)
80       return QString("%1m %2d").arg(days/30).arg(days%30);
81     else if (days < 365)
82       return QString("1y 0m");
83     else
84       return QString("%1y %2m").arg(days/365).arg((days%365)/30);
85   }
86 }
87
88 void WeightStatsView::updateStats()
89 {
90   if (wdm->size() == 0) {
91     last->setText("Last: No data");
92     lastNote->setText("");
93     change->setText("Change: No data");
94     changeNote->setText("");
95     max->setText("Max: No data");
96     min->setText("Min: No data");
97     return;
98   }
99   QString unit = Settings::weightUnit();
100   const DW &f = wdm->getWeights().first();
101   const DW &l = wdm->getWeights().last();
102   last->setText(tr("Last: %1 %2")
103                 .arg(l.weight,0,'f',1)
104                 .arg(unit));
105   lastNote->setText(tr("(%1)").arg(dateString(l.date)));
106
107   change->setText(tr("Change: %1 %2")
108                   .arg(l.weight-f.weight,0,'f',1)
109                   .arg(unit));
110   changeNote->setText(tr("(%1)").arg(dateIntervalString(f.date.daysTo(l.date))));
111
112   max->setText(tr("Max: %1 %2").arg(wdm->maxWeight()).arg(unit));
113   min->setText(tr("Min: %1 %2").arg(wdm->minWeight()).arg(unit));
114 }