first get the xml doc, then pass it to DomModel. Not the other way around
[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             if index.data().toString() == txt:
31                 recursiveExpand(index)
32                 self.scrollTo(index, self.PositionAtCenter)
33                 return
34             
35             rows = model.rowCount(index)
36             for r in range(rows):
37                 child_index = index.child(r, 0)
38                 search(child_index)
39             
40         root_rows = model.rowCount()
41         for r in range(root_rows):
42             index = model.index(r, 0)
43             search(index)
44
45 class RSpecWindow(QDialog):
46     def __init__(self, parent=None):
47         QDialog.__init__(self, parent)
48
49         self.title = 'RSpec Window'
50         self.setWindowTitle(self.title)
51
52         self.document = None
53         self.model = None
54
55         self.view = RSpecView(self)
56
57         layout = QVBoxLayout()
58         layout.addWidget(self.view)
59         self.setLayout(layout)
60         
61         print 'CHILDREN', str(self.children())
62         self.updateView()
63
64     def showNode(self, hostname):
65         self.view.expandMatchingText(hostname)
66
67     def updateView(self):
68
69         del self.document
70         del self.model
71         self.document = None
72         self.model = None
73
74         self.document = QDomDocument(self.title)
75
76         rspec_file = config.getSliceRSpecFile()
77         if not os.path.exists(rspec_file):
78             return
79
80         self.document.setContent(open(rspec_file,'r').read())
81         # DomModel.__init__ is gonna purge the doc
82         # from the "xml bla bla bla" node.
83         # so the arg 'document' needs to be not None
84         # for this to happen
85         self.model = DomModel(self.document, self)
86
87         self.view.setModel(self.model)
88         self.view.expand(self.model.index(0, 0)) #expand first level only
89