styles module

Gather GUI style-related objects.

In particular, define functions to display math equations.

MATH_LABEL_DPI = 150

DPI used when rasterising mathtext labels.

MATH_LABEL_FONTSIZE = 6

Font size (pt) passed to matplotlib.

MATH_LABEL_DEFAULT_WIDTH_PX = 300

Fallback width (px) used for the first render, before the widget has been laid out and its real width is known.

_normalize_key(key)

Convert all RST :math:`...` spans to $...$.

This allows keys written in either notation to go through the same rendering pipeline.

Parameters:

key (str) – Raw key string, possibly containing :math:`...` spans.

Returns:

Key with every :math:`...` replaced by the equivalent $...$.

Return type:

str

_parse_key(key)

Split key into (text, units).

Parameters:

key (str) – String following the "text with possible $math$ [units]" convention. Any of the parts may be absent.

Return type:

tuple[str, str]

Returns:

  • str – Content before the first [...].

  • str – Content between the first [...], without the brackets.

_render_to_pixmap(text, width_px, fontsize=6, dpi=150)

Render text as a matplotlib mathtext pixmap of width_px pixels.

Parameters:
  • text (str) – Fully composed mathtext string (output of compose_mathtext()).

  • width_px (int) – Available horizontal space in pixels. The figure is sized to this width so that matplotlib’s wrap=True breaks lines at the correct point.

  • fontsize (int, default: 6) – Font size in points.

  • dpi (int, default: 150) – Rasterisation resolution.

Returns:

Rendered pixmap, or None if rendering failed.

Return type:

QPixmap or None

_compose_mathtext(body, units)

Assemble body and units into a single matplotlib mathtext string.

Parameters:
  • body (str) – Mixed plain/math string (may contain any number of $...$ environments).

  • units (str) – Unit string without brackets (may be empty).

Returns:

A string suitable for matplotlib mathtext rendering, where units are typeset upright inside a math environment.

Return type:

str

class MathTextLabel(body, units, fontsize=6, dpi=150, parent=None)

Bases: QLabel

A QLabel that renders mixed plain/math text as a pixmap.

Re-renders automatically whenever the widget is resized, so the text always wraps to fit the available column width.

Parameters:
  • body (str) – Mixed plain/math string (may contain any number of $…$ environments).

  • units (str) – Unit string without brackets (may be empty).

  • fontsize (int, default: 6) – Font size in points passed to matplotlib.

  • dpi (int, default: 150) – Rasterisation resolution.

resizeEvent(event)

Re-render the pixmap whenever the widget width changes.

Return type:

None

_render(width_px)

Render at width_px and update the displayed pixmap.

Return type:

None

math_text_label_from_key(key, fontsize=6, dpi=150)

Convenience wrapper: parse key then render it.

Parameters:
  • key (str) – String following the "prefix $math$ [units]" convention.

  • fontsize (int, default: 6) – Font size in points passed to matplotlib.

  • dpi (int, default: 150) – Resolution used when rasterising the figure.

Return type:

tuple[QLabel, QLabel]

Returns:

  • QLabel – Rendered label ready to be passed to QTableWidget.setCellWidget.

  • QLabel – Rendered unit ready to be passed to QTableWidget.setCellWidget.

format_number(value)

Format the given number.

Parameters:

value (float) – Number to format.

Return type:

str