Browse Source

Forum post widget showing latest from forum.wownero.com

pull/64/head
dsc 3 months ago
parent
commit
c02e084dcf
  1. 25
      src/appcontext.cpp
  2. 3
      src/appcontext.h
  3. 1
      src/mainwindow.cpp
  4. 7
      src/mainwindow.h
  5. 32
      src/mainwindow.ui
  6. 104
      src/model/ForumModel.cpp
  7. 41
      src/model/ForumModel.h
  8. 2
      src/utils/config.cpp
  9. 19
      src/widgets/ForumPost.h
  10. 74
      src/widgets/forumwidget.cpp
  11. 42
      src/widgets/forumwidget.h
  12. 61
      src/widgets/forumwidget.ui

25
src/appcontext.cpp

@ -433,7 +433,6 @@ void AppContext::onWSMessage(const QJsonObject &msg) {
QJsonArray crypto_rates = msg.value("data").toArray();
AppContext::prices->cryptoPricesReceived(crypto_rates);
}
else if(cmd == "fiat_rates") {
QJsonObject fiat_rates = msg.value("data").toObject();
AppContext::prices->fiatPricesReceived(fiat_rates);
@ -442,17 +441,18 @@ void AppContext::onWSMessage(const QJsonObject &msg) {
QJsonArray reddit_data = msg.value("data").toArray();
this->onWSReddit(reddit_data);
}
else if(cmd == "forum") {
QJsonArray forum_data = msg.value("data").toArray();
this->onWSForum(forum_data);
}
else if(cmd == "funding_proposals") {
auto ccs_data = msg.value("data").toArray();
this->onWSCCS(ccs_data);
}
else if(cmd == "suchwow") {
QJsonArray such_data = msg.value("data").toArray();
emit suchWowUpdated(such_data);
}
else if(cmd == "txFiatHistory") {
auto txFiatHistory_data = msg.value("data").toObject();
AppContext::txFiatHistory->onWSData(txFiatHistory_data);
@ -507,6 +507,23 @@ void AppContext::onWSNodes(const QJsonArray &nodes) {
this->nodes->onWSNodesReceived(l);
}
void AppContext::onWSForum(const QJsonArray& forum_data) {
QList<QSharedPointer<ForumPost>> l;
for (auto &&entry: forum_data) {
auto obj = entry.toObject();
auto forumPost = new ForumPost(
obj.value("title").toString(),
obj.value("author").toString(),
obj.value("permalink").toString(),
obj.value("comments").toInt());
QSharedPointer<ForumPost> r = QSharedPointer<ForumPost>(forumPost);
l.append(r);
}
emit forumUpdated(l);
}
void AppContext::onWSReddit(const QJsonArray& reddit_data) {
QList<QSharedPointer<RedditPost>> l;

3
src/appcontext.h

@ -19,6 +19,7 @@
#include "utils/txfiathistory.h"
#include "utils/WowletSeed.h"
#include "widgets/RedditPost.h"
#include "widgets/ForumPost.h"
#include "widgets/CCSEntry.h"
#include "utils/RestoreHeightLookup.h"
#include "utils/nodes.h"
@ -160,6 +161,7 @@ private slots:
void onWSMessage(const QJsonObject& msg);
void onWSCCS(const QJsonArray &ccs_data);
void onWSReddit(const QJsonArray& reddit_data);
void onWSForum(const QJsonArray& forum_data);
void onMoneySpent(const QString &txId, quint64 amount);
void onMoneyReceived(const QString &txId, quint64 amount);
@ -198,6 +200,7 @@ signals:
void wsConnected();
void wsDisconnected();
void redditUpdated(QList<QSharedPointer<RedditPost>> &posts);
void forumUpdated(QList<QSharedPointer<ForumPost>> &posts);
void nodesUpdated(QList<QSharedPointer<WowletNode>> &nodes);
void ccsUpdated(QList<QSharedPointer<CCSEntry>> &entries);
void suchWowUpdated(const QJsonArray &such_data);

1
src/mainwindow.cpp

@ -182,6 +182,7 @@ MainWindow::MainWindow(AppContext *ctx, QWidget *parent) :
connect(ui->ccsWidget, &CCSWidget::selected, this, &MainWindow::showSendScreen);
connect(m_ctx, &AppContext::ccsUpdated, ui->ccsWidget->model(), &CCSModel::updateEntries);
connect(m_ctx, &AppContext::redditUpdated, ui->redditWidget->model(), &RedditModel::updatePosts);
connect(m_ctx, &AppContext::forumUpdated, ui->forumWidget->model(), &ForumModel::updatePosts);
connect(m_ctx, &AppContext::suchWowUpdated, ui->suchWowWidget, &SuchWowWidget::onWS);
connect(ui->suchWowWidget, &SuchWowWidget::donate, this, &MainWindow::suchDonate);

7
src/mainwindow.h

@ -25,6 +25,7 @@
#include "calcwindow.h"
#include "widgets/ccswidget.h"
#include "widgets/redditwidget.h"
#include "widgets/forumwidget.h"
#include "widgets/tickerwidget.h"
#include "widgets/xmrigwidget.h"
#include "utils/networking.h"
@ -83,8 +84,10 @@ public:
};
enum TabsHome {
CCS,
REDDIT
FORUM,
REDDIT,
SUCHWOW,
WFS
};
public slots:

32
src/mainwindow.ui

@ -101,9 +101,31 @@
<property name="documentMode">
<bool>true</bool>
</property>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>Forum</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="ForumWidget" name="forumWidget" native="true"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>/r/Wownero</string>
<string>Reddit</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_7">
<property name="leftMargin">
@ -326,7 +348,7 @@
<x>0</x>
<y>0</y>
<width>1156</width>
<height>20</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -755,6 +777,12 @@
<header>widgets/suchwowwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>ForumWidget</class>
<extends>QWidget</extends>
<header>widgets/forumwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="assets.qrc"/>

104
src/model/ForumModel.cpp

@ -0,0 +1,104 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include "ForumModel.h"
ForumModel::ForumModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
void ForumModel::clear() {
beginResetModel();
m_posts.clear();
endResetModel();
}
void ForumModel::updatePosts(const QList<QSharedPointer<ForumPost>>& posts) {
beginResetModel();
m_posts.clear();
for (const auto& post : posts) {
m_posts.push_back(post);
}
endResetModel();
}
int ForumModel::rowCount(const QModelIndex &parent) const{
if (parent.isValid()) {
return 0;
}
return m_posts.count();
}
int ForumModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return ModelColumn::COUNT;
}
QVariant ForumModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_posts.count())
return QVariant();
QSharedPointer<ForumPost> post = m_posts.at(index.row());
if(role == Qt::DisplayRole) {
switch(index.column()) {
case Title:
return post->title;
case Author:
return post->author;
case Comments:
return QString::number(post->comments);
default:
return QVariant();
}
}
else if (role == Qt::TextAlignmentRole) {
switch(index.column()) {
case Comments:
return Qt::AlignRight;
default:
return QVariant();
}
}
return QVariant();
}
QVariant ForumModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) {
return QVariant();
}
if (orientation == Qt::Horizontal)
{
switch(section) {
case Title:
return QString("Forum Post");
case Author:
return QString("Author");
case Comments:
return QString("Comments");
default:
return QVariant();
}
}
return QVariant();
}
QSharedPointer<ForumPost> ForumModel::post(int row) {
if (row < 0 || row >= m_posts.size()) {
qCritical("%s: no forum post for index %d", __FUNCTION__, row);
return QSharedPointer<ForumPost>();
}
return m_posts.at(row);
}

