void Decision::setOpinions(QJSValue newOpinions) { if (newOpinions.strictlyEquals(opinions())) return; if (!newOpinions.isObject()) { qDebug() << "Cannot set opinions because opinions is not an object."; return; } // The only way I know of to determine how many opinions there are is to iterate them and count int opinionCount = 0; QJSValueIterator itr(newOpinions); while (itr.hasNext()) { itr.next(); ++opinionCount; } itr = newOpinions; // Now we actually allocate space for and set the opinions auto opinionList = m_decision.initOpinions(opinionCount); opinionCount = 0; while (itr.hasNext()) { itr.next(); auto builder = opinionList[opinionCount++]; builder.setContestant(itr.name().toInt()); builder.setOpinion(itr.value().toInt()); } emit opinionsChanged(); }
void Decision::setOpinions(QVariantMap newOpinions) { canonicalizeOpinions(newOpinions); if (newOpinions == opinions()) return; // Now we actually allocate space for and set the opinions auto opinionList = m_decision.initOpinions(newOpinions.size()); unsigned currentIndex = 0; while (!newOpinions.empty()) { auto builder = opinionList[currentIndex++]; auto key = newOpinions.firstKey(); builder.setContestant(key.toInt()); builder.setOpinion(newOpinions.take(key).toInt()); } emit opinionsChanged(); }