Add data and add functions for WeightModel

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-05-01 10:11:46 -07:00
parent 730e055e5a
commit 5c2ce0ac20
2 changed files with 39 additions and 3 deletions

View file

@ -149,7 +149,29 @@ int WeightModel::columnCount(const QModelIndex& parent) const
QVariant WeightModel::data(const QModelIndex& index, int role) const QVariant WeightModel::data(const QModelIndex& index, int role) const
{ {
return QVariant(); QVariant ret;
if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) {
return ret;
}
weightsystem_t *ws = &current_dive->weightsystem[index.row()];
if (role == Qt::DisplayRole) {
switch(index.column()) {
case TYPE:
ret = QString(ws->description);
break;
case WEIGHT:
if (get_units()->weight == units::KG) {
int gr = ws->weight.grams % 1000;
int kg = ws->weight.grams / 1000;
ret = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100);
} else {
ret = QString("%1").arg((unsigned)(grams_to_lbs(ws->weight.grams) + 0.5));
}
break;
}
}
return ret;
} }
int WeightModel::rowCount(const QModelIndex& parent) const int WeightModel::rowCount(const QModelIndex& parent) const
@ -175,8 +197,22 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r
return ret; return ret;
} }
void WeightModel::add(weight_t* weight) void WeightModel::add(weightsystem_t* weight)
{ {
if (usedRows[current_dive] >= MAX_WEIGHTSYSTEMS) {
free(weight);
}
int row = usedRows[current_dive];
weightsystem_t *ws = &current_dive->weightsystem[row];
ws->description = weight->description;
ws->weight.grams = weight->weight.grams;
beginInsertRows(QModelIndex(), row, row);
usedRows[current_dive]++;
endInsertRows();
} }
void WeightModel::update() void WeightModel::update()

View file

@ -62,7 +62,7 @@ class WeightModel : public QAbstractTableModel {
/*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
/*reimp*/ int rowCount(const QModelIndex& parent = QModelIndex()) const; /*reimp*/ int rowCount(const QModelIndex& parent = QModelIndex()) const;
void add(weight_t *weight); void add(weightsystem_t *weight);
void clear(); void clear();
void update(); void update();
private: private: