Browse Source

- only download thumbnails for the overview. Download the large format images later - when requesting to view the image. This saves bandwidth.

- Images are properly sorted by date now
pull/55/head
dsc 3 months ago
parent
commit
474d4b1994
  1. 2
      src/mainwindow.h
  2. 5
      src/utils/networking.cpp
  3. 2
      src/utils/networking.h
  4. 57
      src/widgets/suchwowwidget.cpp
  5. 45
      src/widgets/suchwowwidget.h

2
src/mainwindow.h

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

5
src/utils/networking.cpp

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

2
src/utils/networking.h

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

57
src/widgets/suchwowwidget.cpp

@ -57,31 +57,33 @@ void SuchWowWidget::setupTable() {
}
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();
}
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();
auto uid = obj.value("id").toInt();
if(m_lookup.contains(uid)) continue;
auto post = new SuchWowPost(m_ctx, this);
post->added_by = obj.value("added_by").toString();
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) {
auto *item = new QListWidgetItem(QIcon(test->thumb_data), test->title);
void SuchWowWidget::addThumb(SuchWowPost *post) {
auto *item = new SuchWidgetItem(QIcon(post->thumb_data), post->title, post);
ui->listWidget->addItem(item);
ui->listWidget->sortItems();
}
void SuchWowWidget::showContextMenu(const QPoint &pos) {
@ -95,12 +97,18 @@ void SuchWowWidget::suchImage() {
auto *post = this->itemToPost();
if(post == nullptr)
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;
}
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::NoIcon,
QMessageBox::Ok,
@ -119,11 +127,12 @@ void SuchWowWidget::suchDonate() {
SuchWowPost *SuchWowWidget::itemToPost() {
QListWidgetItem *item = ui->listWidget->currentItem();
QString title = item->text();
for(auto &post: m_posts){
if(post->title == title) {
return post;
}
for(const auto &such_key: m_lookup.keys()) {
if(m_lookup[such_key]->title == title)
return m_lookup[such_key];
}
return nullptr;
}

45
src/widgets/suchwowwidget.h

@ -4,9 +4,11 @@
#ifndef SUCHWOWWIDGET_H
#define SUCHWOWWIDGET_H
#include <QObject>
#include <QMenu>
#include <QWidget>
#include <QItemSelection>
#include <QListWidgetItem>
#include "utils/networking.h"
#include "appcontext.h"
@ -15,7 +17,6 @@ namespace Ui {
class SuchWowWidget;
}
class SuchWowPost : public QObject
{
Q_OBJECT
@ -30,31 +31,33 @@ public:
QString img;
QString addy;
QString href;
uint uid;
QPixmap thumb_data;
QPixmap img_data;
void download_thumb() {
const QString url = QString("%1/%2").arg(m_weburl).arg(this->thumb);
qDebug() << url;
const QString url = QString("%1/%2").arg(m_weburl, this->thumb);
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;
const QString url = QString("%1/%2").arg(m_weburl, this->img);
connect(m_networkImg, &UtilsNetworking::webReceived, this, &SuchWowPost::onImgReceived);
m_networkImg->get(url);
}
bool isFetchingImage() { return m_networkImg->busy; }
bool isFetchingThumb() { return m_networkThumb->busy; }
private slots:
void onThumbReceived(QByteArray data);
void onImgReceived(QByteArray data);
signals:
void imgReceived(SuchWowPost *test);
void thumbReceived(SuchWowPost *test);
void imgReceived(SuchWowPost *post);
void thumbReceived(SuchWowPost *post);
private:
QString m_weburl;
@ -75,11 +78,12 @@ public slots:
void onWS(QJsonArray such_data);
private slots:
void addThumb(SuchWowPost *test);
void addThumb(SuchWowPost *post);
void showImage(SuchWowPost *post);
signals:
void donate(QString donate);
void openImage(SuchWowPost *test);
void openImage(SuchWowPost *post);
private:
void setupTable();
@ -88,11 +92,28 @@ private:
SuchWowPost* itemToPost();
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;
QList<SuchWowPost*> m_posts;
AppContext *m_ctx = nullptr;
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
Loading…
Cancel
Save