diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-09 19:30:24 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-09 19:30:24 +0200 |
commit | b5525d4ec3478118f9358837d3c0fae11ebbcd7b (patch) | |
tree | cfc71e76a1cf3f7ff685ad169e436342ccc7a70b | |
parent | be485946b8ad0bc08941035ae5fe653626e15364 (diff) | |
download | serenity-b5525d4ec3478118f9358837d3c0fae11ebbcd7b.zip |
GTableView: Add a mode for automatically sizing column to fit content
You can now call GTableView::set_size_columns_to_fit_content(true) and
the table columns will grow to fit the content. They will never shrink,
only grow.
This means I can spend a lot less time fidgeting with column widths :^)
-rw-r--r-- | Libraries/LibGUI/GTableView.cpp | 42 | ||||
-rw-r--r-- | Libraries/LibGUI/GTableView.h | 7 |
2 files changed, 47 insertions, 2 deletions
diff --git a/Libraries/LibGUI/GTableView.cpp b/Libraries/LibGUI/GTableView.cpp index 4ab8e655d4..102b2fec01 100644 --- a/Libraries/LibGUI/GTableView.cpp +++ b/Libraries/LibGUI/GTableView.cpp @@ -21,6 +21,39 @@ GTableView::~GTableView() { } +void GTableView::update_column_sizes() +{ + if (!m_size_columns_to_fit_content) + return; + + if (!model()) + return; + + auto& model = *this->model(); + int column_count = model.column_count(); + int row_count = model.row_count(); + + for (int column = 0; column < column_count; ++column) { + if (is_column_hidden(column)) + continue; + int header_width = header_font().width(model.column_name(column)); + int column_width = header_width; + for (int row = 0; row < row_count; ++row) { + auto cell_data = model.data(model.index(row, column)); + int cell_width = 0; + if (cell_data.is_bitmap()) { + cell_width = cell_data.as_bitmap().width(); + } else { + cell_width = font().width(cell_data.to_string()); + } + column_width = max(column_width, cell_width); + } + auto& column_data = this->column_data(column); + column_data.width = max(column_data.width, column_width); + column_data.has_initialized_width = true; + } +} + void GTableView::update_content_size() { if (!model()) @@ -41,6 +74,7 @@ void GTableView::update_content_size() void GTableView::did_update_model() { GAbstractView::did_update_model(); + update_column_sizes(); update_content_size(); update(); } @@ -71,6 +105,7 @@ int GTableView::column_width(int column_index) const return 0; auto& column_data = this->column_data(column_index); if (!column_data.has_initialized_width) { + ASSERT(!m_size_columns_to_fit_content); column_data.has_initialized_width = true; column_data.width = model()->column_metadata(column_index).preferred_width; } @@ -308,7 +343,7 @@ void GTableView::paint_headers(Painter& painter) text = model()->column_name(column_index); } auto text_rect = cell_rect.translated(horizontal_padding(), 0); - painter.draw_text(text_rect, text, Font::default_bold_font(), TextAlignment::CenterLeft, Color::Black); + painter.draw_text(text_rect, text, header_font(), TextAlignment::CenterLeft, Color::Black); x_offset += column_width + horizontal_padding() * 2; } } @@ -471,3 +506,8 @@ void GTableView::leave_event(CEvent&) { window()->set_override_cursor(GStandardCursor::None); } + +const Font& GTableView::header_font() +{ + return Font::default_bold_font(); +} diff --git a/Libraries/LibGUI/GTableView.h b/Libraries/LibGUI/GTableView.h index a5e8851cd6..1935bab13e 100644 --- a/Libraries/LibGUI/GTableView.h +++ b/Libraries/LibGUI/GTableView.h @@ -31,6 +31,9 @@ public: bool is_column_hidden(int) const; void set_column_hidden(int, bool); + void set_size_columns_to_fit_content(bool b) { m_size_columns_to_fit_content = b; } + bool size_columns_to_fit_content() const { return m_size_columns_to_fit_content; } + Point adjusted_position(const Point&) const; virtual Rect content_rect(const GModelIndex&) const override; @@ -56,6 +59,8 @@ private: Rect column_resize_grabbable_rect(int) const; int column_width(int) const; void update_content_size(); + void update_column_sizes(); + static const Font& header_font(); struct ColumnData { int width { 0 }; @@ -69,7 +74,7 @@ private: int m_horizontal_padding { 5 }; bool m_headers_visible { true }; bool m_alternating_row_colors { true }; - + bool m_size_columns_to_fit_content { false }; bool m_in_column_resize { false }; Point m_column_resize_origin; int m_column_resize_original_width { 0 }; |