File: DropDownWidget.py

package info (click to toggle)
python-processview 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,192 kB
  • sloc: python: 1,362; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 2,075 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
from silx.gui import qt


class DropDownWidget(qt.QWidget):
    """Simple 'dropdown' widget"""

    _BUTTON_ICON = qt.QStyle.SP_ToolBarVerticalExtensionButton  # noqa

    def __init__(self, parent, direction=qt.Qt.LeftToRight):
        super().__init__(parent)

        self.setLayout(qt.QGridLayout())
        # toggable button
        self._toggleButton = qt.QPushButton("", self)
        self._toggleButton.setCheckable(True)
        self._toggleButton.setSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Fixed)
        spacer = qt.QWidget()
        spacer.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Expanding)

        if direction == qt.Qt.LeftToRight:
            self.layout().addWidget(self._toggleButton, 0, 0, 1, 1)
            self.layout().addWidget(spacer, 0, 1, 1, 1)
        else:
            self.layout().addWidget(spacer, 0, 0, 1, 1)
            self.layout().addWidget(self._toggleButton, 0, 1, 1, 1)

        self._mainWidget = None

        # set up interface
        self.layout().setContentsMargins(2, 2, 2, 2)
        self.layout().setSpacing(0)

        self._setButtonIcon(show=True)
        self._toggleButton.setChecked(True)

        # connect signal / slot
        self._toggleButton.toggled.connect(self._toggleVisibility)

    def setWidget(self, widget: qt.QWidget):
        if self._mainWidget is not None:
            self.layout().removeWidget(self._mainWidget)

        self._mainWidget = widget
        self.layout().addWidget(self._mainWidget, 1, 0, 2, 2)

    def _setButtonIcon(self, show):
        style = qt.QApplication.instance().style()
        # return a QIcon
        icon = style.standardIcon(self._BUTTON_ICON)
        if show is True:
            pixmap = icon.pixmap(32, 32).transformed(qt.QTransform().scale(1, -1))
            icon = qt.QIcon(pixmap)
        self._toggleButton.setIcon(icon)

    def _toggleVisibility(self, visible):
        self._setButtonIcon(show=visible)
        self._mainWidget.setVisible(visible)

    def setChecked(self, checked: bool):
        self._toggleButton.setChecked(checked)