root/trunk/doc/sphinx/source/doc/delegates.rst @ 1180

Revision 1180, 3.8 KB (checked in by erikj, 6 months ago)

updated docs

Line 
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
11used to display and edit data from a `model`.
12
13In the Camelot framework, every field of an `Entity` has an associated delegate
14that specifies how the field will be displayed and edited.  When a new form or
15table is constructed, the delegates of all fields on the form or table will
16construct `editors` for their fields and fill them with data from the model.
17When the data has been edited in the form, the delegates will take care of
18updating the model with the new data.
19
20All Camelot delegates are subclasses of :class:`QAbstractItemDelegate`.
21
22The `PyQT website <http://www.riverbankcomputing.com/static/Docs/PyQt4/html/classes.html>`_
23provides detailed information the differenct classes involved in the
24model/delegate/view framework.
25
26.. _specifying-delgates:
27
28Specifying delegates
29====================
30
31The use of a specific delegate can be forced by using the ``delegate`` field
32attribute.  Suppose ``rating`` is a field of type :ctype:`integer`, then it can
33be 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       
45The above code will result in:
46
47.. image:: ../_static/editors/StarEditor_editable.png
48
49If no `delegate` field attribute is given, a default one will be taken
50depending on the sqlalchemy field type.
51
52All available delegates can be found in :mod:`camelot.view.controls.delegates`
53
54Available delegates
55===================
56
57.. automodule:: camelot.view.controls.delegates
58
59Attributes common to most delegates
60===================================
61
62editable
63--------
64
65:const:`True` or :const:`False`
66 
67Indicates whether the user can edit the field.
68
69minimum, maximum
70----------------
71
72The minimum and maximum allowed values for :ctype:`Integer` and
73:ctype:`Float` delegates or their related delegates like the Star delegate.
74
75choices
76-------
77
78A function taking as a single argument the object to which the field
79belongs.  The function returns a list of tuples containing for each
80possible choice the value to be stored on the model and the value
81displayed to the user.
82
83The 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                                                         
88minimal_column_width
89--------------------
90
91An integer specifying the minimal column width when this field is
92displayed in a table view.  The width is expressed as the number of
93characters that should fit in the column::
94
95  field_attributes = {'name':{'minimal_column_width':50}}
96 
97will make the column wide enough to display at least 50 characters.
98 
99.. _tooltips:
100
101tooltip
102-------
103
104A function taking as a single argument the object to which the field
105belongs.  The function should return a string that will be used as a
106tooltip.  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
112background_color
113----------------
114
115A function taking as a single argument the object to which the field
116belongs.  The function should return None if the default background should
117be 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
Note: See TracBrowser for help on using the browser.