| 1 | .. _doc-delegates:
|
|---|
| 2 |
|
|---|
| 3 | #############
|
|---|
| 4 | Delegates
|
|---|
| 5 | #############
|
|---|
| 6 |
|
|---|
| 7 | :Release: |version|
|
|---|
| 8 | :Date: |today|
|
|---|
| 9 |
|
|---|
| 10 | `Delegates` are a cornerstone of the Qt model/delegate/view framework. A delegate is
|
|---|
| 11 | used to display and edit data from a `model`.
|
|---|
| 12 |
|
|---|
| 13 | In the Camelot framework, every field of an `Entity` has an associated delegate
|
|---|
| 14 | that specifies how the field will be displayed and edited. When a new form or
|
|---|
| 15 | table is constructed, the delegates of all fields on the form or table will
|
|---|
| 16 | construct `editors` for their fields and fill them with data from the model.
|
|---|
| 17 | When the data has been edited in the form, the delegates will take care of
|
|---|
| 18 | updating the model with the new data.
|
|---|
| 19 |
|
|---|
| 20 | All Camelot delegates are subclasses of :class:`QAbstractItemDelegate`.
|
|---|
| 21 |
|
|---|
| 22 | The `PyQT website <http://www.riverbankcomputing.com/static/Docs/PyQt4/html/classes.html>`_
|
|---|
| 23 | provides detailed information the differenct classes involved in the
|
|---|
| 24 | model/delegate/view framework.
|
|---|
| 25 |
|
|---|
| 26 | .. _specifying-delgates:
|
|---|
| 27 |
|
|---|
| 28 | Specifying delegates
|
|---|
| 29 | ====================
|
|---|
| 30 |
|
|---|
| 31 | The use of a specific delegate can be forced by using the ``delegate`` field
|
|---|
| 32 | attribute. Suppose ``rating`` is a field of type :ctype:`integer`, then it can
|
|---|
| 33 | be forced to be visualized as stars::
|
|---|
| 34 |
|
|---|
| 35 | from camelot.view.controls import delegates
|
|---|
| 36 |
|
|---|
| 37 | class Movie(Entity):
|
|---|
| 38 | title = Field(Unicode(50))
|
|---|
| 39 | rating = Field(Integer)
|
|---|
| 40 |
|
|---|
| 41 | class Admin(EntityAdmin):
|
|---|
| 42 | list_display = ['title', 'rating']
|
|---|
| 43 | field_attributes = {'rating':{'delegate':delegates.StarDelegate}}
|
|---|
| 44 |
|
|---|
| 45 | The above code will result in:
|
|---|
| 46 |
|
|---|
| 47 | .. image:: ../_static/editors/StarEditor_editable.png
|
|---|
| 48 |
|
|---|
| 49 | If no `delegate` field attribute is given, a default one will be taken
|
|---|
| 50 | depending on the sqlalchemy field type.
|
|---|
| 51 |
|
|---|
| 52 | All available delegates can be found in :mod:`camelot.view.controls.delegates`
|
|---|
| 53 |
|
|---|
| 54 | Available delegates
|
|---|
| 55 | ===================
|
|---|
| 56 |
|
|---|
| 57 | .. automodule:: camelot.view.controls.delegates
|
|---|
| 58 |
|
|---|
| 59 | Attributes common to most delegates
|
|---|
| 60 | ===================================
|
|---|
| 61 |
|
|---|
| 62 | editable
|
|---|
| 63 | --------
|
|---|
| 64 |
|
|---|
| 65 | :const:`True` or :const:`False`
|
|---|
| 66 |
|
|---|
| 67 | Indicates whether the user can edit the field.
|
|---|
| 68 |
|
|---|
| 69 | minimum, maximum
|
|---|
| 70 | ----------------
|
|---|
| 71 |
|
|---|
| 72 | The minimum and maximum allowed values for :ctype:`Integer` and
|
|---|
| 73 | :ctype:`Float` delegates or their related delegates like the Star delegate.
|
|---|
| 74 |
|
|---|
| 75 | choices
|
|---|
| 76 | -------
|
|---|
| 77 |
|
|---|
| 78 | A function taking as a single argument the object to which the field
|
|---|
| 79 | belongs. The function returns a list of tuples containing for each
|
|---|
| 80 | possible choice the value to be stored on the model and the value
|
|---|
| 81 | displayed to the user.
|
|---|
| 82 |
|
|---|
| 83 | The use of :attr:`choices` forces the use of the ComboBox delegate::
|
|---|
| 84 |
|
|---|
| 85 | field_attributes = {'state':{'choices':lambda o:[(1, 'Active'),
|
|---|
| 86 | (2, 'Passive')]}}
|
|---|
| 87 |
|
|---|
| 88 | minimal_column_width
|
|---|
| 89 | --------------------
|
|---|
| 90 |
|
|---|
| 91 | An integer specifying the minimal column width when this field is
|
|---|
| 92 | displayed in a table view. The width is expressed as the number of
|
|---|
| 93 | characters that should fit in the column::
|
|---|
| 94 |
|
|---|
| 95 | field_attributes = {'name':{'minimal_column_width':50}}
|
|---|
| 96 |
|
|---|
| 97 | will make the column wide enough to display at least 50 characters.
|
|---|
| 98 |
|
|---|
| 99 | .. _tooltips:
|
|---|
| 100 |
|
|---|
| 101 | tooltip
|
|---|
| 102 | -------
|
|---|
| 103 |
|
|---|
| 104 | A function taking as a single argument the object to which the field
|
|---|
| 105 | belongs. The function should return a string that will be used as a
|
|---|
| 106 | tooltip. The string may contain html markup.
|
|---|
| 107 |
|
|---|
| 108 | .. literalinclude:: ../../../../test/snippet/fields_with_tooltips.py
|
|---|
| 109 |
|
|---|
| 110 | .. image:: ../_static/snippets/fields_with_tooltips.png
|
|---|
| 111 |
|
|---|
| 112 | background_color
|
|---|
| 113 | ----------------
|
|---|
| 114 |
|
|---|
| 115 | A function taking as a single argument the object to which the field
|
|---|
| 116 | belongs. The function should return None if the default background should
|
|---|
| 117 | be used, or a QColor to be used as the background.
|
|---|
| 118 |
|
|---|
| 119 | .. literalinclude:: ../../../../test/snippet/background_color.py
|
|---|
| 120 |
|
|---|
| 121 | .. image:: ../_static/snippets/background_color.png |
|---|