summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-28 17:58:53 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-28 17:58:53 +0100
commit8eefdbdce8030ecd242664b17ed3bd67d8eeed7d (patch)
tree056d13ffb60bebecf7e28eaa51d71ef83a57a7a4 /LibGUI
parent2f266ef6498c5be114c61068ee5b83a3b9c03678 (diff)
downloadserenity-8eefdbdce8030ecd242664b17ed3bd67d8eeed7d.zip
LibGUI: Add horizontal scrollbar to GTableView.
Now we can scroll content in both directions if it won't fit in the view.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GTableView.cpp76
-rw-r--r--LibGUI/GTableView.h9
2 files changed, 57 insertions, 28 deletions
diff --git a/LibGUI/GTableView.cpp b/LibGUI/GTableView.cpp
index c01164e847..8c6042604a 100644
--- a/LibGUI/GTableView.cpp
+++ b/LibGUI/GTableView.cpp
@@ -7,10 +7,18 @@ GTableView::GTableView(GWidget* parent)
: GWidget(parent)
{
set_fill_with_background_color(false);
- m_scrollbar = new GScrollBar(Orientation::Vertical, this);
- m_scrollbar->set_step(4);
- m_scrollbar->set_big_step(30);
- m_scrollbar->on_change = [this] (int) {
+
+ m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
+ m_vertical_scrollbar->set_step(4);
+ m_vertical_scrollbar->set_big_step(30);
+ m_vertical_scrollbar->on_change = [this] (int) {
+ update();
+ };
+
+ m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
+ m_horizontal_scrollbar->set_step(4);
+ m_horizontal_scrollbar->set_big_step(30);
+ m_horizontal_scrollbar->on_change = [this] (int) {
update();
};
}
@@ -32,30 +40,45 @@ void GTableView::set_model(OwnPtr<GTableModel>&& model)
void GTableView::resize_event(GResizeEvent& event)
{
- m_scrollbar->set_relative_rect(event.size().width() - m_scrollbar->preferred_size().width(), 0, m_scrollbar->preferred_size().width(), event.size().height());
- update_scrollbar_range();
+ update_scrollbar_ranges();
+ m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
+ m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
}
-void GTableView::update_scrollbar_range()
+void GTableView::update_scrollbar_ranges()
{
int excess_height = max(0, (item_count() * item_height()) - height());
- m_scrollbar->set_range(0, excess_height);
+ m_vertical_scrollbar->set_range(0, excess_height);
+
+ int excess_width = max(0, content_width() - width());
+ m_horizontal_scrollbar->set_range(0, excess_width);
+}
+
+int GTableView::content_width() const
+{
+ if (!m_model)
+ return 0;
+ int width = 0;
+ int column_count = m_model->column_count();
+ for (int i = 0; i < column_count; ++i)
+ width += m_model->column_metadata(i).preferred_width + horizontal_padding() * 2;
+ return width;
}
void GTableView::did_update_model()
{
- update_scrollbar_range();
+ update_scrollbar_ranges();
update();
}
Rect GTableView::row_rect(int item_index) const
{
- return { 0, header_height() + (item_index * item_height()), width(), item_height() };
+ return { 0, header_height() + (item_index * item_height()), max(content_width(), width()), item_height() };
}
void GTableView::mousedown_event(GMouseEvent& event)
{
- auto adjusted_position = event.position().translated(0, m_scrollbar->value());
+ auto adjusted_position = event.position().translated(0, m_vertical_scrollbar->value());
if (event.button() == GMouseButton::Left) {
for (int i = 0; i < item_count(); ++i) {
if (row_rect(i).contains(adjusted_position)) {
@@ -72,12 +95,10 @@ void GTableView::mousedown_event(GMouseEvent& event)
void GTableView::paint_event(GPaintEvent&)
{
Painter painter(*this);
+ painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
- painter.translate(0, -m_scrollbar->value());
-
- int horizontal_padding = 5;
+ int exposed_width = max(content_width(), width());
int painted_item_index = 0;
-
int y_offset = header_height();
for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
@@ -98,32 +119,35 @@ void GTableView::paint_event(GPaintEvent&)
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
- Rect cell_rect(horizontal_padding + x_offset, y, column_width, item_height());
+ Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
painter.draw_text(cell_rect, m_model->data(row_index, column_index).to_string(), column_metadata.text_alignment, text_color);
- x_offset += column_width + horizontal_padding * 2;
+ x_offset += column_width + horizontal_padding() * 2;
}
++painted_item_index;
};
- Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), width(), height());
- unpainted_rect.intersect(rect());
+ Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
painter.fill_rect(unpainted_rect, Color::White);
// Untranslate the painter and paint the column headers.
- painter.translate(0, m_scrollbar->value());
- painter.fill_rect({ 0, 0, width(), header_height() }, Color::LightGray);
+ painter.translate(0, m_vertical_scrollbar->value());
+ painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
- Rect cell_rect(x_offset, 0, column_width + horizontal_padding * 2, item_height());
- painter.draw_text(cell_rect.translated(horizontal_padding, 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
- x_offset += column_width + horizontal_padding * 2;
+ Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, item_height());
+ painter.draw_text(cell_rect.translated(horizontal_padding(), 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
+ x_offset += column_width + horizontal_padding() * 2;
painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
}
- painter.draw_line({ 0, 0 }, { width() - 1, 0 }, Color::White);
- painter.draw_line({ 0, header_height() - 1 }, { width() - 1, header_height() - 1 }, Color::DarkGray);
+ painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
+ painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
+
+ // Then untranslate and fill in the scroll corner. This is pretty messy, tbh.
+ painter.translate(m_horizontal_scrollbar->value(), 0);
+ painter.fill_rect({ m_horizontal_scrollbar->relative_rect().top_right().translated(1, 0), { m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height() } }, Color::LightGray);
}
int GTableView::item_count() const
diff --git a/LibGUI/GTableView.h b/LibGUI/GTableView.h
index a134d5541c..c5aaab28fa 100644
--- a/LibGUI/GTableView.h
+++ b/LibGUI/GTableView.h
@@ -21,15 +21,20 @@ public:
void did_update_model();
+ int content_width() const;
+ int horizontal_padding() const { return m_horizontal_padding; }
+
private:
virtual void paint_event(GPaintEvent&) override;
virtual void resize_event(GResizeEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;
- void update_scrollbar_range();
+ void update_scrollbar_ranges();
int item_count() const;
Rect row_rect(int item_index) const;
- GScrollBar* m_scrollbar { nullptr };
+ GScrollBar* m_vertical_scrollbar { nullptr };
+ GScrollBar* m_horizontal_scrollbar { nullptr };
OwnPtr<GTableModel> m_model;
+ int m_horizontal_padding { 5 };
};