gui.utils.keyword_highlight_delegate

File:

EuljiroBible/gui/utils/keyword_highlight_delegate.py

Author:

Benjamin Jaedon Choi - https://github.com/saintbenjamin

Affiliated Church:

The Eulji-ro Presbyterian Church [대한예수교장로회(통합) 을지로교회]

Address:

The Eulji-ro Presbyterian Church, 24-10, Eulji-ro 20-gil, Jung-gu, Seoul 04549, South Korea

Telephone:

+82-2-2266-3070

E-mail:

euljirochurch [at] G.M.A.I.L. (replace [at] with @ and G.M.A.I.L as you understood.)

License:

MIT License with Attribution Requirement (see LICENSE file for details); Copyright (c) 2025 The Eulji-ro Presbyterian Church.

Provides a QStyledItemDelegate that highlights specified keywords in red within a QTextDocument, supporting HTML formatting and line wrapping.

class gui.utils.keyword_highlight_delegate.KeywordHighlightDelegate(*args, **kwargs)[source]

Bases: QStyledItemDelegate

Item delegate that highlights keywords in keyword-search result cells.

This delegate renders cell text via QTextDocument so that:

  • HTML formatting (keyword spans) is supported

  • multiline text is wrapped and drawn correctly

  • selected-row background rendering remains consistent with Qt styles

keywords

Keywords to highlight. Each keyword is highlighted by wrapping exact string matches in an HTML <span> with a colored style.

Type:

List[str]

__init__(keywords, parent=None)[source]

Initialize the delegate.

Parameters:
  • keywords (List[str]) – List of keywords to highlight.

  • parent (QWidget | None) – Optional parent widget.

paint(painter, option, index)[source]

Paint the table cell using HTML rendering with keyword highlights.

This method:

  • draws the row background (selected vs non-selected)

  • converts the model text into highlighted HTML

  • uses QTextDocument to render HTML with wrapping inside the cell rectangle

Parameters:
  • painter (QPainter) – Painter used to draw the item.

  • option (QStyleOptionViewItem) – Style options for the item.

  • index (QModelIndex) – Model index for the item being painted.

_highlight_keywords(text)[source]

Convert plain text into HTML with keyword highlights.

This escapes HTML-sensitive characters, converts newlines to <br>, and wraps each keyword occurrence with an HTML <span> marker.

Parameters:

text (str) – Original cell text.

Returns:

HTML-formatted string with highlighted keywords.

Return type:

str