mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:18:11 +00:00
LibGUI: Support bitmaps in GTableView cells.
Use this to add an icon for each process in the ProcessManager. Right now they all use a generic gear icon, but I'd like to have per-process icons, obviously. :^)
This commit is contained in:
parent
3fe7ddadaf
commit
b132150799
4 changed files with 30 additions and 13 deletions
|
@ -4,14 +4,15 @@
|
|||
#include <pwd.h>
|
||||
|
||||
enum Column {
|
||||
PID = 0,
|
||||
Icon = 0,
|
||||
Name,
|
||||
CPU,
|
||||
State,
|
||||
User,
|
||||
Priority,
|
||||
User,
|
||||
PID,
|
||||
Linear,
|
||||
Physical,
|
||||
CPU,
|
||||
Name,
|
||||
__Count
|
||||
};
|
||||
|
||||
|
@ -21,6 +22,9 @@ ProcessTableModel::ProcessTableModel()
|
|||
while (auto* passwd = getpwent())
|
||||
m_usernames.set(passwd->pw_uid, passwd->pw_name);
|
||||
endpwent();
|
||||
|
||||
m_generic_process_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/gear16.rgb", { 16, 16 });
|
||||
ASSERT(m_generic_process_icon);
|
||||
}
|
||||
|
||||
ProcessTableModel::~ProcessTableModel()
|
||||
|
@ -40,6 +44,7 @@ int ProcessTableModel::column_count() const
|
|||
String ProcessTableModel::column_name(int column) const
|
||||
{
|
||||
switch (column) {
|
||||
case Column::Icon: return "";
|
||||
case Column::PID: return "PID";
|
||||
case Column::State: return "State";
|
||||
case Column::User: return "User";
|
||||
|
@ -55,14 +60,15 @@ String ProcessTableModel::column_name(int column) const
|
|||
GTableModel::ColumnMetadata ProcessTableModel::column_metadata(int column) const
|
||||
{
|
||||
switch (column) {
|
||||
case Column::PID: return { 30, TextAlignment::CenterRight };
|
||||
case Column::State: return { 80, TextAlignment::CenterLeft };
|
||||
case Column::Priority: return { 75, TextAlignment::CenterLeft };
|
||||
case Column::User: return { 60, TextAlignment::CenterLeft };
|
||||
case Column::Linear: return { 70, TextAlignment::CenterRight };
|
||||
case Column::Physical: return { 70, TextAlignment::CenterRight };
|
||||
case Column::CPU: return { 30, TextAlignment::CenterRight };
|
||||
case Column::Name: return { 200, TextAlignment::CenterLeft };
|
||||
case Column::Icon: return { 16, TextAlignment::CenterLeft };
|
||||
case Column::PID: return { 25, TextAlignment::CenterRight };
|
||||
case Column::State: return { 75, TextAlignment::CenterLeft };
|
||||
case Column::Priority: return { 65, TextAlignment::CenterLeft };
|
||||
case Column::User: return { 50, TextAlignment::CenterLeft };
|
||||
case Column::Linear: return { 65, TextAlignment::CenterRight };
|
||||
case Column::Physical: return { 65, TextAlignment::CenterRight };
|
||||
case Column::CPU: return { 25, TextAlignment::CenterRight };
|
||||
case Column::Name: return { 140, TextAlignment::CenterLeft };
|
||||
default: ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +99,7 @@ GVariant ProcessTableModel::data(int row, int column) const
|
|||
auto it = m_processes.find(m_pids[row]);
|
||||
auto& process = *(*it).value;
|
||||
switch (column) {
|
||||
case Column::Icon: return *m_generic_process_icon;
|
||||
case Column::PID: return process.current_state.pid;
|
||||
case Column::State: return process.current_state.state;
|
||||
case Column::User: return process.current_state.user;
|
||||
|
|
|
@ -44,4 +44,5 @@ private:
|
|||
HashMap<pid_t, OwnPtr<Process>> m_processes;
|
||||
Vector<pid_t> m_pids;
|
||||
int m_selected_row { -1 };
|
||||
RetainPtr<GraphicsBitmap> m_generic_process_icon;
|
||||
};
|
||||
|
|
|
@ -121,7 +121,11 @@ void GTableView::paint_event(GPaintEvent& event)
|
|||
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());
|
||||
painter.draw_text(cell_rect, m_model->data(row_index, column_index).to_string(), column_metadata.text_alignment, text_color);
|
||||
auto data = m_model->data(row_index, column_index);
|
||||
if (data.is_bitmap())
|
||||
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
|
||||
else
|
||||
painter.draw_text(cell_rect, data.to_string(), column_metadata.text_alignment, text_color);
|
||||
x_offset += column_width + horizontal_padding() * 2;
|
||||
}
|
||||
++painted_item_index;
|
||||
|
|
|
@ -23,6 +23,11 @@ public:
|
|||
};
|
||||
|
||||
bool is_valid() const { return m_type != Type::Invalid; }
|
||||
bool is_bool() const { return m_type == Type::Bool; }
|
||||
bool is_int() const { return m_type == Type::Int; }
|
||||
bool is_float() const { return m_type == Type::Float; }
|
||||
bool is_string() const { return m_type == Type::String; }
|
||||
bool is_bitmap() const { return m_type == Type::Bitmap; }
|
||||
Type type() const { return m_type; }
|
||||
|
||||
bool as_bool() const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue