File: splash_screen.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (189 lines) | stat: -rw-r--r-- 6,012 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# 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: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" A splash screen. """


# Standard library imports.
import logging

# Major package imports.
import wx

# Enthought library imports.
from enthought.traits.api import Bool, Font, Instance, Int, Str, Tuple
from enthought.traits.ui.wx.color_trait import WxColor

# Local imports.
from image_resource import ImageResource
from splash_screen_log_handler import SplashScreenLogHandler
from util.font_helper import new_font_like
from window import Window


# Create a logger for this module.
logger = logging.getLogger(__name__)


class SplashScreen(Window):
    """ A splash screen. """

    #### 'SplashScreen' interface #############################################
    
    # The image to display on the splash screen.
    image = Instance(ImageResource, ImageResource('splash'))

    # If log messages are to be displayed then this is the logging level. See
    # the Python documentation for the 'logging' module for more details.
    log_level = Int(logging.DEBUG)

    # Should the splash screen display log messages in the splash text?
    show_log_messages = Bool(True)

    # handler will
    # Optional text to display on top of the splash image.
    text = Str

    # The text color.
    text_color = WxColor('black')

    # The text font.
    text_font = Font(None)

    # The x, y location where the text will be drawn.
    text_location  = Tuple(5, 5)
    
    ###########################################################################
    # 'Window' interface.
    ###########################################################################

    def open(self):
        """ Creates the toolkit-specific control for the widget. """

        super(SplashScreen, self).open()

        if self.show_log_messages:
            self._log_handler = SplashScreenLogHandler(self)
            self._log_handler.setLevel(self.log_level)
            
            logger.addHandler(self._log_handler)

        return

    def close(self):
        """ Close the window. """

        if self.show_log_messages:
            logger.removeHandler(self._log_handler)

        super(SplashScreen, self).close()
        
        return

    ###########################################################################
    # Protected 'Window' interface.
    ###########################################################################

    def _create_control(self, parent):
        """ Create the toolkit-specific control that represents the window.

        This method is intended to be overridden!  By default we just create an
        empty frame.

        """

        # Get the splash screen image.
        image = self.image.create_image()
        
        splash_screen = wx.SplashScreen(
            # The bitmap to display on the splash screen.
            image.ConvertToBitmap(),
            # Splash Style.
            wx.SPLASH_NO_TIMEOUT | wx.SPLASH_CENTRE_ON_SCREEN,
            # Timeout in milliseconds (we don't currently timeout!).
            0,
            # The parent of the splash screen.
            parent,
            # wx Id.
            -1,
            # Window style.
            style = wx.SIMPLE_BORDER | wx.FRAME_NO_TASKBAR
        )

        # By default we create a font slightly bigger and slightly more italic
        # than the normal system font ;^)  The font is used inside the event
        # handler for 'EVT_PAINT'.
        if self.text_font is None:
            self.text_font = self._default_text_font()

        # This allows us to write status text on the splash screen.
        wx.EVT_PAINT(splash_screen, self._on_paint)

        return splash_screen

    def _create_contents(self, parent):
        """ Creates the window contents. """

        # In this case, 'wx.SplashScreen' does it all for us.
        pass
        
    ###########################################################################
    # Private interface.
    ###########################################################################

    def _default_text_font(self):
        """ Creates the default text font. """

        font = new_font_like(
            wx.NORMAL_FONT,
            point_size = wx.NORMAL_FONT.GetPointSize() + 1,
            style      = wx.ITALIC
        )

        return font
        
    #### Trait event handlers #################################################

    def _text_changed(self):
        """ Called when the splash screen text has been changed. """

        if self.control is not None:
            # Passing 'False' to 'Refresh' means "do not erase the background".
            self.control.Refresh(False)
            self.control.Update()
            wx.Yield()
        
        return

    #### wx event handlers ####################################################
    
    def _on_paint(self, event):
        """ Called when the splash window is being repainted. """

        if self.control is not None:
            # Get the window that the splash image is drawn in.
            window = self.control.GetSplashWindow()

            dc = wx.PaintDC(window)
            dc.SetFont(self.text_font)
            dc.SetTextForeground(self.text_color)

            x, y = self.text_location
            dc.DrawText(self.text, x, y)

        # Let the normal wx paint handling do its stuff.
        event.Skip()
        
        return

#### EOF ######################################################################