widgetdelegate

Module for having arbitrary widgets in views.

Delegate

The WidgetDelegate enables an easy way to put arbitrary widgets in views. This is done by rendering the widget in the cells of the view. Ones the user edits the item, the delegate will give him an editor widget. Combined with the special views below, this will almost feel like real widgets. So if your widget has buttons, and you click on them, the click actually gets propagated to the widget.

Views

To make the widget delegates in the views behave like real widgets, we have to propagate click events. A user might see a button in a view and will try to click it. This will edit the item, so the delegate will give us a real editor widget and the click will be propagated to given widget. Mouse hovering etc is not supported at the moment and probably not good for performance.

You can either use the WidgetDelegateViewMixin for your own views or use one of the premade views: WD_AbstractItemView, WD_ListView, WD_TableView WD_TreeView.

class jukeboxcore.gui.widgetdelegate.WidgetDelegate(parent=None)[source]

Bases: PySide.QtGui.QStyledItemDelegate

A delegate for drawing a arbitrary QWidget

When subclassing, reimplement:

Note

Make sure that the model returns the ItemIsEditable flag!

I recommend using one of the views in this module, because they issue click events, when an index is clicked.

Create a new abstract widget delegate that draws the given widget.

Parameters:
  • widget (QtGui.QWidget | None) – the widget to draw. If None, it behaves like a QtGui.QStyledItemDelegate
  • parent (QtCore.QObject) – the parent object
Raises:

None

keep_editor_size = None

If True, resize the editor at least to its size Hint size, or if the section allows is, bigger.

widget[source]

Return the widget that is used by the delegate for drawing

Returns:widget
Return type:QtGui.QWidget
Raises:None
paint(painter, option, index)[source]

Use the painter and style option to render the item specified by the item index.

Parameters:
  • painter (QtGui.QPainter) – the painter to paint
  • option (QtGui.QStyleOptionViewItem) – the options for painting
  • index (QtCore.QModelIndex) – the index to paint
Returns:

None

Return type:

None

Raises:

None

sizeHint(option, index)[source]

Return the appropriate amount for the size of the widget

The widget will always be expanded to at least the size of the viewport.

Parameters:
  • option (QtGui.QStyleOptionViewItem) – the options for painting
  • index (QtCore.QModelIndex) – the index to paint
Returns:

None

Return type:

None

Raises:

None

set_widget_index(index)[source]

Set the index for the widget. The widget should retrieve data from the index and display it.

You might want use the same function as for WidgetDelegate.setEditorData().

Parameters:index (QtCore.QModelIndex) – the index to paint
Returns:None
Return type:None
Raises:None
create_widget(parent=None)[source]

Return a widget that should get painted by the delegate

You might want to use this in WidgetDelegate.create_editor_widget()

Parameters:parent (QtGui.QWidget | None) – the parent widget
Returns:The created widget | None
Return type:QtGui.QWidget | None
Raises:None
close_editors()[source]

Close all current editors

Returns:None
Return type:None
Raises:None
createEditor(parent, option, index)[source]

Return the editor to be used for editing the data item with the given index.

Note that the index contains information about the model being used. The editor’s parent widget is specified by parent, and the item options by option.

This will set auto fill background to True on the editor, because else, you would see The rendered delegate below.

Parameters:
  • parent (QtGui.QWidget) – the parent widget
  • option (QtGui.QStyleOptionViewItem) – the options for painting
  • index (QtCore.QModelIndex) – the index to paint
Returns:

The created widget | None

Return type:

QtGui.QWidget | None

Raises:

None

create_editor_widget(parent, option, index)[source]

Return a editor widget for the given index.

Parameters:
  • parent (QtGui.QWidget) – the parent widget
  • option (QtGui.QStyleOptionViewItem) – the options for painting
  • index (QtCore.QModelIndex) – the index to paint
Returns:

The created widget | None

Return type:

QtGui.QWidget | None

Raises:

None

commit_close_editor(index, endedithint=PySide.QtGui.QAbstractItemDelegate.EndEditHint.NoHint)[source]

Commit and close the editor

Call this method whenever the user finished editing.

Parameters:
  • index (QtCore.QModelIndex) – The index of the editor
  • endedithint (QtGui.QAbstractItemDelegate.EndEditHint) – Hints that the delegate can give the model and view to make editing data comfortable for the user
Returns:

None

Return type:

None

Raises:

None

edit_widget(index)[source]

Return the current edit widget at the givent index if there is one

Parameters:index (QtCore.QModelIndex) – The index of the editor
Returns:The editor widget | None
Return type:QtGui.QWidget | None
Raises:None
editor_destroyed(index=None, *args)[source]

Callback for when the editor widget gets destroyed. Set edit_widget to None.

Returns:None
Return type:None
Raises:None
updateEditorGeometry(editor, option, index)[source]

Make sure the editor is the same size as the widget

By default it can get smaller because does not expand over viewport size. This will make sure it will resize to the same size as the widget.

Parameters:
  • editor (QtGui.QWidget) – the editor to update
  • option (QtGui.QStyleOptionViewItem) – the options for painting
  • index (QtCore.QModelIndex) – the index to paint
Returns:

None

Return type:

None

Raises:

None

staticMetaObject = <PySide.QtCore.QMetaObject object at 0x000000000505A748>
class jukeboxcore.gui.widgetdelegate.CommentDelegate(parent=None)[source]

Bases: jukeboxcore.gui.widgetdelegate.WidgetDelegate

A delegate for drawing a jukeboxcore.gui.djitemdata.NoteItemData.

