Skip to content

Commit

Permalink
Merge pull request #2605 from jamescowens/implement_my_vote_info
Browse files Browse the repository at this point in the history
gui, voting: Implement information for wallet holder's votes on poll info cards
  • Loading branch information
jamescowens authored Nov 25, 2022
2 parents 48c82b4 + 11ff878 commit 36a34a0
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 29 deletions.
24 changes: 23 additions & 1 deletion src/gridcoin/voting/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "gridcoin/researcher.h"
#include "txdb.h"
#include "util/reverse_iterator.h"
#include "wallet/wallet.h"
#include "init.h"

#include <optional>
#include <queue>
Expand Down Expand Up @@ -100,6 +102,16 @@ class VoteCandidate
return m_payload.As<GRC::LegacyVote>();
}

//!
//! \brief returns IsMine flag on transaction containing vote contract
//!
isminetype IsMine() const
{
LOCK(pwalletMain->cs_wallet);

return pwalletMain->IsMine(m_tx);
}

//!
//! \brief Serialize a vote for claim signing and verification.
//!
Expand Down Expand Up @@ -162,6 +174,7 @@ class VoteResolver

VoteDetail detail;
detail.m_amount = Resolve(vote.m_claim.m_balance_claim, message);
detail.m_ismine = candidate.IsMine();

if (m_poll.IncludesMagnitudeWeight()) {
detail.m_mining_id = vote.m_claim.m_magnitude_claim.m_mining_id;
Expand Down Expand Up @@ -1112,6 +1125,8 @@ PollResult::PollResult(Poll poll)
, m_vote_percent_avw()
, m_poll_results_validated()
, m_finished(m_poll.Expired(GetAdjustedTime()))
, m_self_voted(false)
, m_self_vote_detail()
{
m_responses.resize(m_poll.Choices().size());
}
Expand Down Expand Up @@ -1174,6 +1189,11 @@ void PollResult::TallyVote(VoteDetail detail)
return;
}

if (detail.m_ismine != ISMINE_NO) {
m_self_voted = true;
m_self_vote_detail = detail;
}

for (const auto& response_pair : detail.m_responses) {
const uint8_t response_offset = response_pair.first;
const Weight response_weight = response_pair.second;
Expand All @@ -1198,11 +1218,13 @@ ResponseDetail::ResponseDetail() : m_weight(0), m_votes(0)
// Class: PollResult::VoteDetail
// -----------------------------------------------------------------------------

VoteDetail::VoteDetail() : m_amount(0), m_magnitude(Magnitude::Zero())
VoteDetail::VoteDetail() : m_amount(0), m_magnitude(Magnitude::Zero()), m_ismine(ISMINE_NO)
{
}

bool VoteDetail::Empty() const
{
return m_amount == 0 && m_magnitude == 0;
}

VoteDetail& VoteDetail::operator=(const VoteDetail& b) = default;
6 changes: 6 additions & 0 deletions src/gridcoin/voting/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef GRIDCOIN_VOTING_RESULT_H
#define GRIDCOIN_VOTING_RESULT_H

#include "wallet/ismine.h"
#include "gridcoin/cpid.h"
#include "gridcoin/magnitude.h"
#include "gridcoin/voting/fwd.h"
Expand Down Expand Up @@ -57,6 +58,7 @@ class PollResult
Weight m_amount; //!< Total balance resolved for the vote.
MiningId m_mining_id; //!< CPID for the vote, if any.
Magnitude m_magnitude; //!< Magnitude resolved for the vote.
isminetype m_ismine; //!< True if the vote is from the wallet holder.

//!
//! \brief The selected poll choice offsets and the associated voting
Expand All @@ -75,6 +77,8 @@ class PollResult
//! \return \c true if the vote claims no balance or magnitude weight.
//!
bool Empty() const;

VoteDetail& operator=(const VoteDetail& b);
};

const Poll m_poll; //!< The poll associated with the result.
Expand All @@ -85,6 +89,8 @@ class PollResult
std::optional<double> m_vote_percent_avw; //!< Vote weight percent of AVW.
std::optional<bool> m_poll_results_validated; //!< Whether the poll's AVW is >= the minimum AVW for the poll.
bool m_finished; //!< Whether the poll finished as of this result.
bool m_self_voted; //!< Whether the wallet holder voted.
VoteDetail m_self_vote_detail; //!< The vote detail from the wallet holder's (last) vote

