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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#-----------------------------------------------------------------------------
#
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# Author: Scott Swarts <swarts@enthought.com>
#
#-----------------------------------------------------------------------------
"""A progress dialog for processing in a background thread.
"""
# Standard library imports.
import logging
import threading
import time
# Major packages.
import wx
# Enthought library imports
from enthought.traits.api import *
# Local imports.
from window import Window
from constant import CANCEL
# Create a logger for this module
logger = logging.getLogger(__name__)
##############################################################################
# class 'BackgroundProgressDialog'
##############################################################################
class BackgroundProgressDialog(Window):
"""A progress dialog for processing in a background thread
"""
##########################################################################
# Traits
##########################################################################
### 'BackgroundProgressDialog' interface #################################
initial_message = String # The initial message in the dialog
show_elapsed_time = false # Whether to show the times
show_estimated_time = false
show_remaining_time = false
exception = Instance(Exception) # Exception raised in background thread
### Private traits #######################################################
_cancel = false # Whether to cancel processing
_thread = Instance(threading.Thread)# The background processing thread
_finished = false # Flag set when the thread finishes
##########################################################################
# 'object' interface
##########################################################################
##########################################################################
# 'Window' interface
##########################################################################
def open(self):
"""Opens the window then creates and starts the thread."""
# Start the thread
self._thread = threading.Thread(target=self._run)
self._thread.setDaemon(1)
self._thread.start()
# Create the dialog
self._create()
self.control.Show(True)
# Wait for it to finish
while not self._finished and not self._cancel:
wx.Yield()
# Close it
self.close()
return self.return_code
def _create_control(self, parent):
"""Create the progress dialog"""
style=wx.PD_AUTO_HIDE | wx.PD_APP_MODAL | wx.PD_CAN_ABORT
if self.show_elapsed_time:
style |= wx.PD_ELAPSED_TIME
if self.show_estimated_time:
style |= wx.PD_ESTIMATED_TIME
if self.show_remaining_time:
style |= wx.PD_REMAINING_TIME
return wx.ProgressDialog(self.title, self.initial_message,
style=style,
parent=parent)
def _create_contents(self, control):
"""Do nothing, the progress dialog already has content."""
pass
##########################################################################
# 'BackgroundProgressDialog' interface
##########################################################################
def _run(self):
"""
Calls run and sets the _finished flag when done.
"""
try:
try:
self.run()
except Exception, ex:
logger.exception("Error in background task: %s", ex)
self.exception = ex
finally:
# Update to 100%. This will set the finished flag
self.update(100)
def run(self):
"""
Does the background processing. This will run in another thread.
Should call update to update the percentage complete and
check for _cancel being set.
This should be overridden by subclasses
"""
for i in range(10):
if self._cancel:
return
self.update(i*10)
time.sleep(1)
self.update(100)
def update(self, value, newmsg=""):
"""
Calls update on the control
"""
wx.CallAfter(self._update, value, newmsg)
def _update(self, value, newmsg):
"""
Does the update. This will run in the main thread.
"""
if self.control is None:
return
#print "Updating with value=%s" % value
if not self.control.Update(value, newmsg):
# Abort has been pressed, set the flag.
self._cancel = True
self.return_code = CANCEL
if value == 100:
self._finished = True
## Test codel ################################################################
def _main():
import sys
dlg = BackgroundProgressDialog(title="Progress",
show_elapsed_time=True,
show_estimated_time=True,
show_remaining_time=True)
res = dlg.open()
print "Result = ", res
if __name__ == '__main__':
import wx
from enthought.pyface.api import GUI, ApplicationWindow
from enthought.pyface.action.api import Action, MenuManager, MenuBarManager
class MainWindow(ApplicationWindow):
""" The main application window. """
######################################################################
# 'object' interface.
######################################################################
def __init__(self, **traits):
""" Creates a new application window. """
# Base class constructor.
super(MainWindow, self).__init__(**traits)
# Add a menu bar.
self.menu_bar_manager = MenuBarManager(
MenuManager(
Action(name='E&xit', on_perform=self.close),
Action(name='DoIt', on_perform=_main),
name = '&File',
)
)
return
gui = GUI(redirect=False)
# Create and open the main window.
window = MainWindow()
window.open()
wx.CallAfter(_main)
# Start the GUI event loop!
gui.event_loop()
#### EOF ######################################################################
|