File: qt_tool_button.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (122 lines) | stat: -rw-r--r-- 3,888 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
#------------------------------------------------------------------------------
# Copyright (c) 2014-2024,, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Typed

from enaml.widgets.tool_button import ProxyToolButton

from .QtCore import Qt
from .QtWidgets import QToolButton, QToolBar, QSizePolicy

from .qt_abstract_button import QtAbstractButton
from .qt_menu import QtMenu


#: A mapping of Enaml popup modes to Qt ToolButtonPopupMode
POPUP_MODES = {
    'button': QToolButton.MenuButtonPopup,
    'instant': QToolButton.InstantPopup,
    'delayed': QToolButton.DelayedPopup,
}


#: A mapping from Enaml button style to Qt ToolButtonStyle
BUTTON_STYLES = {
    'icon_only': Qt.ToolButtonIconOnly,
    'text_only': Qt.ToolButtonTextOnly,
    'text_beside_icon': Qt.ToolButtonTextBesideIcon,
    'text_under_icon': Qt.ToolButtonTextUnderIcon,
}


class QtToolButton(QtAbstractButton, ProxyToolButton):
    """ A Qt implementation of an Enaml ProxyToolButton.

    """
    #: A reference to the widget created by the proxy.
    widget = Typed(QToolButton)

    def create_widget(self):
        """ Create the underlying widget.

        """
        parent = self.parent_widget()
        widget = QToolButton(parent)
        if not isinstance(parent, QToolBar):
            sp = widget.sizePolicy()
            sp.setHorizontalPolicy(QSizePolicy.Minimum)
            widget.setSizePolicy(sp)
        self.widget = widget

    def init_widget(self):
        """ Initialize the underlying widget.

        """
        super(QtToolButton, self).init_widget()
        d = self.declaration
        self.set_button_style(d.button_style)
        self.set_auto_raise(d.auto_raise)
        self.set_popup_mode(d.popup_mode)

    def init_layout(self):
        """ Initialize the widget layout.

        """
        super(QtToolButton, self).init_layout()
        for child in self.children():
            if isinstance(child, QtMenu):
                self.widget.setMenu(child.widget)
                break

    #--------------------------------------------------------------------------
    # Child Events
    #--------------------------------------------------------------------------
    def child_added(self, child):
        """ Handle the child added event for the tool button.

        """
        super(QtToolButton, self).child_added(child)
        if isinstance(child, QtMenu):
            self.widget.setMenu(child.widget)

    def child_removed(self, child):
        """ Handle the child removed event for the tool button.

        """
        super(QtToolButton, self).child_removed(child)
        if isinstance(child, QtMenu):
            if child.widget is self.widget.menu():
                self.widget.setMenu(None)

    #--------------------------------------------------------------------------
    # ProxyToolButton API
    #--------------------------------------------------------------------------
    def set_button_style(self, style):
        """ Set the button style of the widget.

        """
        with self.geometry_guard():
            self.widget.setToolButtonStyle(BUTTON_STYLES[style])

    def set_auto_raise(self, auto):
        """ Set the auto-raise flag on the widget.

        """
        with self.geometry_guard():
            self.widget.setAutoRaise(auto)

    def set_popup_mode(self, mode):
        """ Set the popup mode for the widget menu.

        """
        widget = self.widget
        q_mode = POPUP_MODES[mode]
        if q_mode == widget.popupMode():
            return
        with self.geometry_guard():
            widget.setPopupMode(q_mode)
            widget.setIcon(widget.icon())  # force-resets the internal cache