41
src/model/ForumModel.h

@ -0,0 +1,41 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef WOWLET_FORUMMODEL_H
#define WOWLET_FORUMMODEL_H
#include <QAbstractTableModel>
#include <QSharedPointer>
#include "widgets/ForumPost.h"
class ForumModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum ModelColumn
{
Title = 0,
Author,
Comments,
COUNT
};
explicit ForumModel(QObject *parent);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
void clear();
void updatePosts(const QList<QSharedPointer<ForumPost>>& posts);
QSharedPointer<ForumPost> post(int row);
private:
QList<QSharedPointer<ForumPost>> m_posts;
};
#endif //WOWLET_FORUMMODEL_H

2
src/utils/config.cpp

@ -22,7 +22,7 @@ static const QHash<Config::ConfigKey, ConfigDirective> configStrings = {
{Config::warnOnStagenet,{QS("warnOnStagenet"), true}},
{Config::warnOnTestnet,{QS("warnOnTestnet"), true}},
{Config::warnOnAlpha,{QS("warnOnAlpha"), true}},
{Config::homeWidget,{QS("homeWidget"), "ccs"}},
{Config::homeWidget,{QS("homeWidget"), 0}},
{Config::donateBeg,{QS("donateBeg"), 1}},
{Config::skin,{QS("skin"), "light"}},
{Config::openVRSkin,{QS("openVRSkin"), "default"}},

19
src/widgets/ForumPost.h

@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef WOWLET_FORUMPOST_H
#define WOWLET_FORUMPOST_H
#include <QString>
struct ForumPost {
ForumPost(const QString &title, const QString &author, const QString &permalink, int comments)
: title(title), author(author), permalink(permalink), comments(comments){};
QString title;
QString author;
QString permalink;
int comments;
};
#endif //WOWLET_FORUMPOST_H

74
src/widgets/forumwidget.cpp

@ -0,0 +1,74 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include <QStandardItemModel>
#include <QTableWidget>
#include <QDesktopServices>
#include "model/ForumModel.h"
#include "forumwidget.h"
#include "ui_forumwidget.h"
#include "utils/utils.h"
#include "utils/config.h"
ForumWidget::ForumWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ForumWidget),
m_model(new ForumModel(this)),
m_contextMenu(new QMenu(this))
{
ui->setupUi(this);
ui->tableView->setModel(m_model);
this->setupTable();
m_contextMenu->addAction("View thread", this, &ForumWidget::linkClicked);
m_contextMenu->addAction("Copy link", this, &ForumWidget::copyUrl);
connect(ui->tableView, &QHeaderView::customContextMenuRequested, this, &ForumWidget::showContextMenu);
connect(ui->tableView, &QTableView::doubleClicked, this, &ForumWidget::linkClicked);
ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
ForumModel* ForumWidget::model() {
return m_model;
}
void ForumWidget::linkClicked() {
QModelIndex index = ui->tableView->currentIndex();
auto post = m_model->post(index.row());
if (post)
Utils::externalLinkWarning(this, post->permalink);
}
void ForumWidget::copyUrl() {
QModelIndex index = ui->tableView->currentIndex();
auto post = m_model->post(index.row());
if (post) {
Utils::copyToClipboard(post->permalink);
emit setStatusText("Link copied to clipboard", true, 1000);
}
}
void ForumWidget::setupTable() {
ui->tableView->verticalHeader()->setVisible(false);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->tableView->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::Stretch);
}
void ForumWidget::showContextMenu(const QPoint &pos) {
QModelIndex index = ui->tableView->indexAt(pos);
if (!index.isValid()) {
return;
}
m_contextMenu->exec(ui->tableView->viewport()->mapToGlobal(pos));
}
ForumWidget::~ForumWidget() {
delete ui;
}

42
src/widgets/forumwidget.h

@ -0,0 +1,42 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef FORUMWIDGET_H
#define FORUMWIDGET_H
#include <QMenu>
#include <QWidget>
#include <QItemSelection>
#include "model/ForumModel.h"
namespace Ui {
class ForumWidget;
}
class ForumWidget : public QWidget
{
Q_OBJECT
public:
explicit ForumWidget(QWidget *parent = nullptr);
~ForumWidget();
ForumModel* model();
public slots:
void linkClicked();
signals:
void setStatusText(const QString &msg, bool override, int timeout);
private:
void setupTable();
void showContextMenu(const QPoint &pos);
void copyUrl();
Ui::ForumWidget *ui;
ForumModel* const m_model;
QMenu *m_contextMenu;
};
#endif // FORUMWIDGET_H

61
src/widgets/forumwidget.ui

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ForumWidget</class>
<widget class="QWidget" name="ForumWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>492</width>
<height>409</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>4</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTableView" name="tableView">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="sortingEnabled">
<bool>false</bool>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
<slots>
<slot>linkClicked()</slot>
</slots>
</ui>
Loading…
Cancel
Save