56. gui.qtcanvas — Interactive OpenGL Canvas embedded in a Qt widget.

This module implements user interaction with the OpenGL canvas defined in module canvas. QtCanvas is a single interactive OpenGL canvas, while MultiCanvas implements a dynamic array of multiple canvases.

56.1. Classes defined in module gui.qtcanvas

class gui.qtcanvas.MouseHandler(canvas)[source]

A class for handling the mouse events on the Canvas.

mousefunc keeps track of the installed mouse functions. For each combination of mouse button and modifier key we keep a list of functions. Installing a function adds it at the start of the list. The first of the list is the active function. Reset pops the first off the list, making the next active.

get(button, mod)[source]

Return the mouse function bound to button and mod

setCursorShape(shape)[source]

Set the cursor shape to shape

class gui.qtcanvas.QtCanvas(*args, **kargs)[source]

A canvas for OpenGL rendering.

This class provides interactive functionality for the OpenGL canvas provided by the canvas.Canvas class.

Interactivity is highly dependent on Qt. Putting the interactive functions in a separate class makes it esier to use the Canvas class in non-interactive situations or combining it with other GUI toolsets.

The QtCanvas constructor may have positional and keyword arguments. The positional arguments are passed to the QtOpenGL.QGLWidget constructor, while the keyword arguments are passed to the canvas.Canvas constructor.

getSize()[source]

Return the size of this canvas

saneSize(width=-1, height=-1)[source]

Return a cleverly resized canvas size.

Computes a new size for the canvas, while trying to keep its current aspect ratio. Specified positive values are returned unchanged.

Parameters:
  • width (int) – Requested width of the canvas. If <=0, it is automatically computed from height and canvas aspect ratio, or set equal to canvas width.

  • height (int) – Requested height of the canvas. If <=0, it is automatically computed from width and canvas aspect ratio, or set equal to canvas height.

Returns:

  • width (int) – Adjusted canvas width.

  • height (int) – Adjusted canvas height.

changeSize(width, height)[source]

Resize the canvas to (width x height).

If a negative value is given for either width or height, the corresponding size is set equal to the maximum visible size (the size of the central widget of the main window).

Note that this may not have the expected result when multiple viewports are used.

image(*, resize=None, picking=None, remove_alpha=True)[source]

Return the current OpenGL rendering in an image format.

Parameters:
  • resize (tuple of int, optional) – A tuple (width, height) with the requested image size. If either of these values is <= 0, it will be set from the other and the canvas aspect ratio. If not provided or both values are <= 0, the current canvas size will be used.

  • remove_alpha (bool) – If True (default), the alpha channel is removed from the image.

Returns:

qim (QImage) – The current OpenGL rendering as a QImage of the specified size.

Notes

The returned image can be written directly to an image file with qim.save(filename).

See also

rgb

returns the canvas rendering as a numpy ndarray

rgb(resize=None, remove_alpha=True, picking=False)[source]

Return the current OpenGL rendering in an array format.

Parameters:
  • resize (tuple of int, optional) – A tuple (width, height) with the requested image size. If either of these values is <= 0, it will be set from the other and the canvas aspect ratio. If not provided or both values are <= 0, the current canvas size will be used.

  • remove_alpha (bool) – If True (default), the alpha channel is removed from the image.

  • picking (bool) – This argument is for internal use only.

Returns:

ar (array) – The current OpenGL rendering as a numpy array of type uint. Its shape is (w,h,3) if remove_alpha is True (default) or (w,h,4) if remove_alpha is False.

See also

image

return the current rendering as an image

split_pickids(ids, obj_type='element')[source]

Convert picked pixel ids to element Collection

insideRect(rect=None, obj_type='element')[source]

Find collection of elements inside a rectangle

outline(size=(0, 0), profile='luminance', level=0.5, bgcolor=None, nproc=None)[source]

Return the outline of the current rendering

Parameters:
  • size (tuple) – A tuple of ints (w,h) specifying the size of the image to be used in outline detection. A non-positive value will be set automatically from the current canvas size or aspect ratio.

  • profile (callable) – The function to be used to translate pixel colors into a single value. The default is to use the luminance of the pixel color.

  • level (float) – The isolevel at which to construct the outline.

  • bgcolor (color_like) – A color that is to be interpreted as background color and will get a pixel value -0.5. This is currently experimental.

  • nproc (int) – The number of processors to be used in the image processing. Default is to use as many as available.

Returns:

Formex – The outline as a Formex of plexitude 2.

draw_state_line(x, y)[source]

Store the pos and draw a rectangle to it.

draw_state_rect(x, y)[source]

Store the pos and draw a line to it.

wait_interaction()[source]

Wait for the user to finish some interaction.

mouse_rectangle(x, y, action)[source]

Draw a rectangle during mouse move.