//!
//! \brief The aggregated voting weight tallied for each poll choice.
Expand Down
98 changes: 70 additions & 28 deletions src/qt/forms/voting/pollcard.ui
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@
<property name="verticalSpacing">
<number>3</number>
</property>
<item row="1" column="2">
<widget class="QLabel" name="totalWeightTextLabel">
<property name="text">
<string>Total Weight:</string>
</property>
</widget>
</item>
<item row="1" column="5">
<widget class="QLabel" name="votePercentAVWLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="topAnswerLabel">
<property name="text">
<string notr="true"/>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="topAnswerTextLabel">
<property name="text">
<string>Top Answer:</string>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QLabel" name="activeVoteWeightTextLabel">
<property name="text">
<string>AVW:</string>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="QLabel" name="votePercentAVWTextLabel">
<property name="text">
Expand All @@ -115,8 +156,8 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="expirationLabel">
<item row="1" column="3">
<widget class="QLabel" name="totalWeightLabel">
<property name="text">
<string notr="true"/>
</property>
Expand All @@ -129,63 +170,64 @@
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLabel" name="totalWeightLabel">
<item row="0" column="3">
<widget class="QLabel" name="voteCountLabel">
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="topAnswerLabel">
<item row="0" column="1">
<widget class="QLabel" name="expirationLabel">
<property name="text">
<string notr="true"/>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QLabel" name="voteCountLabel">
<item row="0" column="5">
<widget class="QLabel" name="activeVoteWeightLabel">
<property name="text">
<string notr="true"/>
<string/>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="totalWeightTextLabel">
<item row="2" column="1">
<widget class="QLabel" name="myLastVoteAnswerLabel">
<property name="text">
<string>Total Weight:</string>
<string/>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QLabel" name="activeVoteWeightTextLabel">
<item row="2" column="0">
<widget class="QLabel" name="myLastVoteAnswerTextLabel">
<property name="text">
<string>AVW:</string>
<string>Your Last Vote:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="topAnswerTextLabel">
<item row="2" column="2">
<widget class="QLabel" name="myVoteWeightTextLabel">
<property name="text">
<string>Top Answer:</string>
<string>Your Vote Weight:</string>
</property>
</widget>
</item>
<item row="0" column="5">
<widget class="QLabel" name="activeVoteWeightLabel">
<item row="2" column="3">
<widget class="QLabel" name="myVoteWeightLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="5">
<widget class="QLabel" name="votePercentAVWLabel">
<item row="2" column="4">
<widget class="QLabel" name="myPercentAVWTextLabel">
<property name="text">
<string>Your % of AVW:</string>
</property>
</widget>
</item>
<item row="2" column="5">
<widget class="QLabel" name="myPercentAVWLabel">
<property name="text">
<string/>
</property>
Expand Down
26 changes: 26 additions & 0 deletions src/qt/voting/pollcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ PollCard::PollCard(const PollItem& poll_item, QWidget* parent)
ui->activeVoteWeightLabel->setText(QString::number(poll_item.m_active_weight));
ui->votePercentAVWLabel->setText(QString::number(poll_item.m_vote_percent_AVW, 'f', 4) + '\%');

if (!poll_item.m_self_voted) {
ui->myLastVoteAnswerLabel->setText("No Vote");
ui->myVoteWeightLabel->setText("N/A");
ui->myPercentAVWLabel->setText("N/A");
} else {
QString choices_str;

int64_t my_total_weight = 0;

for (const auto& choice : poll_item.m_self_vote_detail.m_responses) {
if (!choices_str.isEmpty()) {
choices_str += ", " + QString(poll_item.m_choices[choice.first].m_label);
} else {
choices_str = QString(poll_item.m_choices[choice.first].m_label);
}

my_total_weight += choice.second / COIN;
}

ui->myLastVoteAnswerLabel->setText(choices_str);
ui->myVoteWeightLabel->setText(QString::number(my_total_weight));
if (poll_item.m_active_weight) ui->myPercentAVWLabel->setText(QString::number((double) my_total_weight
/ (double) poll_item.m_active_weight
* (double) 100.0, 'f', 4) + '\%');
}

if (!(poll_item.m_weight_type == (int)GRC::PollWeightType::BALANCE ||
poll_item.m_weight_type == (int)GRC::PollWeightType::BALANCE_AND_MAGNITUDE)) {
ui->balanceLabel->hide();
Expand Down
5 changes: 5 additions & 0 deletions src/qt/voting/votingmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ std::optional<PollItem> BuildPollItem(const PollRegistry::Sequence::Iterator& it
result->m_responses[i].m_weight / COIN);
}

item.m_self_voted = result->m_self_voted;
if (result->m_self_voted) {
item.m_self_vote_detail = result->m_self_vote_detail;
}

if (!result->m_votes.empty()) {
item.m_top_answer = QString::fromStdString(result->WinnerLabel()).replace("_", " ");
}
Expand Down
3 changes: 3 additions & 0 deletions src/qt/voting/votingmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "gridcoin/voting/filter.h"
#include "qt/voting/poll_types.h"
#include "gridcoin/voting/poll.h"
#include "gridcoin/voting/result.h"

#include <QDateTime>
#include <QObject>
Expand Down Expand Up @@ -83,6 +84,8 @@ class PollItem
bool m_multiple_choice;
std::vector<AdditionalFieldEntry> m_additional_field_entries;
std::vector<VoteResultItem> m_choices;
bool m_self_voted;
GRC::PollResult::VoteDetail m_self_vote_detail;
};

//!
Expand Down

0 comments on commit 36a34a0

Please sign in to comment.