Skip to content

Commit

Permalink
add a loop_delay keyword argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jborbely committed Aug 4, 2017
1 parent 9c9722b commit aba1204
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
9 changes: 4 additions & 5 deletions msl/examples/qt/loop_until_abort.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""
Example script to repeatedly write data to a file until aborted by the user.
"""
import time

from msl.qt import LoopUntilAbort


class LoopExample(LoopUntilAbort):

def __init__(self):
"""Initialize the user interface."""
super(LoopExample, self).__init__()
"""Initialize the dialog window.
Use a 250 ms delay between successive calls to the loop method."""
super(LoopExample, self).__init__(loop_delay=250)

def setup(self):
"""This method gets called before looping starts."""
Expand All @@ -22,7 +22,6 @@ def loop(self):
self.f.write('Counter: {}\n'.format(self.counter))
self.f.write('Elapsed: {}\n'.format(self.elapsed_time))
self.update_label('The current time is\n{}'.format(self.current_time))
time.sleep(0.25)

def teardown(self):
"""This method gets called after looping stops."""
Expand Down
35 changes: 22 additions & 13 deletions msl/qt/loop_until_abort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class LoopUntilAbort(object):

def __init__(self, title=None, bg_color='#DFDFDF', fg_color='#20548B',
def __init__(self, loop_delay=0, title=None, bg_color='#DFDFDF', text_color='#20548B',
font_family='Helvetica', font_size=14, max_iterations=None):
"""Repeatedly perform a task until aborted by the user.
Expand All @@ -28,12 +28,19 @@ def __init__(self, title=None, bg_color='#DFDFDF', fg_color='#20548B',
Parameters
----------
loop_delay : :obj:`int`
The delay time, in milliseconds, to wait between successive calls
to the :meth:`loop` method. For example, if `loop_delay` = ``0``
then there is no time delay between successive calls to the
:meth:`loop` method; if `loop_delay` = ``1000`` then wait 1 second
between successive calls to the :meth:`loop` method. The time delay
occurs **before** the :meth:`loop` method is executed.
title : :obj:`str`
The text to display in the title bar of the dialog window.
If :obj:`None` then uses the name of the subclass as the title.
bg_color : :obj:`str` or :obj:`QColor`
The background color of the dialog window.
fg_color : :obj:`str` or :obj:`QColor`
text_color : :obj:`str` or :obj:`QColor`
The color of the **Elapsed time** and **Iterations** text.
font_family : :obj:`str`
The font family to use for the text.
Expand All @@ -48,8 +55,8 @@ def __init__(self, title=None, bg_color='#DFDFDF', fg_color='#20548B',

self._counter = 0
self._loop_error = False
fg_hex_color = QtGui.QColor(fg_color).name()
bg_hex_color = QtGui.QColor(bg_color).name()
text_hex_color = QtGui.QColor(text_color).name()

self._max_iterations = int(max_iterations) if max_iterations is not None else None

Expand All @@ -69,13 +76,13 @@ def __init__(self, title=None, bg_color='#DFDFDF', fg_color='#20548B',
font = QtGui.QFont(font_family, pointSize=font_size)
self._runtime_label = QtWidgets.QLabel()
self._runtime_label.setFont(font)
self._runtime_label.setStyleSheet('color:{};'.format(fg_hex_color))
self._runtime_label.setStyleSheet('color:{};'.format(text_hex_color))
self._runtime_timer = QtCore.QTimer()
self._runtime_timer.timeout.connect(self._update_runtime_label)

self._counter_label = QtWidgets.QLabel()
self._counter_label.setFont(font)
self._counter_label.setStyleSheet('color:{};'.format(fg_hex_color))
self._counter_label.setStyleSheet('color:{};'.format(text_hex_color))

self._user_label = QtWidgets.QLabel()
self._user_label.setFont(font)
Expand All @@ -92,20 +99,17 @@ def __init__(self, title=None, bg_color='#DFDFDF', fg_color='#20548B',
self._start_time = datetime.datetime.now()
s = self._start_time.strftime('%d %B %Y at %H:%M:%S')
self._main_window.statusBar().showMessage('Started ' + s)
self._update_runtime_label()

try:
self.setup()
setup_successful = True
except:
msg = 'The following exception occurred in the setup() method:\n\n{}'.format(traceback.format_exc())
prompt.critical(msg, title)
setup_successful = False

if setup_successful:
self._runtime_timer.start(1000)
self._loop_timer.start(0)
self._loop_timer.start(max(0, int(loop_delay)))
self._main_window.show()
self._app.exec_()
except:
msg = 'The following exception occurred in the setup() method:\n\n{}'.format(traceback.format_exc())
prompt.critical(msg, title)

@property
def counter(self):
Expand Down Expand Up @@ -137,6 +141,11 @@ def max_iterations(self):
""":obj:`int` or :obj:`None`: The maximum number of times to call the :meth:`loop` method."""
return self._max_iterations

@property
def loop_delay(self):
""":obj:`int`: The time delay, in milliseconds, between successive calls to the loop method."""
return self._loop_timer.interval()

def setup(self):
"""This method gets called before the :meth:`loop` starts.
Expand Down

0 comments on commit aba1204

Please sign in to comment.