use a simple QObject and StringIO for log window
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 15 Sep 2010 13:42:05 +0000 (15:42 +0200)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 15 Sep 2010 13:42:05 +0000 (15:42 +0200)
sface.py
sface/logwindow.py
sface/mainwindow.py

index 256c8be..a66d739 100644 (file)
--- a/sface.py
+++ b/sface.py
@@ -37,7 +37,7 @@ QLabel {
 
 
     win = MainWindow()
 
 
     win = MainWindow()
-#    win.redirectOutputToLog()
+    win.redirectOutputToLog()
     win.setWindowTitle("Sface: SFA Interface")
     win.show()
     win.raise_()
     win.setWindowTitle("Sface: SFA Interface")
     win.show()
     win.raise_()
index 3368185..a77eab2 100644 (file)
@@ -1,10 +1,23 @@
 import sys
 import sys
+from cStringIO import StringIO
 
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 
 from sface.config import config
 
 
 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):
 
 class LogWindow(QDialog):
     def __init__(self, parent=None):
@@ -16,25 +29,17 @@ class LogWindow(QDialog):
         layout.addWidget(self.text)
         self.setLayout(layout)
 
         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):
 
     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
         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())
         
         
         
         
         
         
index 140a6ff..b04e638 100644 (file)
@@ -118,7 +118,7 @@ class MainWindow(QWidget):
 
     def showLogWindow(self, link):
         self.logWindow.show()
 
     def showLogWindow(self, link):
         self.logWindow.show()
-        self.logWindow.resize(500, 500)
+        self.logWindow.resize(800, 400)
         self.logWindow.raise_()
         self.logWindow.activateWindow()
 
         self.logWindow.raise_()
         self.logWindow.activateWindow()