From 1c8117db8ebbbd928e00a98a6547171b485f0245 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 29 Jul 2010 07:39:59 -0400 Subject: [PATCH] Added gui.panel.output for displaying text output --- hooke/ui/gui/__init__.py | 36 ++++++++++++++++++++++----------- hooke/ui/gui/panel/__init__.py | 1 + hooke/ui/gui/panel/output.py | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 hooke/ui/gui/panel/output.py diff --git a/hooke/ui/gui/__init__.py b/hooke/ui/gui/__init__.py index f6e881e..08ee6e3 100644 --- a/hooke/ui/gui/__init__.py +++ b/hooke/ui/gui/__init__.py @@ -154,11 +154,13 @@ class HookeFrame (wx.Frame): # pos=wx.Point(0, 0), # size=wx.Size(150, 90), # style=wx.NO_BORDER|wx.TE_MULTILINE), 'right'), -# ('output', wx.TextCtrl( -# parent=self, -# pos=wx.Point(0, 0), -# size=wx.Size(150, 90), -# style=wx.NO_BORDER|wx.TE_MULTILINE), 'bottom'), + ('output', panel.PANELS['output']( + buffer_lines=5, + parent=self, + pos=wx.Point(0, 0), + size=wx.Size(150, 90), + style=wx.TE_READONLY|wx.NO_BORDER|wx.TE_MULTILINE), + 'bottom'), # ('results', panel.results.Results(self), 'bottom'), ]: self._add_panel(label, p, style) @@ -247,6 +249,7 @@ class HookeFrame (wx.Frame): self.Destroy() + # Command handling def _command_by_name(self, name): @@ -264,7 +267,6 @@ class HookeFrame (wx.Frame): while True: msg = self.outqueue.get() results.append(msg) - print type(msg), msg if isinstance(msg, Exit): self._on_close() break @@ -279,9 +281,9 @@ class HookeFrame (wx.Frame): h.run(self, msg) # TODO: pause for response? continue pp = getattr( - self, '_postprocess_%s' % command.name.replace(' ', '_'), None) - if pp != None: - pp(command=command, results=results) + self, '_postprocess_%s' % command.name.replace(' ', '_'), + self._postprocess_text) + pp(command=command, results=results) return results def _handle_request(self, msg): @@ -311,6 +313,19 @@ class HookeFrame (wx.Frame): # Command-specific postprocessing + def _postprocess_text(self, command, results): + """Print the string representation of the results to the Results window. + + This is similar to :class:`~hooke.ui.commandline.DoCommand`'s + approach, except that :class:`~hooke.ui.commandline.DoCommand` + doesn't print some internally handled messages + (e.g. :class:`~hooke.interaction.ReloadUserInterfaceConfig`). + """ + for result in results: + if isinstance(result, CommandExit): + self._c['output'].write(result.__class__.__name__+'\n') + self._c['output'].write(str(result).rstrip()+'\n') + def _postprocess_get_curve(self, command, results): """Update `self` to show the curve. """ @@ -380,9 +395,6 @@ class HookeFrame (wx.Frame): playlist.reset() self.AddTayliss(playlist) - def AppendToOutput(self, text): - self.panelOutput.AppendText(''.join([text, '\n'])) - def AppliesPlotmanipulator(self, name): ''' Returns True if the plotmanipulator 'name' is applied, False otherwise diff --git a/hooke/ui/gui/panel/__init__.py b/hooke/ui/gui/panel/__init__.py index 8537598..50aa1dc 100644 --- a/hooke/ui/gui/panel/__init__.py +++ b/hooke/ui/gui/panel/__init__.py @@ -7,6 +7,7 @@ PANEL_MODULES = [ 'commands', # 'note', # 'notebook', + 'output', # 'playlist', # 'plot', # 'propertyeditor', diff --git a/hooke/ui/gui/panel/output.py b/hooke/ui/gui/panel/output.py new file mode 100644 index 0000000..d3b66b5 --- /dev/null +++ b/hooke/ui/gui/panel/output.py @@ -0,0 +1,37 @@ +# Copyright + +"""Scrolling text buffer panel for Hooke. +""" + +import wx + +from . import Panel + + +class OutputPanel (Panel, wx.TextCtrl): + """Scrolling text buffer panel. + """ + def __init__(self, name=None, callbacks=None, buffer_lines=1000, **kwargs): + self._buffer_lines = buffer_lines + if (kwargs.get('style') & wx.TE_READONLY == 0): + raise NotImplementedError('%s assumes a readonly TextCtrl' + % self.__class__.__name__) + super(OutputPanel, self).__init__( + name='output', callbacks=callbacks, **kwargs) + + def write(self, text): + self.AppendText(text) + self._limit_to_buffer() + + def _limit_to_buffer(self): + """Limit number of lines retained in the buffer to `._buffer_lines`. + """ + num_lines = self.GetNumberOfLines() + line_index = num_lines - self._buffer_lines + if line_index > 0: + first_pos = 0 # character index for the first character to keep + for i in range(line_index): + first_pos += self.GetLineLength(i) + 1 # +1 for '\n' + self.Remove(0, first_pos) + + -- 2.26.2