refactoring on the way. Need some more debug
[sface.git] / sface / logwindow.py
1 import sys
2 from cStringIO import StringIO
3
4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6
7 from sface.config import config
8
9 class LogIO(QObject):
10     def __init__(self, parent, old_stdout):
11         QObject.__init__(self, parent)
12         self.io = StringIO()
13         self.old_stdout = old_stdout
14
15     def write(self, txt):
16         self.io.write(txt)
17         self.parent().update()
18
19     def getText(self):
20         return self.io.getvalue()
21
22 class LogWindow(QDialog):
23     def __init__(self, parent=None):
24         QWidget.__init__(self, parent)
25         self.setWindowTitle("SFI Log")
26         self.text = QTextBrowser(self)
27         self.text.ensureCursorVisible()
28
29         layout = QVBoxLayout()
30         layout.addWidget(self.text)
31         self.setLayout(layout)
32
33         self.logio = LogIO(self, sys.stdout)
34
35     def redirectOutput(self):
36         print "\n\nRedirecting all output to Log Window. Please open the log window to see the output\n"
37         self.old_stdout = sys.stdout
38         self.old_stderr = sys.stderr
39         sys.stdout = self.logio
40         sys.stderr = self.logio
41
42     def update(self):
43         self.text.setText(self.logio.getText())
44         c = self.text.textCursor()
45         c.movePosition(QTextCursor.End)
46         self.text.setTextCursor(c)
47         
48         
49