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 QtCore
34 from python_qt_binding import QtGui
35
36 import node_manager_fkie as nm
39 '''
40 The service item stored in the service model. This class stores the service as
41 L{master_discovery_fkie.ServiceInfo}. The name of the service is represented in HTML.
42 '''
43
44 ITEM_TYPE = QtGui.QStandardItem.UserType + 37
45 NAME_ROLE = QtCore.Qt.UserRole + 1
46 TYPE_ROLE = QtCore.Qt.UserRole + 2
47 NODENAMES_ROLE = QtCore.Qt.UserRole + 3
48
49 - def __init__(self, service, parent=None):
50 '''
51 Initialize the service item.
52 @param service: the service object to view
53 @type service: L{master_discovery_fkie.ServiceInfo}
54 '''
55 QtGui.QStandardItem.__init__(self, service.name)
56 self.service = service
57 '''@ivar: service info as L{master_discovery_fkie.ServiceInfo}.'''
58
60 '''
61 Updates the view of the service on changes.
62 @param parent: the item containing this item
63 @type parent: L{PySide.QtGui.QStandardItem}
64 '''
65 if not parent is None:
66
67 child = parent.child(self.row(), 1)
68 if not child is None:
69 self.updateTypeView(self.service, child)
70
73
74 @classmethod
76 '''
77 Creates the list of the items from service. This list is used for the
78 visualization of service data as a table row.
79 @param service: the service data
80 @type service: L{master_discovery_fkie.ServiceInfo}
81 @return: the list for the representation as a row
82 @rtype: C{[L{ServiceItem} or L{PySide.QtGui.QStandardItem}, ...]}
83 '''
84 items = []
85 item = ServiceItem(service)
86
87
88 items.append(item)
89 typeItem = QtGui.QStandardItem()
90 ServiceItem.updateTypeView(service, typeItem)
91 items.append(typeItem)
92 return items
93
94 @classmethod
96 '''
97 Updates the representation of the column contains the type of the service.
98 @param service: the service data
99 @type service: L{master_discovery_fkie.ServiceInfo}
100 @param item: corresponding item in the model
101 @type item: L{ServiceItem}
102 '''
103 try:
104 if service.isLocal and service.type:
105 service_class = service.get_service_class(nm.is_local(nm.nameres().getHostname(service.uri)))
106 item.setText(service_class._type)
107 elif service.type:
108 item.setText(service.type)
109 else:
110 item.setText('unknown type')
111
112
113
114
115
116
117
118
119
120
121 item.setToolTip('')
122 except:
123
124
125 if not service.isLocal:
126 tooltip = ''.join(['<h4>', 'Service type is not available due to he running on another host.', '</h4>'])
127 item.setToolTip(''.join(['<div>', tooltip, '</div>']))
128
129
130 - def data(self, role):
131 if role == self.NAME_ROLE:
132 return self.service.name
133 elif role == self.TYPE_ROLE:
134 return str(self.service.get_service_class(False))
135 elif role == self.NODENAMES_ROLE:
136 return str(self.service.serviceProvider)
137 else:
138 return QtGui.QStandardItem.data(self, role)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 -class ServiceModel(QtGui.QStandardItemModel):
162 '''
163 The model to manage the list with services in ROS network.
164 '''
165 header = [('Name', 300),
166 ('Type', -1)]
167 '''@ivar: the list with columns C{[(name, width), ...]}'''
168
170 '''
171 Creates a new list model.
172 '''
173 QtGui.QStandardItemModel.__init__(self)
174 self.setColumnCount(len(ServiceModel.header))
175 self.setHorizontalHeaderLabels([label for label, width in ServiceModel.header])
176 self.pyqt_workaround = dict()
177
179 '''
180 @param index: parent of the list
181 @type index: L{PySide.QtCore.QModelIndex}
182 @return: Flag or the requestet item
183 @rtype: L{PySide.QtCore.Qt.ItemFlag}
184 @see: U{http://www.pyside.org/docs/pyside-1.0.1/PySide/QtCore/Qt.html}
185 '''
186 if not index.isValid():
187 return QtCore.Qt.NoItemFlags
188 return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
189
190 - def updateModelData(self, services, added_srvs, updated_srvs, removed_srvs):
191 '''
192 Updates the service list model. New services will be inserted in sorting
193 order. Not available services removed from the model.
194 @param services: The dictionary with services
195 @type services: C{dict(service name : L{master_discovery_fkie.ServiceInfo})}
196 @param added_srvs: the list of new services in the :service: list
197 @type added_srvs: list or set
198 @param updated_srvs: the list of updated services in the :service: list
199 @type updated_srvs: list or set
200 @param removed_srvs: the list of removed services in the :service: list
201 @type removed_srvs: list or set
202 '''
203 root = self.invisibleRootItem()
204
205
206
207
208 for i in reversed(range(root.rowCount())):
209 serviceItem = root.child(i)
210 if serviceItem.service.name in removed_srvs:
211 root.removeRow(i)
212 try:
213 del self.pyqt_workaround[serviceItem.service.name]
214 except:
215 pass
216 elif serviceItem.service.name in updated_srvs:
217 serviceItem.updateServiceView(root)
218
219 for srv_name in added_srvs:
220 try:
221 doAddItem = True
222 service = services[srv_name]
223 for i in range(root.rowCount()):
224 if not srv_name in updated_srvs:
225 serviceItem = root.child(i)
226 if cmp(serviceItem.service.name, service.name) > 0:
227 service_item_row = ServiceItem.getItemList(service)
228 root.insertRow(i, service_item_row)
229 self.pyqt_workaround[srv_name] = service_item_row
230 doAddItem = False
231 break
232 else:
233 doAddItem = False
234 break
235 if doAddItem:
236 service_item_row = ServiceItem.getItemList(service)
237 root.appendRow(service_item_row)
238 self.pyqt_workaround[srv_name] = service_item_row
239 except:
240 pass
241
242
243
244
245
246
248 '''
249 Returns for given services the list of QModelIndex in this model.
250 :param services: the list of service names
251 :type services: [str, ...]
252 :return: the list of QModelIndex
253 :rtype: [QtCore.QModelIndex, ...]
254 '''
255 result = []
256 root = self.invisibleRootItem()
257 for i in range(root.rowCount()):
258 serviceItem = root.child(i)
259 if serviceItem.service.name in services:
260 result.append(self.index(i, 0))
261 return result
262