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