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
#pragma once
#include "core/events/EventBus.h"
#include "core/symbol/IGroupLinked.h"
#include "screens/common/IStatefulScreen.h"
#include "services/markets/MarketDataService.h"
#include "storage/repositories/WatchlistRepository.h"
#include "ui/tables/DataTable.h"
#include <QHash>
#include <QHideEvent>
#include <QLabel>
#include <QLineEdit>
#include <QList>
#include <QListWidget>
#include <QPushButton>
#include <QShowEvent>
#include <QSplitter>
#include <QTimer>
#include <QWidget>
namespace fincept::screens {
/// Full-screen Watchlist — manage multiple watchlists with live quotes.
/// Layout: Watchlist Sidebar | Stock Table + Controls
class WatchlistScreen : public QWidget, public IStatefulScreen, public IGroupLinked {
Q_OBJECT
Q_INTERFACES(fincept::IGroupLinked)
public:
explicit WatchlistScreen(QWidget* parent = nullptr);
void restore_state(const QVariantMap& state) override;
QVariantMap save_state() const override;
QString state_key() const override { return "watchlist"; }
int state_version() const override { return 1; }
// IGroupLinked
void set_group(SymbolGroup g) override { link_group_ = g; }
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_watchlist_selected(int row);
void on_add_watchlist();
void on_delete_watchlist();
void on_add_stock();
void on_remove_stock();
void on_refresh();
void on_export_csv();
void on_import_csv();
void refresh_theme();
private:
void build_ui();
QWidget* build_sidebar();
QWidget* build_main_panel();
/// Re-apply tr() lookups to every widget whose text we keep a handle to.
/// Called from changeEvent() on QEvent::LanguageChange.
void retranslateUi();
void load_watchlists();
void load_stocks();
void fetch_quotes();
void populate_table(const QVector<services::QuoteData>& quotes);
void hub_resubscribe_stocks();
void hub_unsubscribe_all();
void rebuild_from_cache();
/// Rename the watchlist at `row` (WatchlistRepository::update existed but nothing called it).
void rename_watchlist(int row);
/// Right-click menus: sidebar list (rename / delete) and stock rows (open in …, backtest, remove).
void show_watchlist_menu(const QPoint& pos);
void show_stock_menu(const QPoint& pos);
/// Hand `symbol` to another screen through the app-wide `nav.open_symbol` event.
void open_symbol_in(const QString& screen_id, const QString& symbol);
/// Ticker of the table row at `row` (column 0 — sort-safe), or empty.
QString symbol_at_row(int row) const;
/// symbol → human-readable name (yfinance, disk-cached by MarketDataService). The quote
/// feed only carries the ticker as "name", so the NAME column used to repeat the symbol.
QHash<QString, QString> names_;
/// Coalesce a burst of per-symbol hub callbacks into ONE table rebuild.
void schedule_table_rebuild();
QTimer* rebuild_timer_ = nullptr;
// Data
QVector<fincept::Watchlist> watchlists_;
QVector<fincept::WatchlistStock> stocks_;
QString current_wl_id_;
// Sidebar
QWidget* sidebar_ = nullptr;
QWidget* sidebar_header_ = nullptr;
QLabel* sidebar_title_ = nullptr;
QListWidget* wl_list_ = nullptr;
QLabel* wl_count_ = nullptr;
QPushButton* add_wl_btn_ = nullptr;
// Main panel
QWidget* main_panel_ = nullptr;
QWidget* top_bar_ = nullptr;
QWidget* add_bar_ = nullptr;
QLabel* panel_title_ = nullptr;
QLabel* stock_count_ = nullptr;
QLabel* add_label_ = nullptr;
QLineEdit* add_input_ = nullptr;
QPushButton* refresh_btn_ = nullptr;
QPushButton* del_wl_btn_ = nullptr;
QPushButton* export_csv_btn_ = nullptr;
QPushButton* import_csv_btn_ = nullptr;
QPushButton* add_btn_ = nullptr;
QPushButton* remove_btn_ = nullptr;
ui::DataTable* table_ = nullptr;
QLabel* empty_label_ = nullptr; // guidance shown in place of a blank grid
QSplitter* splitter_ = nullptr;
/// Show/hide the empty-state guidance based on the current selection.
void update_empty_state();
QHash<QString, services::QuoteData> row_cache_;
bool hub_active_ = false;
// populate_table() updates rows in place while the symbol set is unchanged.
// The visible order must stay exactly what a full rebuild produced: Qt's
// stable sort starting from stocks_ order. These say whether the current row
// order still is that, so the rows are only re-seated + re-sorted when needed.
bool sort_order_dirty_ = true; // the sort indicator changed (header click / restore)
QStringList sorted_stock_order_; // stocks_ order the current row order derives from
// Symbol group link — SymbolGroup::None when unlinked.
SymbolGroup link_group_ = SymbolGroup::None;
// Emit the currently-selected symbol into the linked group, if any.
// Safe to call with no selection — no-op.
void publish_selection_to_group();
// EventBus subscriptions — registered in showEvent, released in hideEvent.
// MCP watchlist tools publish watchlist.created / .deleted / .updated when
// the LLM mutates watchlists via Finagent or AI Chat. Each handler
// reloads watchlists + the current stock list.
QList<EventBus::HandlerId> mcp_event_subs_;
void subscribe_mcp_events();
void unsubscribe_mcp_events();
};
} // namespace fincept::screens