lintrans.gui.plots package

Module contents

This package provides widgets for the visualization plot in the main window and the visual definition dialog.

Submodules

lintrans.gui.plots.classes module

This module provides superclasses for plotting transformations.

class lintrans.gui.plots.classes.BackgroundPlot[source]

Bases: QWidget

This class provides a background for plotting, as well as setup for a Qt widget.

This class provides a background (untransformed) plane, and all the backend details for a Qt application, but does not provide useful functionality. To be useful, this class must be subclassed and behaviour must be implemented by the subclass.

DEFAULT_GRID_SPACING: int = 85

This is the starting spacing between grid lines (in pixels).

_BRUSH_SOLID_WHITE: QBrush = <PyQt5.QtGui.QBrush object>

This brush is just solid white. Used to draw the insides of circles.

_COLOUR_BACKGROUND_AXES: QColor = <PyQt5.QtGui.QColor object>

This is the colour of the background axes.

_COLOUR_BACKGROUND_GRID: QColor = <PyQt5.QtGui.QColor object>

This is the colour of the background grid lines.

_MINIMUM_GRID_SPACING: int = 5

This is the minimum spacing between grid lines (in pixels).

_PEN_POLYGON: QPen = <PyQt5.QtGui.QPen object>

This is the pen used to draw the normal polygon.

_WIDTH_BACKGROUND_GRID: float = 0.3

This is the width of the background grid lines, as a multiple of the QPainter line width.

__init__(*args, **kwargs)[source]

Create the widget and setup backend stuff for rendering.

Note

*args and **kwargs are passed the superclass constructor (QWidget).

property _canvas_origin: Tuple[int, int]

Return the canvas coords of the grid origin.

The return value is intended to be unpacked and passed to a QPainter.drawLine() call.

See canvas_coords().

Returns

The canvas coordinates of the grid origin

Return type

Tuple[int, int]

_canvas_x(x: float) int[source]

Convert an x coordinate from grid coords to canvas coords.

_canvas_y(y: float) int[source]

Convert a y coordinate from grid coords to canvas coords.

_draw_background(painter: QPainter, draw_grid: bool) None[source]

Draw the background grid.

Note

This method is just a utility method for subclasses to use to render the background grid.

Parameters
  • painter (QPainter) – The painter to draw the background with

  • draw_grid (bool) – Whether to draw the grid lines

_grid_coords(x: int, y: int) Tuple[float, float][source]

Convert a coordinate from canvas coords to grid coords.

Parameters
  • x (int) – The x component of the canvas coordinate

  • y (int) – The y component of the canvas coordinate

Returns

The resultant grid coordinates

Return type

Tuple[float, float]

_grid_corner() Tuple[float, float][source]

Return the grid coords of the top right corner.

canvas_coords(x: float, y: float) Tuple[int, int][source]

Convert a coordinate from grid coords to canvas coords.

This method is intended to be used like

painter.drawLine(*self.canvas_coords(x1, y1), *self.canvas_coords(x2, y2))

or like

painter.drawLine(*self._canvas_origin, *self.canvas_coords(x, y))

See _canvas_origin.

Parameters
  • x (float) – The x component of the grid coordinate

  • y (float) – The y component of the grid coordinate

Returns

The resultant canvas coordinates

Return type

Tuple[int, int]

abstract paintEvent(event: QPaintEvent) None[source]

Handle a QPaintEvent.

Note

This method is abstract and must be overridden by all subclasses.

wheelEvent(event: QWheelEvent) None[source]

Handle a QWheelEvent by zooming in or our of the grid.

class lintrans.gui.plots.classes.InteractivePlot[source]

Bases: BackgroundPlot

This class represents an interactive plot, which allows the user to click and/or drag point(s).

It declares the Qt methods needed for mouse cursor interaction to be abstract, requiring all subclasses to implement these.

_is_within_epsilon(cursor_pos: Tuple[float, float], point: Tuple[float, float]) bool[source]

Check if the cursor position (in canvas coords) is within range of the given point.

_round_to_int_coord(point: Tuple[float, float]) Tuple[float, float][source]

Take a coordinate in grid coords and round it to an integer coordinate if it’s within the snapping distance.

If the point is not close enough, we just return the original point. See lintrans.global_settings.GlobalSettingsData.snap_dist.

abstract mouseMoveEvent(event: QMouseEvent) None[source]

Handle the mouse moving on the widget.

abstract mousePressEvent(event: QMouseEvent) None[source]

Handle the mouse being pressed.

abstract mouseReleaseEvent(event: QMouseEvent) None[source]

Handle the mouse being released.

class lintrans.gui.plots.classes.VectorGridPlot[source]

Bases: BackgroundPlot

This class represents a background plot, with vectors and their grid drawn on top. It provides utility methods.

Note

This is a simple superclass for vectors and is not for visualizing transformations. See VisualizeTransformationPlot.

This class should be subclassed to be used for visualization and matrix definition widgets. All useful behaviour should be implemented by any subclass.

Warning

This class should never be directly instantiated, only subclassed.

_ARROWHEAD_LENGTH = 0.15

