Skip to content

log_record_delegate

log_record_delegate

Attributes

ERROR_SEVERITY_COLOR = QtGui.QColor('#e5484d') module-attribute

INFORMATION_SEVERITY_COLOR = QtGui.QColor('#3e63dd') module-attribute

WARNING_SEVERITY_COLOR = QtGui.QColor('#f5a524') module-attribute

Classes

LogRecordItemDelegate

Bases: QStyledItemDelegate

Draws a log record row with a severity bar, level, time and message.

Attributes
EXPECTED_LEVEL_NAME_TEXTS = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') class-attribute instance-attribute
HORIZONTAL_MARGIN_IN_PIXELS = 8 class-attribute instance-attribute
HORIZONTAL_SPACING_IN_PIXELS = 10 class-attribute instance-attribute
ROW_VERTICAL_PADDING_IN_PIXELS = 4 class-attribute instance-attribute
SEVERITY_BAR_WIDTH_IN_PIXELS = 4 class-attribute instance-attribute
Functions
paint(painter, option, index)

Draw the log record row.

Parameters:

Name Type Description Default
painter QPainter

painter.

required
option QStyleOptionViewItem

style options.

required
index QModelIndex | QPersistentModelIndex

item index.

required
Source code in src/nyx/editor/widgets/output_log/log_record_delegate.py
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def paint(
    self,
    painter: QtGui.QPainter,
    option: QtWidgets.QStyleOptionViewItem,
    index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
) -> None:
    """Draw the log record row.

    Args:
        painter (QtGui.QPainter): painter.
        option (QtWidgets.QStyleOptionViewItem): style options.
        index (QtCore.QModelIndex | QtCore.QPersistentModelIndex): item index.
    """
    painter.save()

    is_row_selected = bool(option.state & QtWidgets.QStyle.StateFlag.State_Selected)
    if is_row_selected:
        painter.fillRect(option.rect, option.palette.highlight())
    text_color = (
        option.palette.highlightedText().color()
        if is_row_selected
        else option.palette.text().color()
    )

    log_record: logging.LogRecord = index.data(LogRecordListModel.LogRecordRole)
    severity_color = color_for_logging_level(log_record.levelno)
    timestamp_text = logging_fn.format_log_record_timestamp(log_record)
    level_name_text = log_record.levelname
    message_text = logging_fn.format_log_record_message_as_single_line(log_record)

    font_metrics: QtGui.QFontMetrics = option.fontMetrics
    row_rect: QtCore.QRect = option.rect

    # Coloured severity bar
    severity_bar_rect = QtCore.QRect(
        row_rect.left() + self.HORIZONTAL_MARGIN_IN_PIXELS,
        row_rect.top() + 2,
        self.SEVERITY_BAR_WIDTH_IN_PIXELS,
        row_rect.height() - 4,
    )
    painter.fillRect(severity_bar_rect, severity_color)

    current_x_position = (
        severity_bar_rect.right() + self.HORIZONTAL_SPACING_IN_PIXELS
    )

    # Timestamp
    timestamp_width_in_pixels = font_metrics.horizontalAdvance("00:00:00")
    timestamp_rect = QtCore.QRect(
        current_x_position,
        row_rect.top(),
        timestamp_width_in_pixels,
        row_rect.height(),
    )
    painter.setPen(text_color)
    painter.setOpacity(0.6)
    painter.drawText(
        timestamp_rect,
        QtCore.Qt.AlignmentFlag.AlignVCenter | QtCore.Qt.AlignmentFlag.AlignLeft,
        timestamp_text,
    )
    painter.setOpacity(1.0)
    current_x_position = timestamp_rect.right() + self.HORIZONTAL_SPACING_IN_PIXELS

    # Level name (bold; measured with the bold font, elided for odd custom names)
    bold_font = painter.font()
    bold_font.setBold(True)
    painter.setFont(bold_font)
    bold_font_metrics = QtGui.QFontMetrics(bold_font)
    level_name_width_in_pixels = max(
        bold_font_metrics.horizontalAdvance(expected_level_name_text)
        for expected_level_name_text in self.EXPECTED_LEVEL_NAME_TEXTS
    )
    level_name_rect = QtCore.QRect(
        current_x_position,
        row_rect.top(),
        level_name_width_in_pixels,
        row_rect.height(),
    )
    painter.setPen(severity_color if not is_row_selected else text_color)
    painter.drawText(
        level_name_rect,
        QtCore.Qt.AlignmentFlag.AlignVCenter | QtCore.Qt.AlignmentFlag.AlignLeft,
        bold_font_metrics.elidedText(
            level_name_text,
            QtCore.Qt.TextElideMode.ElideRight,
            level_name_rect.width(),
        ),
    )
    bold_font.setBold(False)
    painter.setFont(bold_font)
    current_x_position = level_name_rect.right() + self.HORIZONTAL_SPACING_IN_PIXELS

    # Message (elided; full text is available as the tooltip)
    message_rect = QtCore.QRect(
        current_x_position,
        row_rect.top(),
        max(
            0,
            row_rect.right() - self.HORIZONTAL_MARGIN_IN_PIXELS - current_x_position,
        ),
        row_rect.height(),
    )
    elided_message_text = font_metrics.elidedText(
        message_text, QtCore.Qt.TextElideMode.ElideRight, message_rect.width()
    )
    painter.setPen(text_color)
    painter.drawText(
        message_rect,
        QtCore.Qt.AlignmentFlag.AlignVCenter | QtCore.Qt.AlignmentFlag.AlignLeft,
        elided_message_text,
    )

    painter.restore()
sizeHint(option, index)

Get the row size.

Parameters:

Name Type Description Default
option QStyleOptionViewItem

style options.

required
index QModelIndex | QPersistentModelIndex

item index.

required

Returns:

Type Description
QSize

row size. The width is left to the view.

Source code in src/nyx/editor/widgets/output_log/log_record_delegate.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def sizeHint(
    self,
    option: QtWidgets.QStyleOptionViewItem,
    index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
) -> QtCore.QSize:
    """Get the row size.

    Args:
        option (QtWidgets.QStyleOptionViewItem): style options.
        index (QtCore.QModelIndex | QtCore.QPersistentModelIndex): item index.

    Returns:
        QtCore.QSize: row size. The width is left to the view.
    """
    row_height_in_pixels = (
        option.fontMetrics.height() + 2 * self.ROW_VERTICAL_PADDING_IN_PIXELS
    )
    return QtCore.QSize(0, row_height_in_pixels)

Functions

color_for_logging_level(logging_level)

Threshold based, so custom levels (e.g. 25) still get a sensible colour.

Source code in src/nyx/editor/widgets/output_log/log_record_delegate.py
12
13
14
15
16
17
18
def color_for_logging_level(logging_level: int) -> QtGui.QColor:
    """Threshold based, so custom levels (e.g. 25) still get a sensible colour."""
    if logging_level >= logging.ERROR:
        return ERROR_SEVERITY_COLOR
    if logging_level >= logging.WARNING:
        return WARNING_SEVERITY_COLOR
    return INFORMATION_SEVERITY_COLOR