File: action.py

package info (click to toggle)
python-pyqtgraph 0.13.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,068 kB
  • sloc: python: 54,043; makefile: 129; ansic: 40; sh: 2
file content (96 lines) | stat: -rw-r--r-- 3,545 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
from ...Qt import QtCore, QtWidgets, QtGui
from ..Parameter import Parameter
from ..ParameterItem import ParameterItem


class ParameterControlledButton(QtWidgets.QPushButton):
    settableAttributes = {
        "title", "tip", "icon", "shortcut", "enabled", "visible"
    }

    def __init__(self, parameter=None, parent=None):
        super().__init__(parent)
        if not parameter:
            return
        parameter.sigNameChanged.connect(self.onNameChange)
        parameter.sigOptionsChanged.connect(self.updateOpts)
        self.clicked.connect(parameter.activate)
        self.updateOpts(parameter, parameter.opts)

    def updateOpts(self, param, opts):
        # Of the attributes that can be set on a QPushButton, only the text
        # and tooltip attributes are different from standard pushbutton names
        nameMap = dict(title="text", tip="toolTip")
        # Special case: "title" could be none, in which case make it something
        # readable by the simple copy-paste logic later
        opts = opts.copy()
        if "name" in opts:
            opts.setdefault("title", opts["name"])
        if "title" in opts and opts["title"] is None:
            opts["title"] = param.title()

        # Another special case: icons should be loaded from data before
        # being passed to the button
        if "icon" in opts:
            opts["icon"] = QtGui.QIcon(opts["icon"])

        for attr in self.settableAttributes.intersection(opts):
            buttonAttr = nameMap.get(attr, attr)
            capitalized = buttonAttr[0].upper() + buttonAttr[1:]
            setter = getattr(self, f"set{capitalized}")
            setter(opts[attr])

    def onNameChange(self, param, name):
        self.updateOpts(param, dict(title=param.title()))


class ActionParameterItem(ParameterItem):
    """ParameterItem displaying a clickable button."""
    def __init__(self, param, depth):
        ParameterItem.__init__(self, param, depth)
        self.layoutWidget = QtWidgets.QWidget()
        self.layout = QtWidgets.QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layoutWidget.setLayout(self.layout)
        self.button = ParameterControlledButton(param, self.layoutWidget)
        #self.layout.addSpacing(100)
        self.layout.addWidget(self.button)
        self.layout.addStretch()
        self.titleChanged()

    def treeWidgetChanged(self):
        ParameterItem.treeWidgetChanged(self)
        tree = self.treeWidget()
        if tree is None:
            return

        self.setFirstColumnSpanned(True)
        tree.setItemWidget(self, 0, self.layoutWidget)

    def titleChanged(self):
        self.setSizeHint(0, self.button.sizeHint())


class ActionParameter(Parameter):
    """
    Used for displaying a button within the tree.

    ``sigActivated(self)`` is emitted when the button is clicked.

    Parameters
    ----------
    icon: str
        Icon to display in the button. Can be any argument accepted
        by :class:`QIcon <QtGui.QIcon>`.
    shortcut: str
        Key sequence to use as a shortcut for the button. Note that this shortcut is
        associated with spawned parameters, i.e. the shortcut will only work when this
        parameter has an item in a tree that is visible. Can be set to any string
        accepted by :class:`QKeySequence <QtGui.QKeySequence>`.
    """
    itemClass = ActionParameterItem
    sigActivated = QtCore.Signal(object)

    def activate(self):
        self.sigActivated.emit(self)
        self.emitStateChanged('activated', None)