This is the minimum length (in grid coord size) of the arrowhead parts.

_COLOUR_I = <PyQt5.QtGui.QColor object>

This is the colour of the i basis vector and associated transformed grid lines.

_COLOUR_J = <PyQt5.QtGui.QColor object>

This is the colour of the j basis vector and associated transformed grid lines.

_COLOUR_TEXT = <PyQt5.QtGui.QColor object>

This is the colour of the text.

_MAX_PARALLEL_LINES = 150

This is the maximum number of parallel transformed grid lines that will be drawn.

The user can zoom out further, but we will stop drawing grid lines beyond this number.

_WIDTH_TRANSFORMED_GRID = 0.8

This is the width of the transformed grid lines, as a multiple of the QPainter line width.

_WIDTH_VECTOR_LINE = 1.8

This is the width of the transformed basis vector lines, as a multiple of the QPainter line width.

__init__(*args, **kwargs)[source]

Create the widget with point_i and point_j attributes.

Note

*args and **kwargs are passed to the superclass constructor (BackgroundPlot).

property _det: float

Return the determinant of the assembled matrix.

_draw_arrowhead_away_from_origin(painter: QPainter, point: Tuple[float, float]) None[source]

Draw an arrowhead at point, pointing away from the origin.

Parameters
  • painter (QPainter) – The painter to draw the arrowhead with

  • point (Tuple[float, float]) – The point to draw the arrowhead at, given in grid coords

_draw_basis_vector_labels(painter: QPainter) None[source]

Label the basis vectors with i and j.

_draw_basis_vectors(painter: QPainter) None[source]

Draw arrowheads at the tips of the basis vectors.

Parameters

painter (QPainter) – The painter to draw the basis vectors with

_draw_oblique_line(painter: QPainter, m: float, c: float) bool[source]

Draw an oblique line, using the equation y = mx + c.

We only draw the part of the line that fits within the canvas, returning True if we were able to draw a line within the boundaries, and False if we couldn’t draw a line

Parameters
  • painter (QPainter) – The painter to draw the vectors and grid lines with

  • m (float) – The gradient of the line to draw

  • c (float) – The y-intercept of the line to draw

Returns bool

Whether we were able to draw a line on the canvas

_draw_pair_of_oblique_lines(painter: QPainter, m: float, c: float) bool[source]

Draw a pair of oblique lines, using the equation y = mx + c.

This method just calls _draw_oblique_line() with c and -c, and returns True if either call returned True.

Parameters
  • painter (QPainter) – The painter to draw the vectors and grid lines with

  • m (float) – The gradient of the lines to draw

  • c (float) – The y-intercept of the lines to draw. We use the positive and negative versions

Returns bool

Whether we were able to draw any lines on the canvas

_draw_parallel_lines(painter: QPainter, vector: Tuple[float, float], point: Tuple[float, float]) None[source]

Draw a set of evenly spaced grid lines parallel to vector intersecting point.

Parameters
  • painter (QPainter) – The painter to draw the lines with

  • vector (Tuple[float, float]) – The vector to draw the grid lines parallel to

  • point (Tuple[float, float]) – The point for the lines to intersect with

_draw_position_vector(painter: QPainter, point: Tuple[float, float], colour: QColor) None[source]

Draw a vector from the origin to the given point.

Parameters
  • painter (QPainter) – The painter to draw the position vector with

  • point (Tuple[float, float]) – The tip of the position vector in grid coords

  • colour (QColor) – The colour to draw the position vector in

_draw_text_at_vector_tip(painter: QPainter, point: Tuple[float, float], text: str, font: Optional[QFont] = None) None[source]

Draw the given text at the point as if it were the tip of a vector, using the custom font if given.

_draw_transformed_grid(painter: QPainter) None[source]

Draw the transformed version of the grid, given by the basis vectors.

Note

This method draws the grid, but not the basis vectors. Use _draw_basis_vectors() to draw them.

Parameters

painter (QPainter) – The painter to draw the grid lines with

property _eigs: Iterable[Tuple[float, VectorType]]

Return the eigenvalues and eigenvectors zipped together to be iterated over.

Return type

Iterable[Tuple[float, VectorType]]

property _matrix: MatrixType

Return the assembled matrix of the basis vectors.

abstract paintEvent(event: QPaintEvent) None[source]

Handle a QPaintEvent.

class lintrans.gui.plots.classes.VisualizeTransformationPlot[source]

Bases: VectorGridPlot

This class is a superclass for visualizing transformations. It provides utility methods.

_COLOUR_EIGEN = <PyQt5.QtGui.QColor object>

This is the colour of the eigenvectors and eigenlines (the spans of the eigenvectors).

_draw_determinant_parallelogram(painter: QPainter) None[source]

Draw the parallelogram of the determinant of the matrix.

Parameters

painter (QPainter) – The painter to draw the parallelogram with

_draw_determinant_text(painter: QPainter) None[source]

Write the string value of the determinant in the middle of the parallelogram.

Parameters

painter (QPainter) – The painter to draw the determinant text with

_draw_eigenlines(painter: QPainter) None[source]

