Delegates

Flexible delegates to set up with a Qt model and view to display the data

class delegates.ViewItemDelegate(view=None)[source]

Bases: QStyledItemDelegate

A generic delegate to display items in a view.

ViewItemDelegate constructor. Sets the default values.

Parameters:

view (sgkt.platform.qt.QtGui.QAbstractItemView) – The view that this delegate will be used with.

class Padding(top, right, bottom, left)[source]

Bases: object

Convenience class structure to store padding values.

Create the Padding object.

Parameters:
  • top (int) – The top padding value.

  • right (int) – The right padding value.

  • bottom (int) – The bottom padding value.

  • left (int) – The left padding value.

classmethod new(padding)[source]

Factory method to create a padding object with equal padding all around.

property thumbnail_role

Get or set the index model data ‘thumbnail’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). This role is used to retrieve the view item thumbnail; e.g. thumbnail = index.idata(thumbnail_role).

property header_role

Get or set the index model data ‘title’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). This role is used to retrieve the view item title text; e.g. text = index.data(header_role).

property subtitle_role

Get or set the index model data ‘subtitle’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). This role is used to retrieve the view item subtitle text; e.g. text = index.data(subtitle_role).

property text_role

Get or set the index model data ‘text’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). This role is used to retrieve the view item main text; e.g. text = index.data(text_role).

property short_text_role

Get or set the index model data role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). This role is used to retrieve the view item shortened text; e.g. text = index.data(short_text_role). The view item shortened text may be useful for thumbnail items that do not display well with longer text.

property icon_role

Get or set the index model data ‘icon’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). This role is used to retrieve the view item icons; e.g. icons = index.data(icon_role). Icons are optional images that can be displayed on the item.

property width_role

Get or set the index model data item ‘width’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole).

property height_role

Get or set the index model data item ‘height’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole).

property expand_role

Get or set the index model data item ‘expand’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). When this role is set, it will allow item rows to expand and collapse based on the text height. The index.data model does not need to do any additional work to enable this functionality, aside from providing a role for the delegate to use.

property loading_role

Get or set the index model data ‘loading’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). The data for this role will determine if a loading icon will be drawn for the view item.

property separator_role

Get or set the index model data ‘separator’ role (sgtk.platform.qt.QtCore.Qt.ItemDataRole). The data for this role will determine if a separator will be drawn for the view item.

property visible_lines

Get or set the number of text lines the view item should display. This will omit any text that is beyond the number of visible lines.

property document_style_sheet

Get or set the default qss for the text document style sheet. This will affect the text style and layout of the item.

property elide_text

Get or set the flag indicating if the main text should be elided. If False, text will be wrapped.

property elide_header

Get or set the flag indicating if the header text should be elided. If False, text will be wrapped.

property item_width

Get or set the view item width. Set to None to ignore this property.

property item_height

Get or set the view item row height. If -1, this property is ignored.

property min_width

Get or set the minimum width for an item.

property min_height

Get or set the minimum height for an item.

property thumbnail_uniform

Get or set the property that affects how thumbnails are rendered.

True will draw all thumbnails in a uniform manner. Thumbnails will fill the avialable space, but may be cropped.

property thumbnail_size

Get or set the preferred size for the view item thumbnail. The value will be used to render the thumbnail, but the size of the thumbnail is not guaranteed to be the exact thumbnail_size.

property thumbnail_width

Get or set the preferred thumbnail width. Convenience property to set the width of the thumbnail_size property.

property thumbnail_height

Get or set the preferred thumbnail height. Convenience property to set the height of the thumbnail_size property.

property pixmap_extent

Get or set the extent used when converting sgtk.platform.qt.QtGui.QIcon to a sgkt.platformqt.QtGui.QPixmap.

property icon_size

Get or set the icon size for the view item action icons.

property badge_height_pct

Get or set the percentage value used to calculate the maximum badge icon height (e.g. badge icon is rendered over a rect, if that rect height is 100 and the badge_height_pct is 0.5, then the badge icon maximum height will be 50). Note that badge icons will only be scaled down to fit, not scaled up if the icon size is smaller than the max height.

property action_item_margin

Get or set the margin used for buttons.

property button_padding

Get or set the padding used for buttons.

property text_document_margin

Get or set the margin for the view item text. A sgtk.platform.qt.QtGui.QTextDocument is used to render the view item text, this value will be used to set the QTextDocument margin property.

property item_padding