Parameters:parent (QObject) – the parent object
Raises:None
create_widget(parent=None)[source]

Return a widget that should get painted by the delegate

You might want to use this in WidgetDelegate.createEditor()

Returns:The created widget | None
Return type:QtGui.QWidget | None
Raises:None
set_widget_index(index)[source]

Set the index for the widget. The widget should retrieve data from the index and display it.

You might want use the same function as for WidgetDelegate.setEditorData().

Parameters:index (QtCore.QModelIndex) – the index to paint
Returns:None
Return type:None
Raises:None
create_editor_widget(parent, option, index)[source]

Return the editor to be used for editing the data item with the given index.

Note that the index contains information about the model being used. The editor’s parent widget is specified by parent, and the item options by option.

Parameters:
  • parent (QtGui.QWidget) – the parent widget
  • option (QtGui.QStyleOptionViewItem) – the options for painting
  • index (QtCore.QModelIndex) – the index to paint
Returns:

None

Return type:

None

Raises:

None

setEditorData(editor, index)[source]

Sets the contents of the given editor to the data for the item at the given index.

Note that the index contains information about the model being used.
Parameters:
  • editor (QtGui.QWidget) – the editor widget
  • index (QtCore.QModelIndex) – the index to paint
Returns:

None

Return type:

None

Raises:

None

staticMetaObject = <PySide.QtCore.QMetaObject object at 0x000000000505A7C8>
class jukeboxcore.gui.widgetdelegate.WidgetDelegateViewMixin(*args, **kwargs)[source]

Bases: object

Mixin for views to allow editing with mouseclicks, if there is a widgetdelegate.

On a mouse click event, try to edit the index at click position. Then take the editor widget and issue the same click on that widget.

index_at_event(event)[source]

Get the index under the position of the given MouseEvent

Parameters:event (QtGui.QMouseEvent) – the mouse event
Returns:the index
Return type:QtCore.QModelIndex
Raises:None
get_pos_in_delegate(index, globalpos)[source]

Map the global position to the position relative to the given index

Parameters:
  • index (QtCore.QModelIndex) – the index to map to
  • globalpos (QtCore.QPoint) – the global position
Returns:

The position relative to the given index

Return type:

QtCore.QPoint

Raises:

None

propagate_event_to_delegate(event, eventhandler)[source]

Propagate the given Mouse event to the widgetdelegate

Enter edit mode, get the editor widget and issue an event on that widget.

Parameters:
  • event (QtGui.QMouseEvent) – the mouse event
  • eventhandler (str) – the eventhandler to use. E.g. "mousePressEvent"
Returns:

None

Return type:

None

Raises:

None

mouseDoubleClickEvent(event)[source]

If a widgetdelegate is double clicked, enter edit mode and propagate the event to the editor widget.

Parameters:event (QtGui.QMouseEvent) – the mouse event
Returns:None
Return type:None
Raises:None
mousePressEvent(event)[source]

If the mouse is presses on a widgetdelegate, enter edit mode and propagate the event to the editor widget.

Parameters:event (QtGui.QMouseEvent) – the mouse event
Returns:None
Return type:None
Raises:None
mouseReleaseEvent(event)[source]

If the mouse is released on a widgetdelegate, enter edit mode and propagate the event to the editor widget.

Parameters:event (QtGui.QMouseEvent) – the mouse event
Returns:None
Return type:None
Raises:None
class jukeboxcore.gui.widgetdelegate.WD_AbstractItemView(*args, **kwargs)[source]

Bases: jukeboxcore.gui.widgetdelegate.WidgetDelegateViewMixin, PySide.QtGui.QAbstractItemView

A abstract item view that that when clicked, tries to issue a left click to the widget delegate.

staticMetaObject = <PySide.QtCore.QMetaObject object at 0x000000000506B3C8>
class jukeboxcore.gui.widgetdelegate.WD_ListView(*args, **kwargs)[source]

Bases: jukeboxcore.gui.widgetdelegate.WidgetDelegateViewMixin, PySide.QtGui.QListView

A list view that that when clicked, tries to issue a left click to the widget delegate.

staticMetaObject = <PySide.QtCore.QMetaObject object at 0x000000000506B048>
class jukeboxcore.gui.widgetdelegate.WD_TableView(*args, **kwargs)[source]

Bases: jukeboxcore.gui.widgetdelegate.WidgetDelegateViewMixin, PySide.QtGui.QTableView

A table view that that when clicked, tries to issue a left click to the widget delegate.

staticMetaObject = <PySide.QtCore.QMetaObject object at 0x000000000506B408>
class jukeboxcore.gui.widgetdelegate.WD_TreeView(*args, **kwargs)[source]

Bases: jukeboxcore.gui.widgetdelegate.WidgetDelegateViewMixin, PySide.QtGui.QTreeView

A tree view that that when clicked, tries to issue a left click to the widget delegate.

By default the resize mode of the header will resize to contents.

Initialize a new treeview

Raises:None
get_total_indentation(index)[source]

Get the indentation for the given index

Parameters:index (QtCore.ModelIndex) – the index to query
Returns:the number of parents
Return type:int
Raises:None
index_at_event(event)[source]

Get the index under the position of the given MouseEvent

This implementation takes the indentation into account.

Parameters:event (QtGui.QMouseEvent) – the mouse event
Returns:the index
Return type:QtCore.QModelIndex
Raises:None
staticMetaObject = <PySide.QtCore.QMetaObject object at 0x000000000505E948>