Draw the eigenlines. These are the invariant lines, or the spans of the eigenvectors.

Parameters

painter (QPainter) – The painter to draw the eigenlines with

_draw_eigenvectors(painter: QPainter) None[source]

Draw the eigenvectors of the displayed matrix transformation.

Parameters

painter (QPainter) – The painter to draw the eigenvectors with

_draw_polygon_from_points(painter: QPainter, points: List[Tuple[float, float]]) None[source]

Draw a polygon from a given list of points.

This is a helper method for _draw_untransformed_polygon() and _draw_transformed_polygon().

_draw_transformed_polygon(painter: QPainter) None[source]

Draw the transformed version of the polygon.

_draw_untransformed_polygon(painter: QPainter) None[source]

Draw the original untransformed polygon with a dashed line.

abstract paintEvent(event: QPaintEvent) None[source]

Handle a QPaintEvent.

lintrans.gui.plots.widgets module

This module provides the actual widgets that can be used to visualize transformations in the GUI.

class lintrans.gui.plots.widgets.DefineMatrixVisuallyWidget[source]

Bases: VisualizeTransformationWidget, InteractivePlot

This widget allows the user to visually define a matrix.

This is just the widget itself. If you want the dialog, use DefineVisuallyDialog.

__init__(*args, display_settings: DisplaySettings, polygon_points: List[Tuple[float, float]], input_vector: Tuple[float, float], **kwargs) None[source]

Create the widget and enable mouse tracking. *args and **kwargs are passed to super().

_draw_input_vector(painter: QPainter) None[source]

Draw the input vector.

_draw_output_vector(painter: QPainter) None[source]

Draw the output vector.

mouseMoveEvent(event: QMouseEvent) None[source]

Handle the mouse moving on the canvas.

mousePressEvent(event: QMouseEvent) None[source]

Set the dragged point if the cursor is within the cursor epsilon.

See lintrans.global_settings.GlobalSettingsData.cursor_epsilon.

mouseReleaseEvent(event: QMouseEvent) None[source]

Handle the mouse click being released by unsetting the dragged point.

paintEvent(event: QPaintEvent) None[source]

Paint the scene by just calling _draw_scene().

class lintrans.gui.plots.widgets.DefinePolygonWidget[source]

Bases: InteractivePlot

This widget allows the user to define a polygon by clicking and dragging points on the canvas.

__init__(*args, polygon_points: List[Tuple[float, float]], **kwargs)[source]

Create the widget with a list of points and a dragged point index.

_draw_polygon(painter: QPainter) None[source]

Draw the polygon with circles at its vertices.

mouseMoveEvent(event: QMouseEvent) None[source]

Handle mouse movement by dragging the selected point.

mousePressEvent(event: QMouseEvent) None[source]

Handle the mouse being clicked by adding a point or setting the dragged point index to an existing point.

mouseReleaseEvent(event: QMouseEvent) None[source]

Handle the mouse click being released by unsetting the dragged point index.

paintEvent(event: QPaintEvent) None[source]

Draw the polygon on the canvas.

reset_polygon() None[source]

Reset the polygon and update the widget.

class lintrans.gui.plots.widgets.MainViewportWidget[source]

Bases: VisualizeTransformationWidget, InteractivePlot

This is the widget for the main viewport.

It extends VisualizeTransformationWidget with input and output vectors.

__init__(*args, **kwargs)[source]

Create the main viewport widget with its input point.

_draw_input_vector(painter: QPainter) None[source]

Draw the input vector.

_draw_output_vector(painter: QPainter) None[source]

Draw the output vector.

mouseMoveEvent(event: QMouseEvent) None[source]

Drag the input vector if the user has clicked on it.

mousePressEvent(event: QMouseEvent) None[source]

Check if the user has clicked on the input vector.

mouseReleaseEvent(event: QMouseEvent) None[source]

Stop dragging the input vector.

paintEvent(event: QPaintEvent) None[source]

Paint the scene by just calling _draw_scene() and drawing the I/O vectors.

class lintrans.gui.plots.widgets.VisualizeTransformationWidget[source]

Bases: VisualizeTransformationPlot

This widget is used in the main window to visualize transformations.

It handles all the rendering itself, and the only method that the user needs to care about is plot_matrix(), which allows you to visualize the given matrix transformation.

_COLOUR_OUTPUT_VECTOR = <PyQt5.QtGui.QColor object>
__init__(*args, display_settings: DisplaySettings, polygon_points: List[Tuple[float, float]], **kwargs)[source]

Create the widget and assign its display settings, passing *args and **kwargs to super.

_draw_scene(painter: QPainter) None[source]

Draw the default scene of the transformation.

This method exists to make it easier to split the main viewport from visual definitions while not using multiple QPainter objects from a single paintEvent() call in a subclass.

abstract paintEvent(event: QPaintEvent) None[source]

Paint the scene of the transformation.

plot_matrix(matrix: MatrixType) None[source]

Plot the given matrix on the grid by setting the basis vectors.

Warning

This method does not call update(). This must be done by the caller.

Parameters

matrix (MatrixType) – The matrix to plot