2 #include "weightstatsview.h"
3 #include <QApplication>
9 WeightStatsView::WeightStatsView(WeightDataModel *wdm, QWidget *parent) :
10 QWidget(parent), wdm(wdm)
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()));
21 QWidget *container = new QWidget(this);
22 container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
23 QVBoxLayout *lo = new QVBoxLayout(container);
25 QFont smallerFont = QApplication::font();
26 smallerFont.setPointSize(smallerFont.pointSize() - 4);
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);
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);
48 max = new QLabel(container);
51 min = new QLabel(container);
58 static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
59 "Friday", "Saturday", "Sunday"};
60 QString dateString(QDate date)
62 int days = date.daysTo(QDate::currentDate());
64 return date.toString(Qt::ISODate);
70 return wdays[date.dayOfWeek()-1]; // Because locale might not be English
72 return date.toString(Qt::ISODate);
75 QString dateIntervalString(int days)
78 return QString("%1 days").arg(days);
80 return QString("%1m %2d").arg(days/30).arg(days%30);
82 return QString("1y 0m");
84 return QString("%1y %2m").arg(days/365).arg((days%365)/30);
88 void WeightStatsView::updateStats()
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");
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)
105 lastNote->setText(tr("(%1)").arg(dateString(l.date)));
107 change->setText(tr("Change: %1 %2")
108 .arg(l.weight-f.weight,0,'f',1)
110 changeNote->setText(tr("(%1)").arg(dateIntervalString(f.date.daysTo(l.date))));
112 max->setText(tr("Max: %1 %2").arg(wdm->maxWeight()).arg(unit));
113 min->setText(tr("Min: %1 %2").arg(wdm->minWeight()).arg(unit));