diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-23 03:53:51 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-23 03:54:45 +0100 |
commit | 19fa70c8211739949c95404d9a87230656a9227f (patch) | |
tree | e11444f26703d7e67653728bb99713e915ed9481 /LibGUI | |
parent | 5707d7f547026544f4d9c7a320d97ccd6e055784 (diff) | |
download | serenity-19fa70c8211739949c95404d9a87230656a9227f.zip |
LibGUI: Add a GItemView class.
This is a GAbstractView subclass that implements a icon-based view onto
a GModel. It still need a bunch of work, but it's in basic usable shape.
Diffstat (limited to 'LibGUI')
-rw-r--r-- | LibGUI/GAbstractView.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GAbstractView.h | 5 | ||||
-rw-r--r-- | LibGUI/GItemView.cpp | 199 | ||||
-rw-r--r-- | LibGUI/GItemView.h | 44 | ||||
-rw-r--r-- | LibGUI/GModel.h | 2 | ||||
-rw-r--r-- | LibGUI/GScrollableWidget.cpp | 15 | ||||
-rw-r--r-- | LibGUI/GScrollableWidget.h | 2 | ||||
-rw-r--r-- | LibGUI/GStackWidget.cpp | 2 | ||||
-rw-r--r-- | LibGUI/GTableView.cpp | 2 | ||||
-rw-r--r-- | LibGUI/GTableView.h | 2 | ||||
-rw-r--r-- | LibGUI/Makefile | 1 |
11 files changed, 270 insertions, 8 deletions
diff --git a/LibGUI/GAbstractView.cpp b/LibGUI/GAbstractView.cpp index e4fa01d897..34c73b4ab6 100644 --- a/LibGUI/GAbstractView.cpp +++ b/LibGUI/GAbstractView.cpp @@ -25,8 +25,10 @@ void GAbstractView::set_model(RetainPtr<GModel>&& model) did_update_model(); } -void GAbstractView::model_notification(const GModelNotification&) +void GAbstractView::model_notification(const GModelNotification& notification) { + if (on_model_notification) + on_model_notification(notification); } void GAbstractView::did_update_model() diff --git a/LibGUI/GAbstractView.h b/LibGUI/GAbstractView.h index 69a45aabad..99fa48e9cd 100644 --- a/LibGUI/GAbstractView.h +++ b/LibGUI/GAbstractView.h @@ -2,6 +2,7 @@ #include <LibGUI/GModel.h> #include <LibGUI/GScrollableWidget.h> +#include <AK/Function.h> class GAbstractView : public GScrollableWidget { friend class GModel; @@ -18,6 +19,10 @@ public: virtual bool accepts_focus() const override { return true; } virtual void did_update_model(); + Function<void(const GModelNotification&)> on_model_notification; + + virtual const char* class_name() const override { return "GAbstractView"; } + protected: virtual void model_notification(const GModelNotification&); diff --git a/LibGUI/GItemView.cpp b/LibGUI/GItemView.cpp new file mode 100644 index 0000000000..4acab7073a --- /dev/null +++ b/LibGUI/GItemView.cpp @@ -0,0 +1,199 @@ +#include <LibGUI/GItemView.h> +#include <LibGUI/GModel.h> +#include <LibGUI/GScrollBar.h> +#include <SharedGraphics/Painter.h> +#include <Kernel/KeyCode.h> + +GItemView::GItemView(GWidget* parent) + : GAbstractView(parent) +{ +} + +GItemView::~GItemView() +{ +} + +void GItemView::scroll_into_view(const GModelIndex& index, Orientation orientation) +{ + GScrollableWidget::scroll_into_view(item_rect(index.row()), orientation); +} + +void GItemView::resize_event(GResizeEvent& event) +{ + GAbstractView::resize_event(event); + update_content_size(); +} + +void GItemView::did_update_model() +{ + GAbstractView::did_update_model(); + update_content_size(); + update(); +} + +void GItemView::update_content_size() +{ + if (!model()) + return set_content_size({ }); + + m_visual_column_count = available_size().width() / effective_item_size().width(); + if (m_visual_column_count) + m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count); + else + m_visual_row_count = 0; + + int content_width = available_size().width(); + int content_height = m_visual_row_count * effective_item_size().height(); + + set_content_size({ content_width, content_height }); +} + +Rect GItemView::item_rect(int item_index) const +{ + if (!m_visual_row_count || !m_visual_column_count) + return { }; + int visual_row_index = item_index / m_visual_column_count; + int visual_column_index = item_index % m_visual_column_count; + return { + visual_column_index * effective_item_size().width(), + visual_row_index * effective_item_size().height(), + effective_item_size().width(), + effective_item_size().height() + }; +} + +void GItemView::mousedown_event(GMouseEvent& event) +{ + if (event.button() == GMouseButton::Left) { + // FIXME: Since all items are the same size, just compute the clicked item index + // instead of iterating over everything. + auto adjusted_position = event.position().translated(0, vertical_scrollbar().value()); + for (int i = 0; i < item_count(); ++i) { + if (item_rect(i).contains(adjusted_position)) { + model()->set_selected_index({ i, 0 }); + update(); + return; + } + } + model()->set_selected_index({ }); + update(); + } +} + +void GItemView::paint_event(GPaintEvent& event) +{ + Painter painter(*this); + painter.set_clip_rect(event.rect()); + painter.fill_rect(event.rect(), Color::White); + painter.save(); + painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); + + auto column_metadata = model()->column_metadata(m_model_column); + const Font& font = column_metadata.font ? *column_metadata.font : this->font(); + + for (int item_index = 0; item_index < model()->row_count(); ++item_index) { + bool is_selected_item = item_index == model()->selected_index().row(); + Color background_color; + if (is_selected_item) { + background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060); + } else { + background_color = Color::White; + } + + Rect item_rect = this->item_rect(item_index); + GModelIndex model_index(item_index, m_model_column); + + auto icon = model()->data(model_index, GModel::Role::Icon); + auto item_text = model()->data(model_index, GModel::Role::Display); + + Rect icon_rect = { 0, 0, 32, 32 }; + icon_rect.center_within(item_rect); + icon_rect.move_by(0, -font.glyph_height() - 6); + + if (icon.is_bitmap()) { + painter.draw_scaled_bitmap(icon_rect, icon.as_bitmap(), icon.as_bitmap().rect()); + } + + Rect text_rect { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() }; + text_rect.center_horizontally_within(item_rect); + text_rect.inflate(4, 4); + + Color text_color; + if (is_selected_item) + text_color = Color::White; + else + text_color = model()->data(model_index, GModel::Role::ForegroundColor).to_color(Color::Black); + painter.fill_rect(text_rect, background_color); + painter.draw_text(text_rect, item_text.to_string(), font, TextAlignment::Center, text_color); + }; + + painter.restore(); + + if (is_focused()) + painter.draw_rect({ { }, available_size() }, Color::from_rgb(0x84351a)); +} + +int GItemView::item_count() const +{ + if (!model()) + return 0; + return model()->row_count(); +} + +void GItemView::keydown_event(GKeyEvent& event) +{ + if (!model()) + return; + auto& model = *this->model(); + if (event.key() == KeyCode::Key_Return) { + model.activate(model.selected_index()); + return; + } + if (event.key() == KeyCode::Key_Up) { + GModelIndex new_index; + if (model.selected_index().is_valid()) + new_index = { model.selected_index().row() - 1, model.selected_index().column() }; + else + new_index = { 0, 0 }; + if (model.is_valid(new_index)) { + model.set_selected_index(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } + return; + } + if (event.key() == KeyCode::Key_Down) { + GModelIndex new_index; + if (model.selected_index().is_valid()) + new_index = { model.selected_index().row() + 1, model.selected_index().column() }; + else + new_index = { 0, 0 }; + if (model.is_valid(new_index)) { + model.set_selected_index(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } + return; + } + if (event.key() == KeyCode::Key_PageUp) { + int items_per_page = visible_content_rect().height() / effective_item_size().height(); + GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column()); + if (model.is_valid(new_index)) { + model.set_selected_index(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } + return; + } + if (event.key() == KeyCode::Key_PageDown) { + int items_per_page = visible_content_rect().height() / effective_item_size().height(); + GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column()); + if (model.is_valid(new_index)) { + model.set_selected_index(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } + return; + } + return GWidget::keydown_event(event); +} diff --git a/LibGUI/GItemView.h b/LibGUI/GItemView.h new file mode 100644 index 0000000000..f6a52a84ae --- /dev/null +++ b/LibGUI/GItemView.h @@ -0,0 +1,44 @@ +#pragma once + +#include <LibGUI/GModel.h> +#include <LibGUI/GAbstractView.h> +#include <AK/Function.h> +#include <AK/HashMap.h> + +class GScrollBar; +class Painter; + +class GItemView : public GAbstractView { +public: + explicit GItemView(GWidget* parent); + virtual ~GItemView() override; + + int content_width() const; + int horizontal_padding() const { return m_horizontal_padding; } + + void scroll_into_view(const GModelIndex&, Orientation); + Size effective_item_size() const { return m_effective_item_size; } + + int model_column() const { return m_model_column; } + void set_model_column(int column) { m_model_column = column; } + + virtual const char* class_name() const override { return "GItemView"; } + +private: + virtual void did_update_model() override; + virtual void paint_event(GPaintEvent&) override; + virtual void resize_event(GResizeEvent&) override; + virtual void mousedown_event(GMouseEvent&) override; + virtual void keydown_event(GKeyEvent&) override; + + int item_count() const; + Rect item_rect(int item_index) const; + void update_content_size(); + + int m_horizontal_padding { 5 }; + int m_model_column { 0 }; + int m_visual_column_count { 0 }; + int m_visual_row_count { 0 }; + + Size m_effective_item_size { 80, 80 }; +}; diff --git a/LibGUI/GModel.h b/LibGUI/GModel.h index c565ae8cce..a20af686f0 100644 --- a/LibGUI/GModel.h +++ b/LibGUI/GModel.h @@ -42,7 +42,7 @@ public: const Font* font { nullptr }; }; - enum class Role { Display, Sort, Custom, ForegroundColor, BackgroundColor }; + enum class Role { Display, Sort, Custom, ForegroundColor, BackgroundColor, Icon }; virtual ~GModel(); diff --git a/LibGUI/GScrollableWidget.cpp b/LibGUI/GScrollableWidget.cpp index 8a7be7536c..a47e99e1d6 100644 --- a/LibGUI/GScrollableWidget.cpp +++ b/LibGUI/GScrollableWidget.cpp @@ -39,14 +39,21 @@ void GScrollableWidget::resize_event(GResizeEvent& event) } } -void GScrollableWidget::update_scrollbar_ranges() +Size GScrollableWidget::available_size() const { + int available_width = width() - m_size_occupied_by_fixed_elements.width() - width_occupied_by_vertical_scrollbar(); int available_height = height() - m_size_occupied_by_fixed_elements.height() - height_occupied_by_horizontal_scrollbar(); - int excess_height = max(0, m_content_size.height() - available_height); + return { available_width, available_height }; +} + +void GScrollableWidget::update_scrollbar_ranges() +{ + auto available_size = this->available_size(); + + int excess_height = max(0, m_content_size.height() - available_size.height()); m_vertical_scrollbar->set_range(0, excess_height); - int available_width = width() - m_size_occupied_by_fixed_elements.width() - width_occupied_by_vertical_scrollbar(); - int excess_width = max(0, m_content_size.width() - available_width); + int excess_width = max(0, m_content_size.width() - available_size.width()); m_horizontal_scrollbar->set_range(0, excess_width); m_vertical_scrollbar->set_big_step(visible_content_rect().height() - m_vertical_scrollbar->step()); diff --git a/LibGUI/GScrollableWidget.h b/LibGUI/GScrollableWidget.h index 43717565cd..30f430bb14 100644 --- a/LibGUI/GScrollableWidget.h +++ b/LibGUI/GScrollableWidget.h @@ -20,6 +20,8 @@ public: void set_scrollbars_enabled(bool); bool is_scrollbars_enabled() const { return m_scrollbars_enabled; } + Size available_size() const; + GScrollBar& vertical_scrollbar() { return *m_vertical_scrollbar; } const GScrollBar& vertical_scrollbar() const { return *m_vertical_scrollbar; } GScrollBar& horizontal_scrollbar() { return *m_horizontal_scrollbar; } diff --git a/LibGUI/GStackWidget.cpp b/LibGUI/GStackWidget.cpp index ba225a7547..dc6bd02f41 100644 --- a/LibGUI/GStackWidget.cpp +++ b/LibGUI/GStackWidget.cpp @@ -39,7 +39,7 @@ void GStackWidget::child_event(GChildEvent& event) if (event.type() == GEvent::ChildAdded) { if (!m_active_widget) set_active_widget(&child); - else + else if (m_active_widget != &child) child.set_visible(false); } else if (event.type() == GEvent::ChildRemoved) { if (m_active_widget == &child) { diff --git a/LibGUI/GTableView.cpp b/LibGUI/GTableView.cpp index f727fa647b..e753002ab7 100644 --- a/LibGUI/GTableView.cpp +++ b/LibGUI/GTableView.cpp @@ -30,9 +30,9 @@ void GTableView::update_content_size() void GTableView::did_update_model() { + GAbstractView::did_update_model(); update_content_size(); update(); - GAbstractView::did_update_model(); } Rect GTableView::row_rect(int item_index) const diff --git a/LibGUI/GTableView.h b/LibGUI/GTableView.h index 5382a219a6..bbd021fbeb 100644 --- a/LibGUI/GTableView.h +++ b/LibGUI/GTableView.h @@ -30,6 +30,8 @@ public: bool is_column_hidden(int) const; void set_column_hidden(int, bool); + virtual const char* class_name() const override { return "GTableView"; } + private: virtual void did_update_model() override; virtual void paint_event(GPaintEvent&) override; diff --git a/LibGUI/Makefile b/LibGUI/Makefile index d7bab746b6..1cd0beaf53 100644 --- a/LibGUI/Makefile +++ b/LibGUI/Makefile @@ -49,6 +49,7 @@ LIBGUI_OBJS = \ GDesktop.o \ GProgressBar.o \ GAbstractView.o \ + GItemView.o \ GWindow.o OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS) |