mirror of
https://github.com/wownero/wowlet.git
synced 2026-11-30 12:04:13 +01:00
refactor: move third party integrations to plugins folder
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
|
||||
|
||||
#include "RedditModel.h"
|
||||
|
||||
RedditModel::RedditModel(QObject *parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RedditModel::clear() {
|
||||
beginResetModel();
|
||||
|
||||
m_posts.clear();
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void RedditModel::updatePosts(const QList<QSharedPointer<RedditPost>>& posts) {
|
||||
beginResetModel();
|
||||
|
||||
m_posts.clear();
|
||||
for (const auto& post : posts) {
|
||||
m_posts.push_back(post);
|
||||
}
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
int RedditModel::rowCount(const QModelIndex &parent) const{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_posts.count();
|
||||
}
|
||||
|
||||
int RedditModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return ModelColumn::COUNT;
|
||||
}
|
||||
|
||||
QVariant RedditModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_posts.count())
|
||||
return QVariant();
|
||||
|
||||
QSharedPointer<RedditPost> 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 RedditModel::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("Reddit Post");
|
||||
case Author:
|
||||
return QString("Author");
|
||||
case Comments:
|
||||
return QString(" Comments ");
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QSharedPointer<RedditPost> RedditModel::post(int row) {
|
||||
if (row < 0 || row >= m_posts.size()) {
|
||||
qCritical("%s: no reddit post for index %d", __FUNCTION__, row);
|
||||
return QSharedPointer<RedditPost>();
|
||||
}
|
||||
|
||||
return m_posts.at(row);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
|
||||
|
||||
#ifndef FEATHER_REDDITMODEL_H
|
||||
#define FEATHER_REDDITMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include "RedditPost.h"
|
||||
|
||||
class RedditModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum ModelColumn
|
||||
{
|
||||
Title = 0,
|
||||
Author,
|
||||
Comments,
|
||||
COUNT
|
||||
};
|
||||
|
||||
explicit RedditModel(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<RedditPost>>& posts);
|
||||
|
||||
QSharedPointer<RedditPost> post(int row);
|
||||
|
||||
private:
|
||||
QList<QSharedPointer<RedditPost>> m_posts;
|
||||
};
|
||||
|
||||
#endif //FEATHER_REDDITMODEL_H
|
||||
@@ -0,0 +1,19 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
|
||||
|
||||
#ifndef FEATHER_REDDITPOST_H
|
||||
#define FEATHER_REDDITPOST_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
struct RedditPost {
|
||||
RedditPost(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 //FEATHER_REDDITPOST_H
|
||||
@@ -0,0 +1,78 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
|
||||
|
||||
#include "RedditWidget.h"
|
||||
#include "ui_RedditWidget.h"
|
||||
|
||||
#include <QDesktopServices>
|
||||
#include <QStandardItemModel>
|
||||
#include <QTableWidget>
|
||||
|
||||
#include "RedditModel.h"
|
||||
#include "utils/Utils.h"
|
||||
#include "utils/config.h"
|
||||
|
||||
RedditWidget::RedditWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::RedditWidget)
|
||||
, m_model(new RedditModel(this))
|
||||
, m_contextMenu(new QMenu(this))
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->tableView->setModel(m_model);
|
||||
this->setupTable();
|
||||
|
||||
m_contextMenu->addAction("View thread", this, &RedditWidget::linkClicked);
|
||||
m_contextMenu->addAction("Copy link", this, &RedditWidget::copyUrl);
|
||||
connect(ui->tableView, &QHeaderView::customContextMenuRequested, this, &RedditWidget::showContextMenu);
|
||||
|
||||
connect(ui->tableView, &QTableView::doubleClicked, this, &RedditWidget::linkClicked);
|
||||
|
||||
ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
||||
}
|
||||
|
||||
RedditModel* RedditWidget::model() {
|
||||
return m_model;
|
||||
}
|
||||
|
||||
void RedditWidget::linkClicked() {
|
||||
QModelIndex index = ui->tableView->currentIndex();
|
||||
auto post = m_model->post(index.row());
|
||||
|
||||
if (post)
|
||||
Utils::externalLinkWarning(this, this->getLink(post->permalink));
|
||||
}
|
||||
|
||||
void RedditWidget::copyUrl() {
|
||||
QModelIndex index = ui->tableView->currentIndex();
|
||||
auto post = m_model->post(index.row());
|
||||
|
||||
if (post) {
|
||||
Utils::copyToClipboard(this->getLink(post->permalink));
|
||||
emit setStatusText("Link copied to clipboard", true, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void RedditWidget::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 RedditWidget::showContextMenu(const QPoint &pos) {
|
||||
QModelIndex index = ui->tableView->indexAt(pos);
|
||||
if (!index.isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_contextMenu->exec(ui->tableView->viewport()->mapToGlobal(pos));
|
||||
}
|
||||
|
||||
QString RedditWidget::getLink(const QString &permaLink) {
|
||||
QString redditFrontend = config()->get(Config::redditFrontend).toString();
|
||||
return QString("https://%1%2").arg(redditFrontend, permaLink);
|
||||
}
|
||||
|
||||
RedditWidget::~RedditWidget() = default;
|
||||
@@ -0,0 +1,43 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
|
||||
|
||||
#ifndef FEATHER_REDDITWIDGET_H
|
||||
#define FEATHER_REDDITWIDGET_H
|
||||
|
||||
#include <QItemSelection>
|
||||
#include <QMenu>
|
||||
#include <QWidget>
|
||||
|
||||
#include "RedditModel.h"
|
||||
|
||||
namespace Ui {
|
||||
class RedditWidget;
|
||||
}
|
||||
|
||||
class RedditWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit RedditWidget(QWidget *parent = nullptr);
|
||||
~RedditWidget();
|
||||
RedditModel* 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();
|
||||
QString getLink(const QString &permaLink);
|
||||
|
||||
QScopedPointer<Ui::RedditWidget> ui;
|
||||
RedditModel* const m_model;
|
||||
QMenu *m_contextMenu;
|
||||
};
|
||||
|
||||
#endif // FEATHER_REDDITWIDGET_H
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>RedditWidget</class>
|
||||
<widget class="QWidget" name="RedditWidget">
|
||||
<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>
|
||||
Reference in New Issue
Block a user