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
34 from python_qt_binding import QtGui
35 from python_qt_binding import QtCore
36
38 '''
39 Enabled the syntax highlightning for the ROS launch files.
40 '''
41
42 LAUNCH_LAUNCH_CHILDS = ['group', 'node', 'test', 'env', 'remap', 'rosparam', 'param', 'machine', 'include', 'arg']
43 LAUNCH_LAUNCH_ATTR = {'deprecated=' : '"message"'}
44 LAUNCH_GROUP_CHILDS = ['node', 'test', 'env', 'remap', 'rosparam', 'param', 'machine', 'include', 'arg']
45 LAUNCH_GROUP_ATTR = {'ns=' : '"foo"',
46 'clear_params=' : '"true|false"'
47 }
48 LAUNCH_MACHINE_CHILDS = ['env']
49 LAUNCH_MACHINE_ATTR = {'name=' : '"machine-name"',
50 'address=' : '"blah.willowgarage.com"',
51 'env-loader=' : '"/opt/ros/fuerte/env.sh"',
52 'default=' : '"true|false|never"',
53 'user=' : '"username"',
54 'password=' : '"passwhat"',
55 'timeout=' : '"10.0"'
56 }
57 LAUNCH_NODE_CHILDS = ['env', 'remap', 'rosparam', 'param']
58 LAUNCH_NODE_ATTR = {'pkg=' : '"mypackage"',
59 'type=' : '"nodetype"',
60 'name=' : '"nodename"',
61 'args=' : '"arg1"',
62 'machine=' : '"machine-name"',
63 'respawn=' : '"true"',
64 'required=' : '"true"',
65 'ns=' : '"foo"',
66 'clear_params=' : '"true|false"',
67 'output=' : '"log|screen"',
68 'cwd=' : '"ROS_HOME|node"',
69 'launch-prefix=': '"prefix arguments"'
70 }
71 LAUNCH_INCLUDE_CHILDS = ['env', 'arg']
72 LAUNCH_INCLUDE_ATTR = {'file=' : '"$(find pkg-name)/path/filename.xml"',
73 'ns=' : '"foo"',
74 'clear_params=' : '"true|false"'
75 }
76
77 LAUNCH_REMAP_ATTR = {'from=' : '"originalname"',
78 'to=' : '"newname"'
79 }
80 LAUNCH_ENV_ATTR = {'name=' : '"name"',
81 'value=' : '"value"'
82 }
83 LAUNCH_PARAM_ATTR = {'name=' : '"namespace/name"',
84 'value=' : '"value"',
85 'type=' : '"str|int|double|bool"',
86 'textfile=' : '"$(find pkg-name)/path/file.txt"',
87 'binfile=' : '"$(find pkg-name)/path/file"',
88 'command=' : '"$(find pkg-name)/exe \'$(find pkg-name)/arg.txt\'"'
89 }
90
91 LAUNCH_ROSPARAM_ATTR = {'command=' : '"load|dump|delete"',
92 'file=' : '"$(find pkg-name)/path/foo.yaml"',
93 'param=' : '"name"',
94 'ns=' : '"foo"'
95 }
96 LAUNCH_ARG_ATTR = {'name=' : '"name"',
97 'value=' : '"bar"',
98 'default=' : '"defbar"'
99 }
100 LAUNCH_TEST_CHILDS = ['env', 'remap', 'rosparam', 'param']
101 LAUNCH_TEST_ATTR = {'pkg=' : '"mypackage"',
102 'type=' : '"nodetype"',
103 'name=' : '"nodename"',
104 'test-name=' : '"test_name"',
105 'args=' : '"arg1"',
106 'ns=' : '"foo"',
107 'clear_params=' : '"true|false"',
108 'retry=' : '"0"',
109 'cwd=' : '"ROS_HOME|node"',
110 'launch-prefix=': '"prefix arguments"',
111 'time-limit=' : '"60.0"'
112 }
113
114 LAUNCH_CHILDS = {'launch' : LAUNCH_LAUNCH_CHILDS,
115 'group' : LAUNCH_GROUP_CHILDS,
116 'machine' : LAUNCH_MACHINE_CHILDS,
117 'node' : LAUNCH_NODE_CHILDS,
118 'include' : LAUNCH_INCLUDE_CHILDS,
119 'remap' : [],
120 'env' : [],
121 'param' : [],
122 'rosparam' : [],
123 'arg' : [],
124 'test' : LAUNCH_TEST_CHILDS
125 }
126
127 LAUNCH_ATT_GLOBAL = {'if=' : '""', 'unless=' : '""'}
128 LAUNCH_LAUNCH_ATTR.update(LAUNCH_ATT_GLOBAL)
129 LAUNCH_GROUP_ATTR.update(LAUNCH_ATT_GLOBAL)
130 LAUNCH_MACHINE_ATTR.update(LAUNCH_ATT_GLOBAL)
131 LAUNCH_NODE_ATTR.update(LAUNCH_ATT_GLOBAL)
132 LAUNCH_INCLUDE_ATTR.update(LAUNCH_ATT_GLOBAL)
133 LAUNCH_REMAP_ATTR.update(LAUNCH_ATT_GLOBAL)
134 LAUNCH_ENV_ATTR.update(LAUNCH_ATT_GLOBAL)
135 LAUNCH_PARAM_ATTR.update(LAUNCH_ATT_GLOBAL)
136 LAUNCH_ROSPARAM_ATTR.update(LAUNCH_ATT_GLOBAL)
137 LAUNCH_ARG_ATTR.update(LAUNCH_ATT_GLOBAL)
138 LAUNCH_TEST_ATTR.update(LAUNCH_ATT_GLOBAL)
139
140 LAUNCH_ATTR = {'launch' : LAUNCH_LAUNCH_ATTR,
141 'group' : LAUNCH_GROUP_ATTR,
142 'machine' : LAUNCH_MACHINE_ATTR,
143 'node' : LAUNCH_NODE_ATTR,
144 'include' : LAUNCH_INCLUDE_ATTR,
145 'remap' : LAUNCH_REMAP_ATTR,
146 'env' : LAUNCH_ENV_ATTR,
147 'param' : LAUNCH_PARAM_ATTR,
148 'rosparam' : LAUNCH_ROSPARAM_ATTR,
149 'arg' : LAUNCH_ARG_ATTR,
150 'test' : LAUNCH_TEST_ATTR,
151 }
152
154 QtGui.QSyntaxHighlighter.__init__(self, parent)
155 self.rules = []
156 self.commentStart = QtCore.QRegExp("<!--")
157 self.commentEnd = QtCore.QRegExp("-->")
158 self.commentFormat = QtGui.QTextCharFormat()
159 f = QtGui.QTextCharFormat()
160 r = QtCore.QRegExp()
161 r.setMinimal(True)
162 f.setFontWeight(QtGui.QFont.Normal)
163 f.setForeground (QtCore.Qt.darkBlue)
164
165 tagList = ["\\b%s\\b"%t for t in self.LAUNCH_CHILDS.keys()]
166 for tag in tagList:
167 r.setPattern(tag)
168 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
169
170 f.setForeground(QtCore.Qt.darkGreen)
171 attrList = set(["\\b%s"%attr for v in self.LAUNCH_ATTR.values() for attr in v.keys()])
172 for attr in attrList:
173 r.setPattern(attr)
174 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
175
176 f.setForeground(QtCore.Qt.magenta)
177 r.setPattern("\".*\"")
178 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
179
180 f.setForeground(QtGui.QColor(127,64,127))
181 r.setPattern ("\\$\\(.*\\)")
182 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
183
184 f.setForeground (QtCore.Qt.lightGray)
185 r.setPattern ("<!DOCTYPE.*>")
186 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
187 r.setPattern ("<\\?xml.*\\?>")
188 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
189
190 self.commentFormat.setFontItalic(True)
191 self.commentFormat.setForeground(QtCore.Qt.darkGray)
192
193
195 for pattern, format in self.rules:
196 index = pattern.indexIn(text)
197 while index >= 0:
198 length = pattern.matchedLength()
199 self.setFormat(index, length, format)
200 index = pattern.indexIn(text, index + length)
201 self.setCurrentBlockState(0)
202 startIndex = 0
203 if self.previousBlockState() != 1:
204 startIndex = self.commentStart.indexIn(text)
205 while startIndex >= 0:
206 endIndex = self.commentEnd.indexIn(text, startIndex)
207 commentLength = 0
208 if endIndex == -1:
209 self.setCurrentBlockState(1)
210 commentLength = len(text) - startIndex
211 else:
212 commentLength = endIndex - startIndex + self.commentEnd.matchedLength()
213 self.setFormat(startIndex, commentLength, self.commentFormat)
214 startIndex = self.commentStart.indexIn(text, startIndex + commentLength)
215