restore baris tree expansion feature. Removed >print< statements.
[sface.git] / sface / rspecwindow.py
1 import os
2 import sys
3
4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6 from PyQt4.QtXml import *
7
8 from sface.xmlwidget import *
9 from sface.config import config
10 from sface.screens.sfascreen import SfaScreen
11
12
13 class RSpecView(XmlView):
14     def __init__(self, parent):
15         XmlView.__init__(self, parent)
16
17     def expandMatchingText(self, txt):
18         self.collapseAll()
19         self.expandToDepth(0)
20
21         model = self.model()
22
23         def recursiveExpand(index):
24             parent = index.parent()
25             if parent and parent.isValid():
26                 recursiveExpand(parent)
27             self.expand(index)
28
29         def search(index):
30             # voodoo alert: baris was using index.data()
31             # and apparently it worked. But after me
32             # messing around, only index.model().data(index)
33             # seems to give non-empty QVariant as output.
34             if index.model().data(index).toString() == txt:
35                 recursiveExpand(index)
36                 self.scrollTo(index, self.PositionAtCenter)
37                 return
38             
39             rows = model.rowCount(index)
40             for r in range(rows):
41                 child_index = index.child(r, 0)
42                 search(child_index)
43             
44         root_rows = model.rowCount()
45         for r in range(root_rows):
46             index = model.index(r, 0)
47             search(index)
48
49 class RSpecWindow(QDialog):
50     def __init__(self, parent=None):
51         QDialog.__init__(self, parent)
52
53         self.title = 'RSpec Window'
54         self.setWindowTitle(self.title)
55
56         self.document = None
57         self.model = None
58
59         self.view = RSpecView(self)
60
61         layout = QVBoxLayout()
62         layout.addWidget(self.view)
63         self.setLayout(layout)
64         
65         self.updateView()
66
67     def showNode(self, hostname):
68         self.view.expandMatchingText(hostname)
69
70     def updateView(self):
71
72         del self.document
73         del self.model
74         self.document = None
75         self.model = None
76
77         self.document = QDomDocument(self.title)
78
79         rspec_file = config.getSliceRSpecFile()
80         if not os.path.exists(rspec_file):
81             return
82
83         self.document.setContent(open(rspec_file,'r').read())
84         # DomModel.__init__ is gonna purge the doc
85         # from the "xml bla bla bla" node.
86         # so the arg 'document' needs to be not None
87         # for this to happen
88         self.model = DomModel(self.document, self)
89
90         self.view.setModel(self.model)
91         self.view.expand(self.model.index(0, 0)) #expand first level only
92