LibGUI: Models should always specify font via Model::Role::Font

This gets rid of one field in ColumnData. The goal is to get rid of all
fields and lose ColumnData entirely.
This commit is contained in:
Andreas Kling 2020-05-21 18:56:52 +02:00
parent 57d15acd4c
commit 04187576ff
Notes: sideshowbarker 2024-07-19 06:16:47 +09:00
8 changed files with 23 additions and 26 deletions

View file

@ -75,6 +75,11 @@ public:
virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override
{
if (role == Role::Font) {
if (index.column() == Column::MatchedText)
return Gfx::Font::default_fixed_width_font();
return {};
}
if (role == Role::Display) {
auto& match = m_matches.at(index.row());
switch (index.column()) {
@ -92,12 +97,12 @@ public:
virtual ColumnMetadata column_metadata(int column) const override
{
if (column == Column::MatchedText) {
return { 0, Gfx::TextAlignment::CenterLeft, &Gfx::Font::default_fixed_width_font() };
return { 0, Gfx::TextAlignment::CenterLeft };
}
return {};
}
virtual void update() override {}
virtual void update() override { }
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& = GUI::ModelIndex()) const override { return create_index(row, column, &m_matches.at(row)); }
private:

View file

@ -61,12 +61,17 @@ GUI::Model::ColumnMetadata VBWidgetPropertyModel::column_metadata(int column) co
{
UNUSED_PARAM(column);
if (column == Column::Name)
return { 110, Gfx::TextAlignment::CenterLeft, &Gfx::Font::default_bold_font() };
return { 110, Gfx::TextAlignment::CenterLeft };
return { 90, Gfx::TextAlignment::CenterLeft };
}
GUI::Variant VBWidgetPropertyModel::data(const GUI::ModelIndex& index, Role role) const
{
if (role == Role::Font) {
if (index.column() == Column::Name)
return Gfx::Font::default_bold_font();
return {};
}
if (role == Role::Custom) {
auto& property = m_widget.m_properties[index.row()];
if (index.column() == Column::Type)

View file

@ -166,9 +166,6 @@ NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) c
if (font_data.is_font())
return font_data.as_font();
auto column_metadata = model()->column_metadata(index.column());
if (column_metadata.font)
return *column_metadata.font;
return font();
}

View file

@ -552,7 +552,7 @@ Model::ColumnMetadata FileSystemModel::column_metadata(int column) const
{
switch (column) {
case Column::Icon:
return { 16, Gfx::TextAlignment::Center, nullptr, Model::ColumnMetadata::Sortable::False };
return { 16, Gfx::TextAlignment::Center, Model::ColumnMetadata::Sortable::False };
case Column::Name:
return { 120, Gfx::TextAlignment::CenterLeft };
case Column::Size:

View file

@ -110,15 +110,14 @@ Gfx::Rect IconView::item_rect(int item_index) const
Vector<int> IconView::items_intersecting_rect(const Gfx::Rect& rect) const
{
ASSERT(model());
const auto& column_metadata = model()->column_metadata(model_column());
const auto& font = column_metadata.font ? *column_metadata.font : this->font();
Vector<int> item_indexes;
for (int item_index = 0; item_index < item_count(); ++item_index) {
Gfx::Rect item_rect;
Gfx::Rect icon_rect;
Gfx::Rect text_rect;
auto item_text = model()->data(model()->index(item_index, model_column()));
get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
auto index = model()->index(item_index, model_column());
auto item_text = model()->data(index);
get_item_rects(item_index, font_for_index(index), item_text, item_rect, icon_rect, text_rect);
if (icon_rect.intersects(rect) || text_rect.intersects(rect))
item_indexes.append(item_index);
}
@ -131,15 +130,13 @@ ModelIndex IconView::index_at_event_position(const Gfx::Point& position) const
// FIXME: Since all items are the same size, just compute the clicked item index
// instead of iterating over everything.
auto adjusted_position = this->adjusted_position(position);
const auto& column_metadata = model()->column_metadata(model_column());
const auto& font = column_metadata.font ? *column_metadata.font : this->font();
for (int item_index = 0; item_index < item_count(); ++item_index) {
Gfx::Rect item_rect;
Gfx::Rect icon_rect;
Gfx::Rect text_rect;
auto index = model()->index(item_index, model_column());
auto item_text = model()->data(index);
get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
get_item_rects(item_index, font_for_index(index), item_text, item_rect, icon_rect, text_rect);
if (icon_rect.contains(adjusted_position) || text_rect.contains(adjusted_position))
return index;
}
@ -279,9 +276,6 @@ void IconView::paint_event(PaintEvent& event)
painter.translate(frame_thickness(), frame_thickness());
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
auto column_metadata = model()->column_metadata(m_model_column);
const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
auto model_index = model()->index(item_index, m_model_column);
bool is_selected_item = selection().contains(model_index);
@ -298,7 +292,7 @@ void IconView::paint_event(PaintEvent& event)
Gfx::Rect item_rect;
Gfx::Rect icon_rect;
Gfx::Rect text_rect;
get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
get_item_rects(item_index, font_for_index(model_index), item_text, item_rect, icon_rect, text_rect);
if (icon.is_icon()) {
if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width())) {
@ -319,7 +313,7 @@ void IconView::paint_event(PaintEvent& event)
else
text_color = model()->data(model_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
painter.fill_rect(text_rect, background_color);
painter.draw_text(text_rect, item_text.to_string(), font, Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
painter.draw_text(text_rect, item_text.to_string(), font_for_index(model_index), Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
if (model_index == m_drop_candidate_index) {
// FIXME: This visualization is not great, as it's also possible to drop things on the text label..

View file

@ -49,7 +49,6 @@ public:
struct ColumnMetadata {
int preferred_width { 0 };
Gfx::TextAlignment text_alignment { Gfx::TextAlignment::CenterLeft };
const Gfx::Font* font { nullptr };
enum class Sortable {
False,
True,

View file

@ -105,7 +105,6 @@ void TableView::paint_event(PaintEvent& event)
continue;
auto column_metadata = model()->column_metadata(column_index);
int column_width = this->column_width(column_index);
const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
bool is_key_column = model()->key_column() == column_index;
Gfx::Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
@ -137,7 +136,7 @@ void TableView::paint_event(PaintEvent& event)
if (cell_background_color.is_valid())
painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
}
painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, Gfx::TextElision::Right);
painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), column_metadata.text_alignment, text_color, Gfx::TextElision::Right);
}
}
x_offset += column_width + horizontal_padding() * 2;
@ -197,5 +196,4 @@ void TableView::keydown_event(KeyEvent& event)
return Widget::keydown_event(event);
}
}

View file

@ -275,7 +275,6 @@ void TreeView::paint_event(PaintEvent& event)
continue;
auto column_metadata = model.column_metadata(column_index);
int column_width = this->column_width(column_index);
auto& font = column_metadata.font ? *column_metadata.font : this->font();
painter.draw_rect(toggle_rect, text_color);
@ -296,7 +295,7 @@ void TreeView::paint_event(PaintEvent& event)
} else {
if (!is_selected_row)
text_color = model.data(cell_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, Gfx::TextElision::Right);
painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), column_metadata.text_alignment, text_color, Gfx::TextElision::Right);
}
}
} else {
@ -316,7 +315,7 @@ void TreeView::paint_event(PaintEvent& event)
rect.width() - icon_size() - icon_spacing(), rect.height()
};
auto node_text = model.data(index, Model::Role::Display).to_string();
painter.draw_text(text_rect, node_text, Gfx::TextAlignment::Center, text_color);
painter.draw_text(text_rect, node_text, font_for_index(index), Gfx::TextAlignment::Center, text_color);
auto index_at_indent = index;
for (int i = indent_level; i > 0; --i) {
auto parent_of_index_at_indent = index_at_indent.parent();