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