Added gui.panel.output for displaying text output
authorW. Trevor King <wking@drexel.edu>
Thu, 29 Jul 2010 11:39:59 +0000 (07:39 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 29 Jul 2010 11:39:59 +0000 (07:39 -0400)
hooke/ui/gui/__init__.py
hooke/ui/gui/panel/__init__.py
hooke/ui/gui/panel/output.py [new file with mode: 0644]

index f6e881e5010d52a5d40a0a65a2149371ff934f27..08ee6e302a27a4aa6cf64fab48e8dbbff88a29d9 100644 (file)
@@ -154,11 +154,13 @@ class HookeFrame (wx.Frame):
 #                    pos=wx.Point(0, 0),\r
 #                    size=wx.Size(150, 90),\r
 #                    style=wx.NO_BORDER|wx.TE_MULTILINE), 'right'),\r
-#            ('output', wx.TextCtrl(\r
-#                    parent=self,\r
-#                    pos=wx.Point(0, 0),\r
-#                    size=wx.Size(150, 90),\r
-#                    style=wx.NO_BORDER|wx.TE_MULTILINE), 'bottom'),\r
+            ('output', panel.PANELS['output'](\r
+                    buffer_lines=5,\r
+                    parent=self,\r
+                    pos=wx.Point(0, 0),\r
+                    size=wx.Size(150, 90),\r
+                    style=wx.TE_READONLY|wx.NO_BORDER|wx.TE_MULTILINE),\r
+             'bottom'),\r
 #            ('results', panel.results.Results(self), 'bottom'),\r
             ]:\r
             self._add_panel(label, p, style)\r
@@ -247,6 +249,7 @@ class HookeFrame (wx.Frame):
         self.Destroy()\r
 \r
 \r
+\r
     # Command handling\r
 \r
     def _command_by_name(self, name):\r
@@ -264,7 +267,6 @@ class HookeFrame (wx.Frame):
         while True:\r
             msg = self.outqueue.get()\r
             results.append(msg)\r
-            print type(msg), msg\r
             if isinstance(msg, Exit):\r
                 self._on_close()\r
                 break\r
@@ -279,9 +281,9 @@ class HookeFrame (wx.Frame):
                 h.run(self, msg)  # TODO: pause for response?\r
                 continue\r
         pp = getattr(\r
-            self, '_postprocess_%s' % command.name.replace(' ', '_'), None)\r
-        if pp != None:\r
-            pp(command=command, results=results)\r
+            self, '_postprocess_%s' % command.name.replace(' ', '_'),\r
+            self._postprocess_text)\r
+        pp(command=command, results=results)\r
         return results\r
 \r
     def _handle_request(self, msg):\r
@@ -311,6 +313,19 @@ class HookeFrame (wx.Frame):
 \r
     # Command-specific postprocessing\r
 \r
+    def _postprocess_text(self, command, results):\r
+        """Print the string representation of the results to the Results window.\r
+\r
+        This is similar to :class:`~hooke.ui.commandline.DoCommand`'s\r
+        approach, except that :class:`~hooke.ui.commandline.DoCommand`\r
+        doesn't print some internally handled messages\r
+        (e.g. :class:`~hooke.interaction.ReloadUserInterfaceConfig`).\r
+        """\r
+        for result in results:\r
+            if isinstance(result, CommandExit):\r
+                self._c['output'].write(result.__class__.__name__+'\n')\r
+            self._c['output'].write(str(result).rstrip()+'\n')\r
+\r
     def _postprocess_get_curve(self, command, results):\r
         """Update `self` to show the curve.\r
         """\r
@@ -380,9 +395,6 @@ class HookeFrame (wx.Frame):
             playlist.reset()\r
             self.AddTayliss(playlist)\r
 \r
-    def AppendToOutput(self, text):\r
-        self.panelOutput.AppendText(''.join([text, '\n']))\r
-\r
     def AppliesPlotmanipulator(self, name):\r
         '''\r
         Returns True if the plotmanipulator 'name' is applied, False otherwise\r
index 8537598a1942b126b27a98161a90b2f172161008..50aa1dc28a899bbc8e5dcd2ffa876c39208ca8e5 100644 (file)
@@ -7,6 +7,7 @@ PANEL_MODULES = [
     'commands',\r
 #    'note',\r
 #    'notebook',\r
+    'output',\r
 #    'playlist',\r
 #    'plot',\r
 #    'propertyeditor',\r
diff --git a/hooke/ui/gui/panel/output.py b/hooke/ui/gui/panel/output.py
new file mode 100644 (file)
index 0000000..d3b66b5
--- /dev/null
@@ -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)
+
+