Skip to content

Commit

Permalink
implementing lightweight variant of tables (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Dec 10, 2022
1 parent 608802a commit a9ed5b4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 28 deletions.
68 changes: 49 additions & 19 deletions src/ct/ct_state_machine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,40 +163,51 @@ CtAnchoredWidget* CtAnchoredWidgetState_Codebox::to_widget(CtMainWin* pCtMainWin
}

// Table
bool CtAnchoredWidgetState_TableCommon::equal(std::shared_ptr<CtAnchoredWidgetState> state)
{
CtAnchoredWidgetState_TableCommon* other_state = dynamic_cast<CtAnchoredWidgetState_TableCommon*>(state.get());
return other_state and
charOffset == other_state->charOffset and
justification == other_state->justification and
colWidthDefault == other_state->colWidthDefault and
colWidths == other_state->colWidths and
currRow == other_state->currRow and
currCol == other_state->currCol and
rows == other_state->rows;
}

CtAnchoredWidgetState_Table::CtAnchoredWidgetState_Table(CtTable* table)
: CtAnchoredWidgetState{table->getOffset(), table->getJustification()}
, colWidthDefault{table->get_col_width_default()}
, colWidths{table->get_col_widths_raw()}
, currRow{table->current_row()}
, currCol{table->current_column()}
: CtAnchoredWidgetState_TableCommon{table}
{
for (const auto& row : table->get_table_matrix()) {
rows.push_back(std::vector<Glib::ustring>());
rows.push_back(std::vector<Glib::ustring>{});
for (void* cell : row) {
rows.back().push_back(static_cast<CtTextCell*>(cell)->get_text_content());
}
}
}

bool CtAnchoredWidgetState_Table::equal(std::shared_ptr<CtAnchoredWidgetState> state)
CtAnchoredWidgetState_TableLight::CtAnchoredWidgetState_TableLight(CtTableLight* table)
: CtAnchoredWidgetState_TableCommon{table}
{
CtAnchoredWidgetState_Table* other_state = dynamic_cast<CtAnchoredWidgetState_Table*>(state.get());
return other_state and
charOffset == other_state->charOffset and
justification == other_state->justification and
colWidthDefault == other_state->colWidthDefault and
colWidths == other_state->colWidths and
currRow == other_state->currRow and
currCol == other_state->currCol and
rows == other_state->rows;
auto f_action = [this, table](const Gtk::TreeIter& treeIter)->bool{
Gtk::TreeRow treeRow = *treeIter;
rows.push_back(std::vector<Glib::ustring>{});
const size_t numCols = table->get_num_columns();
const CtTableLightColumns& cols = table->get_columns();
for (size_t c = 0u; c < numCols; ++c) {
rows.back().push_back(treeRow[cols.columnsText.at(c)]);
}
return false; /* to continue */
};
table->exec_action_on_every_tree_iter(f_action);
}

CtAnchoredWidget* CtAnchoredWidgetState_Table::to_widget(CtMainWin* pCtMainWin)
{
CtTableMatrix tableMatrix;
for (const auto& row : rows)
{
tableMatrix.push_back(CtTableRow());
for (const auto& row : rows) {
tableMatrix.push_back(CtTableRow{});
for (const auto& cell : row) {
tableMatrix.back().push_back(new CtTextCell{pCtMainWin, cell, CtConst::TABLE_CELL_TEXT_ID});
}
Expand All @@ -211,6 +222,25 @@ CtAnchoredWidget* CtAnchoredWidgetState_Table::to_widget(CtMainWin* pCtMainWin)
currCol};
}

CtAnchoredWidget* CtAnchoredWidgetState_TableLight::to_widget(CtMainWin* pCtMainWin)
{
CtTableMatrix tableMatrix;
for (const auto& row : rows) {
tableMatrix.push_back(CtTableRow{});
for (const auto& cell : row) {
tableMatrix.back().push_back(new Glib::ustring{cell});
}
}
return new CtTableLight{pCtMainWin,
tableMatrix,
colWidthDefault,
charOffset,
justification,
colWidths,
currRow,
currCol};
}

CtStateMachine::CtStateMachine(CtMainWin *pCtMainWin)
: _pCtMainWin{pCtMainWin}
{
Expand Down
23 changes: 18 additions & 5 deletions src/ct/ct_state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,34 @@ class CtAnchoredWidgetState_Codebox : public CtAnchoredWidgetState
bool widthInPixels, brackets, showNum;
};

class CtAnchoredWidgetState_Table : public CtAnchoredWidgetState
struct CtAnchoredWidgetState_TableCommon : public CtAnchoredWidgetState
{
public:
CtAnchoredWidgetState_Table(CtTable* table);
CtAnchoredWidgetState_TableCommon(CtTableCommon* table)
: CtAnchoredWidgetState{table->getOffset(), table->getJustification()}
, colWidthDefault{table->get_col_width_default()}
, colWidths{table->get_col_widths_raw()}
, currRow{table->current_row()}
, currCol{table->current_column()}
{}

bool equal(std::shared_ptr<CtAnchoredWidgetState> state) override;
CtAnchoredWidget* to_widget(CtMainWin* pCtMainWin) override;

public:
int colWidthDefault;
CtTableColWidths colWidths;
std::vector<std::vector<Glib::ustring>> rows;
size_t currRow;
size_t currCol;
};
struct CtAnchoredWidgetState_TableLight : public CtAnchoredWidgetState_TableCommon
{
CtAnchoredWidgetState_TableLight(CtTableLight* table);
CtAnchoredWidget* to_widget(CtMainWin* pCtMainWin) override;
};
struct CtAnchoredWidgetState_Table : public CtAnchoredWidgetState_TableCommon
{
CtAnchoredWidgetState_Table(CtTable* table);
CtAnchoredWidget* to_widget(CtMainWin* pCtMainWin) override;
};

struct CtNodeState
{
Expand Down
24 changes: 20 additions & 4 deletions src/ct/ct_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class CtTableCommon : public CtAnchoredWidget

void apply_width_height(const int /*parentTextWidth*/) override {}

CtAnchWidgType get_type() override { return CtAnchWidgType::TableLight; }

const CtTableColWidths& get_col_widths_raw() const { return _colWidths; }
int get_col_width_default() const { return _colWidthDefault; }
int get_col_width(const std::optional<size_t> optColIdx = std::nullopt) const {
Expand All @@ -56,6 +54,9 @@ class CtTableCommon : public CtAnchoredWidget
return colWidths;
}

virtual size_t current_row() = 0;
virtual size_t current_column() = 0;

protected:
int _colWidthDefault;
CtTableColWidths _colWidths;
Expand Down Expand Up @@ -87,6 +88,21 @@ class CtTableLight : public CtTableCommon
const CtTableColWidths& colWidths,
const size_t currRow = 0,
const size_t currCol = 0);
size_t get_num_rows() const { return _numRows; }
size_t get_num_columns() const { return _numColumns; }
void exec_action_on_every_tree_iter(const Gtk::TreeModel::SlotForeachIter& slot) const;
const CtTableLightColumns& get_columns() const { return *_pColumns; }

void apply_syntax_highlighting(const bool forceReApply) override {}
void to_xml(xmlpp::Element* p_node_parent, const int offset_adjustment, CtStorageCache* cache) override {}
bool to_sqlite(sqlite3* pDb, const gint64 node_id, const int offset_adjustment, CtStorageCache* cache) override {}
void set_modified_false() override {}
CtAnchWidgType get_type() override { return CtAnchWidgType::TableLight; }
std::shared_ptr<CtAnchoredWidgetState> get_state() override;

size_t current_row() override { return _currentRow < _numRows ? _currentRow : 0; }
size_t current_column() override { return _currentColumn < _numColumns ? _currentColumn : 0; }

protected:
static void _free_matrix(CtTableMatrix& tableMatrix);
std::unique_ptr<CtTableLightColumns> _pColumns;
Expand Down Expand Up @@ -136,8 +152,8 @@ class CtTable : public CtTableCommon
const CtTableMatrix& get_table_matrix() const { return _tableMatrix; }

public:
size_t current_row() { return _currentRow < _tableMatrix.size() ? _currentRow : 0; }
size_t current_column() { return _currentColumn < _tableMatrix.front().size() ? _currentColumn : 0; }
size_t current_row() override { return _currentRow < _tableMatrix.size() ? _currentRow : 0; }
size_t current_column() override { return _currentColumn < _tableMatrix.front().size() ? _currentColumn : 0; }

void column_add(const size_t afterColIdx);
void column_delete(const size_t colIdx);
Expand Down
10 changes: 10 additions & 0 deletions src/ct/ct_table_light.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@ CtTableLight::CtTableLight(CtMainWin* pCtMainWin,
}
}
}

void CtTableLight::exec_action_on_every_tree_iter(const Gtk::TreeModel::SlotForeachIter& slot) const
{
_pListStore->foreach_iter(slot);
}

std::shared_ptr<CtAnchoredWidgetState> CtTableLight::get_state()
{
return std::shared_ptr<CtAnchoredWidgetState>(new CtAnchoredWidgetState_TableLight{this});
}

0 comments on commit a9ed5b4

Please sign in to comment.