File: autoconnect.py

package info (click to toggle)
python-echo 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: python: 2,421; makefile: 148
file content (122 lines) | stat: -rw-r--r-- 5,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from .connect import (connect_checkable_button,
                      connect_value,
                      connect_combo_data,
                      connect_combo_text,
                      connect_float_text,
                      connect_text,
                      connect_button,
                      connect_combo_selection,
                      connect_list_selection,
                      connect_datetime)

__all__ = ['autoconnect_callbacks_to_qt']

HANDLERS = {}
HANDLERS['value'] = connect_value
HANDLERS['valuetext'] = connect_float_text
HANDLERS['bool'] = connect_checkable_button
HANDLERS['text'] = connect_text
HANDLERS['combodata'] = connect_combo_data
HANDLERS['combotext'] = connect_combo_text
HANDLERS['button'] = connect_button
HANDLERS['combosel'] = connect_combo_selection
HANDLERS['listsel'] = connect_list_selection
HANDLERS['datetime'] = connect_datetime


def autoconnect_callbacks_to_qt(instance, widget, connect_kwargs={}):
    """
    Given a class instance with callback properties and a Qt widget/window,
    connect callback properties to Qt widgets automatically.

    The matching is done based on the objectName of the Qt widgets. Qt widgets
    that need to be connected should be named using the syntax ``type_name``
    where ``type`` describes the kind of matching to be done, and ``name``
    matches the name of a callback property. By default, the types can be:

    * ``value``: the callback property is linked to a Qt widget that has
      ``value`` and ``setValue`` methods. Note that for this type, two
      additional keyword arguments can be specified using ``connect_kwargs``
      (see below): these are ``value_range``, which is used for cases where
      the Qt widget is e.g. a slider which has a range of values, and you want
      to map this range of values onto a different range for the callback
      property, and the second is ``log``, which can be set to `True` if this
      mapping should be done in log space.

    * ``valuetext``: the callback property is linked to a Qt widget that has
      ``text`` and ``setText`` methods, and the text is set to a string
      representation of the value. Note that for this type, an additional
      argument ``fmt`` can be provided, which gives either the format to use
      using the ``{}`` syntax, or should be a function that takes a value
      and returns a string. Optionally, if the Qt widget supports
      the ``editingFinished`` signal, this signal is connected to the callback
      property too.

    * ``bool``: the callback property is linked to a Qt widget that has
      ``isChecked`` and ``setChecked`` methods, such as a checkable button.

    * ``text``: the callback property is linked to a Qt widget that has
      ``text`` and ``setText`` methods. Optionally, if the Qt widget supports
      the ``editingFinished`` signal, this signal is connected to the callback
      property too.

    * ``combodata``: the callback property is linked to a QComboBox based on
      the ``userData`` of the entries in the combo box.

    * ``combotext``: the callback property is linked to a QComboBox based on
      the label of the entries in the combo box.

    * ``datetime``: the callback property is linked to a QDateTimeEdit.
      Note that this connection will also work for the more specific QDateEdit
      and QTimeEdit widgets, as they are subclasses of QDateTimeEdit.

    Applications can also define additional mappings between type and
    auto-linking. To do this, simply add a new entry to the ``HANDLERS`` object::

        >>> echo.qt.autoconnect import HANDLERS
        >>> HANDLERS['color'] = connect_color

    The handler function (``connect_color`` in the example above) should take
    the following arguments: the instance the callback property is attached to,
    the name of the callback property, the Qt widget, and optionally some
    keyword arguments.

    When calling ``autoconnect_callbacks_to_qt``, you can specify
    ``connect_kwargs``, where each key should be a valid callback property name,
    and which gives any additional keyword arguments that can be taken by the
    connect functions, as described above. These include for example
    ``value_range``, ``log``, and ``fmt``.

    This function is especially useful when defining ui files, since widget
    objectNames can be easily set during the editing process.
    """

    # We need a dictionary to store the returned connection handlers in cases
    # where these are defined.
    returned_handlers = {}

    for original_name in dir(widget):
        if original_name.startswith('_') or '_' not in original_name:
            continue
        # FIXME: this is a temorary workaround to allow multiple widgets to be
        # connected to a state attribute.
        if original_name.endswith('_'):
            full_name = original_name[:-1]
        else:
            full_name = original_name
        if '_' in full_name:
            wtype, wname = full_name.split('_', 1)
            if full_name in connect_kwargs:
                kwargs = connect_kwargs[full_name]
            elif wname in connect_kwargs:
                kwargs = connect_kwargs[wname]
            else:
                kwargs = {}
            if hasattr(instance, wname):
                if wtype in HANDLERS:
                    child = getattr(widget, original_name)
                    # NOTE: we need to use original_name here since we need a
                    # unique key, and some wname values might be duplicate.
                    returned_handlers[original_name] = HANDLERS[wtype](instance, wname, child, **kwargs)

    return returned_handlers