Skip to content

Commit

Permalink
Wire up Poll notification GUI machinery to signals
Browse files Browse the repository at this point in the history
Also address wallet restart with existing current polls.
  • Loading branch information
jamescowens committed Nov 20, 2023
1 parent b075021 commit bca8ce9
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 40 deletions.
35 changes: 24 additions & 11 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "signverifymessagedialog.h"
#include "optionsdialog.h"
#include "aboutdialog.h"
#include "voting/polltab.h"
#include "voting/votingpage.h"
#include "clientmodel.h"
#include "walletmodel.h"
Expand All @@ -43,6 +44,7 @@
#include "univalue.h"
#include "upgradeqt.h"
#include "voting/votingmodel.h"
#include "voting/polltablemodel.h"

#ifdef Q_OS_MAC
#include "macdockiconhandler.h"
Expand Down Expand Up @@ -1946,28 +1948,39 @@ void BitcoinGUI::extracted(QStringList& expiring_polls, QString& notification)

void BitcoinGUI::handleExpiredPoll()
{
if (!clientModel || !clientModel->getOptionsModel()) {
if (!clientModel) {
return;
}

if (!clientModel->getOptionsModel()) {
return;
}

if (!votingModel) {
return;
}

if (!clientModel->getOptionsModel()->getDisablePollNotifications()) {
QStringList expiring_polls = votingModel->getExpiringPollsNotNotified();
// Only do if in sync.
if (researcherModel && !researcherModel->outOfSync() && votingPage->getActiveTab()) {

// First refresh the active poll tab and underlying table
votingPage->getActiveTab()->refresh();

if (!expiring_polls.isEmpty()) {
QString notification = tr("The following poll(s) are about to expire:\n");
if (!clientModel->getOptionsModel()->getDisablePollNotifications()) {
QStringList expiring_polls = votingModel->getExpiringPollsNotNotified();

extracted(expiring_polls, notification);
if (!expiring_polls.isEmpty()) {
QString notification = tr("The following poll(s) are about to expire:\n");

notification += tr("Open Gridcoin to vote.");
extracted(expiring_polls, notification);

notificator->notify(
Notificator::Information,
tr("Poll(s) about to expire"),
notification);
notification += tr("Open Gridcoin to vote.");

notificator->notify(
Notificator::Information,
tr("Poll(s) about to expire"),
notification);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class WalletModel;
class ResearcherModel;
class MRCModel;
class VotingModel;
class PollTableModel;
class TransactionView;
class OverviewPage;
class FavoritesPage;
Expand Down
10 changes: 5 additions & 5 deletions src/qt/voting/pollcardview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ PollCardView::~PollCardView()

void PollCardView::setModel(PollTableModel* model)
{
m_model = model;
m_polltable_model = model;

if (!model) {
return;
}

connect(model, &PollTableModel::layoutChanged, this, &PollCardView::redraw);

if (!m_refresh_timer && m_model->includesActivePolls()) {
if (!m_refresh_timer && m_polltable_model->includesActivePolls()) {
m_refresh_timer.reset(new QTimer(this));
m_refresh_timer->setTimerType(Qt::VeryCoarseTimer);

Expand Down Expand Up @@ -76,15 +76,15 @@ void PollCardView::redraw()
// sorting and filtering. Hook up model events for these operations.
clear();

if (!m_model) {
if (!m_polltable_model) {
return;
}

const QDateTime now = QDateTime::currentDateTimeUtc();
const QModelIndex dummy_parent;

for (int i = 0; i < m_model->rowCount(dummy_parent); ++i) {
if (const PollItem* poll_item = m_model->rowItem(i)) {
for (int i = 0; i < m_polltable_model->rowCount(dummy_parent); ++i) {
if (const PollItem* poll_item = m_polltable_model->rowItem(i)) {
PollCard* card = new PollCard(*poll_item, this);
card->updateRemainingTime(now);
card->updateIcons(m_theme);
Expand Down
2 changes: 1 addition & 1 deletion src/qt/voting/pollcardview.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public slots:

private:
Ui::PollCardView* ui;
PollTableModel* m_model;
PollTableModel* m_polltable_model;
std::unique_ptr<QTimer> m_refresh_timer;
QString m_theme;

Expand Down
34 changes: 17 additions & 17 deletions src/qt/voting/polltab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private slots:
PollTab::PollTab(QWidget* parent)
: QWidget(parent)
, ui(new Ui::PollTab)
, m_model(new PollTableModel(this))
, m_polltable_model(new PollTableModel(this))
, m_no_result(new NoResult(this))
, m_loading(new LoadingBar(this))
{
Expand All @@ -152,7 +152,7 @@ PollTab::PollTab(QWidget* parent)
connect(ui->cards, &PollCardView::detailsRequested, this, &PollTab::showDetailsRowDialog);
connect(ui->table, &QAbstractItemView::doubleClicked, this, &PollTab::showPreferredDialog);
connect(ui->table, &QWidget::customContextMenuRequested, this, &PollTab::showTableContextMenu);
connect(m_model.get(), &PollTableModel::layoutChanged, this, &PollTab::finishRefresh);
connect(m_polltable_model.get(), &PollTableModel::layoutChanged, this, &PollTab::finishRefresh);
}

PollTab::~PollTab()
Expand All @@ -163,15 +163,15 @@ PollTab::~PollTab()
void PollTab::setVotingModel(VotingModel* model)
{
m_voting_model = model;
m_model->setModel(model);
m_polltable_model->setModel(model);

ui->cards->setModel(m_model.get());
ui->table->setModel(m_model.get());
ui->cards->setModel(m_polltable_model.get());
ui->table->setModel(m_polltable_model.get());
}

void PollTab::setPollFilterFlags(PollFilterFlag flags)
{
m_model->setPollFilterFlags(flags);
m_polltable_model->setPollFilterFlags(flags);
}

void PollTab::changeViewMode(const ViewId view_id)
Expand All @@ -181,26 +181,26 @@ void PollTab::changeViewMode(const ViewId view_id)

void PollTab::refresh()
{
if (m_model->empty()) {
if (m_polltable_model->empty()) {
m_no_result->showDefaultLoadingTitle();
m_no_result->contentWidgetAs<QLabel>()->setText(WaitMessage());
}

m_loading->start();
m_model->refresh();
m_polltable_model->refresh();
}

void PollTab::filter(const QString& needle)
{
if (needle != m_last_filter) {
m_model->changeTitleFilter(needle);
m_polltable_model->changeTitleFilter(needle);
m_last_filter = needle;
}
}

void PollTab::sort(const int column)
{
const Qt::SortOrder order = m_model->sort(column);
const Qt::SortOrder order = m_polltable_model->sort(column);
ui->table->horizontalHeader()->setSortIndicator(column, order);
}

Expand All @@ -215,7 +215,7 @@ const PollItem* PollTab::selectedTableItem() const
return nullptr;
}

return m_model->rowItem(
return m_polltable_model->rowItem(
ui->table->selectionModel()->selectedIndexes().first().row());
}

Expand All @@ -228,18 +228,18 @@ void PollTab::resizeEvent(QResizeEvent* event)
void PollTab::finishRefresh()
{
m_loading->finish();
ui->stack->setVisible(!m_model->empty());
m_no_result->setVisible(m_model->empty());
ui->stack->setVisible(!m_polltable_model->empty());
m_no_result->setVisible(m_polltable_model->empty());

if (m_model->empty()) {
if (m_polltable_model->empty()) {
m_no_result->showDefaultNoResultTitle();
m_no_result->contentWidgetAs<QLabel>()->setText(FullRefreshMessage());
}
}

void PollTab::showVoteRowDialog(int row)
{
if (const PollItem* const poll_item = m_model->rowItem(row)) {
if (const PollItem* const poll_item = m_polltable_model->rowItem(row)) {
showVoteDialog(*poll_item);
}
}
Expand All @@ -251,7 +251,7 @@ void PollTab::showVoteDialog(const PollItem& poll_item)

void PollTab::showDetailsRowDialog(int row)
{
if (const PollItem* const poll_item = m_model->rowItem(row)) {
if (const PollItem* const poll_item = m_polltable_model->rowItem(row)) {
showDetailsDialog(*poll_item);
}
}
Expand All @@ -263,7 +263,7 @@ void PollTab::showDetailsDialog(const PollItem& poll_item)

void PollTab::showPreferredDialog(const QModelIndex& index)
{
if (const PollItem* const poll_item = m_model->rowItem(index.row())) {
if (const PollItem* const poll_item = m_polltable_model->rowItem(index.row())) {
if (poll_item->m_finished) {
showDetailsDialog(*poll_item);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/qt/voting/polltab.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public slots:
private:
Ui::PollTab* ui;
VotingModel* m_voting_model;
std::unique_ptr<PollTableModel> m_model;
std::unique_ptr<PollTableModel> m_polltable_model;
std::unique_ptr<NoResult> m_no_result;
std::unique_ptr<LoadingBar> m_loading;
QString m_last_filter;
Expand Down
8 changes: 4 additions & 4 deletions src/qt/voting/polltablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ PollTableModel::~PollTableModel()

void PollTableModel::setModel(VotingModel* model)
{
m_model = model;
m_voting_model = model;

// Connect poll stale handler to newVoteReceived signal from voting model, which propagates
// from the core.
connect(m_model, &VotingModel::newVoteReceived, this, &PollTableModel::handlePollStaleFlag);
connect(m_voting_model, &VotingModel::newVoteReceived, this, &PollTableModel::handlePollStaleFlag);
}

void PollTableModel::setPollFilterFlags(PollFilterFlag flags)
Expand Down Expand Up @@ -254,13 +254,13 @@ const PollItem* PollTableModel::rowItem(int row) const

void PollTableModel::refresh()
{
if (!m_model || !m_refresh_mutex.tryLock()) {
if (!m_voting_model || !m_refresh_mutex.tryLock()) {
return;
}

QtConcurrent::run([this]() {
static_cast<PollTableDataModel*>(m_data_model.get())
->reload(m_model->buildPollTable(m_filter_flags));
->reload(m_voting_model->buildPollTable(m_filter_flags));

m_refresh_mutex.unlock();
});
Expand Down
2 changes: 1 addition & 1 deletion src/qt/voting/polltablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public slots:
void handlePollStaleFlag(QString poll_txid_string);

private:
VotingModel* m_model;
VotingModel* m_voting_model;
std::unique_ptr<PollTableDataModel> m_data_model;
GRC::PollFilterFlag m_filter_flags;
QMutex m_refresh_mutex;
Expand Down
11 changes: 11 additions & 0 deletions src/qt/voting/votingpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,15 @@ void VotingPage::setVotingModel(VotingModel* model)
return;
}

// Now that PollItem caching is available, automatically refresh current poll tab on receipt of new poll or vote.
connect(model, &VotingModel::newPollReceived, [this]() {
ui->pollReceivedLabel->show();
currentTab().refresh();
ui->pollReceivedLabel->hide();
});

connect(model, &VotingModel::newVoteReceived, [this]() {
currentTab().refresh();
});
}

Expand All @@ -149,6 +156,10 @@ PollTab& VotingPage::currentTab()
return *qobject_cast<PollTab*>(ui->tabWidget->currentWidget());
}

PollTab* VotingPage::getActiveTab()
{
return m_tabs[0];
}
void VotingPage::updateIcons(const QString& theme)
{
m_filter_action->setIcon(QIcon(":/icons/" + theme + "_search"));
Expand Down
1 change: 1 addition & 0 deletions src/qt/voting/votingpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class VotingPage : public QWidget
void setOptionsModel(OptionsModel* model);

PollTab& currentTab();
PollTab* getActiveTab();

private:
Ui::VotingPage* ui;
Expand Down

0 comments on commit bca8ce9

Please sign in to comment.