Merge pull request 'SuchWow improvements' (#55) from dsc/wowlet:suchwow-improvements into master

Reviewed-on: https://git.wownero.com/wowlet/wowlet/pulls/55
This commit is contained in:
wowario 2021-05-02 22:51:03 +00:00
commit 096b28318c
5 changed files with 74 additions and 35 deletions

View file

@ -4,6 +4,8 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QtGlobal>
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
#include "src/kdmactouchbar.h" #include "src/kdmactouchbar.h"
#endif #endif

View file

@ -16,6 +16,7 @@ void UtilsNetworking::setUserAgent(const QString &userAgent) {
} }
void UtilsNetworking::get(const QString &url) { void UtilsNetworking::get(const QString &url) {
busy = true;
QNetworkRequest request; QNetworkRequest request;
request.setUrl(QUrl(url)); request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", m_userAgent.toUtf8()); request.setRawHeader("User-Agent", m_userAgent.toUtf8());
@ -26,6 +27,7 @@ void UtilsNetworking::get(const QString &url) {
} }
QNetworkReply* UtilsNetworking::getJson(const QString &url) { QNetworkReply* UtilsNetworking::getJson(const QString &url) {
busy = true;
QNetworkRequest request; QNetworkRequest request;
request.setUrl(QUrl(url)); request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", m_userAgent.toUtf8()); request.setRawHeader("User-Agent", m_userAgent.toUtf8());
@ -35,6 +37,7 @@ QNetworkReply* UtilsNetworking::getJson(const QString &url) {
} }
QNetworkReply* UtilsNetworking::postJson(const QString &url, const QJsonObject &data) { QNetworkReply* UtilsNetworking::postJson(const QString &url, const QJsonObject &data) {
busy = true;
QNetworkRequest request; QNetworkRequest request;
request.setUrl(QUrl(url)); request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", m_userAgent.toUtf8()); request.setRawHeader("User-Agent", m_userAgent.toUtf8());
@ -61,6 +64,8 @@ void UtilsNetworking::webResponse(QNetworkReply *reply) {
emit webErrorReceived(err); emit webErrorReceived(err);
else else
emit webReceived(data); emit webReceived(data);
busy = false;
} }
QString UtilsNetworking::validateJSON(QNetworkReply *reply){ QString UtilsNetworking::validateJSON(QNetworkReply *reply){

View file

@ -26,6 +26,8 @@ public:
void setUserAgent(const QString &userAgent); void setUserAgent(const QString &userAgent);
static QString validateJSON(QNetworkReply *reply); static QString validateJSON(QNetworkReply *reply);
bool busy = false;
private slots: private slots:
void webResponse(QNetworkReply *reply); void webResponse(QNetworkReply *reply);

View file

@ -57,31 +57,33 @@ void SuchWowWidget::setupTable() {
} }
void SuchWowWidget::onWS(QJsonArray such_data) { void SuchWowWidget::onWS(QJsonArray such_data) {
if(this->m_rendered) return; // only draw once if(this->m_ctx == nullptr)
if(this->m_ctx == nullptr) {
m_ctx = MainWindow::getContext(); m_ctx = MainWindow::getContext();
}
for (auto &&such_post: such_data) { for (auto &&such_post: such_data) {
auto obj = such_post.toObject(); auto obj = such_post.toObject();
auto s = new SuchWowPost(m_ctx, this); auto uid = obj.value("id").toInt();
s->added_by = obj.value("added_by").toString(); if(m_lookup.contains(uid)) continue;
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); auto post = new SuchWowPost(m_ctx, this);
s->download_thumb(); post->added_by = obj.value("added_by").toString();
s->download_img(); post->addy = obj.value("addy").toString();
post->title = obj.value("title").toString();
post->img = obj.value("img").toString();
post->thumb = obj.value("thumb").toString();
post->href = obj.value("href").toString();;
post->uid = uid;
m_lookup[post->uid] = post;
connect(post, &SuchWowPost::thumbReceived, this, &SuchWowWidget::addThumb);
post->download_thumb();
} }
} }
void SuchWowWidget::addThumb(SuchWowPost *test) { void SuchWowWidget::addThumb(SuchWowPost *post) {
auto *item = new QListWidgetItem(QIcon(test->thumb_data), test->title); auto *item = new SuchWidgetItem(QIcon(post->thumb_data), post->title, post);
ui->listWidget->addItem(item); ui->listWidget->addItem(item);
ui->listWidget->sortItems();
} }
void SuchWowWidget::showContextMenu(const QPoint &pos) { void SuchWowWidget::showContextMenu(const QPoint &pos) {
@ -95,12 +97,18 @@ void SuchWowWidget::suchImage() {
auto *post = this->itemToPost(); auto *post = this->itemToPost();
if(post == nullptr) if(post == nullptr)
return; return;
if(!post->img_data) {
QMessageBox::warning(this, "wh00pz", "Image not dowloaded yet."); if(!post->img_data && !post->isFetchingImage()) {
connect(post, &SuchWowPost::imgReceived, this, &SuchWowWidget::showImage);
post->download_img();
return; return;
} }
const auto title = QString("%1 - %2").arg(post->title).arg(post->added_by); this->showImage(post);
}
void SuchWowWidget::showImage(SuchWowPost *post) {
const auto title = QString("%1 - %2").arg(post->title, post->added_by);
QMessageBox mb(title, "", QMessageBox mb(title, "",
QMessageBox::NoIcon, QMessageBox::NoIcon,
QMessageBox::Ok, QMessageBox::Ok,
@ -119,11 +127,12 @@ void SuchWowWidget::suchDonate() {
SuchWowPost *SuchWowWidget::itemToPost() { SuchWowPost *SuchWowWidget::itemToPost() {
QListWidgetItem *item = ui->listWidget->currentItem(); QListWidgetItem *item = ui->listWidget->currentItem();
QString title = item->text(); QString title = item->text();
for(auto &post: m_posts){
if(post->title == title) { for(const auto &such_key: m_lookup.keys()) {
return post; if(m_lookup[such_key]->title == title)
} return m_lookup[such_key];
} }
return nullptr; return nullptr;
} }

View file

@ -4,9 +4,11 @@
#ifndef SUCHWOWWIDGET_H #ifndef SUCHWOWWIDGET_H
#define SUCHWOWWIDGET_H #define SUCHWOWWIDGET_H
#include <QObject>
#include <QMenu> #include <QMenu>
#include <QWidget> #include <QWidget>
#include <QItemSelection> #include <QItemSelection>
#include <QListWidgetItem>
#include "utils/networking.h" #include "utils/networking.h"
#include "appcontext.h" #include "appcontext.h"
@ -15,7 +17,6 @@ namespace Ui {
class SuchWowWidget; class SuchWowWidget;
} }
class SuchWowPost : public QObject class SuchWowPost : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -30,31 +31,33 @@ public:
QString img; QString img;
QString addy; QString addy;
QString href; QString href;
uint uid;
QPixmap thumb_data; QPixmap thumb_data;
QPixmap img_data; QPixmap img_data;
void download_thumb() { void download_thumb() {
const QString url = QString("%1/%2").arg(m_weburl).arg(this->thumb); const QString url = QString("%1/%2").arg(m_weburl, this->thumb);
qDebug() << url;
connect(m_networkThumb, &UtilsNetworking::webReceived, this, &SuchWowPost::onThumbReceived); connect(m_networkThumb, &UtilsNetworking::webReceived, this, &SuchWowPost::onThumbReceived);
m_networkThumb->get(url); m_networkThumb->get(url);
} }
void download_img() { void download_img() {
const QString url = QString("%1/%2").arg(m_weburl).arg(this->img); const QString url = QString("%1/%2").arg(m_weburl, this->img);
qDebug() << url;
connect(m_networkImg, &UtilsNetworking::webReceived, this, &SuchWowPost::onImgReceived); connect(m_networkImg, &UtilsNetworking::webReceived, this, &SuchWowPost::onImgReceived);
m_networkImg->get(url); m_networkImg->get(url);
} }
bool isFetchingImage() { return m_networkImg->busy; }
bool isFetchingThumb() { return m_networkThumb->busy; }
private slots: private slots:
void onThumbReceived(QByteArray data); void onThumbReceived(QByteArray data);
void onImgReceived(QByteArray data); void onImgReceived(QByteArray data);
signals: signals:
void imgReceived(SuchWowPost *test); void imgReceived(SuchWowPost *post);
void thumbReceived(SuchWowPost *test); void thumbReceived(SuchWowPost *post);
private: private:
QString m_weburl; QString m_weburl;
@ -75,11 +78,12 @@ public slots:
void onWS(QJsonArray such_data); void onWS(QJsonArray such_data);
private slots: private slots:
void addThumb(SuchWowPost *test); void addThumb(SuchWowPost *post);
void showImage(SuchWowPost *post);
signals: signals:
void donate(QString donate); void donate(QString donate);
void openImage(SuchWowPost *test); void openImage(SuchWowPost *post);
private: private:
void setupTable(); void setupTable();
@ -88,11 +92,28 @@ private:
SuchWowPost* itemToPost(); SuchWowPost* itemToPost();
void showContextMenu(const QPoint &pos); void showContextMenu(const QPoint &pos);
bool m_rendered = false; // because we're too lazy to re-render on changes. QMap<uint, SuchWowPost*> m_lookup;
Ui::SuchWowWidget *ui; Ui::SuchWowWidget *ui;
QList<SuchWowPost*> m_posts;
AppContext *m_ctx = nullptr; AppContext *m_ctx = nullptr;
QMenu *m_contextMenu; QMenu *m_contextMenu;
}; };
#endif // SUCHWOWWIDGET_H class SuchWidgetItem : public QListWidgetItem
{
public:
explicit SuchWidgetItem(const QIcon &icon, const QString &text, SuchWowPost *post, QListWidget *parent = nullptr, int type = Type) {
this->setIcon(icon);
this->setText(text);
this->post = post;
}
SuchWowPost *post;
// sort
virtual bool operator< (const QListWidgetItem &other) const {
auto const *_other = dynamic_cast<const SuchWidgetItem *>(&other);
return this->post->uid > _other->post->uid;
}
};
#endif // SUCHWOWWIDGET_H