FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
#include "screens/dashboard/widgets/NewsCategoryWidget.h"
#include "datahub/DataHub.h"
#include "datahub/DataHubMetaTypes.h"
#include "services/news/NewsService.h"
#include "ui/theme/Theme.h"
#include <QDateTime>
#include <QDesktopServices>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QJsonObject>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QSpinBox>
#include <QUrl>
namespace fincept::screens::widgets {
NewsCategoryWidget::NewsCategoryWidget(const QJsonObject& cfg, QWidget* parent)
: BaseWidget(tr("NEWS — CATEGORY"), parent) {
auto* vl = content_layout();
vl->setContentsMargins(8, 6, 8, 6);
vl->setSpacing(4);
auto* header = new QHBoxLayout();
header->setContentsMargins(0, 0, 0, 0);
badge_ = new QLabel();
header->addWidget(badge_);
header->addStretch(1);
vl->addLayout(header);
list_ = new QListWidget(this);
list_->setUniformItemSizes(false);
list_->setWordWrap(true);
list_->setSelectionMode(QAbstractItemView::NoSelection);
list_->setFocusPolicy(Qt::NoFocus);
list_->setToolTip(tr("Click a headline to open the article"));
vl->addWidget(list_, 1);
// Headline → article. The URL rides on the item (Qt::UserRole); only
// http(s) links are opened.
connect(list_, &QListWidget::itemClicked, this, [](QListWidgetItem* item) {
const QUrl url(item->data(Qt::UserRole).toString());
if (url.isValid() && (url.scheme() == QLatin1String("http") || url.scheme() == QLatin1String("https")))
QDesktopServices::openUrl(url);
});
// The title-bar refresh button was not connected here at all (the
// watchdog's "click refresh to retry" pointed at a dead button).
connect(this, &BaseWidget::refresh_requested, this, [this]() {
if (is_showing_error())
set_loading(true);
datahub::DataHub::instance().request(QStringLiteral("news:category:") + category_, /*force=*/true);
});
set_configurable(true);
apply_styles();
apply_config(cfg);
}
QJsonObject NewsCategoryWidget::config() const {
QJsonObject o;
o.insert("category", category_);
o.insert("max_rows", max_rows_);
return o;
}
void NewsCategoryWidget::apply_config(const QJsonObject& cfg) {
category_ = cfg.value("category").toString("markets").trimmed();
if (category_.isEmpty())
category_ = "markets";
max_rows_ = qBound(3, cfg.value("max_rows").toInt(12), 50);
badge_->setText(category_.toUpper());
list_->clear();
// Show the loading overlay until the first delivery for this category —
// the list used to sit blank (indistinguishable from "broken") while the
// topic fetched.
set_loading(true);
if (isVisible())
hub_resubscribe();
}
void NewsCategoryWidget::hub_resubscribe() {
auto& hub = datahub::DataHub::instance();
hub.unsubscribe(this);
const QString topic = QStringLiteral("news:category:") + category_;
hub.subscribe(this, topic, [this](const QVariant& v) { on_articles(v); });
hub.subscribe_errors(this, topic, [this](const QString& error) {
if (is_waiting_for_first_data())
set_error(tr("News feed failed: %1 — click refresh to retry").arg(error));
});
const QVariant snap = hub.peek(topic);
if (snap.isValid())
on_articles(snap);
hub_active_ = true;
}
void NewsCategoryWidget::hub_unsubscribe_all() {
datahub::DataHub::instance().unsubscribe(this);
hub_active_ = false;
}
void NewsCategoryWidget::showEvent(QShowEvent* e) {
BaseWidget::showEvent(e);
if (!hub_active_)
hub_resubscribe();
}
void NewsCategoryWidget::hideEvent(QHideEvent* e) {
BaseWidget::hideEvent(e);
if (hub_active_)
hub_unsubscribe_all();
}
void NewsCategoryWidget::on_articles(const QVariant& v) {
if (!v.canConvert<QVector<fincept::services::NewsArticle>>())
return;
const auto articles = v.value<QVector<fincept::services::NewsArticle>>();
list_->clear();
int n = std::min<int>(max_rows_, articles.size());
for (int i = 0; i < n; ++i) {
const auto& a = articles[i];
const QString time = (a.sort_ts > 0) ? QDateTime::fromSecsSinceEpoch(a.sort_ts).toString("HH:mm") : a.time;
const QString line = QString("[%1] %2").arg(time, a.headline);
auto* item = new QListWidgetItem(line, list_);
item->setToolTip(a.summary.isEmpty() ? a.headline : a.summary);
item->setData(Qt::UserRole, a.link);
}
// Empty state — an unknown/quiet category rendered a blank list.
if (n == 0) {
auto* empty = new QListWidgetItem(tr("No articles in this category"), list_);
empty->setFlags(Qt::NoItemFlags);
empty->setTextAlignment(Qt::AlignCenter);
}
set_loading(false);
}
QDialog* NewsCategoryWidget::make_config_dialog(QWidget* parent) {
auto* dlg = new QDialog(parent);
dlg->setWindowTitle(tr("Configure — News Category"));
auto* form = new QFormLayout(dlg);
auto* edit = new QLineEdit(dlg);
edit->setText(category_);
edit->setPlaceholderText(tr("markets | geopolitics | crypto | …"));
form->addRow(tr("Category"), edit);
auto* spin = new QSpinBox(dlg);
spin->setRange(3, 50);
spin->setValue(max_rows_);
form->addRow(tr("Max rows"), spin);
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dlg);
form->addRow(buttons);
connect(buttons, &QDialogButtonBox::accepted, dlg, [this, dlg, edit, spin]() {
QJsonObject cfg;
cfg.insert("category", edit->text().trimmed());
cfg.insert("max_rows", spin->value());
apply_config(cfg);
emit config_changed(cfg);
dlg->accept();
});
connect(buttons, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
return dlg;
}
void NewsCategoryWidget::on_theme_changed() {
apply_styles();
}
void NewsCategoryWidget::apply_styles() {
badge_->setStyleSheet(QString("color:%1;background:%2;border:1px solid %3;"
"border-radius:3px;padding:1px 6px;font-size:9px;font-weight:700;")
.arg(ui::colors::TEXT_SECONDARY(), ui::colors::BG_RAISED(), ui::colors::BORDER_DIM()));
list_->setStyleSheet(QString("QListWidget{background:transparent;color:%1;font-size:10px;border:none;}"
"QListWidget::item{padding:3px 4px;border-bottom:1px solid %2;}")
.arg(ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM()));
}
void NewsCategoryWidget::retranslateUi() {
BaseWidget::retranslateUi();
set_title(tr("NEWS — CATEGORY"));
// Re-render from the hub — but only while subscribed. This used to call
// hub_resubscribe() unconditionally, so a language switch made every hidden
// tile subscribe (and stay subscribed until its next hide).
if (hub_active_)
hub_resubscribe();
}
} // namespace fincept::screens::widgets