Files
htab/engine/workbook.hpp
T

151 lines
4.7 KiB
C++

#pragma once
#include "engine/address.hpp"
#include "engine/parser.hpp"
#include "engine/value.hpp"
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
namespace htab {
class DependencyGraph;
struct Cell {
std::string raw_input;
std::string number_format;
Value value;
std::shared_ptr<Expr> ast;
bool parse_error = false;
};
struct Sheet {
std::string name;
std::unordered_map<CellKey, Cell, CellKeyHash> cells;
std::unordered_map<int, int> col_widths;
std::unordered_map<int, int> row_heights;
};
struct CellEdit {
int row = 0;
int col = 0;
std::string raw_input;
std::optional<std::string> number_format;
};
struct ClipboardCell {
int dr = 0;
int dc = 0;
std::string raw_input;
std::string number_format;
Value evaluated_value;
};
struct ClipboardGrid {
int src_sheet = 0;
CellRange src_range;
std::vector<ClipboardCell> cells;
bool is_cut = false;
};
class Workbook {
public:
struct Command {
virtual ~Command() = default;
virtual void undo(Workbook& wb) = 0;
virtual void redo(Workbook& wb) = 0;
};
Workbook();
~Workbook();
Workbook(Workbook&&) noexcept;
Workbook& operator=(Workbook&&) noexcept;
Workbook(const Workbook&) = delete;
Workbook& operator=(const Workbook&) = delete;
int sheet_count() const { return static_cast<int>(sheets_.size()); }
int active_sheet() const { return active_sheet_; }
void set_active_sheet(int idx);
const Sheet& sheet(int idx) const { return sheets_[static_cast<size_t>(idx)]; }
Sheet& sheet_mut(int idx) { return sheets_[static_cast<size_t>(idx)]; }
int find_sheet(std::string_view name) const;
int add_sheet(std::string name = "");
bool rename_sheet(int sheet_idx, const std::string& new_name);
bool delete_sheet(int sheet_idx);
void set_cell_input(int sheet_idx, int row, int col, const std::string& raw);
void apply_cell_edits(int sheet_idx, const std::vector<CellEdit>& edits);
void set_cell_format(int sheet_idx, int row, int col, const std::string& fmt);
void apply_range_format(int sheet_idx, CellRange range, const std::string& fmt);
void insert_rows(int sheet_idx, int row, int count = 1);
void delete_rows(int sheet_idx, int row, int count = 1);
void insert_cols(int sheet_idx, int col, int count = 1);
void delete_cols(int sheet_idx, int col, int count = 1);
void set_col_width(int sheet_idx, int col, int width);
int get_col_width(int sheet_idx, int col, int default_width) const;
void set_row_height(int sheet_idx, int row, int height);
int get_row_height(int sheet_idx, int row, int default_height) const;
bool can_undo() const { return !undo_stack_.empty(); }
bool can_redo() const { return !redo_stack_.empty(); }
bool undo();
bool redo();
void clear_history();
const Cell* get_cell(int sheet_idx, int row, int col) const;
Value get_scalar_value(int sheet_idx, int row, int col) const;
std::string get_formatted_value(int sheet_idx, int row, int col) const;
bool is_spilled_cell(int sheet_idx, int row, int col) const;
std::optional<CellRange> get_spill_range(int sheet_idx, int row, int col) const;
ClipboardGrid copy_range(int sheet_idx, CellRange range, bool is_cut = false) const;
void paste_range(int sheet_idx,
int dest_row,
int dest_col,
const ClipboardGrid& grid,
bool translate_refs = true);
void autofill_range(int sheet_idx, CellRange src, CellRange dst);
void replace_all_sheets(std::vector<Sheet> new_sheets, int active_sheet);
void replace_sheet_cells(int sheet_idx,
std::unordered_map<CellKey, Cell, CellKeyHash> new_cells);
std::optional<CellRange> get_used_bounds(int sheet_idx) const;
void recalculate_all();
const DependencyGraph& dag() const { return *dag_; }
private:
friend class EditCellsCmd;
friend class SnapshotCmd;
void parse_cell_input(Cell& cell, const std::string& raw);
void set_cell_raw_no_recalc(int sheet_idx,
int row,
int col,
const std::string& raw,
const std::optional<std::string>& fmt_override,
std::vector<GlobalAddr>& dirty_seeds);
void push_command(std::unique_ptr<Command> cmd);
void rewrite_all_formulas(const std::string& target_sheet,
StructuralOp op,
int index,
int count,
const std::string& new_name = "");
std::vector<Sheet> sheets_;
int active_sheet_ = 0;
std::unique_ptr<DependencyGraph> dag_;
std::vector<std::unique_ptr<Command>> undo_stack_;
std::vector<std::unique_ptr<Command>> redo_stack_;
};
} // namespace htab