mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:17:35 +00:00
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.
This commit is contained in:
parent
5707d7f547
commit
19fa70c821
16 changed files with 361 additions and 47 deletions
|
@ -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()
|
||||
|
|
|
@ -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&);
|
||||
|
||||
|
|
199
LibGUI/GItemView.cpp
Normal file
199
LibGUI/GItemView.cpp
Normal file
|
@ -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);
|
||||
}
|
44
LibGUI/GItemView.h
Normal file
44
LibGUI/GItemView.h
Normal file
|
@ -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 };
|
||||
};
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -39,14 +39,21 @@ void GScrollableWidget::resize_event(GResizeEvent& event)
|
|||
}
|
||||
}
|
||||
|
||||
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();
|
||||
return { available_width, available_height };
|
||||
}
|
||||
|
||||
void GScrollableWidget::update_scrollbar_ranges()
|
||||
{
|
||||
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);
|
||||
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());
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -49,6 +49,7 @@ LIBGUI_OBJS = \
|
|||
GDesktop.o \
|
||||
GProgressBar.o \
|
||||
GAbstractView.o \
|
||||
GItemView.o \
|
||||
GWindow.o
|
||||
|
||||
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue