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
// src/screens/gov_data/GovDataScreen.cpp
#include "screens/gov_data/GovDataScreen.h"
#include "core/logging/Logger.h"
#include "core/session/ScreenStateManager.h"
#include "screens/gov_data/GovDataAustraliaPanel.h"
#include "screens/gov_data/GovDataCongressPanel.h"
#include "screens/gov_data/GovDataFrancePanel.h"
#include "screens/gov_data/GovDataHKPanel.h"
#include "screens/gov_data/GovDataProviderPanel.h"
#include "screens/gov_data/GovDataTreasuryPanel.h"
#include "screens/gov_data/GovDataUKPanel.h"
#include "services/gov_data/GovDataService.h"
#include "ui/theme/Theme.h"
#include <QEvent>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QSplitter>
#include <QStackedWidget>
#include <QVBoxLayout>
namespace fincept::screens {
using namespace fincept::ui;
// ── Dynamic stylesheet ──────────────────────────────────────────────────────
static QString build_screen_style() {
const auto& t = ThemeManager::instance().tokens();
const auto ac = QColor(t.accent);
const QString ar = QString("%1,%2,%3").arg(ac.red()).arg(ac.green()).arg(ac.blue());
const QString bg = QString::fromLatin1(t.bg_base);
const QString text = QString::fromLatin1(t.text_primary);
const QString raised = QString::fromLatin1(t.bg_raised);
const QString bdim = QString::fromLatin1(t.border_dim);
const QString surface = QString::fromLatin1(t.bg_surface);
const QString sec = QString::fromLatin1(t.text_secondary);
const QString hover = QString::fromLatin1(t.bg_hover);
const QString accent = QString::fromLatin1(t.accent);
const QString dim = QString::fromLatin1(t.text_dim);
QString s;
s += QString("#govScreen { background:%1; }").arg(bg);
// Toolbar
s += QString("#govToolbar { background:%1; border-bottom:1px solid %2; }").arg(raised, bdim);
s += QString("#govToolbarTitle { color:%1; font-size:12px; font-weight:700;"
" letter-spacing:1px; background:transparent; }")
.arg(text);
s += QString("#govToolbarSub { color:%1; font-size:10px; background:transparent; }").arg(sec);
// Sidebar
s += QString("#govSidebar { background:%1; border-right:1px solid %2; }").arg(surface, bdim);
s += QString("#govSidebarHeader { background:%1; border-bottom:1px solid %2; }").arg(raised, bdim);
s += QString("#govSidebarTitle { color:%1; font-size:9px; font-weight:700;"
" letter-spacing:1px; background:transparent; }")
.arg(sec);
s += "#govProviderList { background:transparent; border:none; outline:none; }";
s += QString("#govProviderList::item { color:%1; padding:7px 14px;"
" border-bottom:1px solid %2; font-size:11px; }")
.arg(sec, bdim);
s += QString("#govProviderList::item:hover { color:%1; background:%2; }").arg(text, hover);
s += QString("#govProviderList::item:selected { color:%1; background:rgba(%2,0.08);"
" border-left:2px solid %1; padding-left:12px; font-weight:700; }")
.arg(accent, ar);
// Status bar
s += QString("#govStatusBar { background:%1; border-top:1px solid %2; }").arg(raised, bdim);
s += QString("#govStatusText { color:%1; font-size:9px; background:transparent; }").arg(dim);
s += QString("#govStatusSep { color:%1; font-size:9px; background:transparent; }").arg(bdim);
s += QString("#govStatusVal { color:%1; font-size:9px; background:transparent; }").arg(sec);
s += QString("#govStatusHigh { color:%1; font-size:9px; font-weight:700; background:transparent; }").arg(accent);
// Scrollbar
s += QString("QScrollBar:vertical { background:%1; width:5px; }").arg(bg);
s += QString("QScrollBar::handle:vertical { background:%1; min-height:20px; }").arg(bdim);
s += "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height:0; }";
return s;
}
// ── Constructor ──────────────────────────────────────────────────────────────
GovDataScreen::GovDataScreen(QWidget* parent) : QWidget(parent) {
setObjectName("govScreen");
build_ui();
retranslateUi();
connect(&ThemeManager::instance(), &ThemeManager::theme_changed, this, &GovDataScreen::refresh_theme);
refresh_theme();
}
// ── Show / Hide ──────────────────────────────────────────────────────────────
void GovDataScreen::showEvent(QShowEvent* event) {
QWidget::showEvent(event);
if (!initial_load_done_) {
initial_load_done_ = true;
// Honour a provider already chosen by restore_state(); the old code
// unconditionally forced row 0 and discarded the restored selection.
const int row = (active_index_ >= 0 && active_index_ < provider_list_->count()) ? active_index_ : 0;
if (provider_list_->count() > 0) {
provider_list_->setCurrentRow(row);
activate_provider(row);
}
}
LOG_INFO("GovDataScreen", "shown");
}
void GovDataScreen::hideEvent(QHideEvent* event) {
QWidget::hideEvent(event);
}
void GovDataScreen::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange) {
retranslateUi();
}
QWidget::changeEvent(event);
}
// ── Build UI ─────────────────────────────────────────────────────────────────
void GovDataScreen::build_ui() {
auto* root = new QVBoxLayout(this);
root->setContentsMargins(0, 0, 0, 0);
root->setSpacing(0);
root->addWidget(build_toolbar());
// Content: sidebar | panel stack
auto* content = new QWidget(this);
auto* content_hl = new QHBoxLayout(content);
content_hl->setContentsMargins(0, 0, 0, 0);
content_hl->setSpacing(0);
content_hl->addWidget(build_sidebar());
panel_stack_ = new QStackedWidget;
// P2 — panels are lazy-constructed on first activation (see make_panel()).
// Eagerly building all provider panels here created every sub-widget tree,
// stylesheet and QTimer for portals the user never opens. Stack index 0 is
// a blank placeholder shown before any provider has been selected.
panel_stack_->addWidget(new QWidget(panel_stack_));
panels_.resize(services::GovDataService::providers().size());
panels_.fill(nullptr);
content_hl->addWidget(panel_stack_, 1);
root->addWidget(content, 1);
root->addWidget(build_status_bar());
}
// ── Lazy panel factory ───────────────────────────────────────────────────────
QWidget* GovDataScreen::make_panel(int index) {
const auto& providers = services::GovDataService::providers();
if (index < 0 || index >= providers.size())
return nullptr;
const auto& prov = providers[index];
QWidget* panel = nullptr;
{
if (prov.id == "us-treasury") {
panel = new GovDataTreasuryPanel(panel_stack_);
} else if (prov.id == "us-congress") {
panel = new GovDataCongressPanel(panel_stack_);
} else if (prov.id == "canada-gov") {
panel = new GovDataProviderPanel(prov.script, prov.color, "Publishers", {}, panel_stack_);
} else if (prov.id == "swiss") {
GovProviderOptions swiss_opts;
swiss_opts.watermark_text = "\xF0\x9F\x87\xA8\xF0\x9F\x87\xAD opendata.swiss";
panel = new GovDataProviderPanel(prov.script, prov.color, "Publishers", swiss_opts, panel_stack_);
} else if (prov.id == "france") {
panel = new GovDataFrancePanel(panel_stack_);
} else if (prov.id == "hk") {
panel = new GovDataHKPanel(panel_stack_);
} else if (prov.id == "universal-ckan") {
// universal_ckan_api.py serves several national CKAN portals: the panel asks it for the
// portal list and sends the selected portal code with every request.
GovProviderOptions ckan_opts;
ckan_opts.portals_command = QStringLiteral("portals");
panel = new GovDataProviderPanel(prov.script, prov.color, "Publishers", ckan_opts, panel_stack_);
} else if (prov.id == "uk") {
// data.gov.uk keeps its dedicated panel (pagination + popular publishers).
panel = new GovDataUKPanel(panel_stack_);
} else if (prov.id == "australia") {
panel = new GovDataAustraliaPanel(panel_stack_);
} else {
// openafrica, spain — still use generic until dedicated scripts exist
panel = new GovDataProviderPanel(prov.script, prov.color, "Organizations", {}, panel_stack_);
}
}
if (panel) {
panel_stack_->addWidget(panel);
panels_[index] = panel;
LOG_INFO("GovDataScreen", "Created panel for: " + prov.id);
}
return panel;
}
// ── Toolbar ──────────────────────────────────────────────────────────────────
QWidget* GovDataScreen::build_toolbar() {
header_bar_ = new QWidget(this);
header_bar_->setObjectName("govToolbar");
header_bar_->setFixedHeight(38);
auto* hl = new QHBoxLayout(header_bar_);
hl->setContentsMargins(14, 0, 14, 0);
hl->setSpacing(8);
header_title_ = new QLabel;
header_title_->setObjectName("govToolbarTitle");
header_title_->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
hl->addWidget(header_title_);
// Separator dot
auto* sep = new QLabel("·");
sep->setObjectName("govToolbarSub");
sep->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
hl->addWidget(sep);
header_subtitle_ = new QLabel;
header_subtitle_->setObjectName("govToolbarSub");
header_subtitle_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
hl->addWidget(header_subtitle_);
hl->addStretch(1);
// Source count badge — compact pill, stored as member for refresh_theme()
provider_badge_ = new QLabel;
provider_badge_->setFixedHeight(20);
provider_badge_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
hl->addWidget(provider_badge_);
return header_bar_;
}
// ── Sidebar ──────────────────────────────────────────────────────────────────
QWidget* GovDataScreen::build_sidebar() {
auto* sidebar = new QWidget(this);
sidebar->setObjectName("govSidebar");
sidebar->setFixedWidth(230);
auto* vl = new QVBoxLayout(sidebar);
vl->setContentsMargins(0, 0, 0, 0);
vl->setSpacing(0);
// Section header
auto* hdr = new QWidget(this);
hdr->setObjectName("govSidebarHeader");
hdr->setFixedHeight(26);
auto* hhl = new QHBoxLayout(hdr);
hhl->setContentsMargins(12, 0, 8, 0);
hhl->setSpacing(6);
sidebar_title_ = new QLabel;
sidebar_title_->setObjectName("govSidebarTitle");
const auto& providers = services::GovDataService::providers();
sidebar_count_ = new QLabel(QString::number(providers.size()));
sidebar_count_->setFixedHeight(16);
sidebar_count_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
hhl->addWidget(sidebar_title_);
hhl->addStretch(1);
hhl->addWidget(sidebar_count_);
vl->addWidget(hdr);
// Provider list
provider_list_ = new QListWidget;
provider_list_->setObjectName("govProviderList");
provider_list_->setAccessibleName(tr("Sovereign open-data portals"));
for (const auto& prov : providers) {
auto* item = new QListWidgetItem(provider_list_);
// Flag + name on first line, country on tooltip
item->setText(QString("%1 %2").arg(prov.flag, prov.name));
item->setToolTip(prov.description + "\n" + prov.country);
// Screen readers can't announce the flag emoji usefully — give the row
// a plain-text accessible label.
item->setData(Qt::AccessibleTextRole, QString("%1 — %2").arg(prov.name, prov.country));
item->setData(Qt::UserRole, prov.id);
item->setData(Qt::UserRole + 1, prov.color);
}
connect(provider_list_, &QListWidget::currentRowChanged, this, &GovDataScreen::on_provider_selected);
vl->addWidget(provider_list_, 1);
return sidebar;
}
// ── Status bar ───────────────────────────────────────────────────────────────
QWidget* GovDataScreen::build_status_bar() {
auto* bar = new QWidget(this);
bar->setObjectName("govStatusBar");
bar->setFixedHeight(24);
auto* hl = new QHBoxLayout(bar);
hl->setContentsMargins(14, 0, 14, 0);
hl->setSpacing(6);
auto sep = [&]() -> QLabel* {
auto* l = new QLabel("·");
l->setObjectName("govStatusSep");
return l;
};
status_govt_lbl_ = new QLabel;
status_govt_lbl_->setObjectName("govStatusText");
hl->addWidget(status_govt_lbl_);
hl->addWidget(sep());
status_portal_lbl_ = new QLabel;
status_portal_lbl_->setObjectName("govStatusText");
status_portal_ = new QLabel("—");
status_portal_->setObjectName("govStatusVal");
hl->addWidget(status_portal_lbl_);
hl->addWidget(status_portal_);
hl->addWidget(sep());
status_country_lbl_ = new QLabel;
status_country_lbl_->setObjectName("govStatusText");
status_country_ = new QLabel("—");
status_country_->setObjectName("govStatusVal");
hl->addWidget(status_country_lbl_);
hl->addWidget(status_country_);
hl->addStretch(1);
// Ready dot
auto* dot = new QLabel;
dot->setFixedSize(6, 6);
dot->setStyleSheet(QString("background:%1; border-radius:3px;").arg(colors::POSITIVE()));
hl->addWidget(dot);
status_ready_lbl_ = new QLabel;
status_ready_lbl_->setObjectName("govStatusText");
status_ready_lbl_->setStyleSheet(
QString("color:%1; font-size:9px; background:transparent;").arg(colors::POSITIVE()));
hl->addWidget(status_ready_lbl_);
return bar;
}
// ── Re-translation ───────────────────────────────────────────────────────────
void GovDataScreen::retranslateUi() {
const int n_providers = services::GovDataService::providers().size();
if (header_title_)
header_title_->setText(tr("GOVERNMENT DATA EXPLORER"));
// Subtitle: when a provider is active, activate_provider() overwrites this
// with "{flag} {full_name}" — which is data, not translatable. Only set
// the default landing-page text when no provider has been selected yet.
if (header_subtitle_ && active_index_ < 0)
header_subtitle_->setText(tr("Open government portals · %1 sovereign sources").arg(n_providers));
if (provider_badge_)
provider_badge_->setText(tr("%1 PORTALS").arg(n_providers));
if (sidebar_title_)
sidebar_title_->setText(tr("SOVEREIGN PORTALS"));
if (status_govt_lbl_)
status_govt_lbl_->setText(tr("GOVT"));
if (status_portal_lbl_)
status_portal_lbl_->setText(tr("PORTAL:"));
if (status_country_lbl_)
status_country_lbl_->setText(tr("COUNTRY:"));
if (status_ready_lbl_)
status_ready_lbl_->setText(tr("READY"));
}
// ── Theme refresh ────────────────────────────────────────────────────────────
void GovDataScreen::refresh_theme() {
setStyleSheet(build_screen_style());
const auto& t = ThemeManager::instance().tokens();
const QString accent_color =
active_provider_color_.isEmpty() ? QString::fromLatin1(t.accent) : active_provider_color_;
const auto ac = QColor(accent_color);
const QString ar = QString("%1,%2,%3").arg(ac.red()).arg(ac.green()).arg(ac.blue());
// Re-style provider count badge (top-right pill)
if (provider_badge_) {
provider_badge_->setStyleSheet(
QString("color:%1; background:rgba(%2,0.1); border:1px solid rgba(%2,0.3);"
" font-size:9px; font-weight:700; padding:2px 8px; letter-spacing:0.5px;")
.arg(accent_color, ar));
}
// Re-style sidebar count pill
if (sidebar_count_) {
const auto& tk = ThemeManager::instance().tokens();
const auto ac2 = QColor(tk.accent);
const QString ar2 = QString("%1,%2,%3").arg(ac2.red()).arg(ac2.green()).arg(ac2.blue());
sidebar_count_->setStyleSheet(QString("color:%1; background:rgba(%2,0.12); font-size:9px;"
" font-weight:700; padding:1px 5px;")
.arg(tk.accent, ar2));
}
// Re-apply provider-color accent on toolbar border + subtitle
if (!active_provider_color_.isEmpty() && header_bar_ && header_subtitle_) {
header_bar_->setStyleSheet(QString("#govToolbar { background:%1; border-bottom:2px solid %2; }")
.arg(t.bg_raised, active_provider_color_));
header_subtitle_->setStyleSheet(
QString("color:%1; font-size:10px; background:transparent;").arg(active_provider_color_));
}
}
// ── Provider activation ──────────────────────────────────────────────────────
void GovDataScreen::on_provider_selected(int row) {
if (row >= 0) {
activate_provider(row);
ScreenStateManager::instance().notify_changed(this);
}
}
void GovDataScreen::activate_provider(int index) {
const auto& providers = services::GovDataService::providers();
if (index < 0 || index >= providers.size())
return;
active_index_ = index;
const auto& prov = providers[index];
// Store provider accent color so refresh_theme() can apply it
active_provider_color_ = prov.color;
// Update toolbar subtitle text
header_subtitle_->setText(QString("%1 %2").arg(prov.flag, prov.full_name));
// Status bar
if (status_portal_)
status_portal_->setText(prov.name);
if (status_country_)
status_country_->setText(prov.country);
// Keep the sidebar highlight in step — restore_state()/programmatic
// activation used to switch the panel without moving the list selection.
if (provider_list_ && provider_list_->currentRow() != index) {
QSignalBlocker block(provider_list_);
provider_list_->setCurrentRow(index);
}
// Switch panel (lazy-constructing it on first use)
QWidget* panel = (index < panels_.size()) ? panels_[index] : nullptr;
const bool first_open = (panel == nullptr);
if (!panel)
panel = make_panel(index);
if (panel)
panel_stack_->setCurrentWidget(panel);
// Re-apply theme so provider-color accents on toolbar/badge reflect new selection
refresh_theme();
// Trigger the initial load only the first time a panel is opened — all
// panel types expose load_initial_data() as a public slot. Re-invoking it
// on every revisit threw away whatever the user had drilled into.
if (panel && first_open)
QMetaObject::invokeMethod(panel, "load_initial_data");
LOG_INFO("GovDataScreen", "Activated: " + prov.id);
}
// ── IStatefulScreen ───────────────────────────────────────────────────────────
QVariantMap GovDataScreen::save_state() const {
const auto& providers = services::GovDataService::providers();
const QString id = (active_index_ >= 0 && active_index_ < providers.size()) ? providers[active_index_].id : QString();
// The id survives reordering of the provider list; the index is kept for older builds.
return {{"provider_id", id}, {"provider_index", active_index_}};
}
void GovDataScreen::restore_state(const QVariantMap& state) {
int idx = -1;
const QString id = state.value("provider_id").toString();
if (!id.isEmpty()) {
const auto& providers = services::GovDataService::providers();
for (int i = 0; i < providers.size(); ++i) {
if (providers[i].id == id) {
idx = i;
break;
}
}
} else {
idx = state.value("provider_index", -1).toInt();
}
if (idx >= 0)
activate_provider(idx);
}
} // namespace fincept::screens