readFromDisk();
}
-void WeightDataModel::clear()
-{
- weights.clear();
-}
-
Qt::ItemFlags WeightDataModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
}
}
+QVariant WeightDataModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if (role != Qt::DisplayRole)
+ return QVariant();
+ if (orientation == Qt::Horizontal) {
+ return section == 0 ? tr("Date") : tr("Weight");
+ }
+ else {
+ return QString("%1").arg(section);
+ }
+}
+
+bool WeightDataModel::dateExists(const QDate &date) const
+{
+ return rowOfDate(date) != -1;
+}
+
+int WeightDataModel::rowOfDate(const QDate &date) const
+{
+ // TODO: binary search
+ for(int i=0; i<weights.size(); i++)
+ if (weights[i].date == date)
+ return i;
+ return -1;
+}
+
+QModelIndex WeightDataModel::indexOfDate(const QDate &date) const
+{
+ return index(rowOfDate(date), 0);
+}
+
QVariant WeightDataModel::data(const QModelIndex &index, int role) const
{
- if (!index.isValid() || index.row() >= rowCount(QModelIndex())
- || index.column() >= columnCount(QModelIndex()))
+ if (!index.isValid() || index.row() >= rowCount()
+ || index.column() >= columnCount())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::SizeHintRole)
return QVariant();
}
+double WeightDataModel::minWeight() const {
+ // TODO: cache minimum and maximum weight on initial read and modifications?
+ double min = std::numeric_limits<double>::max();
+ foreach(const DW& dw, weights) {
+ if (dw.weight < min)
+ min = dw.weight;
+ }
+ return min;
+}
+
+double WeightDataModel::maxWeight() const {
+ double max = std::numeric_limits<double>::min();
+ foreach(const DW& dw, weights) {
+ if (dw.weight > max)
+ max = dw.weight;
+ }
+ return max;
+}
+
bool WeightDataModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || role != Qt::EditRole)
return false;
switch (index.column()) {
- case 0:
- weights[index.row()].date = value.toDate();
+ case 0: {
+ QDate date = value.toDate();
+ if (!date.isValid())
+ return false;
+ weights[index.row()].date = date;
break;
+ }
case 1: {
- double weight = value.toDouble();
+ bool ok;
+ double weight = value.toDouble(&ok);
+ if (!ok)
+ return false;
weights[index.row()].weight = weight;
break;
}
DateWeight dw = {date, weight};
setDataForRow(row, dw);
}
-
-void WeightDataModel::setWeightForDate(const WeightDataModel::DateWeight &dw)
+int WeightDataModel::rowForNewDate(const QDate &date) const
{
- setWeightForDate(dw.date, dw.weight);
+ if (weights.size() == 0)
+ return 0;
+ if (date < weights.first().date)
+ return 0;
+ // TODO: binary search
+ for(int i=1; i<weights.size(); i++) {
+ if (weights.at(i-1).date < date && weights.at(i).date > date)
+ return i;
+ }
+ if (date > weights.last().date)
+ return weights.size();
+ assert(0 && "UNREACHABLE");
+ return -1;
}
-QVariant WeightDataModel::headerData(int section, Qt::Orientation orientation, int role) const
+void WeightDataModel::setWeightForDate(const WeightDataModel::DateWeight &dw)
{
- if (role != Qt::DisplayRole)
- return QVariant();
- if (orientation == Qt::Horizontal) {
- return section == 0 ? tr("Date") : tr("Weight");
- }
- else {
- return QString("%1").arg(section);
- }
+ setWeightForDate(dw.date, dw.weight);
}
// Insert count "empty" rows starting from row.
{
beginInsertRows(QModelIndex(), row, row+count-1);
DateWeight empty;
- //empty.date.setDate(2000,1,1);
- empty.weight = 0.0;
while(count--)
weights.insert(row, empty);
endInsertRows();
return true;
}
-//Returns the row of the date or -1 if it doesn't exist.
-int WeightDataModel::rowOfDate(const QDate &date) const
-{
- for(int i=0; i<weights.size(); i++)
- if (weights[i].date == date)
- return i;
- return -1;
-}
-QModelIndex WeightDataModel::indexOfDate(const QDate &date) const
-{
- return index(rowOfDate(date), 0);
-}
-
-bool WeightDataModel::dateExists(const QDate &date) const
-{
- return rowOfDate(date) != -1;
-}
-
-int WeightDataModel::rowForNewDate(const QDate &date) const
+void WeightDataModel::clear()
{
- if (weights.size() == 0)
- return 0;
- if (date < weights.first().date)
- return 0;
- for(int i=1; i<weights.size(); i++) {
- if (weights.at(i-1).date < date
- && weights.at(i).date > date)
- return i;
- }
- if (date > weights.last().date)
- return weights.size();
- assert(0 && "UNREACHABLE");
- return -1;
+ weights.clear();
}
void WeightDataModel::writeToDisk()