Get or set the view item padding. This value will add padding to the view item rect.

property thumbnail_padding

Get or set the padding for the item thumbnail.

property text_padding

Get or set the padding for the item text.

property text_rect_halign

Get or set the text block horizontal alignment. This aligns the text block, not the text lines themselves (e.g. the block of text will be aligned within the available space, but the text lines will remain aligned left). Horizontal alignment will have no effect if there is a header and/or subtitle present (this causes the text block to expand to the full available width).

property text_rect_valign

Get or set the text block vertical alignment.

property font

Get or set the font used to render the item text. If not specified, the QStyleOptionViewItem font value will be used.

property background_pen

Get or set the sgtk.platform.qt.QGui.QPen pen used to draw the view item background. Setting this value will add a border around the view item.

property loading_pen

Get or set the sgtk.platform.qt.QtGui.QPen pen used to draw the item loading indicator.

property loading_brush

Get or set the sgtk.platform.qt.QtGui.QBrush brush used to paint the background of the item loading rect.

property selection_brush

Get or set the sgtk.platform.qt.QGui.QBrush used to draw the view item selection.

property separator_brush

Get or set the sgtk.platform.qt.QGui.QBrush used to draw the view item separator.

property show_hover_selection

Get or set the flag indicating to draw selection on hover or not. The default is set to True.

property override_item_tooltip

Get or set the flag indicating to override the tooltip set on the QStandardItem object. Note that this will only affect indexes that have an associated QStandardItem (e.g. the index model derives from the QStandardItemModel class).

property show_text_tooltip

Get or set the flag indicating to show a tooltip if the item text has been elided.

property action_hover_cursor

Get or set the cursor sgtk.platform.qt.QtGui.QCursor for when an action button is hovered over.

static get_option_background_brush(option)[source]

Return the background brush for the QStyleOptionviewItem object.

NOTE: This method should be used to retrieve the QStyleOptionViewItem’s backgroundBrush property. For different version of Qt, the backgroundBrush property may not exist.

Parameters:

option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option to get the background brush from.

Returns:

The option’s background brush object.

Return type:

sgtk.platform.qt.QtGui.QBrush

static is_selected(option)[source]

Return True if the item is selected.

Parameters:

option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

Returns:

True if the item is selected, else False.

Return type:

bool

static get_value(index, role, default_value=None)[source]

Return the index.data for the given role. If the data returned is a function, return the value of the executing the function.

Parameters:

role (sgtk.platform.qt.QtCore.Qt.ItemDataRole) – The item data role

Returns:

The value for the index and role. If the value found is None, the default_value will be returned.

static get_display_values_list(index, role, flat=False, join_char=None)[source]

Return a list of display string values. Each return item in the list represents a text line.

Parameters:
  • role (sgtk.platform.qt.QtCore.Qt.ItemDataRole) – The item data role

  • flat (bool) – If True, the value returned will be the list of values joined by the ‘join_char’ (default join char is “”)

  • join_char (str) – Used to join the list of values to return a single value, when flat is True.

Returns:

A list of display values. An empty list is returned if no data is found, or was unable to parse the data for the index’s role.

Return type:

list<str>

static split_html_lines(html_text)[source]

Convenience method to split html text by line break tags.

is_hover(option)[source]

Return True if the mouse is over the item. This will always return False if the option does not have a widget or the option’s widget does not have mouse tracking enabeld.

Parameters:

option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

Returns:

True if the mouse is over the item, else False.

Return type:

bool

has_mouse_tracking(option)[source]

Return True if the item’s option widget has mouse tracking enabled.

Parameters:

option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

Returns:

True if the item has mouse tracking enabled.

Return type:

bool

get_cursor_pos(option)[source]

Return the mouse cursor position relative to the item’s widget. This will always return False if the option’s widget does not have mouse tracking enabled.

Parameters:

option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

Returns:

The cursor position relative to the option widget.

Return type:

sgkt.platform.qt.QtCore.QPoint

get_option_widget(option)[source]

Return the widget for the QStyleOptionviewItem object.

NOTE: This method should be used to retrieve the QStyleOptionViewItem’s widget property. For different version of Qt, the widget is stored in different properties.

Parameters:

option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option to get the widget from.

Returns:

The option’s widget object.

Return type:

sgtk.platform.qt.QtGui.QWidget

add_actions(actions, position=3)[source]

Add actions that will be made available to show for each item. This method should be used to add actions to the _actions field instead of editing it directly.

