diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-15 14:50:36 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-15 14:50:36 +0100 |
commit | a5d538b389829e1deb8f6857283104c6b4daf83f (patch) | |
tree | af75273f1c0ed3f08d61e5313523d5fa92c06545 | |
parent | a23dddc56fe753ae5c81a08da66df39aa955d381 (diff) | |
download | serenity-a5d538b389829e1deb8f6857283104c6b4daf83f.zip |
GTableView: Add a way to hide the column headers.
There are many situations where you would want a table view without headers.
-rw-r--r-- | LibGUI/GTableView.cpp | 40 | ||||
-rw-r--r-- | LibGUI/GTableView.h | 11 |
2 files changed, 34 insertions, 17 deletions
diff --git a/LibGUI/GTableView.cpp b/LibGUI/GTableView.cpp index f7918d4949..2934ab4910 100644 --- a/LibGUI/GTableView.cpp +++ b/LibGUI/GTableView.cpp @@ -19,6 +19,9 @@ GTableView::GTableView(GWidget* parent) m_horizontal_scrollbar->on_change = [this] (int) { update(); }; + + m_corner_widget = new GWidget(this); + m_corner_widget->set_fill_with_background_color(true); } GTableView::~GTableView() @@ -41,6 +44,7 @@ void GTableView::resize_event(GResizeEvent& event) 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()); + m_corner_widget->set_relative_rect(m_horizontal_scrollbar->rect().right() + 1, m_vertical_scrollbar->rect().bottom() + 1, m_horizontal_scrollbar->height(), m_vertical_scrollbar->width()); } void GTableView::update_scrollbar_ranges() @@ -135,6 +139,7 @@ void GTableView::paint_event(GPaintEvent& event) { Painter painter(*this); painter.set_clip_rect(event.rect()); + painter.save(); painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value()); int exposed_width = max(content_width(), width()); @@ -181,8 +186,27 @@ void GTableView::paint_event(GPaintEvent& event) 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. + // Untranslate the painter vertically and do the column headers. painter.translate(0, m_vertical_scrollbar->value()); + if (headers_visible()) + paint_headers(painter); + + painter.restore(); + + if (is_focused()) { + Rect item_area_rect { + 0, + header_height(), + width() - m_vertical_scrollbar->width(), + height() - header_height() - m_horizontal_scrollbar->height() + }; + painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a)); + }; +} + +void GTableView::paint_headers(Painter& painter) +{ + int exposed_width = max(content_width(), width()); painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray); 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); @@ -203,20 +227,6 @@ void GTableView::paint_event(GPaintEvent& event) } // Draw the "start" of a new column to make the last separator look right. painter.draw_line({ x_offset, 1 }, { x_offset, header_height() - 2 }, Color::White); - - // 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); - - if (is_focused()) { - Rect item_area_rect { - 0, - header_height(), - width() - m_vertical_scrollbar->width(), - height() - header_height() - m_horizontal_scrollbar->height() - }; - painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a)); - }; } int GTableView::item_count() const diff --git a/LibGUI/GTableView.h b/LibGUI/GTableView.h index 803d699d09..bfed3121a0 100644 --- a/LibGUI/GTableView.h +++ b/LibGUI/GTableView.h @@ -6,19 +6,23 @@ #include <AK/HashMap.h> class GScrollBar; +class Painter; class GTableView : public GWidget { public: explicit GTableView(GWidget* parent); virtual ~GTableView() override; - virtual int header_height() const { return 16; } - virtual int item_height() const { return 16; } + int header_height() const { return m_headers_visible ? 16 : 0; } + int item_height() const { return 16; } void set_model(OwnPtr<GTableModel>&&); GTableModel* model() { return m_model.ptr(); } const GTableModel* model() const { return m_model.ptr(); } + bool headers_visible() const { return m_headers_visible; } + void set_headers_visible(bool headers_visible) { m_headers_visible = headers_visible; } + void did_update_model(); int content_width() const; @@ -37,6 +41,7 @@ private: virtual void mousedown_event(GMouseEvent&) override; virtual void keydown_event(GKeyEvent&) override; + void paint_headers(Painter&); void update_scrollbar_ranges(); int item_count() const; Rect row_rect(int item_index) const; @@ -45,6 +50,8 @@ private: GScrollBar* m_vertical_scrollbar { nullptr }; GScrollBar* m_horizontal_scrollbar { nullptr }; + GWidget* m_corner_widget { nullptr }; OwnPtr<GTableModel> m_model; int m_horizontal_padding { 5 }; + bool m_headers_visible { true }; }; |