bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role) { weightsystem_t *ws = ¤t->weightsystem[index.row()]; switch(index.column()) { case TYPE: if (!value.isNull()) { QByteArray ba = value.toString().toUtf8(); const char *text = ba.constData(); if (!ws->description || strcmp(ws->description, text)) { ws->description = strdup(text); changed = true; } } break; case WEIGHT: if (CHANGED(toDouble, "kg", "lbs")) { if (prefs.units.weight == prefs.units.LBS) ws->weight.grams = lbs_to_grams(value.toDouble()); else ws->weight.grams = value.toDouble() * 1000.0 + 0.5; // now update the ws_info changed = true; WSInfoModel *wsim = WSInfoModel::instance(); QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, ws->description); if (!matches.isEmpty()) wsim->setData(wsim->index(matches.first().row(), WSInfoModel::GR), ws->weight.grams); } break; } dataChanged(index, index); return true; }
void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const { WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model); WSInfoModel *wsim = WSInfoModel::instance(); QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, currCombo.activeText); int row; if (matches.isEmpty()) { // we need to add this puppy wsim->insertRows(wsim->rowCount(), 1); wsim->setData(wsim->index(wsim->rowCount() - 1, 0), currCombo.activeText); row = wsim->rowCount() - 1; } else { row = matches.first().row(); } int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt(); QVariant v = QString(currCombo.activeText); // don't set if it's the same as it was before setting. if (mymodel->data(thisindex, WeightModel::TYPE).toString() == currCombo.activeText){ return; } mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole); mymodel->passInData(IDX(WeightModel::WEIGHT), grams); qDebug() << "Fixme, every weigth is 0.0 grams. see:" << grams; }
void WSInfoDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &thisindex) const { WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model); WSInfoModel *wsim = WSInfoModel::instance(); QModelIndexList matches = wsim->match(wsim->index(0, 0), Qt::DisplayRole, currCombo.activeText); int row; if (matches.isEmpty()) { // we need to add this puppy wsim->insertRows(wsim->rowCount(), 1); wsim->setData(wsim->index(wsim->rowCount() - 1, 0), currCombo.activeText); row = wsim->rowCount() - 1; } else { row = matches.first().row(); } int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt(); QVariant v = QString(currCombo.activeText); mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole); mymodel->passInData(IDX(WeightModel::WEIGHT), grams); }
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role) { QString vString = value.toString(); weightsystem_t *ws = &displayed_dive.weightsystem[index.row()]; switch (index.column()) { case TYPE: if (!value.isNull()) { //TODO: C-function weigth_system_set_description ? if (!ws->description || gettextFromC::instance()->tr(ws->description) != vString) { // loop over translations to see if one matches int i = -1; while (ws_info[++i].name) { if (gettextFromC::instance()->tr(ws_info[i].name) == vString) { ws->description = ws_info[i].name; break; } } if (ws_info[i].name == NULL) // didn't find a match ws->description = strdup(vString.toUtf8().constData()); changed = true; } } break; case WEIGHT: if (CHANGED()) { ws->weight = string_to_weight(vString.toUtf8().data()); // now update the ws_info changed = true; WSInfoModel *wsim = WSInfoModel::instance(); QModelIndexList matches = wsim->match(wsim->index(0, 0), Qt::DisplayRole, gettextFromC::instance()->tr(ws->description)); if (!matches.isEmpty()) wsim->setData(wsim->index(matches.first().row(), WSInfoModel::GR), ws->weight.grams); } break; } dataChanged(index, index); return true; }