support for separate ListResources calls for slice and resources
[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 import time
10
11 class LogIO(QObject):
12     def __init__(self, parent, old_stdout):
13         QObject.__init__(self, parent)
14         self.io = StringIO()
15         self.old_stdout = old_stdout
16
17     def write(self, txt):
18         self.io.write(txt)
19         self.parent().update()
20
21     def getText(self):
22         val = self.io.getvalue()
23         # looks like there is no other way
24         # to empty the StringIO
25         del self.io
26         self.io = StringIO()
27         return val
28
29 class LogWindow(QDialog):
30     def __init__(self, parent=None):
31         QWidget.__init__(self, parent)
32         self.setWindowTitle("SFI Log")
33         self.text = QTextBrowser(self)
34         self.text.ensureCursorVisible()
35
36         layout = QVBoxLayout()
37         layout.addWidget(self.text)
38         self.setLayout(layout)
39
40         self.logio = LogIO(self, sys.stdout)
41
42     def redirectOutput(self):
43         print "\n\nRedirecting all output to Log Window. Please open the log window to see the output\n"
44         self.old_stdout = sys.stdout
45         self.old_stderr = sys.stderr
46         sys.stdout = self.logio
47         sys.stderr = self.logio
48
49     def update(self):
50         self.text.insertPlainText(self.logio.getText())
51         c = self.text.textCursor()
52         c.movePosition(QTextCursor.End)
53         self.text.setTextCursor(c)