1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 from python_qt_binding import QtGui
34 from python_qt_binding import QtCore
35
36 from rosgraph.names import is_legal_name
40 '''
41 A class to display the HTML text in QTreeView.
42 '''
43
44 - def paint(self, painter, option, index):
45 '''
46 Use the QTextDokument to represent the HTML text.
47 @see: U{http://www.pyside.org/docs/pyside/PySide/QtGui/QAbstractItemDelegate.html#PySide.QtGui.QAbstractItemDelegate}
48 '''
49 options = QtGui.QStyleOptionViewItemV4(option)
50 self.initStyleOption(options, index)
51
52 style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
53
54 doc = QtGui.QTextDocument()
55 doc.setHtml(self.toHTML(options.text))
56 doc.setTextWidth(option.rect.width())
57
58 options.text = ''
59 style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter);
60
61 ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
62
63
64
65
66
67 textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options, options.widget)
68 painter.save()
69 painter.translate(QtCore.QPoint(textRect.topLeft().x(), textRect.topLeft().y()-3))
70 painter.setClipRect(textRect.translated(-textRect.topLeft()))
71 doc.documentLayout().draw(painter, ctx)
72
73 painter.restore()
74
76 '''
77 Determines and returns the size of the text after the format.
78 @see: U{http://www.pyside.org/docs/pyside/PySide/QtGui/QAbstractItemDelegate.html#PySide.QtGui.QAbstractItemDelegate}
79 '''
80 options = QtGui.QStyleOptionViewItemV4(option)
81 self.initStyleOption(options,index)
82
83 doc = QtGui.QTextDocument()
84 doc.setHtml(options.text)
85 doc.setTextWidth(options.rect.width())
86 metric = QtGui.QFontMetrics(doc.defaultFont())
87 return QtCore.QSize(doc.idealWidth(), metric.height()+4)
88
89 @classmethod
91 '''
92 Creates a HTML representation of the topic name.
93 @param topic_name: the topic name
94 @type topic_name: C{str}
95 @return: the HTML representation of the topic name
96 @rtype: C{str}
97 '''
98 if text.rfind('@') > 0:
99 name, sep, host = text.rpartition('@')
100 result = ''
101 if sep:
102 result = '<div>%s<span style="color:gray;">%s%s</span></div>'%(name, sep, host)
103 else:
104 result = text
105 elif text.find('{') > -1:
106 text = text.strip('{}')
107 ns, sep, name = text.rpartition('/')
108 result = ''
109 if sep:
110 result = '<div><b>{</b><span style="color:gray;">%s%s</span><b>%s}</b></div>'%(ns, sep, name)
111 else:
112 result = '<div><b>{%s}</b></div>'%(name)
113 elif not is_legal_name(text):
114 ns, sep, name = text.rpartition('/')
115 result = ''
116 if sep:
117 result = '<div><span style="color:#FF6600;">%s%s<b>%s</b></span></div>'%(ns, sep, name)
118 else:
119 result = '<div><span style="color:#FF6600;">%s</span></div>'%(name)
120 else:
121 ns, sep, name = text.rpartition('/')
122 result = ''
123 if sep:
124 result = '<div><span style="color:gray;">%s%s</span><b>%s</b></div>'%(ns, sep, name)
125 else:
126 result = name
127 return result
128