From c269400dbb2e40d60349ab0a1d67414303653c91 Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Thu, 5 Jan 2023 02:54:59 -0500 Subject: Display status info when list item activated --- src/archive_parser.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++ src/archive_parser.h | 1 + src/list_item.cpp | 9 +++++- src/list_item.h | 3 +- src/mainwindow.cpp | 9 ++++++ src/mainwindow.h | 3 ++ src/mainwindow.ui | 14 +++++++- src/templates/status_info.html | 19 +++++++++++ 8 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/templates/status_info.html diff --git a/src/archive_parser.cpp b/src/archive_parser.cpp index 603ffa7..d3b9197 100644 --- a/src/archive_parser.cpp +++ b/src/archive_parser.cpp @@ -142,3 +142,75 @@ void Archive::update_status_list(ViewStatusTypes allowed_types, QListWidget *par ++i; } } + +QString* get_html_status_info_template() { + static QString* html = new QString(); + + if (html->isNull()) { + QFile file("src/templates/status_info.html"); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + html->append("Error loading status_info template."); + return html; + } + + html->append(file.readAll()); + } + + return html; +} + +QString get_html_status_object_as_list(QJsonObject obj, QString element_name) { + QString text; + + if (obj.contains(element_name)) { + QJsonArray to = obj.value(element_name).toArray(); + + for (auto collection : to) + text.append(collection.toString() + ", "); + } + + if (text.isEmpty()) + text = "(empty)"; + else + text.chop(2); // remove leftover ", " + + return text; +} + +// status_index is assumed to be a valid index +QString Archive::get_html_status_info(int status_index) { + QString info_text(*get_html_status_info_template()); + QJsonObject obj = outbox_items->at(status_index).toObject(); + + if (obj.contains("type") and obj["type"].isString()) + info_text.replace("{{type}}", obj["type"].toString()); + + if (obj.contains("published") and obj["published"].isString()) + info_text.replace("{{published}}", obj["published"].toString()); + + if (obj.contains("id") and obj["id"].isString()) + info_text.replace("{{url-id}}", obj["id"].toString()); + + info_text.replace("{{to}}", get_html_status_object_as_list(obj, "to")); + info_text.replace("{{cc}}", get_html_status_object_as_list(obj, "cc")); + + if (obj["object"].isObject()) { + QJsonObject activity = obj["object"].toObject(); + + if (activity.contains("summary")) { + QString summary_text = activity["summary"].toString(); + if (summary_text.isEmpty()) + summary_text = "(empty)"; + info_text.replace("{{summary}}", summary_text); + } + + if (activity.contains("url") and activity["url"].isString()) + info_text.replace("{{url-status}}", activity["url"].toString()); + + if (activity["content"].isString()) { + info_text.replace("{{content}}", activity["content"].toString()); + } + } + + return info_text; +} diff --git a/src/archive_parser.h b/src/archive_parser.h index 73adb2c..98fd2b8 100644 --- a/src/archive_parser.h +++ b/src/archive_parser.h @@ -30,6 +30,7 @@ public: InitError init(); void update_status_list(ViewStatusTypes allowed_types, QListWidget *parent); + QString get_html_status_info(int status_index); private: QString outbox_filename; ArchiveType archive_type; diff --git a/src/list_item.cpp b/src/list_item.cpp index ce7c86e..b84f147 100644 --- a/src/list_item.cpp +++ b/src/list_item.cpp @@ -13,8 +13,11 @@ QIcon* choose_icon(StatusType status_type) { } ListItem::ListItem(const QString &text, StatusType status_type, QListWidget *parent, int index) : - QListWidgetItem(*choose_icon(status_type), text, parent, ArchiveListItemType), status_index(index) + status_index(index) { + setText(text); + setIcon(*choose_icon(status_type)); + parent->addItem(this); #ifndef NDEBUG QString tool_tip; switch (status_type) { @@ -28,3 +31,7 @@ ListItem::ListItem(const QString &text, StatusType status_type, QListWidget *par setToolTip(tool_tip); #endif } + +int ListItem::get_status_index() { + return status_index; +} diff --git a/src/list_item.h b/src/list_item.h index e2b3c3f..ce5b6b2 100644 --- a/src/list_item.h +++ b/src/list_item.h @@ -3,13 +3,14 @@ #include #include "types.h" -class ListItem : QListWidgetItem { +class ListItem : public QListWidgetItem { public: enum ItemType { ArchiveListItemType = QListWidgetItem::UserType }; ListItem(const QString &text, StatusType status_type, QListWidget *parent = nullptr, int index = 0); + int get_status_index(); private: int status_index; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0b77012..f8440c8 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include "./ui_mainwindow.h" #include "src/archive_parser.h" +#include "src/list_item.h" #include #include @@ -68,6 +69,14 @@ void MainWindow::on_actionAbout_triggered(bool checked) { QMessageBox::information(this, "title", "text"); } +void MainWindow::on_listWidget_itemActivated(QListWidgetItem *item) { + ListItem* casted = dynamic_cast(item); + if (casted != nullptr) { // this is always a nullptr, but it should work?? https://cplusplus.com/forum/beginner/78766/ + QString status_info = data_archive->get_html_status_info(casted->get_status_index()); + ui->statusInfoText->setHtml(status_info); + } +} + void MainWindow::relist_statuses() { if (data_archive) { ui->listWidget->clear(); diff --git a/src/mainwindow.h b/src/mainwindow.h index 881aa2d..ce72397 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "archive_parser.h" @@ -32,6 +33,8 @@ private slots: void on_menuView_aboutToHide(); + void on_listWidget_itemActivated(QListWidgetItem *item); + private: void reset_view_filters(); void relist_statuses(); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index a89812b..c0ca438 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -19,7 +19,16 @@ - + + + IBeamCursor + + + false + + + true + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> @@ -27,6 +36,9 @@ p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p align="center" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-style:italic;">Select Status to display from list.</span></p></body></html> + + Qt::TextBrowserInteraction + diff --git a/src/templates/status_info.html b/src/templates/status_info.html new file mode 100644 index 0000000..a081463 --- /dev/null +++ b/src/templates/status_info.html @@ -0,0 +1,19 @@ + + + + + Status info + + +

{{type}}

+ + +
CW: {{summary}}
+
{{content}}
+ + -- cgit v1.2.3-54-g00ecf