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 17, 2022
1 parent ef5c6de commit 0d42996
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/ct/ct_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,15 @@ void CtTable::row_add(const size_t afterRowIdx, const std::vector<Glib::ustring>

void CtTable::row_delete(const size_t rowIdx)
{
if (1 == _tableMatrix.size() or rowIdx >= _tableMatrix.size()) {
if (1 == get_num_rows() or rowIdx >= get_num_rows()) {
return;
}
_grid.remove_row(rowIdx);
for (void* pTextCell : _tableMatrix.at(rowIdx)) {
delete static_cast<CtTextCell*>(pTextCell);
}
_tableMatrix.erase(_tableMatrix.begin()+rowIdx);
if (_currentRow == _tableMatrix.size()) {
if (_currentRow == get_num_rows()) {
--_currentRow;
}
static_cast<CtTextCell*>(_tableMatrix.at(_currentRow).at(_currentColumn))->get_text_view().grab_focus();
Expand Down
25 changes: 16 additions & 9 deletions src/ct/ct_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ class CtTableCommon : public CtAnchoredWidget
virtual size_t get_num_columns() const = 0;
size_t current_row() const { return _currentRow < get_num_rows() ? _currentRow : 0; }
size_t current_column() const { return _currentColumn < get_num_columns() ? _currentColumn : 0; }
bool row_sort_asc() { return _row_sort(true/*sortAsc*/); }
bool row_sort_desc() { return _row_sort(false/*sortAsc*/); }

virtual void row_add(const size_t afterRowIdx, const std::vector<Glib::ustring>* pNewRow = nullptr) = 0;
virtual void row_delete(const size_t rowIdx) = 0;
virtual void row_move_up(const size_t rowIdx) = 0;
virtual void row_move_down(const size_t rowIdx) = 0;

protected:
virtual void _populate_xml_rows_cells(xmlpp::Element* p_table_node) const = 0;
virtual bool _row_sort(const bool sortAsc) = 0;

int _colWidthDefault;
CtTableColWidths _colWidths;
Expand Down Expand Up @@ -110,10 +116,15 @@ class CtTableLight : public CtTableCommon
size_t get_num_columns() const override { return _pColumns->columnsText.size(); }

void row_add(const size_t afterRowIdx, const std::vector<Glib::ustring>* pNewRow = nullptr) override;
void row_delete(const size_t rowIdx) override;
void row_move_up(const size_t rowIdx) override;
void row_move_down(const size_t rowIdx) override;

protected:
static void _free_matrix(CtTableMatrix& tableMatrix);

void _populate_xml_rows_cells(xmlpp::Element* p_table_node) const override;
bool _row_sort(const bool sortAsc) override;

std::unique_ptr<CtTableLightColumns> _pColumns;
Gtk::TreeView* _pManagedTreeView;
Expand Down Expand Up @@ -166,25 +177,21 @@ class CtTable : public CtTableCommon
void column_move_left(const size_t colIdx);
void column_move_right(const size_t colIdx);
void row_add(const size_t afterRowIdx, const std::vector<Glib::ustring>* pNewRow = nullptr) override;
void row_delete(const size_t rowIdx);
void row_move_up(const size_t rowIdx);
void row_move_down(const size_t rowIdx);
bool row_sort_asc() { return _row_sort(true/*sortAsc*/); }
bool row_sort_desc() { return _row_sort(false/*sortAsc*/); }
void row_delete(const size_t rowIdx) override;
void row_move_up(const size_t rowIdx) override;
void row_move_down(const size_t rowIdx) override;

void set_col_width_default(const int colWidthDefault);
void set_col_width(const int colWidth, std::optional<size_t> optColIdx = std::nullopt);

private:
protected:
void _apply_styles_to_cells(const bool forceReApply);
void _new_text_cell_attach(const size_t rowIdx, const size_t colIdx, CtTextCell* pTextCell);
bool _row_sort(const bool sortAsc);
void _apply_remove_header_style(const bool isApply, CtTextView& textView);

protected:
bool _row_sort(const bool sortAsc) override;
void _populate_xml_rows_cells(xmlpp::Element* p_table_node) const override;

private:
void _on_populate_popup_cell(Gtk::Menu* menu);
bool _on_key_press_event_cell(GdkEventKey* event);
bool _on_button_press_event_grid(GdkEventButton* event);
Expand Down
93 changes: 93 additions & 0 deletions src/ct/ct_table_light.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,96 @@ void CtTableLight::row_add(const size_t afterRowIdx, const std::vector<Glib::ust
}
}
}

void CtTableLight::row_delete(const size_t rowIdx)
{
if (1 == get_num_rows() or rowIdx >= get_num_rows()) {
return;
}
#if 0
_grid.remove_row(rowIdx);
for (void* pTextCell : _tableMatrix.at(rowIdx)) {
delete static_cast<CtTextCell*>(pTextCell);
}
_tableMatrix.erase(_tableMatrix.begin()+rowIdx);
if (_currentRow == get_num_rows()) {
--_currentRow;
}
static_cast<CtTextCell*>(_tableMatrix.at(_currentRow).at(_currentColumn))->get_text_view().grab_focus();
#endif
}

void CtTableLight::row_move_up(const size_t rowIdx)
{
if (0 == rowIdx) {
return;
}
#if 0
const size_t rowIdxUp = rowIdx - 1;
_grid.remove_row(rowIdxUp);
_grid.insert_row(rowIdx);
std::swap(_tableMatrix[rowIdxUp], _tableMatrix[rowIdx]);
for (size_t colIdx = 0; colIdx < _tableMatrix.front().size(); ++colIdx) {
CtTextView& textView = static_cast<CtTextCell*>(_tableMatrix.at(rowIdx).at(colIdx))->get_text_view();
_grid.attach(textView, colIdx, rowIdx, 1/*# cell horiz*/, 1/*# cell vert*/);
if (0 == rowIdxUp) {
// we swapped header
CtTextView& textViewUp = static_cast<CtTextCell*>(_tableMatrix.at(rowIdxUp).at(colIdx))->get_text_view();
_apply_remove_header_style(true/*isApply*/, textViewUp);
_apply_remove_header_style(false/*isApply*/, textView);
}
}
_currentRow = rowIdxUp;
#endif
}

void CtTableLight::row_move_down(const size_t rowIdx)
{
#if 0
if (rowIdx == _tableMatrix.size()-1) {
return;
}
const size_t rowIdxDown = rowIdx + 1;
row_move_up(rowIdxDown);
_currentRow = rowIdxDown;
static_cast<CtTextCell*>(_tableMatrix.at(_currentRow).at(_currentColumn))->get_text_view().grab_focus();
#endif
}

bool CtTableLight::_row_sort(const bool sortAsc)
{
#if 0
auto f_tableCompare = [sortAsc](const CtTableRow& l, const CtTableRow& r)->bool{
const size_t minCols = std::min(l.size(), r.size());
for (size_t i = 0; i < minCols; ++i) {
const int cmpResult = CtStrUtil::natural_compare(static_cast<CtTextCell*>(l.at(i))->get_text_content(),
static_cast<CtTextCell*>(r.at(i))->get_text_content());
if (0 != cmpResult) {
return sortAsc ? cmpResult < 0 : cmpResult > 0;
}
}
return sortAsc; // if we get here means that the rows are equal, so just use one rule and stick to it
};
auto pPrevState = std::static_pointer_cast<CtAnchoredWidgetState_Table>(get_state());
std::sort(_tableMatrix.begin()+1, _tableMatrix.end(), f_tableCompare);
auto pCurrState = std::static_pointer_cast<CtAnchoredWidgetState_Table>(get_state());
std::list<size_t> changed;
for (size_t rowIdx = 1; rowIdx < _tableMatrix.size(); ++rowIdx) {
if (pPrevState->rows.at(rowIdx) != pCurrState->rows.at(rowIdx)) {
changed.push_back(rowIdx);
_grid.remove_row(rowIdx);
_grid.insert_row(rowIdx);
}
}
if (changed.size()) {
for (auto rowIdx : changed) {
for (size_t colIdx = 0; colIdx < _tableMatrix.at(rowIdx).size(); ++colIdx) {
CtTextView& textView = static_cast<CtTextCell*>(_tableMatrix.at(rowIdx).at(colIdx))->get_text_view();
_grid.attach(textView, colIdx, rowIdx, 1/*# cell horiz*/, 1/*# cell vert*/);
}
}
return true;
}
#endif
return false;
}

0 comments on commit 0d42996

Please sign in to comment.