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.

2.6 KB
Raw Blame
#pragma once
#include "screens/dashboard/widgets/BaseWidget.h"
#include "services/markets/MarketDataService.h"

#include <QComboBox>
#include <QHash>
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>

namespace fincept::screens::widgets {

/// Stock Screener Widget — screens a broad basket by volume, change%, or price.
/// Fetches live data via yfinance batch_quotes and sorts/filters client-side.
///
/// Subscribes to `market:quote:<sym>` on the DataHub for the fixed
/// screener basket. Each delivery updates `row_cache_`;
/// `rebuild_all_quotes()` rebuilds `all_quotes_` and re-applies the current
/// filter so the sorted/top-20 view stays live.
class ScreenerWidget : public BaseWidget {
    Q_OBJECT
  public:
    explicit ScreenerWidget(QWidget* parent = nullptr);

  protected:
    void on_theme_changed() override;
    void showEvent(QShowEvent* e) override;
    void hideEvent(QHideEvent* e) override;
    void retranslateUi() override;

  private:
    void apply_styles();
    void refresh_data();
    void apply_filter();
    void render_rows(const QVector<services::QuoteData>& sorted);

    void hub_subscribe_all();
    void hub_unsubscribe_all();
    /// Recompute `all_quotes_` from `row_cache_` then `apply_filter()`.
    void rebuild_all_quotes();

    QComboBox* filter_combo_ = nullptr;
    QVBoxLayout* list_layout_ = nullptr;
    /// Owner of `list_layout_`. Carries the ONE stylesheet that styles every
    /// data row via object-name selectors (P7) — rows themselves never call
    /// setStyleSheet.
    QWidget* list_widget_ = nullptr;
    QVector<services::QuoteData> all_quotes_;

    /// Rank-indexed row widgets, created on first use and refilled in place on
    /// every render (rows past the current count are hidden). Rendering used to
    /// delete and re-create ~100 widgets per delivery burst.
    struct ScreenerRow {
        QWidget* row = nullptr;
        QLabel* sym = nullptr;
        QLabel* price = nullptr;
        QLabel* chg = nullptr;
        QLabel* vol = nullptr;
    };
    QVector<ScreenerRow> rows_;
    ScreenerRow make_row(int rank);

    // Widgets needing theme-aware restyling
    QWidget* filter_bar_ = nullptr;
    QLabel* filter_lbl_ = nullptr;
    QLabel* count_lbl_ = nullptr;
    QWidget* header_ = nullptr;
    QVector<QLabel*> header_labels_;
    QScrollArea* scroll_ = nullptr;
    /// Placeholder shown when the screener has no data yet / all rows filtered.
    QLabel* empty_lbl_ = nullptr;

    QHash<QString, services::QuoteData> row_cache_;
    bool hub_active_ = false;
};

} // namespace fincept::screens::widgets