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.

4.9 KB
Raw Blame
#pragma once
#include "core/symbol/IGroupLinked.h"
#include "screens/common/IStatefulScreen.h"
#include "services/equity/EquityResearchModels.h"

#include <QHideEvent>
#include <QLabel>
#include <QPushButton>
#include <QShowEvent>
#include <QTabWidget>
#include <QTimer>
#include <QWidget>

namespace fincept::symbol_dnd {
class SymbolDragFilter;
}

namespace fincept::screens {

class EquityOverviewTab;
class EquityFinancialsTab;
class EquityFilingsTab;
class EquityAnalysisTab;
class EquityTechnicalsTab;
class EquityPeersTab;
class EquityNewsTab;
class EquitySentimentTab;
class EquityValuationTab;

class EquityResearchScreen : public QWidget, public IStatefulScreen, public IGroupLinked {
    Q_OBJECT
    Q_INTERFACES(fincept::IGroupLinked)
  public:
    explicit EquityResearchScreen(QWidget* parent = nullptr);

    void restore_state(const QVariantMap& state) override;
    QVariantMap save_state() const override;
    QString state_key() const override { return "equity_research"; }

    void set_group(SymbolGroup g) override;
    SymbolGroup group() const override { return link_group_; }
    void on_group_symbol_changed(const SymbolRef& ref) override;
    SymbolRef current_symbol() const override;

  protected:
    void showEvent(QShowEvent* event) override;
    void hideEvent(QHideEvent* event) override;
    void changeEvent(QEvent* event) override;

  private slots:
    void on_quote_loaded(services::equity::QuoteData quote);
    void on_info_loaded(services::equity::StockInfo info);
    void on_tab_changed(int index);
    // BUY/SELL from the title bar → routes the current symbol to the existing
    // Equity Trading order ticket (same form + paper/live engine) via EventBus.
    void on_trade_clicked(bool is_buy);
    // Opens the "Download Price Data (CSV)" dialog and exports yfinance OHLCV
    // history for current_symbol_ to a user-chosen file.
    void on_download_csv_clicked();
    void on_financials_loaded(services::equity::FinancialsData data);

  private:
    void build_ui();
    QWidget* build_title_bar();
    QWidget* build_quote_bar();
    void update_quote_bar(const services::equity::QuoteData& q);
    /// Blank every quote-bar field that belongs to the previous symbol (change, volume,
    /// H/L, market cap, recommendation) while the new symbol's data is in flight.
    void reset_quote_bar();
    /// `publish_to_group` = false when the symbol was translated from a linked panel's own
    /// form (see research_symbol_from_ref): echoing the yfinance form back would make e.g.
    /// Equity Trading select "RELIANCE.NS" as a broker symbol.
    void load_symbol(const QString& symbol, bool publish_to_group = true);
    /// Lazy tabs: only the tab on screen is handed the symbol (and fetches); the others
    /// load the first time they are shown. Each tab's set_symbol() is a no-op for the
    /// symbol it already shows.
    void activate_tab(QWidget* tab);
    /// "OPEN IN ▾": the active symbol to Equity Trading / News / Backtesting / a watchlist.
    void show_open_in_menu(QWidget* anchor);
    void retranslateUi();
    void hub_subscribe_broker_quote();
    void hub_unsubscribe_broker_quote();
    // Show the BUY/SELL buttons only when a usable broker (paper, or live + connected)
    // trades the market the current symbol is listed on.
    void update_trade_buttons();

    QLabel* title_label_ = nullptr;
    QLabel* symbol_label_ = nullptr;
    QPushButton* buy_btn_ = nullptr;
    QPushButton* sell_btn_ = nullptr;
    QPushButton* backtest_btn_ = nullptr;
    QPushButton* csv_btn_ = nullptr;
    QPushButton* watch_btn_ = nullptr;
    QPushButton* open_in_btn_ = nullptr;
    QLabel* hint_label_ = nullptr;
    symbol_dnd::SymbolDragFilter* drag_filter_ = nullptr;

    QLabel* sym_label_ = nullptr;
    QLabel* price_label_ = nullptr;
    QLabel* change_label_ = nullptr;
    QLabel* vol_label_ = nullptr;
    QLabel* hl_label_ = nullptr;
    QLabel* mktcap_label_ = nullptr;
    QLabel* rec_label_ = nullptr;

    QTabWidget* tab_widget_ = nullptr;
    EquityOverviewTab* overview_tab_ = nullptr;
    EquityFinancialsTab* financials_tab_ = nullptr;
    EquityFilingsTab* filings_tab_ = nullptr;
    EquityAnalysisTab* analysis_tab_ = nullptr;
    EquityTechnicalsTab* technicals_tab_ = nullptr;
    EquityPeersTab* peers_tab_ = nullptr;
    EquityNewsTab* news_tab_ = nullptr;
    EquitySentimentTab* sentiment_tab_ = nullptr;
    EquityValuationTab* valuation_tab_ = nullptr;

    QTimer* refresh_timer_ = nullptr;
    QString current_symbol_;
    QString current_currency_;
    double last_price_ = 0.0; // freshest quote price, seeds the order ticket
    // Freshest quote for the current symbol — re-rendered when the instrument's
    // currency becomes known (the quote usually lands before the info does).
    services::equity::QuoteData last_quote_;
    bool hub_broker_active_ = false;

    // Symbol group link — SymbolGroup::None when unlinked.
    SymbolGroup link_group_ = SymbolGroup::None;
};

} // namespace fincept::screens