mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:08:11 +00:00
LibGUI: Allow specifying per-column text alignment.
This commit is contained in:
parent
ce7019f38c
commit
b4c20789fb
6 changed files with 35 additions and 23 deletions
|
@ -7,7 +7,7 @@ enum Column {
|
||||||
State,
|
State,
|
||||||
Priority,
|
Priority,
|
||||||
Linear,
|
Linear,
|
||||||
Committed,
|
Physical,
|
||||||
CPU,
|
CPU,
|
||||||
Name,
|
Name,
|
||||||
__Count
|
__Count
|
||||||
|
@ -38,23 +38,23 @@ String ProcessTableModel::column_name(int column) const
|
||||||
case Column::State: return "State";
|
case Column::State: return "State";
|
||||||
case Column::Priority: return "Priority";
|
case Column::Priority: return "Priority";
|
||||||
case Column::Linear: return "Linear";
|
case Column::Linear: return "Linear";
|
||||||
case Column::Committed: return "Committed";
|
case Column::Physical: return "Physical";
|
||||||
case Column::CPU: return "CPU";
|
case Column::CPU: return "CPU";
|
||||||
case Column::Name: return "Name";
|
case Column::Name: return "Name";
|
||||||
default: ASSERT_NOT_REACHED();
|
default: ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ProcessTableModel::column_width(int column) const
|
GTableModel::ColumnMetadata ProcessTableModel::column_metadata(int column) const
|
||||||
{
|
{
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case Column::PID: return 30;
|
case Column::PID: return { 30, TextAlignment::CenterRight };
|
||||||
case Column::State: return 80;
|
case Column::State: return { 80, TextAlignment::CenterLeft };
|
||||||
case Column::Priority: return 80;
|
case Column::Priority: return { 80, TextAlignment::CenterLeft };
|
||||||
case Column::Linear: return 80;
|
case Column::Linear: return { 70, TextAlignment::CenterRight };
|
||||||
case Column::Committed: return 80;
|
case Column::Physical: return { 70, TextAlignment::CenterRight };
|
||||||
case Column::CPU: return 30;
|
case Column::CPU: return { 30, TextAlignment::CenterRight };
|
||||||
case Column::Name: return 200;
|
case Column::Name: return { 200, TextAlignment::CenterLeft };
|
||||||
default: ASSERT_NOT_REACHED();
|
default: ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ String ProcessTableModel::data(int row, int column) const
|
||||||
case Column::State: return process.current_state.state;
|
case Column::State: return process.current_state.state;
|
||||||
case Column::Priority: return process.current_state.priority;
|
case Column::Priority: return process.current_state.priority;
|
||||||
case Column::Linear: return pretty_byte_size(process.current_state.linear);
|
case Column::Linear: return pretty_byte_size(process.current_state.linear);
|
||||||
case Column::Committed: return pretty_byte_size(process.current_state.committed);
|
case Column::Physical: return pretty_byte_size(process.current_state.physical);
|
||||||
case Column::CPU: return String::format("%d", (int)process.current_state.cpu_percent);
|
case Column::CPU: return String::format("%d", (int)process.current_state.cpu_percent);
|
||||||
case Column::Name: return process.current_state.name;
|
case Column::Name: return process.current_state.name;
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ void ProcessTableModel::update()
|
||||||
state.name = parts[11];
|
state.name = parts[11];
|
||||||
state.linear = parts[12].to_uint(ok);
|
state.linear = parts[12].to_uint(ok);
|
||||||
ASSERT(ok);
|
ASSERT(ok);
|
||||||
state.committed = parts[13].to_uint(ok);
|
state.physical = parts[13].to_uint(ok);
|
||||||
ASSERT(ok);
|
ASSERT(ok);
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,7 +14,7 @@ public:
|
||||||
virtual int row_count() const override;
|
virtual int row_count() const override;
|
||||||
virtual int column_count() const override;
|
virtual int column_count() const override;
|
||||||
virtual String column_name(int column) const override;
|
virtual String column_name(int column) const override;
|
||||||
virtual int column_width(int column) const override;
|
virtual ColumnMetadata column_metadata(int column) const override;
|
||||||
virtual GModelIndex selected_index() const override;
|
virtual GModelIndex selected_index() const override;
|
||||||
virtual void set_selected_index(GModelIndex) override;
|
virtual void set_selected_index(GModelIndex) override;
|
||||||
virtual String data(int row, int column) const override;
|
virtual String data(int row, int column) const override;
|
||||||
|
@ -30,8 +30,8 @@ private:
|
||||||
String state;
|
String state;
|
||||||
String user;
|
String user;
|
||||||
String priority;
|
String priority;
|
||||||
unsigned linear;
|
size_t linear;
|
||||||
unsigned committed;
|
size_t physical;
|
||||||
float cpu_percent;
|
float cpu_percent;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,18 +5,24 @@
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
#include <LibGUI/GModelIndex.h>
|
#include <LibGUI/GModelIndex.h>
|
||||||
|
#include <SharedGraphics/TextAlignment.h>
|
||||||
|
|
||||||
class GTableView;
|
class GTableView;
|
||||||
|
|
||||||
class GTableModel {
|
class GTableModel {
|
||||||
public:
|
public:
|
||||||
|
struct ColumnMetadata {
|
||||||
|
int preferred_width { 0 };
|
||||||
|
TextAlignment text_alignment { TextAlignment::CenterLeft };
|
||||||
|
};
|
||||||
|
|
||||||
virtual ~GTableModel();
|
virtual ~GTableModel();
|
||||||
|
|
||||||
virtual int row_count() const = 0;
|
virtual int row_count() const = 0;
|
||||||
virtual int column_count() const = 0;
|
virtual int column_count() const = 0;
|
||||||
virtual String row_name(int) const { return { }; }
|
virtual String row_name(int) const { return { }; }
|
||||||
virtual String column_name(int) const { return { }; }
|
virtual String column_name(int) const { return { }; }
|
||||||
virtual int column_width(int) const { return 0; }
|
virtual ColumnMetadata column_metadata(int) const { return { }; }
|
||||||
virtual String data(int row, int column) const = 0;
|
virtual String data(int row, int column) const = 0;
|
||||||
virtual void set_selected_index(GModelIndex) { }
|
virtual void set_selected_index(GModelIndex) { }
|
||||||
virtual GModelIndex selected_index() const { return GModelIndex(); }
|
virtual GModelIndex selected_index() const { return GModelIndex(); }
|
||||||
|
|
|
@ -87,9 +87,11 @@ void GTableView::paint_event(GPaintEvent&)
|
||||||
|
|
||||||
int x_offset = 0;
|
int x_offset = 0;
|
||||||
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
|
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());
|
auto column_metadata = m_model->column_metadata(column_index);
|
||||||
painter.draw_text(cell_rect, m_model->data(row_index, column_index), TextAlignment::CenterLeft, text_color);
|
int column_width = column_metadata.preferred_width;
|
||||||
x_offset += m_model->column_width(column_index) + horizontal_padding;
|
Rect cell_rect(horizontal_padding + x_offset, y, column_width, item_height());
|
||||||
|
painter.draw_text(cell_rect, m_model->data(row_index, column_index), column_metadata.text_alignment, text_color);
|
||||||
|
x_offset += column_width + horizontal_padding;
|
||||||
}
|
}
|
||||||
++painted_item_index;
|
++painted_item_index;
|
||||||
};
|
};
|
||||||
|
@ -103,9 +105,11 @@ void GTableView::paint_event(GPaintEvent&)
|
||||||
painter.fill_rect({ 0, 0, width(), header_height() }, Color::LightGray);
|
painter.fill_rect({ 0, 0, width(), header_height() }, Color::LightGray);
|
||||||
int x_offset = 0;
|
int x_offset = 0;
|
||||||
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
|
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
|
||||||
Rect cell_rect(x_offset, 0, m_model->column_width(column_index) + horizontal_padding, item_height());
|
auto column_metadata = m_model->column_metadata(column_index);
|
||||||
|
int column_width = column_metadata.preferred_width;
|
||||||
|
Rect cell_rect(x_offset, 0, column_width + horizontal_padding, item_height());
|
||||||
painter.draw_text(cell_rect.translated(horizontal_padding, 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
|
painter.draw_text(cell_rect.translated(horizontal_padding, 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
|
||||||
x_offset += m_model->column_width(column_index) + horizontal_padding;
|
x_offset += column_width + horizontal_padding;
|
||||||
painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
|
painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
|
||||||
painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
|
painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "Point.h"
|
#include "Point.h"
|
||||||
#include "Rect.h"
|
#include "Rect.h"
|
||||||
#include "Size.h"
|
#include "Size.h"
|
||||||
|
#include <SharedGraphics/TextAlignment.h>
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
|
|
||||||
class CharacterBitmap;
|
class CharacterBitmap;
|
||||||
|
@ -16,8 +17,6 @@ class GWidget;
|
||||||
class GWindow;
|
class GWindow;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum class TextAlignment { TopLeft, CenterLeft, Center, CenterRight };
|
|
||||||
|
|
||||||
class Painter {
|
class Painter {
|
||||||
public:
|
public:
|
||||||
#ifdef USERLAND
|
#ifdef USERLAND
|
||||||
|
|
3
SharedGraphics/TextAlignment.h
Normal file
3
SharedGraphics/TextAlignment.h
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum class TextAlignment { TopLeft, CenterLeft, Center, CenterRight };
|
Loading…
Add table
Add a link
Reference in a new issue