1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:27:43 +00:00

LibGUI: Take ProcessManager's process view and turn it into GTableView.

Make it sufficiently generic that it can be reused for any table data. :^)
This commit is contained in:
Andreas Kling 2019-02-28 10:57:09 +01:00
parent b3ae1163ef
commit dc9f8a9361
11 changed files with 173 additions and 88 deletions

33
LibGUI/GTableModel.cpp Normal file
View file

@ -0,0 +1,33 @@
#include <LibGUI/GTableModel.h>
#include <LibGUI/GTableView.h>
GTableModel::GTableModel()
{
}
GTableModel::~GTableModel()
{
}
void GTableModel::register_view(Badge<GTableView>, GTableView& view)
{
m_views.set(&view);
}
void GTableModel::unregister_view(Badge<GTableView>, GTableView& view)
{
m_views.remove(&view);
}
void GTableModel::for_each_view(Function<void(GTableView&)> callback)
{
for (auto* view : m_views)
callback(*view);
}
void GTableModel::did_update()
{
for_each_view([] (GTableView& view) {
view.did_update_model();
});
}

View file

@ -1,12 +1,16 @@
#pragma once
#include <AK/AKString.h>
#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/HashTable.h>
#include <LibGUI/GModelIndex.h>
class GTableView;
class GTableModel {
public:
GTableModel() { }
virtual ~GTableModel() { }
virtual ~GTableModel();
virtual int row_count() const = 0;
virtual int column_count() const = 0;
@ -22,4 +26,16 @@ public:
{
return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
}
void register_view(Badge<GTableView>, GTableView&);
void unregister_view(Badge<GTableView>, GTableView&);
protected:
GTableModel();
void for_each_view(Function<void(GTableView&)>);
void did_update();
private:
HashTable<GTableView*> m_views;
};

117
LibGUI/GTableView.cpp Normal file
View file

@ -0,0 +1,117 @@
#include <LibGUI/GTableView.h>
#include <LibGUI/GTableModel.h>
#include <LibGUI/GScrollBar.h>
#include <SharedGraphics/Painter.h>
GTableView::GTableView(GWidget* parent)
: GWidget(parent)
{
m_scrollbar = new GScrollBar(Orientation::Vertical, this);
m_scrollbar->set_step(4);
m_scrollbar->set_big_step(30);
m_scrollbar->on_change = [this] (int) {
update();
};
}
GTableView::~GTableView()
{
}
void GTableView::set_model(OwnPtr<GTableModel>&& model)
{
if (model.ptr() == m_model.ptr())
return;
if (m_model)
m_model->unregister_view(Badge<GTableView>(), *this);
m_model = move(model);
if (m_model)
m_model->register_view(Badge<GTableView>(), *this);
}
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());
}
void GTableView::did_update_model()
{
int excess_height = max(0, (item_count() * item_height()) - height());
m_scrollbar->set_range(0, excess_height);
update();
}
Rect GTableView::row_rect(int item_index) const
{
return { 0, header_height() + (item_index * item_height()), width(), item_height() };
}
void GTableView::mousedown_event(GMouseEvent& event)
{
auto adjusted_position = event.position().translated(0, m_scrollbar->value());
if (event.button() == GMouseButton::Left) {
for (int i = 0; i < item_count(); ++i) {
if (!row_rect(i).contains(adjusted_position))
continue;
m_model->set_selected_index({ i, 0 });
update();
}
}
}
void GTableView::paint_event(GPaintEvent&)
{
Painter painter(*this);
painter.translate(0, -m_scrollbar->value());
int horizontal_padding = 5;
int painted_item_index = 0;
int y_offset = header_height();
for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
int y = y_offset + painted_item_index * item_height();
Color background_color;
Color text_color;
if (row_index == m_model->selected_index().row()) {
background_color = Color::from_rgb(0x84351a);
text_color = Color::White;
} else {
background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
text_color = Color::Black;
}
painter.fill_rect(row_rect(painted_item_index), background_color);
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
Rect cell_rect(horizontal_padding + x_offset, y, m_model->column_width(column_index), item_height());
painter.draw_text(cell_rect, m_model->data(row_index, column_index), TextAlignment::CenterLeft, text_color);
x_offset += m_model->column_width(column_index) + horizontal_padding;
}
++painted_item_index;
};
Rect unpainted_rect(0, painted_item_index * item_height(), width(), height());
unpainted_rect.intersect(rect());
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);
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
Rect cell_rect(horizontal_padding + x_offset, 0, m_model->column_width(column_index), item_height());
painter.draw_text(cell_rect, m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
x_offset += m_model->column_width(column_index) + horizontal_padding;
}
painter.draw_line({ 0, 0 }, { width() - 1, 0 }, Color::White);
painter.draw_line({ 0, header_height() - 1 }, { width() - 1, header_height() - 1 }, Color::DarkGray);
}
int GTableView::item_count() const
{
return m_model->row_count();
}

34
LibGUI/GTableView.h Normal file
View file

@ -0,0 +1,34 @@
#pragma once
#include <LibGUI/GWidget.h>
#include <AK/Function.h>
#include <AK/HashMap.h>
class GScrollBar;
class GTableModel;
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; }
void set_model(OwnPtr<GTableModel>&&);
GTableModel* model() { return m_model.ptr(); }
const GTableModel* model() const { return m_model.ptr(); }
void did_update_model();
private:
virtual void paint_event(GPaintEvent&) override;
virtual void resize_event(GResizeEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;
int item_count() const;
Rect row_rect(int item_index) const;
GScrollBar* m_scrollbar { nullptr };
OwnPtr<GTableModel> m_model;
};

View file

@ -28,6 +28,8 @@ LIBGUI_OBJS = \
GAction.o \
GFontDatabase.o \
GToolBar.o \
GTableView.o \
GTableModel.o \
GWindow.o
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)