forked from wowlet/wowlet
Merge pull request 'Forum post widget showing latest from forum.wownero.com' (#64) from dsc/wowlet:forum-posts-homewidget into master
Reviewed-on: wowlet/wowlet#64beta-4
commit
d8cb29c4d4
@ -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);
|
||||||
|
}
|
@ -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
|
@ -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
|
@ -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;
|
||||||
|
}
|
@ -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
|
@ -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…
Reference in new issue