On PRESS, record the mouse position. On MOVE, show a rectangle. On RELEASE, store the picked rectangle and possibly execute a function

mouse_line(x, y, action)[source]

Draw a line during mouse move.

On PRESS, record the mouse position. On MOVE, create a rectangular zoom window. On RELEASE, store the picked rectangle and possibly execute a function

(self.statex, self.statey) is the start point self.state is the current end point

getRectangle(yup=True)[source]

Let the user pick a rectangle.

Returns: x0, y0, x1, y1 where x0<x1, y0<y1 If yup is False, y values are downwards

start_selection(mode, tool, filter, pickable=None)[source]

Start an interactive picking mode.

If selection mode was already started, mode is disregarded and this can be used to change the tool or filter.

finish_selection()[source]

End an interactive picking mode.

accept_selection(clear=False)[source]

Accept or cancel an interactive picking mode.

If clear == True, the current selection is cleared.

cancel_selection()[source]

Cancel an interactive picking mode and clear the selection.

pick_pixels()[source]

Set the list of actor parts inside the pick_window.

This implements the ‘pix’ picking tool. The picked object numbers are stored in self.picked.

pick_parts()[source]

Set the list of actor parts inside the pick_window.

This implements the ‘any’ and ‘all’ picking tool. The picked object numbers are stored in self.picked.

filter_closest(picked)[source]

Narrow a Collection to its single item closest to the camera plane

filter_connected(picked, level=1)[source]

Narrow a Collection to the items connected to self.selection

modify_selection()[source]

Modify the current selection.

This method is intended for use in the func of the pick() method, to update the selection after each atomic pick. It modifies the selection depending on the used filters and on the modifier key pressed when doing the pick. Default is:

  • None: add to the selection

  • SHIFT: set as the selection (forgetting previous picks)

  • CTRL: remove from the selection

Without filter, all the items in the last pick are involved. With a filter only a subset may be involved.

modify_and_highlight()[source]

Modify selection and highlight updated selection.

This method is the default func used in the pick method after each atomic pick. It modifies the selection according to the modifiers and filters, and highlights the resulting selection.

pick(mode, tool='pix', oneshot=False, func=None, filter=None, pickable=None, _rect=None, minobj=0)[source]

Interactively pick objects from the canvas.

Parameters:
  • mode (str) – Defines what to pick: one of actor, element, point.

  • oneshot (bool) – If True, the function returns as soon as the user ends an atomic picking operation (left mouse press and release). If False (default) the user can modify his selection until he explicitely accepts (right mouse button press or ENTER) or cancels (ESC) the pick operation.

  • func (callable) – If provided, this function is called after each atomic pick operation (from mouse button press to mouse button release). The canvas self is passed as an argument. The last atomic pick is then available as self.picked and the previously collected selection (if collection is done) is in self.selection. This is commonly used to highlight the picked items, collect picked items, report picked items, compute and display features of picked items. If not provided, the default function modify_and_highlight() is used. See there for details.

  • filter (str) –

    Defines a filter to retain only some of the picked items in the selection. If not provided, all the picked items are retained. Available filters:

    • single: keeps only a single item

    • closest: keeps only the item closest to the user.

    • conn?: keeps only the items connected to the already selected items or to the closest picked item if nothing has been selected yet. The ? can be one of 0, 1 or 2 to define the level of the connectors (point, edge, face). The default is 1 (edge). The conn? filters only work when picking mode is ‘element’ and for objects of type Mesh.

  • _rect (tuple) – A tuple (x0, y0, x1, y1) speciying the rectangular part on the canvas that will be picked. Allows simulated picking.

Returns:

Collection – A (possibly empty) Collection with the picked items. After return, the value of the selection_accepted attribute can be tested to find how the picking operation was exited:

  • True: the selection was accepted (right mouse click, ENTER key, or OK button),

  • False: the selection was canceled (ESC key, or Cancel button). In the latter case, the returned Collection is always empty. It is also possible to test on the length of the selection.

mouse_rect_pick_events(rect=None)[source]

Create the events for a mouse rectangle pick.

Parameters:

rect (tuple of ints, optional) – A tuple (x0,y0,x1,y1) specifying the top left corner and the bottom right corner of the rectangular are to be picked. Values are in pixels relative to the canvas widget. If not provided, the whole canvas area will be picked.

Returns:

list – A nested list of events. The list contains two sublists. The first holds the events to make the rectangle pick:

  • Press the left button mouse at (x0,y0).

  • Move the mouse while holding the left button pressed to (x1,y1).

  • Release the left mouse button at (x1,y1).

The second sublist holds the events to accept the picked area:

  • Press the right mouse button at (x1,y1).

  • Release the right mouse button at (x1,y1).

