Skip to content

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.

10.0 KB
Raw Blame
#include "screens/dashboard/widgets/StockQuoteWidget.h"

#include "datahub/DataHub.h"
#include "datahub/DataHubMetaTypes.h"
#include "screens/dashboard/widgets/DashboardFormat.h"
#include "ui/theme/Theme.h"

#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QFrame>
#include <QLineEdit>

namespace fincept::screens::widgets {

namespace {
QString normalize_symbol(const QString& s) {
    const QString t = s.trimmed().toUpper();
    return t.isEmpty() ? QStringLiteral("AAPL") : t;
}
} // namespace

StockQuoteWidget::StockQuoteWidget(const QString& symbol, QWidget* parent)
    : BaseWidget(tr("QUOTE: %1").arg(symbol.toUpper()), parent), symbol_(symbol.toUpper()) {
    auto* vl = content_layout();
    vl->setContentsMargins(12, 8, 12, 8);
    vl->setSpacing(8);

    // ── Price row ──
    auto* price_row = new QWidget(this);
    auto* prl = new QHBoxLayout(price_row);
    prl->setContentsMargins(0, 0, 0, 0);
    prl->setSpacing(8);

    price_label_ = new QLabel("--");
    prl->addWidget(price_label_);

    auto* change_col = new QWidget(this);
    auto* ccl = new QVBoxLayout(change_col);
    ccl->setContentsMargins(0, 4, 0, 0);
    ccl->setSpacing(0);

    arrow_label_ = new QLabel;
    ccl->addWidget(arrow_label_);

    change_label_ = new QLabel("--");
    ccl->addWidget(change_label_);

    prl->addWidget(change_col);
    prl->addStretch();

    ticker_label_ = new QLabel(symbol_);
    prl->addWidget(ticker_label_);
    link_symbol(ticker_label_, symbol_); // double-click the ticker → Equity Research
    link_symbol(price_label_, symbol_);

    vl->addWidget(price_row);

    // ── Separator ──
    sep_ = new QFrame;
    sep_->setFixedHeight(1);
    vl->addWidget(sep_);

    // ── Stats grid ──
    auto* stats = new QWidget(this);
    auto* gl = new QGridLayout(stats);
    gl->setContentsMargins(0, 0, 0, 0);
    gl->setSpacing(6);

    auto make_stat = [&](int row, int col, const char* key, QLabel*& val_out) {
        auto* cell = new QWidget(this);
        stat_cells_.append(cell);
        auto* cl = new QVBoxLayout(cell);
        cl->setContentsMargins(8, 6, 8, 6);
        cl->setSpacing(2);

        auto* lbl = new QLabel(tr(key));
        stat_labels_.append(lbl);
        stat_label_keys_.append(QString::fromLatin1(key));
        cl->addWidget(lbl);

        val_out = new QLabel("--");
        stat_values_.append(val_out);
        cl->addWidget(val_out);

        gl->addWidget(cell, row, col);
    };

    make_stat(0, 0, QT_TR_NOOP("OPEN"), open_val_);
    make_stat(0, 1, QT_TR_NOOP("PREV CLOSE"), prev_val_);
    make_stat(1, 0, QT_TR_NOOP("HIGH"), high_val_);
    make_stat(1, 1, QT_TR_NOOP("LOW"), low_val_);
    make_stat(2, 0, QT_TR_NOOP("VOLUME"), volume_val_);

    vl->addWidget(stats);
    vl->addStretch();

    set_configurable(true);
    connect(this, &BaseWidget::refresh_requested, this, &StockQuoteWidget::refresh_data);

    apply_styles();
    set_loading(true);
}

QJsonObject StockQuoteWidget::config() const {
    QJsonObject o;
    o.insert("symbol", symbol_);
    return o;
}

void StockQuoteWidget::apply_config(const QJsonObject& cfg) {
    const QString next = normalize_symbol(cfg.value("symbol").toString(symbol_));
    if (next == symbol_)
        return;
    set_symbol(next);
}

QDialog* StockQuoteWidget::make_config_dialog(QWidget* parent) {
    auto* dlg = new QDialog(parent);
    dlg->setWindowTitle(tr("Configure — Stock Quote"));
    auto* form = new QFormLayout(dlg);

    auto* edit = new QLineEdit(dlg);
    edit->setText(symbol_);
    edit->setPlaceholderText(tr("e.g. AAPL"));
    edit->setAccessibleName(tr("Symbol"));
    form->addRow(tr("Symbol"), edit);

    auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dlg);
    form->addRow(buttons);
    dlg->setTabOrder(edit, buttons);