Parameters:
  • actions (list<dict>, where each dict represents the action. The dict will be passed to the ViewItemAction constructor to create a ViewItemAction object.) – The actions to add.

  • position (POSITION enum, defaults to float on the bottom right of the item rect.) – The position to display the actions.

add_action(action, position=3)[source]

Convenience method to add an action. See add_actions.

Parameters:
  • actions (list<dict>, where each dict represents the action. The dict will be passed to the ViewItemAction constructor to create a ViewItemAction object.) – The actions to add.

  • position (POSITION enum, defaults to float on the bottom right of the item rect.) – The position to display the actions.

remove_actions(positions=None)[source]

Remove the actions at the specified position. If positions are not specified, remove all actions.

Parameters:

positions (list<POSITION>, where POSITION is one of the POSITIONS enum.) – The list of positions to remove actions from.

scale_thumbnail_to_item_height(scale_value)[source]

If scale_value is not None, the thumbnail width will scale with the row height by a factor of scale_value (e.g. thumbnail_width = item_height * scale_value). This is convenient when the item size is adjusted dynmaically (e.g. using a slider) and this will scale up and down the thumbnail with the item size.

Parameters:

scale_value (float) – The value to scale the thumbnail by.

get_displayed_text(index)[source]

Return the text that is displayed for the item. The text will be plain (not rich HTML). This may be useful for filtering items displayed by this delegate, since the model data may be formatted by the delegate before being displayed.

Parameters:

index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

initStyleOption(option, index)[source]

Overrides sgtk.platform.qt.QtGui.QStyledItemDelegate method.

Initialize the option used for rendering the item. This should be called before using the option (e.g. sizeHint, paint, etc.).

Parameters:
  • option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

  • index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

createEditor(parent, option, index)[source]

Overrides sgtk.platform.qt.QtGui.QStyledItemDelegate method.

This default implementation does not provide an editor.

Parameters:
  • parent (sgtk.platform.qt.QtGui.QWidget) – The editor’s parent widget.

  • option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

  • index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

Returns:

The editor widget for the item.

Return type:

sgtk.platform.qt.QtGui.QWidget

editorEvent(event, model, option, index)[source]

Overrides sgtk.platform.qt.QtGui.QStyledItemDelegate method.

Listens for left mouse button press events to check if a view item action should be triggered.

If mouse tracking has been enabled on the option’s widget, this method will listens and act on mouse move events, even if there is no editor set up for the item.

Parameters:
  • event (sgtk.platform.qt.QtCore.QEvent) – The event that triggered the editing. Mouse events are sent to this method even if they do not start editing the item.

  • model (sgtk.platform.qt.QtCore.QAbstractItemModel) – The model of the item.

  • option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

  • index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

Returns:

True if the event was handled, else False (it was ignored).

Return type:

bool

sizeHint(option, index)[source]

Overrides sgtk.platform.qt.QtGui.QStyledItemDelegate method.

Returns the size hint for the view item.

Parameters:
  • option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

  • index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

paint(painter, option, index)[source]

Overrides sgtk.platform.qt.QtGui.QStyledItemDelegate method.

The main paint method that draws each item in the view. Override specific draw methods to customize the rendered item, if needed.

Parameters:
  • painter (sgkt.platform.qt.QtGui.QPainter) – the object used for painting.

  • option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

  • index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

class delegates.ThumbnailViewItemDelegate(parent=None)[source]

Bases: ViewItemDelegate

A subclass of the ViewItemDelegate, where the thumbnail is the focus of the display, and is more compact (ideal for items with less text).

Constructor

Call the base constructor and set up properties specific to this delegate class.

Parameters:

parent (sgtk.platform.qt.QtGui.QAbstractItemView) – The parent widget that will be used with this delegate.

sizeHint(option, index)[source]

Override the base ViewItemDelegate method.

The size hint is based on the thumbnail size. The width is set to the thumbnail width, plus the item padding. If the min_width property is set, the width will be at least the min_width. The height is set to the thumbnail height, plus the height of the text and item padding. Currently, the min_height property is not supported by this delegate.

Parameters:
  • option (sgtk.platform.qt.QtGui.QStyleOptionViewItem) – The option used for rendering the item.

  • index (sgtk.platform.qt.QtCore.QModelIndex) – The index of the item.

Returns:

The size hint for this index.

Return type:

sgtk.platform.qt.QtCore.QSize