idraw(mode='point', npoints=-1, zplane=0.0, func=None, coords=None, preview=False, mouseline=False)[source]

Interactively draw on the canvas.

This function allows the user to interactively create points in 2.5D space and collects the subsequent points in a Coords object. The interpretation of these points is left to the caller. The drawing operation is finished when the number of requested points has been reached, or when the user clicks the right mouse button or hits ‘ENTER’ or presses the ESC-button.

Parameters:
  • mode (str) – One of the drawing modes, specifying the kind of objects you want to draw. This is passed to the specified func.

  • npoints (int) – Specifies how many points can be created before returning. If < 0, the continuous drawing mode has to be ended explicitely with an accept or cancel.

  • zplane (float) – The depth of the z-plane on which the 2D drawing is done.

  • func (callable) –

    A function that is called after each atomic drawing operation. It is typically used to accumulate the drawn points in a single set of points and draw a preview of the drawing. If not provided, the default will just do that. The function is passed the canvas as a parameter, from which the following data are available:

    • canvas.drawn: the newly drawn point,

    • canvas.drawing: the accumulated set of points

    • canvas.drawmode: the current drawing mode

  • coords (Coords) – An initial set of coordinates to which the newly created points should be added. THis can be used to continue a previous idraw operation. If provided, npoints also counts these initial points.

  • preview (bool) – If True, the func will also be called during mouse movement with a depressed button, allowing to preview the result before a point is actually created.

Returns:

Coords (npts, 3) – The Coordinates of the created points. On return canvas.draw_accepted will be True if the function returned because the number of points was reached or the result was accepted with a right mouse click or ENTER key; it will be False if the ESC button was hit.

start_draw(mode, zplane, coords, mouseline)[source]

Start an interactive drawing mode.

finish_draw()[source]

End an interactive drawing mode.

accept_draw(clear=False)[source]

Cancel an interactive drawing mode.

If clear == True, the current drawing is cleared.

cancel_draw()[source]

Cancel an interactive drawing mode and clear the drawing.

mouse_draw(x, y, action)[source]

Process mouse events during interactive drawing.

On PRESS, do nothing. On MOVE, do nothing. On RELEASE, compute the unprojected point

drawLinesInter(mode='line', oneshot=False, func=None)[source]

Interactively draw lines on the canvas.

  • oneshot: if True, the function returns as soon as the user ends a drawing operation. The default is to let the user draw multiple lines and only to return after an explicit cancel (ESC or right mouse button).

  • func: if specified, this function will be called after each atomic drawing operation. The current drawing is passed as an argument. This can e.g. be used to show the drawing.

When the drawing operation is finished, the drawing is returned. The return value is a (n,2,2) shaped array.

start_drawing(mode)[source]

Start an interactive line drawing mode.

wait_drawing()[source]

Wait for the user to interactively draw a line.

finish_drawing()[source]

End an interactive drawing mode.

accept_drawing(clear=False)[source]

Cancel an interactive drawing mode.

If clear == True, the current drawing is cleared.

cancel_drawing()[source]

Cancel an interactive drawing mode and clear the drawing.

edit_drawing(mode)[source]

Edit an interactive drawing.

initializeGL(self)[source]
resizeGL(self, w: int, h: int)[source]
paintGL(self)[source]
dynarot(x, y, action)[source]

Perform dynamic rotation operation.

This function processes mouse button events controlling a dynamic rotation operation. The action is one of PRESS, MOVE or RELEASE.

dynapan(x, y, action)[source]

Perform dynamic pan operation.

This function processes mouse button events controlling a dynamic pan operation. The action is one of PRESS, MOVE or RELEASE.

dynazoom(x, y, action)[source]

Perform dynamic zoom operation.

This function processes mouse button events controlling a dynamic zoom operation. The action is one of PRESS, MOVE or RELEASE.

wheel_zoom(delta)[source]

Zoom by rotating a wheel over an angle delta

emit_done(x, y, action)[source]

Emit a DONE event by clicking the mouse.

This is equivalent to pressing the ENTER button.

emit_cancel(x, y, action)[source]

Emit a CANCEL event by clicking the mouse.

This is equivalent to pressing the ESC button.

mousePressEvent(e)[source]

Process a mouse press event.

mouseMoveEvent(e)[source]

Process a mouse move event.

mouseReleaseEvent(e)[source]

Process a mouse release event.

wheelEvent(e)[source]

Process a wheel event.

keyPressEvent(self, a0: QKeyEvent)[source]

56.2. Functions defined in module gui.qtcanvas

gui.qtcanvas.dotpr(v, w)[source]

Return the dot product of vectors v and w

gui.qtcanvas.length(v)[source]

Return the length of the vector v

gui.qtcanvas.projection(v, w)[source]

Return the (signed) length of the projection of vector v on vector w.