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 "screens/common/IStatefulScreen.h"
#include "storage/repositories/DataMappingRepository.h"
#include <QComboBox>
#include <QEvent>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QStackedWidget>
#include <QTableWidget>
#include <QTextEdit>
#include <QTreeWidget>
#include <QWidget>
namespace fincept::screens {
/// Data Mapping configuration screen.
/// 5-step wizard: API Config → Schema Select → Field Mapping → Cache → Test & Save.
/// Supports predefined schemas (OHLCV, QUOTE, TICK, ORDER, POSITION, PORTFOLIO, INSTRUMENT),
/// custom schemas, multiple parser engines, and Indian broker templates.
class DataMappingScreen : public QWidget, public IStatefulScreen {
Q_OBJECT
public:
explicit DataMappingScreen(QWidget* parent = nullptr);
void restore_state(const QVariantMap& state) override;
QVariantMap save_state() const override;
QString state_key() const override { return "data_mapping"; }
int state_version() const override { return 1; }
private slots:
void on_view_changed(int view); // 0=list, 1=create, 2=templates
void on_step_changed(int step); // 0..4
void on_next_step();
void on_prev_step();
void on_test_api();
void on_test_mapping();
void on_save_mapping();
void on_run_mapping();
void on_edit_mapping();
void on_template_selected(int index);
void on_new_mapping();
void on_delete_mapping();
private:
void setup_ui();
// Top-level layout builders
QWidget* create_header();
QWidget* create_step_bar();
QWidget* create_main_area();
QWidget* create_nav_footer();
QWidget* create_status_bar();
// Step panels (center content)
QWidget* create_api_config_panel();
QWidget* create_schema_panel();
QWidget* create_field_mapping_panel();
QWidget* create_cache_panel();
QWidget* create_test_save_panel();
// Side panels
QWidget* create_left_panel();
QWidget* create_right_panel();
// View panels
QWidget* create_list_view();
QWidget* create_template_view();
// Helpers
QWidget* create_form_row(const QString& label, QWidget* input);
QWidget* create_form_two_col(QWidget* left, QWidget* right);
void update_step_indicators();
void update_right_panel();
void populate_json_tree(const QJsonValue& val, QTreeWidgetItem* parent, const QString& key);
void populate_mapping_list();
void refresh_saved_mappings();
void load_mappings_from_db();
/// The non-empty rows of the field-mapping table as
/// [{target, expression, transform, default_val}] — the shape
/// DataNormalizationService reads (and the one persisted).
QJsonArray collect_field_mappings() const;
/// Snapshot of every wizard field as a DataMapping (id = the mapping being
/// edited, or empty for a new one).
DataMapping build_mapping_from_form() const;
/// Back to a blank wizard: fields, mapping table, sample data, test state,
/// and the "editing" marker.
void reset_wizard_state();
/// Fill the wizard from a saved mapping so it can be changed and re-saved.
void load_mapping_into_wizard(const DataMapping& dm);
/// Put the JSONPath of a sample-response tree node into the Expression cell of
/// the selected (else first empty) field-mapping row.
void use_tree_item_as_expression(QTreeWidgetItem* item);
protected:
void showEvent(QShowEvent* e) override;
void changeEvent(QEvent* event) override;
private:
/// Re-apply tr() lookups to every widget whose text we keep a handle to.
void retranslateUi();
protected:
// State
int current_view_ = 0; // 0=list, 1=create, 2=templates
int current_step_ = 0; // 0..4
bool loading_ = false;
// Saved mappings (persisted via DataMappingRepository)
QVector<DataMapping> saved_mappings_;
/// Id of the saved mapping loaded into the wizard (SAVE overwrites it);
/// empty when composing a new mapping. editing_source_id_ carries its
/// data-source link through the edit unchanged.
QString editing_mapping_id_;
QString editing_source_id_;
/// A RUN is in flight (double-click on the list bypasses the disabled button).
bool run_in_flight_ = false;
// View & step buttons
QList<QPushButton*> view_btns_;
QList<QPushButton*> step_btns_;
// Stacked widgets
QStackedWidget* view_stack_ = nullptr; // list | create | templates
QStackedWidget* step_stack_ = nullptr; // 5 step panels
// Navigation footer
QPushButton* prev_btn_ = nullptr;
QPushButton* next_btn_ = nullptr;
QLabel* step_label_ = nullptr;
QWidget* nav_footer_ = nullptr;
// Status bar
QLabel* status_view_ = nullptr;
QLabel* status_step_ = nullptr;
QLabel* status_mappings_ = nullptr;
// ── Step 0: API Config ──
QLineEdit* api_name_ = nullptr;
QLineEdit* api_base_url_ = nullptr;
QLineEdit* api_endpoint_ = nullptr;
QComboBox* api_method_ = nullptr;
QComboBox* api_auth_type_ = nullptr;
QLineEdit* api_auth_value_ = nullptr;
QPlainTextEdit* api_headers_ = nullptr;
QPlainTextEdit* api_body_ = nullptr;
QPushButton* api_test_btn_ = nullptr;
QLabel* api_test_status_ = nullptr;
// ── Step 1: Schema ──
QComboBox* schema_select_ = nullptr; // OHLCV, QUOTE, etc.
QLabel* schema_desc_ = nullptr;
QTableWidget* schema_fields_table_ = nullptr;
// ── Step 2: Field Mapping ──
QTreeWidget* json_tree_ = nullptr; // JSON explorer
QTableWidget* mapping_table_ = nullptr; // target field → expression
QComboBox* parser_engine_ = nullptr;
// ── Step 3: Cache ──
QSpinBox* cache_ttl_ = nullptr;
QComboBox* cache_enabled_ = nullptr;
// ── Step 4: Test & Save ──
QPushButton* test_btn_ = nullptr;
QTextEdit* test_output_ = nullptr;
QLabel* test_status_ = nullptr;
QPushButton* save_btn_ = nullptr;
// ── List view ──
QListWidget* mapping_list_ = nullptr;
// ── Template view ──
QListWidget* template_list_ = nullptr;
QLabel* template_detail_ = nullptr;
// ── Right panel info ──
QLabel* right_engine_status_ = nullptr;
QLabel* right_schema_info_ = nullptr;
QLabel* right_fields_info_ = nullptr;
QLabel* right_test_info_ = nullptr;
// ── Static chrome cached for retranslateUi ──
QLabel* header_title_ = nullptr;
QLabel* header_sub_ = nullptr;
QLabel* header_badge_ = nullptr;
QLabel* left_panel_title_ = nullptr;
QList<QPushButton*> left_step_btns_;
QLabel* quick_stats_title_ = nullptr;
QLabel* schemas_count_lbl_ = nullptr;
QLabel* parsers_count_lbl_ = nullptr;
QLabel* right_panel_title_ = nullptr;
QLabel* mapping_engine_title_ = nullptr;
QLabel* parser_engines_title_ = nullptr;
QLabel* security_title_ = nullptr;
QLabel* current_mapping_title_ = nullptr;
QLabel* api_panel_title_ = nullptr;
QWidget* api_name_row_ = nullptr;
QWidget* api_base_url_row_ = nullptr;
QWidget* api_endpoint_row_ = nullptr;
QWidget* api_method_row_ = nullptr;
QWidget* api_auth_type_row_ = nullptr;
QWidget* api_auth_value_row_ = nullptr;
QWidget* api_headers_row_ = nullptr;
QWidget* api_body_row_ = nullptr;
QLabel* schema_panel_title_ = nullptr;
QWidget* schema_select_row_ = nullptr;
QLabel* field_panel_title_ = nullptr;
QLabel* parser_label_ = nullptr;
QLabel* cache_panel_title_ = nullptr;
QWidget* cache_enabled_row_ = nullptr;
QWidget* cache_ttl_row_ = nullptr;
QLabel* encryption_title_ = nullptr;
QLabel* encryption_detail_ = nullptr;
QLabel* test_save_panel_title_ = nullptr;
QLabel* list_title_ = nullptr;
QPushButton* list_run_btn_ = nullptr;
QPushButton* list_edit_btn_ = nullptr;
QPushButton* list_del_btn_ = nullptr;
QPushButton* list_new_btn_ = nullptr;
QLabel* list_empty_ = nullptr;
QPushButton* template_use_btn_ = nullptr;
QLabel* template_toolbar_title_ = nullptr;
QLabel* template_count_lbl_ = nullptr;
QLabel* status_version_ = nullptr;
// Sample data from API test
QJsonDocument sample_data_;
QJsonObject test_result_;
};
} // namespace fincept::screens