021d8fa5661f59e804aa25cce8ce8f672d52846b
[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         print "LHS", txt
19         self.collapseAll()
20         self.expandToDepth(0)
21
22         model = self.model()
23
24         def recursiveExpand(index):
25             parent = index.parent()
26             if parent and parent.isValid():
27                 recursiveExpand(parent)
28             self.expand(index)
29
30         def search(index):
31             print index.data().toString()
32             if index.data().toString() == txt:
33                 recursiveExpand(index)
34                 self.scrollTo(index, self.PositionAtCenter)
35                 return
36             
37             rows = model.rowCount(index)
38             for r in range(rows):
39                 child_index = index.child(r, 0)
40                 search(child_index)
41             
42         root_rows = model.rowCount()
43         for r in range(root_rows):
44             index = model.index(r, 0)
45             search(index)
46
47 class RSpecWindow(QDialog):
48     def __init__(self, parent=None):
49         QDialog.__init__(self, parent)
50
51         self.title = 'RSpec Window'
52         self.setWindowTitle(self.title)
53
54         self.document = None
55         self.model = None
56
57         self.view = RSpecView(self)
58
59         layout = QVBoxLayout()
60         layout.addWidget(self.view)
61         self.setLayout(layout)
62         
63         print 'CHILDREN', str(self.children())
64         self.updateView()
65
66     def showNode(self, hostname):
67         print "SHOWNODE"
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