File: qt_push_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 (92 lines) | stat: -rw-r--r-- 3,047 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
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, 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.push_button import ProxyPushButton

from .QtWidgets import QPushButton

from .qt_abstract_button import QtAbstractButton
from .qt_menu import QtMenu


class QtPushButton(QtAbstractButton, ProxyPushButton):
    """ A Qt implementation of an Enaml ProxyPushButton.

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

    #--------------------------------------------------------------------------
    # Initialization API
    #--------------------------------------------------------------------------
    def create_widget(self):
        """ Create the underlying QPushButton widget.

        """
        self.widget = QPushButton(self.parent_widget())

    def init_widget(self):
        """ Initialize the state of the widget.

        """
        super(QtPushButton, self).init_widget()
        self.set_default(self.declaration.default)

    def init_layout(self):
        """ Handle layout initialization for the push button.

        """
        super(QtPushButton, self).init_layout()
        self.widget.setMenu(self.menu())

    #--------------------------------------------------------------------------
    # Utility Methods
    #--------------------------------------------------------------------------
    def menu(self):
        """ Find and return the menu child for this widget.

        Returns
        -------
        result : QMenu or None
            The menu defined for this widget, or None if not defined.

        """
        m = self.declaration.menu()
        if m is not None:
            return m.proxy.widget

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

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

    def child_removed(self, child):
        """ Handle the child removed event for a QtPushButton.

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

    #--------------------------------------------------------------------------
    # ProxyPushButton API
    #--------------------------------------------------------------------------
    def set_default(self, default):
        """ Set the default button behavior for the widget.

        """
        self.widget.setDefault(default)
        if default:
            self.widget.setFocus()