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.

5.5 KB
Raw Blame
#pragma once
#include "core/symbol/IGroupLinked.h"
#include "screens/common/IStatefulScreen.h"

#include <QComboBox>
#include <QEvent>
#include <QHeaderView>
#include <QJsonArray>
#include <QJsonObject>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QPushButton>
#include <QSplitter>
#include <QStackedWidget>
#include <QTableWidget>
#include <QTextEdit>
#include <QWidget>

#include <functional>

namespace fincept::screens {

/// Data source descriptor for AkShare scripts.
struct AkShareSource {
    QString id;
    QString name;
    QString script; // e.g. "akshare_stocks_realtime.py"
    QString icon;   // emoji
};

/// AkShare Data Explorer screen.
/// Provides access to 25 AkShare data sources with 1000+ endpoints.
/// Users select a source, pick an endpoint, optionally set parameters,
/// and view results in a table or raw JSON.
///
/// Symbol-group linked whenever a result carries a security-code column (see AsiaSymbolLinks.h):
/// selecting a row publishes its yfinance symbol, a linked A-share/HK symbol fills the SYMBOL
/// parameter and is highlighted when listed, and rows offer "Open in…".
class AkShareScreen : public QWidget, public IStatefulScreen, public fincept::IGroupLinked {
    Q_OBJECT
    Q_INTERFACES(fincept::IGroupLinked)
  public:
    explicit AkShareScreen(QWidget* parent = nullptr);

    // IStatefulScreen — remembers active source, last endpoint, search text,
    // and whether the user prefers table vs JSON view.
    QVariantMap save_state() const override;
    void restore_state(const QVariantMap& state) override;
    QString state_key() const override { return "akshare"; }

    // IGroupLinked
    void set_group(fincept::SymbolGroup g) override { link_group_ = g; }
    fincept::SymbolGroup group() const override { return link_group_; }
    void on_group_symbol_changed(const fincept::SymbolRef& ref) override;
    fincept::SymbolRef current_symbol() const override;

  protected:
    void changeEvent(QEvent* event) override;

  private slots:
    void on_source_clicked(int index);
    void on_endpoint_clicked(QListWidgetItem* item);
    void on_search_changed(const QString& text);
    void on_execute();
    void on_view_toggle();
    void on_refresh();

  private:
    void setup_ui();
    QWidget* create_header();
    QWidget* create_source_grid();
    QWidget* create_endpoint_panel();
    QWidget* create_params_panel();
    QWidget* create_data_panel();
    QWidget* create_status_bar();

    /// Re-apply tr() lookups to every widget whose text we keep a handle to.
    /// Called from changeEvent() on QEvent::LanguageChange.
    void retranslateUi();

    void load_endpoints(const AkShareSource& source);
    void populate_endpoint_list(const QJsonObject& result);
    /// `force` skips (and evicts) the 2-minute result cache — used by REFRESH.
    void execute_query(const QString& script, const QString& endpoint, const QStringList& args, bool force = false);
    void display_table_data(const QJsonArray& data, const QStringList& columns = {});
    void display_json_data(const QJsonArray& data);
    void display_error(const QString& error);
    void set_loading(bool loading);
    void run_query(bool force = false);
    /// Select (without executing) the endpoint remembered by restore_state(), once the list exists.
    void apply_pending_endpoint();

    // Symbol linking (see class comment)
    QString yf_symbol_at_row(int row) const; // "" when the row has no recognisable security code
    QString name_at_row(int row) const;
    void publish_selection_to_group();
    void show_row_menu(const QPoint& pos);
    int symbol_col_ = -1; // security-code column of the current result, -1 if none
    int name_col_ = -1;
    fincept::SymbolGroup link_group_ = fincept::SymbolGroup::None;

    // Data sources
    QList<AkShareSource> sources_;
    int active_source_ = -1;
    QString active_endpoint_;

    // Endpoint cache: script -> { endpoints json }
    QHash<QString, QJsonObject> endpoint_cache_;

    // UI
    QList<QPushButton*> source_btns_;
    QListWidget* endpoint_list_ = nullptr;
    QLineEdit* search_input_ = nullptr;
    QLabel* endpoint_count_ = nullptr;

    // Header (cached for retranslateUi)
    QLabel* header_title_ = nullptr;
    QLabel* header_sub_ = nullptr;
    QLabel* header_badge_ = nullptr;
    QLabel* empty_state_ = nullptr;

    // Parameters
    QLineEdit* param_symbol_ = nullptr;
    QLineEdit* param_start_ = nullptr;
    QLineEdit* param_end_ = nullptr;
    QComboBox* param_period_ = nullptr;

    // Parameter labels (cached for retranslateUi)
    QLabel* sym_label_ = nullptr;
    QLabel* start_label_ = nullptr;
    QLabel* end_label_ = nullptr;
    QLabel* period_label_ = nullptr;

    // Data display
    QStackedWidget* view_stack_ = nullptr;
    QTableWidget* data_table_ = nullptr;
    QTextEdit* json_view_ = nullptr;
    QPushButton* view_toggle_btn_ = nullptr;
    QPushButton* exec_btn_ = nullptr;
    QPushButton* refresh_btn_ = nullptr;
    QLabel* data_status_ = nullptr;
    QLabel* record_count_ = nullptr;

    // Status bar
    QLabel* status_left_ = nullptr;
    QLabel* status_source_ = nullptr;
    QLabel* status_endpoint_ = nullptr;

    bool is_table_view_ = true;
    bool loading_ = false;

    // Bumped whenever the source or the query changes; an async callback that finds a different
    // value is stale (e.g. the endpoint list of source A arriving after the user moved on to B)
    // and must not touch the UI.
    int request_seq_ = 0;
    QString pending_endpoint_; // endpoint to re-select once the list is populated (restore_state)
};

} // namespace fincept::screens