File: GroupBox.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 (93 lines) | stat: -rw-r--r-- 3,185 bytes parent folder | download | duplicates (2)
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
from ..Qt import QtCore, QtGui, QtWidgets
from .PathButton import PathButton

__all__ = ['GroupBox']

class GroupBox(QtWidgets.QGroupBox):
    """Subclass of QGroupBox that implements collapse handle.
    """
    sigCollapseChanged = QtCore.Signal(object)
    
    def __init__(self, *args):
        QtWidgets.QGroupBox.__init__(self, *args)
        
        self._collapsed = False
        # We modify the size policy when the group box is collapsed, so 
        # keep track of the last requested policy:
        self._lastSizePlocy = self.sizePolicy()
        
        self.closePath = QtGui.QPainterPath()
        self.closePath.moveTo(0, -1)
        self.closePath.lineTo(0, 1)
        self.closePath.lineTo(1, 0)
        self.closePath.lineTo(0, -1)
        
        self.openPath = QtGui.QPainterPath()
        self.openPath.moveTo(-1, 0)
        self.openPath.lineTo(1, 0)
        self.openPath.lineTo(0, 1)
        self.openPath.lineTo(-1, 0)
        
        self.collapseBtn = PathButton(path=self.openPath, size=(12, 12), margin=0)
        self.collapseBtn.setStyleSheet("""
            border: none;
        """)
        self.collapseBtn.setPen('k')
        self.collapseBtn.setBrush('w')
        self.collapseBtn.setParent(self)
        self.collapseBtn.move(3, 3)
        self.collapseBtn.setFlat(True)
        
        self.collapseBtn.clicked.connect(self.toggleCollapsed)

        if len(args) > 0 and isinstance(args[0], str):
            self.setTitle(args[0])
        
    def toggleCollapsed(self):
        self.setCollapsed(not self._collapsed)

    def collapsed(self):
        return self._collapsed
    
    def setCollapsed(self, c):
        if c == self._collapsed:
            return
        
        if c is True:
            self.collapseBtn.setPath(self.closePath)
            self.setSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Preferred, closing=True)
        elif c is False:
            self.collapseBtn.setPath(self.openPath)
            self.setSizePolicy(self._lastSizePolicy)
        else:
            raise TypeError("Invalid argument %r; must be bool." % c)
        
        for ch in self.children():
            if isinstance(ch, QtWidgets.QWidget) and ch is not self.collapseBtn:
                ch.setVisible(not c)
        
        self._collapsed = c
        self.sigCollapseChanged.emit(c)

    def setSizePolicy(self, *args, **kwds):
        QtWidgets.QGroupBox.setSizePolicy(self, *args)
        if kwds.pop('closing', False) is True:
            self._lastSizePolicy = self.sizePolicy()

    def setHorizontalPolicy(self, *args):
        QtWidgets.QGroupBox.setHorizontalPolicy(self, *args)
        self._lastSizePolicy = self.sizePolicy()

    def setVerticalPolicy(self, *args):
        QtWidgets.QGroupBox.setVerticalPolicy(self, *args)
        self._lastSizePolicy = self.sizePolicy()

    def setTitle(self, title):
        # Leave room for button
        QtWidgets.QGroupBox.setTitle(self, "   " + title)
        
    def widgetGroupInterface(self):
        return (self.sigCollapseChanged, 
                GroupBox.collapsed, 
                GroupBox.setCollapsed, 
                True)