SuchWidget - display listing of suchwow.xyz posts

This commit is contained in:
dsc
2021-02-18 12:22:36 +03:00
committed by wowario
parent 7cf050ff8e
commit db5ba12190
8 changed files with 322 additions and 0 deletions
+131
View File
@@ -0,0 +1,131 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include <QStandardItemModel>
#include <QMessageBox>
#include <QTableWidget>
#include <QDesktopServices>
#include <QListWidgetItem>
#include "suchwowwidget.h"
#include "ui_suchwowwidget.h"
#include "utils/utils.h"
#include "utils/config.h"
#include "mainwindow.h"
SuchWowPost::SuchWowPost(AppContext *ctx, QObject *parent) :
QObject(parent),
m_ctx(ctx) {
m_networkImg = new UtilsNetworking(m_ctx->network, this);
m_networkThumb = new UtilsNetworking(m_ctx->network, this);
}
void SuchWowPost::onThumbReceived(QByteArray data) {
thumb_data.loadFromData(data, "JPEG");
emit thumbReceived(this);
}
void SuchWowPost::onImgReceived(QByteArray data) {
img_data.loadFromData(data);
emit imgReceived(this);
}
SuchWowPost::~SuchWowPost() = default;
SuchWowWidget::SuchWowWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SuchWowWidget),
m_contextMenu(new QMenu(this))
{
ui->setupUi(this);
this->setupTable();
m_contextMenu->addAction("View image", this, &SuchWowWidget::suchImage);
m_contextMenu->addAction("Donate", this, &SuchWowWidget::suchDonate);
connect(ui->listWidget, &QListWidget::customContextMenuRequested, this, &SuchWowWidget::showContextMenu);
}
void SuchWowWidget::setupTable() {
ui->listWidget->setViewMode(QListWidget::IconMode);
ui->listWidget->setIconSize(QSize(256, 256));
ui->listWidget->setResizeMode(QListWidget::Adjust);
ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
}
void SuchWowWidget::onWS(QJsonArray such_data) {
if(this->m_rendered) return; // only draw once
if(this->m_ctx == nullptr) {
m_ctx = MainWindow::getContext();
}
for (auto &&such_post: such_data) {
auto obj = such_post.toObject();
auto s = new SuchWowPost(m_ctx, this);
s->added_by = obj.value("added_by").toString();
s->addy = obj.value("addy").toString();
s->title = obj.value("title").toString();
s->img = obj.value("img").toString();
s->thumb = obj.value("thumb").toString();
s->href = obj.value("href").toString();
m_posts.push_back(s);
connect(s, &SuchWowPost::thumbReceived, this, &SuchWowWidget::addThumb);
s->download_thumb();
s->download_img();
}
}
void SuchWowWidget::addThumb(SuchWowPost *test) {
auto *item = new QListWidgetItem(QIcon(test->thumb_data), test->title);
ui->listWidget->addItem(item);
}
void SuchWowWidget::showContextMenu(const QPoint &pos) {
QModelIndex index = ui->listWidget->indexAt(pos);
if (!index.isValid())
return;
m_contextMenu->exec(ui->listWidget->viewport()->mapToGlobal(pos));
}
void SuchWowWidget::suchImage() {
auto *post = this->itemToPost();
if(post == nullptr)
return;
if(!post->img_data) {
QMessageBox::warning(this, "wh00pz", "Image not dowloaded yet.");
return;
}
const auto title = QString("%1 - %2").arg(post->title).arg(post->added_by);
QMessageBox mb(title, "",
QMessageBox::NoIcon,
QMessageBox::Ok,
QMessageBox::NoButton,
QMessageBox::NoButton);
mb.setIconPixmap(post->img_data);
mb.exec();
}
void SuchWowWidget::suchDonate() {
auto *post = this->itemToPost();
if(post != nullptr)
emit donate(post->addy);
}
SuchWowPost *SuchWowWidget::itemToPost() {
QListWidgetItem *item = ui->listWidget->currentItem();
QString title = item->text();
for(auto &post: m_posts){
if(post->title == title) {
return post;
}
}
return nullptr;
}
SuchWowWidget::~SuchWowWidget() {
delete ui;
}
+98
View File
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef SUCHWOWWIDGET_H
#define SUCHWOWWIDGET_H
#include <QMenu>
#include <QWidget>
#include <QItemSelection>
#include "utils/networking.h"
#include "appcontext.h"
namespace Ui {
class SuchWowWidget;
}
class SuchWowPost : public QObject
{
Q_OBJECT
public:
explicit SuchWowPost(AppContext *ctx, QObject *parent = nullptr);
~SuchWowPost();
QString title;
QString added_by;
QString thumb;
QString img;
QString addy;
QString href;
QPixmap thumb_data;
QPixmap img_data;
void download_thumb() {
const QString url = QString("%1/%2").arg(m_weburl).arg(this->thumb);
qDebug() << url;
connect(m_networkThumb, &UtilsNetworking::webReceived, this, &SuchWowPost::onThumbReceived);
m_networkThumb->get(url);
}
void download_img() {
const QString url = QString("%1/%2").arg(m_weburl).arg(this->img);
qDebug() << url;
connect(m_networkImg, &UtilsNetworking::webReceived, this, &SuchWowPost::onImgReceived);
m_networkImg->get(url);
}
private slots:
void onThumbReceived(QByteArray data);
void onImgReceived(QByteArray data);
signals:
void imgReceived(SuchWowPost *test);
void thumbReceived(SuchWowPost *test);
private:
QString m_weburl = "http://feathercitimllbmdktu6cmjo3fizgmyfrntntqzu6xguqa2rlq5cgid.onion/suchwow";
AppContext *m_ctx = nullptr;
UtilsNetworking *m_networkThumb = nullptr;
UtilsNetworking *m_networkImg = nullptr;
};
class SuchWowWidget : public QWidget
{
Q_OBJECT
public:
explicit SuchWowWidget(QWidget *parent = nullptr);
~SuchWowWidget();
public slots:
void onWS(QJsonArray such_data);
private slots:
void addThumb(SuchWowPost *test);
signals:
void donate(QString donate);
void openImage(SuchWowPost *test);
private:
void setupTable();
void suchDonate();
void suchImage();
SuchWowPost* itemToPost();
void showContextMenu(const QPoint &pos);
bool m_rendered = false; // because we're too lazy to re-render on changes.
Ui::SuchWowWidget *ui;
QList<SuchWowPost*> m_posts;
AppContext *m_ctx = nullptr;
QMenu *m_contextMenu;
};
#endif // SUCHWOWWIDGET_H
+42
View File
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SuchWowWidget</class>
<widget class="QWidget" name="SuchWowWidget">
<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="QListWidget" name="listWidget"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
<slots>
<slot>linkClicked()</slot>
</slots>
</ui>