1 #include "weightstatsview.h"
8 WeightStatsView::WeightStatsView(WeightDataModel *wdm, QWidget *parent) :
9 QWidget(parent), wdm(wdm)
11 connect(wdm, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
12 this, SLOT(updateStats()));
13 connect(wdm, SIGNAL(rowsInserted(const QModelIndex&,int,int)),
14 this, SLOT(updateStats()));
15 connect(wdm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
16 this, SLOT(updateStats()));
17 connect(Settings::self(), SIGNAL(settingChanged()),
18 this, SLOT(updateStats()));
20 QVBoxLayout *lo = new QVBoxLayout(this);
22 last = new QLabel(this);
25 change = new QLabel(this);
26 lo->addWidget(change);
31 static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
32 "Friday", "Saturday", "Sunday"};
33 QString dateString(QDate date)
35 int days = date.daysTo(QDate::currentDate());
37 return date.toString(Qt::ISODate);
43 // return date.toString("dddd"); (Use this with l10n!)
44 // The following is used because the system locale might not be English
45 return wdays[date.dayOfWeek()-1];
47 return date.toString(Qt::ISODate);
50 QString dateIntervalString(int days)
53 return QString("%1 days").arg(days);
55 return QString("%1m %2d").arg(days/30).arg(days%30);
57 return QString("1y 0m");
59 return QString("%1y %2m").arg(days/365).arg((days%365)/30);
62 void WeightStatsView::updateStats()
64 if (wdm->size() == 0) {
65 last->setText("Last: No data");
66 change->setText("Change: No data");
69 QString unit = Settings::weightUnit();
70 const DW &f = wdm->getWeights().first();
71 const DW &l = wdm->getWeights().last();
72 last->setText(tr("Last: %1 %2\n (%3)")
73 .arg(l.weight,0,'f',1)
75 .arg(dateString(l.date)));
76 change->setText(tr("Change: %1 %2\n"
78 .arg(l.weight-f.weight,0,'f',1)
80 .arg(dateIntervalString(f.date.daysTo(l.date))));