File: example_editor_manager.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (166 lines) | stat: -rw-r--r-- 4,864 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# (C) Copyright 2007-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

# -----------------------------------------------------------------------------
# Copyright (c) 2007, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Riverbank Computing Limited
# Description: <Enthought undo package component>
# -----------------------------------------------------------------------------


# Enthought library imports.
from traits.etsconfig.api import ETSConfig
from pyface.workbench.api import Editor, EditorManager


class _wxLabelEditor(Editor):
    """ _wxLabelEditor is the wx implementation of a label editor. """

    def create_control(self, parent):
        import wx

        w = wx.TextCtrl(parent, style=wx.TE_RICH2)
        style = w.GetDefaultStyle()
        style.SetAlignment(wx.TEXT_ALIGNMENT_CENTER)
        w.SetDefaultStyle(style)

        self._set_text(w)
        self._set_size_and_style(w)

        self.obj.observe(self._update_text, 'text')
        self.obj.observe(self._update_size, 'size')
        self.obj.observe(self._update_style, 'style')

        return w

    def _name_default(self):
        return self.obj.text

    def _update_text(self, event):
        self._set_text(self.control)

    def _set_text(self, w):
        w.SetValue("")
        w.WriteText(
            "%s(%d points, %s)" % (
                self.obj.text,
                self.obj.size,
                self.obj.style
            )
        )

    def _update_size(self, event):
        self._set_size_and_style(self.control)

    def _update_style(self, event):
        self._set_size_and_style(self.control)

    def _set_size_and_style(self, w):
        import wx
        if self.obj.style == 'normal':
            style, weight = wx.NORMAL, wx.NORMAL
        elif self.obj.style == 'italic':
            style, weight = wx.ITALIC, wx.NORMAL
        elif self.obj.style == 'bold':
            style, weight = wx.NORMAL, wx.BOLD
        else:
            raise NotImplementedError(
                "style '%s' not supported" % self.obj.style
            )

        f = wx.Font(self.obj.size, wx.ROMAN, style, weight, False)
        style = wx.TextAttr("BLACK", wx.NullColour, f)
        w.SetDefaultStyle(style)
        self._set_text(w)


class _PyQt4LabelEditor(Editor):
    """ _PyQt4LabelEditor is the PyQt implementation of a label editor. """

    def create_control(self, parent):

        from pyface.qt import QtCore, QtGui

        w = QtGui.QLabel(parent)
        w.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)

        self._set_text(w)
        self._set_size(w)
        self._set_style(w)

        self.obj.observe(self._update_text, 'text')
        self.obj.oberve(self._update_size, 'size')
        self.obj.observe(self._update_style, 'style')

        return w

    def _name_default(self):
        return self.obj.text

    def _update_text(self, event):
        self._set_text(self.control)

    def _set_text(self, w):
        w.setText(
            "%s\n(%d points, %s)" % (
                self.obj.text,
                self.obj.size,
                self.obj.style
            )
        )

    def _update_size(self, event):
        self._set_size(self.control)

    def _set_size(self, w):
        f = w.font()
        f.setPointSize(self.obj.size)
        w.setFont(f)

        self._set_text(w)

    def _update_style(self, event):
        self._set_style(self.control)

    def _set_style(self, w):
        f = w.font()
        f.setBold(self.obj.style == 'bold')
        f.setItalic(self.obj.style == 'italic')
        w.setFont(f)

        self._set_text(w)


class ExampleEditorManager(EditorManager):
    """ The ExampleEditorManager class creates the example editors. """

    def create_editor(self, window, obj, kind):

        # Create the toolkit specific editor.
        tk_name = ETSConfig.toolkit

        if tk_name == 'wx':
            ed = _wxLabelEditor(window=window, obj=obj)
        elif tk_name == 'qt4' or tk_name == 'qt':
            ed = _PyQt4LabelEditor(window=window, obj=obj)
        else:
            raise NotImplementedError(
                "unsupported toolkit: %s" % tk_name
            )

        return ed