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.0 KB
Raw Blame
// src/screens/excel/ExcelScreen.h
// Full-featured Excel/Spreadsheet screen with multi-sheet tabs,
// formula bar, import/export (.xlsx), snapshots, and dark theme.
#pragma once

#include "screens/common/IStatefulScreen.h"

#include <QEvent>
#include <QHideEvent>
#include <QLabel>
#include <QPushButton>
#include <QShowEvent>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>

namespace fincept::screens {

class SpreadsheetWidget;
struct ExcelImportResult; // defined in ExcelScreen.cpp (worker-thread XLSX read result)

class ExcelScreen : public QWidget, public IStatefulScreen {
    Q_OBJECT
  public:
    explicit ExcelScreen(QWidget* parent = nullptr);

    /// Load a .xlsx/.csv from disk, replacing the workbook (asks before
    /// discarding unsaved edits). Returns false if nothing was opened.
    bool open_file_path(const QString& path);

    /// For callers that may run before the screen has been constructed (the
    /// File Manager "EXCEL" button navigates here lazily): opens the file now when
    /// a live ExcelScreen exists, otherwise parks the path so the next
    /// ExcelScreen to be shown picks it up.
    static void open_when_ready(const QString& path);

    void restore_state(const QVariantMap& state) override;
    QVariantMap save_state() const override;
    QString state_key() const override { return "excel"; }
    int state_version() const override { return 1; }

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

  private slots:
    void on_import();
    void on_export();
    /// Ctrl+S: write back to the open .xlsx without a dialog (Save As otherwise).
    void on_save();
    void on_add_sheet();
    void on_delete_sheet();
    void on_rename_sheet();
    void on_tab_changed(int index);
    void on_export_csv();

  private:
    void build_ui();
    QWidget* build_toolbar();
    void retranslateUi();
    void update_status();
    SpreadsheetWidget* current_sheet() const;
    QString generate_sheet_name() const;

    /// Load a .csv into a single new sheet, replacing the current workbook.
    bool import_csv(const QString& path);
    /// Route a path to the CSV or XLSX importer (no discard prompt — callers ask first).
    void import_path(const QString& path);
    /// XLSX is read on a worker thread (a large workbook is up to 1.3M cells of
    /// xlsx.read() calls); finish_xlsx_import() builds the sheets back on the UI thread.
    void import_xlsx(const QString& path);
    void finish_xlsx_import(const QString& path, const ExcelImportResult& result);
    /// Same for export: the workbook is written on a worker from a data snapshot.
    /// outcome: 0 = ok, 1 = could not build workbook, 2 = could not write file.
    void finish_xlsx_export(const QString& path, int outcome);
    /// Lock/unlock the toolbar and grid while a worker is reading/writing.
    void set_busy(bool busy, const QString& message = {});
    /// Track edits so we can warn before discarding them.
    void mark_dirty();
    /// Returns true if it is safe to proceed (nothing unsaved, or the user
    /// confirmed discarding). `action` names what is about to happen.
    bool confirm_discard(const QString& action);
    /// Attach a new sheet's data_changed to mark_dirty().
    void watch_sheet(SpreadsheetWidget* sheet);

    QTabWidget* sheet_tabs_ = nullptr;
    QLabel* status_label_ = nullptr;
    QString file_name_ = "Untitled.xlsx";
    QString file_path_;
    bool dirty_ = false;
    bool busy_ = false; // an XLSX read/write is running on a worker thread
    bool quick_save_ = false; // set by on_save(): reuse file_path_ when it is an .xlsx

    // Toolbar text widgets (cached for retranslateUi)
    QLabel* toolbar_title_ = nullptr;
    QPushButton* import_btn_ = nullptr;
    QPushButton* export_btn_ = nullptr;
    QPushButton* export_csv_btn_ = nullptr;
    QPushButton* add_sheet_btn_ = nullptr;
    QPushButton* rename_btn_ = nullptr;
    QPushButton* delete_btn_ = nullptr;
};

} // namespace fincept::screens