revuo: add upcoming events

This commit is contained in:
tobtoht 2024-10-07 16:08:28 +02:00
parent 6aa661d7ba
commit 2f11cf2710
No known key found for this signature in database
GPG key ID: E45B10DD027D2472
4 changed files with 83 additions and 71 deletions

View file

@ -7,13 +7,18 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
struct RevuoItem { struct RevuoItem : QObject
RevuoItem(const QString &title, const QString &url, const QStringList &newsbytes) {
: title(title), url(url), newsbytes(newsbytes){}; Q_OBJECT
public:
explicit RevuoItem(QObject *parent)
: QObject(parent) {};
QString title; QString title;
QString url; QString url;
QStringList newsbytes; QStringList newsbytes;
QList<QPair<QString, QString>> events;
}; };
#endif //FEATHER_REVUOITEM_H #endif //FEATHER_REVUOITEM_H

View file

@ -8,28 +8,24 @@
#include "utils/ColorScheme.h" #include "utils/ColorScheme.h"
#include "Utils.h" #include "Utils.h"
#include "utils/Icons.h"
#include "utils/WebsocketNotifier.h" #include "utils/WebsocketNotifier.h"
RevuoWidget::RevuoWidget(QWidget *parent) RevuoWidget::RevuoWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, ui(new Ui::RevuoWidget) , ui(new Ui::RevuoWidget)
, m_contextMenu(new QMenu(this))
{ {
ui->setupUi(this); ui->setupUi(this);
ui->textBrowser->setOpenLinks(false); ui->textBrowser->setOpenLinks(false);
ui->textBrowser->document()->setDefaultStyleSheet("a {color: white; }"); ui->textBrowser->document()->setDefaultStyleSheet("a {color: white; }");
ui->textBrowser->setText("<h4>No item selected</h4>");
connect(ui->textBrowser, &QTextBrowser::anchorClicked, this, &RevuoWidget::onLinkActivated); connect(ui->textBrowser, &QTextBrowser::anchorClicked, this, &RevuoWidget::onLinkActivated);
ui->textBrowser->setText("<h4>No item selected</h4>"); ui->btn_openLink->setIcon(icons()->icon("external-link.svg"));
connect(ui->btn_openLink, &QPushButton::clicked, this, &RevuoWidget::onOpenLink);
m_contextMenu->addAction("Open link", this, &RevuoWidget::onOpenLink); connect(ui->combo_issue, &QComboBox::currentIndexChanged, this, &RevuoWidget::onSelectItem);
m_contextMenu->addAction("Donate to author", this, &RevuoWidget::onDonate);
ui->splitter->setStretchFactor(1, 5);
connect(ui->listWidget, &QListWidget::currentTextChanged, this, &RevuoWidget::onSelectItem);
connect(ui->listWidget, &QListWidget::customContextMenuRequested, this, &RevuoWidget::showContextMenu);
connect(websocketNotifier(), &WebsocketNotifier::dataReceived, this, [this](const QString& type, const QJsonValue& json) { connect(websocketNotifier(), &WebsocketNotifier::dataReceived, this, [this](const QString& type, const QJsonValue& json) {
if (type == "revuo") { if (type == "revuo") {
@ -39,18 +35,21 @@ RevuoWidget::RevuoWidget(QWidget *parent)
for (const auto &entry: revuo_data) { for (const auto &entry: revuo_data) {
auto obj = entry.toObject(); auto obj = entry.toObject();
QStringList newsbytes; QSharedPointer<RevuoItem> item = QSharedPointer<RevuoItem>(new RevuoItem(this));
for (const auto &n : obj.value("newsbytes").toArray()) { for (const auto &n : obj.value("newsbytes").toArray()) {
newsbytes.append(n.toString()); item->newsbytes.append(n.toString());
} }
auto revuoItem = new RevuoItem( for (const auto &e : obj.value("events").toArray()) {
obj.value("title").toString(), auto f = e.toObject();
obj.value("url").toString(), item->events.append({f.value("date").toString(), f.value("description").toString()});
newsbytes); }
QSharedPointer<RevuoItem> r = QSharedPointer<RevuoItem>(revuoItem); item->title = obj.value("title").toString();
l.append(r); item->url = obj.value("url").toString();
l.append(item);
} }
this->updateItems(l); this->updateItems(l);
@ -59,6 +58,10 @@ RevuoWidget::RevuoWidget(QWidget *parent)
} }
void RevuoWidget::updateItems(const QList<QSharedPointer<RevuoItem>> &items) { void RevuoWidget::updateItems(const QList<QSharedPointer<RevuoItem>> &items) {
m_items.clear();
m_links.clear();
ui->combo_issue->clear();
QStringList titles; QStringList titles;
for (const auto &item : items) { for (const auto &item : items) {
titles << item->title; titles << item->title;
@ -67,26 +70,34 @@ void RevuoWidget::updateItems(const QList<QSharedPointer<RevuoItem>> &items) {
for (const auto &newsbyte : item->newsbytes) { for (const auto &newsbyte : item->newsbytes) {
text += "<p> • " + newsbyte + "</p>\n"; text += "<p> • " + newsbyte + "</p>\n";
} }
text += QString("<br>\nRead the whole issue in your <a href=\"%1\">browser</a>.").arg(item->url); text += "<h3>Upcoming Events</h3>\n";
if (item->events.isEmpty()) {
text += "<p>There are no upcoming events.</p>\n";
}
for (const auto &event : item->events) {
text += "<h4>" + event.first + "</h4>\n";
text += "<p>" + event.second + "</p>\n";
}
text += "<hr>";
text += QString("Read the whole issue in your <a href=\"%1\">browser</a>.").arg(item->url);
text += "<br><br>\nEnjoy Revuo? Consider a <a href=\"feather://donate-revuo\">donation</a> to the author."; text += "<br><br>\nEnjoy Revuo? Consider a <a href=\"feather://donate-revuo\">donation</a> to the author.";
m_items[item->title] = text; m_items.append(text);
m_links[item->title] = item->url; m_links.append(item->url);
ui->combo_issue->addItem(item->title);
} }
ui->listWidget->clear(); ui->combo_issue->setCurrentIndex(0);
ui->listWidget->addItems(titles);
ui->listWidget->setCurrentRow(0);
ui->listWidget->setMinimumWidth(ui->listWidget->sizeHintForColumn(0) + 10);
} }
void RevuoWidget::onSelectItem(const QString &item) { void RevuoWidget::onSelectItem(int index) {
auto *currentItem = ui->listWidget->currentItem(); if (index >= m_items.length()) {
if (currentItem == nullptr) { ui->textBrowser->setText("<h4>No item selected</h4>");
return; return;
} }
QString title = currentItem->text();
ui->textBrowser->setText(m_items[title]); ui->textBrowser->setText(m_items[index]);
} }
void RevuoWidget::onLinkActivated(const QUrl &link) { void RevuoWidget::onLinkActivated(const QUrl &link) {
@ -98,12 +109,8 @@ void RevuoWidget::onLinkActivated(const QUrl &link) {
Utils::externalLinkWarning(this, link.toString()); Utils::externalLinkWarning(this, link.toString());
} }
void RevuoWidget::showContextMenu(const QPoint &pos) {
m_contextMenu->exec(ui->listWidget->viewport()->mapToGlobal(pos));
}
void RevuoWidget::onOpenLink() { void RevuoWidget::onOpenLink() {
QString currentItem = ui->listWidget->currentItem()->text(); int currentItem = ui->combo_issue->currentIndex();
Utils::externalLinkWarning(this, m_links[currentItem]); Utils::externalLinkWarning(this, m_links[currentItem]);
} }
@ -119,7 +126,7 @@ void RevuoWidget::skinChanged() {
auto stylesheet = QString("a {color: %1; }").arg(color); auto stylesheet = QString("a {color: %1; }").arg(color);
ui->textBrowser->document()->setDefaultStyleSheet(stylesheet); ui->textBrowser->document()->setDefaultStyleSheet(stylesheet);
this->onSelectItem(""); this->onSelectItem(0);
} }
RevuoWidget::~RevuoWidget() = default; RevuoWidget::~RevuoWidget() = default;

View file

@ -30,17 +30,15 @@ public slots:
private slots: private slots:
void onLinkActivated(const QUrl &link); void onLinkActivated(const QUrl &link);
void onSelectItem(const QString &item); void onSelectItem(int index);
void onOpenLink(); void onOpenLink();
void onDonate(); void onDonate();
void showContextMenu(const QPoint &pos);
private: private:
QScopedPointer<Ui::RevuoWidget> ui; QScopedPointer<Ui::RevuoWidget> ui;
QMenu *m_contextMenu; QStringList m_items;
QHash<QString, QString> m_items; QStringList m_links;
QHash<QString, QString> m_links;
}; };

View file

@ -14,34 +14,36 @@
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<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> <item>
<widget class="QSplitter" name="splitter"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="orientation"> <item>
<enum>Qt::Horizontal</enum> <layout class="QHBoxLayout" name="horizontalLayout_2">
</property> <item>
<widget class="QListWidget" name="listWidget"> <widget class="QComboBox" name="combo_issue"/>
<property name="contextMenuPolicy"> </item>
<enum>Qt::CustomContextMenu</enum> <item>
</property> <widget class="QPushButton" name="btn_openLink">
</widget> <property name="sizePolicy">
<widget class="QTextBrowser" name="textBrowser"> <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<property name="html"> <horstretch>0</horstretch>
<string></string> <verstretch>0</verstretch>
</property> </sizepolicy>
</widget> </property>
</widget> <property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTextBrowser" name="textBrowser">
<property name="html">
<string></string>
</property>
</widget>
</item>
</layout>
</item> </item>
</layout> </layout>
</widget> </widget>