    connect(buttons, &QDialogButtonBox::accepted, dlg, [this, dlg, edit]() {
        QJsonObject cfg;
        cfg.insert("symbol", normalize_symbol(edit->text()));
        apply_config(cfg);
        emit config_changed(cfg);
        dlg->accept();
    });
    connect(buttons, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
    return dlg;
}

void StockQuoteWidget::apply_styles() {
    // Theme change: forget the cached data-driven sheets (set_css_cached).
    reset_css_cache(price_label_);
    reset_css_cache(arrow_label_);
    reset_css_cache(change_label_);
    price_label_->setStyleSheet(QString("color: %1; font-size: 28px; font-weight: bold; background: transparent;")
                                    .arg(ui::colors::TEXT_PRIMARY()));
    arrow_label_->setStyleSheet("font-size: 12px; background: transparent;");
    change_label_->setStyleSheet(QString("color: %1; font-size: 11px; font-weight: bold; background: transparent;")
                                     .arg(ui::colors::TEXT_SECONDARY()));
    ticker_label_->setStyleSheet(
        QString("color: %1; font-size: 14px; font-weight: bold; background: transparent;").arg(ui::colors::AMBER()));
    sep_->setStyleSheet(QString("background: %1;").arg(ui::colors::BORDER_DIM()));

    for (auto* cell : stat_cells_)
        cell->setStyleSheet(QString("background: %1; border-radius: 2px;").arg(ui::colors::BG_RAISED()));
    for (auto* lbl : stat_labels_)
        lbl->setStyleSheet(
            QString("color: %1; font-size: 9px; background: transparent;").arg(ui::colors::TEXT_TERTIARY()));
    for (auto* val : stat_values_)
        val->setStyleSheet(QString("color: %1; font-size: 11px; font-weight: bold; background: transparent;")
                               .arg(ui::colors::TEXT_PRIMARY()));
}

void StockQuoteWidget::on_theme_changed() {
    apply_styles();
}

void StockQuoteWidget::showEvent(QShowEvent* e) {
    BaseWidget::showEvent(e);
    if (!hub_active_)
        hub_resubscribe();
}

void StockQuoteWidget::hideEvent(QHideEvent* e) {
    BaseWidget::hideEvent(e);
    if (hub_active_)
        hub_unsubscribe_all();
}

void StockQuoteWidget::set_symbol(const QString& symbol) {
    symbol_ = normalize_symbol(symbol);
    set_title(tr("QUOTE: %1").arg(symbol_));
    ticker_label_->setText(symbol_);
    link_symbol(ticker_label_, symbol_);
    link_symbol(price_label_, symbol_);

    // Blank the previous symbol's numbers immediately — leaving AAPL's price
    // under an "MSFT" heading until the first delivery is actively misleading.
    price_label_->setText(QStringLiteral("--"));
    change_label_->setText(QStringLiteral("--"));
    arrow_label_->setText(QString());
    for (auto* v : stat_values_)
        v->setText(QStringLiteral("--"));

    // Re-subscribe to the new topic; old sub for previous symbol is
    // dropped wholesale by `unsubscribe(this)` inside hub_resubscribe().
    if (isVisible()) {
        set_loading(true);
        hub_resubscribe();
    }
}

void StockQuoteWidget::refresh_data() {
    // User-triggered refresh: force past the topic TTL / min_interval (the
    // producer-side rate limit still applies). Without `force` the title-bar
    // refresh button was a no-op whenever the cached quote was still fresh.
    datahub::DataHub::instance().request(QStringLiteral("market:quote:") + symbol_, /*force=*/true);
}

void StockQuoteWidget::hub_resubscribe() {
    auto& hub = datahub::DataHub::instance();
    hub.unsubscribe(this);
    const QString topic = QStringLiteral("market:quote:") + symbol_;
    hub.subscribe(this, topic, [this](const QVariant& v) {
        if (!v.canConvert<services::QuoteData>())
            return;
        set_loading(false);
        populate(v.value<services::QuoteData>());
    });
    hub_active_ = true;
}

void StockQuoteWidget::hub_unsubscribe_all() {
    datahub::DataHub::instance().unsubscribe(this);
    hub_active_ = false;
}

void StockQuoteWidget::populate(const services::QuoteData& q) {
    // No currency sign: the quote feed carries none, and "$" was wrong for
    // every non-USD listing, index level and FX rate this tile can show.
    price_label_->setText(dash_fmt::price(q.price, symbol_));

    const int sign = q.change_pct > 0 ? 1 : q.change_pct < 0 ? -1 : 0;
    const QString color = sign > 0 ? ui::colors::POSITIVE() : sign < 0 ? ui::colors::NEGATIVE() : ui::colors::TEXT_SECONDARY();

    arrow_label_->setText(sign > 0 ? QString(QChar(0x25B2)) : sign < 0 ? QString(QChar(0x25BC)) : QString(QChar(0x25C6)));
    change_label_->setText(QString("%1 (%2)").arg(dash_fmt::signed_change(q.change, q.price, symbol_),
                                                  dash_fmt::signed_pct(q.change_pct)));
    // Colours only change when the sign does — set_css_cached skips the CSS
    // re-parse on every other tick.
    set_css_cached(arrow_label_, QString("color: %1; font-size: 12px; background: transparent;").arg(color));
    set_css_cached(change_label_,
                   QString("color: %1; font-size: 11px; font-weight: bold; background: transparent;").arg(color));
    set_css_cached(price_label_,
                   QString("color: %1; font-size: 28px; font-weight: bold; background: transparent;").arg(color));

    // The batch quote snapshot carries last/change/high/low/volume but no
    // session open. Showing `high` here (as this did previously) prints a
    // wrong number under an "OPEN" heading — on a trading terminal that is
    // worse than showing nothing.
    open_val_->setText(QStringLiteral("--"));
    open_val_->setToolTip(tr("Session open is not available in the batch quote feed"));
    high_val_->setText(dash_fmt::price(q.high, symbol_));
    low_val_->setText(dash_fmt::price(q.low, symbol_));
    prev_val_->setText(q.price > 0 ? dash_fmt::price(q.price - q.change, symbol_) : QStringLiteral("--"));
    volume_val_->setText(dash_fmt::compact_volume(q.volume));
}

void StockQuoteWidget::retranslateUi() {
    BaseWidget::retranslateUi();
    set_title(tr("QUOTE: %1").arg(symbol_));
    // Re-run tr() on the kept stat-cell headings. (This previously called
    // refresh_data(), which fires a network request and never touched a
    // single label.)
    for (int i = 0; i < stat_labels_.size() && i < stat_label_keys_.size(); ++i)
        stat_labels_[i]->setText(tr(stat_label_keys_[i].toUtf8().constData()));
}

} // namespace fincept::screens::widgets