From 31b504789431d48feb67d8c6c79cc98d4d7df4f0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 22 Oct 2019 21:38:04 +0200 Subject: [PATCH] LibGUI: Allow override the font on a per-index basis in GListView This should be ported to all of the GAbstractView subclasses. --- Libraries/LibGUI/GAbstractView.cpp | 15 +++++++++++++++ Libraries/LibGUI/GAbstractView.h | 2 ++ Libraries/LibGUI/GListView.cpp | 3 ++- Libraries/LibGUI/GModel.h | 3 ++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Libraries/LibGUI/GAbstractView.cpp b/Libraries/LibGUI/GAbstractView.cpp index aea8ada294..0c98a8ffa1 100644 --- a/Libraries/LibGUI/GAbstractView.cpp +++ b/Libraries/LibGUI/GAbstractView.cpp @@ -108,3 +108,18 @@ void GAbstractView::notify_selection_changed(Badge) on_selection_change(); update(); } + +NonnullRefPtr GAbstractView::font_for_index(const GModelIndex& index) const +{ + if (!model()) + return font(); + + auto font_data = model()->data(index, GModel::Role::Font); + 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(); +} diff --git a/Libraries/LibGUI/GAbstractView.h b/Libraries/LibGUI/GAbstractView.h index 95d6520cde..218d305bb7 100644 --- a/Libraries/LibGUI/GAbstractView.h +++ b/Libraries/LibGUI/GAbstractView.h @@ -45,6 +45,8 @@ public: void notify_selection_changed(Badge); + NonnullRefPtr font_for_index(const GModelIndex&) const; + protected: virtual void did_scroll() override; void activate(const GModelIndex&); diff --git a/Libraries/LibGUI/GListView.cpp b/Libraries/LibGUI/GListView.cpp index dae6d9ae15..b0aea7f0b0 100644 --- a/Libraries/LibGUI/GListView.cpp +++ b/Libraries/LibGUI/GListView.cpp @@ -113,11 +113,12 @@ void GListView::paint_event(GPaintEvent& event) } auto column_metadata = model()->column_metadata(m_model_column); - const Font& font = column_metadata.font ? *column_metadata.font : this->font(); + Rect row_rect(0, y, content_width(), item_height()); painter.fill_rect(row_rect, background_color); auto index = model()->index(row_index, m_model_column); auto data = model()->data(index); + auto font = font_for_index(index); if (data.is_bitmap()) { painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); } else if (data.is_icon()) { diff --git a/Libraries/LibGUI/GModel.h b/Libraries/LibGUI/GModel.h index 3f2654c3f5..6c3505a26e 100644 --- a/Libraries/LibGUI/GModel.h +++ b/Libraries/LibGUI/GModel.h @@ -34,7 +34,8 @@ public: Custom, ForegroundColor, BackgroundColor, - Icon + Icon, + Font, }; virtual ~GModel();