X-Git-Url: http://git.maemo.org/git/?p=weightgraph;a=blobdiff_plain;f=weightgraph%2Fweightdata.cpp;h=248744e199fc5be7e77c2be967605c45d1c5e000;hp=23c18e387dd5b5be1a05a646fde21561092c0aee;hb=5ff06d66b2b038664c78049e8f2cb3c2c61bc3a1;hpb=e13ccfc0b642e8d04d77e32354b0083f8ad26897 diff --git a/weightgraph/weightdata.cpp b/weightgraph/weightdata.cpp index 23c18e3..248744e 100644 --- a/weightgraph/weightdata.cpp +++ b/weightgraph/weightdata.cpp @@ -15,11 +15,6 @@ WeightDataModel::WeightDataModel(QString &datafilename, QObject *parent) : readFromDisk(); } -void WeightDataModel::clear() -{ - weights.clear(); -} - Qt::ItemFlags WeightDataModel::flags(const QModelIndex &index) const { if(!index.isValid()) @@ -31,10 +26,41 @@ Qt::ItemFlags WeightDataModel::flags(const QModelIndex &index) const } } +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= 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) @@ -61,16 +87,42 @@ QVariant WeightDataModel::data(const QModelIndex &index, int role) const return QVariant(); } +double WeightDataModel::minWeight() const { + // TODO: cache minimum and maximum weight on initial read and modifications? + double min = std::numeric_limits::max(); + foreach(const DW& dw, weights) { + if (dw.weight < min) + min = dw.weight; + } + return min; +} + +double WeightDataModel::maxWeight() const { + double max = std::numeric_limits::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; } @@ -107,22 +159,26 @@ void WeightDataModel::setWeightForDate(const QDate &date, double weight) 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 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. @@ -132,8 +188,6 @@ bool WeightDataModel::insertRows(int row, int count, const QModelIndex &/*parent { 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(); @@ -150,45 +204,18 @@ bool WeightDataModel::removeRows(int row, int count, const QModelIndex &/*parent 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 date) - return i; - } - if (date > weights.last().date) - return weights.size(); - assert(0 && "UNREACHABLE"); - return -1; + weights.clear(); } void WeightDataModel::writeToDisk() { if (QFile::exists(datafile.fileName())) { - QFile::copy(datafile.fileName(), datafile.fileName()+".bak"); + QString backupfile = datafile.fileName()+".bak"; + if (QFile::exists(backupfile)) + QFile::remove(backupfile); + QFile::copy(datafile.fileName(), backupfile); } if (datafile.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&datafile); @@ -210,6 +237,9 @@ void WeightDataModel::readFromDisk() QString line = stream.readLine(); while (!line.isNull()) { QStringList parts = line.split(';'); + if (parts.size() != 2) { + throw(QString("Invalid line in file: '%1'").arg(line)); + } DateWeight w; w.date = QDate::fromString(parts[0], Qt::ISODate); if (!w.date.isValid()) {