use a simple QObject and StringIO for log window
[sface.git] / sface / logwindow.py
index 3368185..a77eab2 100644 (file)
@@ -1,10 +1,23 @@
 import sys
+from cStringIO import StringIO
 
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 
 from sface.config import config
 
+class LogIO(QObject):
+    def __init__(self, parent, old_stdout):
+        QObject.__init__(self, parent)
+        self.io = StringIO()
+        self.old_stdout = old_stdout
+
+    def write(self, txt):
+        self.io.write(txt)
+        self.parent().update()
+
+    def getText(self):
+        return self.io.getvalue()
 
 class LogWindow(QDialog):
     def __init__(self, parent=None):
@@ -16,25 +29,17 @@ class LogWindow(QDialog):
         layout.addWidget(self.text)
         self.setLayout(layout)
 
-        # To Baris: from the doc,
-        # QIODevice is abstract and can not be instantiated
-        # I am putting QObject just to let the GUI start.
-        #self.io = QIODevice(self)
-        self.io = QObject()
-        self.connect(self.io, SIGNAL('canReadLine()'), self.appendLine)
+        self.logio = LogIO(self, sys.stdout)
 
     def redirectOutput(self):
-        print "Redirecting all output to Log Window. Please open the log window to see the output"
+        print "\n\nRedirecting all output to Log Window. Please open the log window to see the output\n"
         self.old_stdout = sys.stdout
         self.old_stderr = sys.stderr
-        sys.stdout = self.io
-        sys.stderr = self.io
-
-        self.old_stdout.write("test")
-        self.text.append("io Test\n")
+        sys.stdout = self.logio
+        sys.stderr = self.logio
 
-    def appendLine(self):
-        self.text.append(self.io.readLine())
+    def update(self):
+        self.text.setText(self.logio.getText())