Skip to content

Commit 7acd483

Browse files
authored
Merge pull request #1844 from cyrossignol/polls
gui: Fix display of polls with no votes yet
2 parents 18f76bf + 33e2fba commit 7acd483

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/qt/votingdialog.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ VotingItem* BuildPollItem(const NN::PollRegistry::Sequence::Iterator& iter)
261261
result->m_responses[i].m_votes);
262262
}
263263

264-
item->bestAnswer_ = QString::fromStdString(result->WinnerLabel()).replace("_"," ");
264+
if (!result->m_votes.empty()) {
265+
item->bestAnswer_ = QString::fromStdString(result->WinnerLabel()).replace("_"," ");
266+
}
265267

266268
return item;
267269
}
@@ -720,35 +722,29 @@ void VotingChartDialog::resetData(const VotingItem *item)
720722
question_->setText(item->question_);
721723
url_->setText("<a href=\""+item->url_+"\">"+item->url_+"</a>");
722724
answer_->setText(item->bestAnswer_);
725+
answer_->setVisible(!item->bestAnswer_.isEmpty());
726+
answerTable_->setRowCount(item->vectorOfAnswers_.size());
723727

724-
std::vector<polling::Vote> vectorOfAnswers = item->vectorOfAnswers_;
725-
answerTable_->setRowCount(vectorOfAnswers.size());
726-
std::vector<int> iShares;
727-
std::vector<QString> sAnswerNames;
728-
int sharesSum = 0;
729-
//for(size_t y=1; y < vAnswers.size(); y++)
730-
for(polling::Vote iterAnswer: vectorOfAnswers)
728+
for (size_t y = 0; y < item->vectorOfAnswers_.size(); y++)
731729
{
732-
sAnswerNames.push_back(QString::fromStdString(iterAnswer.answer).replace("_"," "));
733-
iShares.push_back(iterAnswer.shares);
734-
sharesSum += iterAnswer.shares;
735-
}
730+
const auto& responses = item->vectorOfAnswers_;
731+
const QString answer = QString::fromStdString(responses[y].answer);
736732

737-
// Protect against possible divide by zero below.
738-
if (!sharesSum) return;
739-
740-
for(size_t y=0; y < sAnswerNames.size(); y++)
741-
{
742-
answerTable_->setItem(y, 0, new QTableWidgetItem(sAnswerNames[y]));
733+
answerTable_->setItem(y, 0, new QTableWidgetItem(answer));
743734
QTableWidgetItem *iSharesItem = new QTableWidgetItem();
744-
iSharesItem->setData(Qt::DisplayRole,iShares[y]);
735+
iSharesItem->setData(Qt::DisplayRole, responses[y].shares);
745736
answerTable_->setItem(y, 1, iSharesItem);
746737
QTableWidgetItem *percentItem = new QTableWidgetItem();
747-
percentItem->setData(Qt::DisplayRole,(float)iShares[y]/(float)sharesSum*100);
738+
739+
if (item->totalShares_ > 0) {
740+
const double ratio = responses[y].shares / (double)item->totalShares_;
741+
percentItem->setData(Qt::DisplayRole, ratio * 100);
742+
}
743+
748744
answerTable_->setItem(y, 2, percentItem);
749745

750746
#ifdef QT_CHARTS_LIB
751-
QtCharts::QPieSlice *slice = new QtCharts::QPieSlice(sAnswerNames[y], iShares[y]);
747+
QtCharts::QPieSlice *slice = new QtCharts::QPieSlice(answer, responses[y].shares);
752748
series->append(slice);
753749
chart_->addSeries(series);
754750
#endif

src/rpcvoting.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ UniValue PollResultToJson(const PollResult& result, const PollReference& poll_re
8282
json.pushKV("votes", (uint64_t)poll_ref.Votes().size());
8383
json.pushKV("invalid_votes", (uint64_t)result.m_invalid_votes);
8484
json.pushKV("total_weight", ValueFromAmount(result.m_total_weight));
85-
json.pushKV("top_choice_id", (uint64_t)result.Winner());
86-
json.pushKV("top_choice", result.WinnerLabel());
85+
86+
if (!result.m_votes.empty()) {
87+
json.pushKV("top_choice_id", (uint64_t)result.Winner());
88+
json.pushKV("top_choice", result.WinnerLabel());
89+
} else {
90+
json.pushKV("top_choice_id", NullUniValue);
91+
json.pushKV("top_choice", NullUniValue);
92+
}
8793

8894
UniValue responses(UniValue::VARR);
8995

0 commit comments

Comments
 (0)