message_tree_model.py
Go to the documentation of this file.
1 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of the TU Darmstadt nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 import rospy
32 from python_qt_binding.QtGui import QStandardItem, QStandardItemModel
33 from .data_items import ReadonlyItem
34 
35 
36 class MessageTreeModel(QStandardItemModel):
37 
38  def __init__(self, parent=None):
39  # FIXME: why is this not working? should be the same as the following line...
40  # super(MessageTreeModel, self).__init__(parent)
41  QStandardItemModel.__init__(self, parent)
42 
43  def add_message(self, message_instance, message_name='', message_type='', message_path=''):
44  if message_instance is None:
45  return
47  self, message_instance, message_name, message_type, message_path)
48 
49  def _get_toplevel_items(self, index_list):
50  items = [self.itemFromIndex(index) for index in index_list]
51  uniqueItems = {}
52  for item in items:
53  while item.parent() is not None:
54  item = item.parent()
55  if item.row() not in uniqueItems:
56  uniqueItems[item.row()] = item
57  return uniqueItems.values()
58 
59  def _get_data_items_for_path(self, slot_name, slot_type_name, slot_path, **kwargs):
60  return (QStandardItem(slot_name), QStandardItem(slot_type_name), QStandardItem(slot_path))
61 
62  def _recursive_create_items(self, parent, slot, slot_name, slot_type_name, slot_path, **kwargs):
63  row = []
64  for item in self._get_data_items_for_path(slot_name, slot_type_name, slot_path, **kwargs):
65  item._path = slot_path
66  item._user_data = kwargs.get('user_data', None)
67  row.append(item)
68 
69  is_leaf_node = False
70  if hasattr(slot, '__slots__') and hasattr(slot, '_slot_types'):
71  for child_slot_name, child_slot_type in zip(slot.__slots__, slot._slot_types):
72  child_slot_path = slot_path + '/' + child_slot_name
73  child_slot = getattr(slot, child_slot_name)
75  row[0], child_slot, child_slot_name, child_slot_type, child_slot_path, **kwargs)
76 
77  elif type(slot) in (list, tuple) and (len(slot) > 0):
78  child_slot_type = slot_type_name[:slot_type_name.find('[')]
79  for index, child_slot in enumerate(slot):
80  child_slot_name = '[%d]' % index
81  child_slot_path = slot_path + child_slot_name
83  row[0], child_slot, child_slot_name, child_slot_type, child_slot_path, **kwargs)
84 
85  else:
86  is_leaf_node = True
87 
88  if parent is self and kwargs.get('top_level_row_number', None) is not None:
89  parent.insertRow(kwargs['top_level_row_number'], row)
90  else:
91  parent.appendRow(row)
92 
93  return (row, is_leaf_node)
94 
95  '''
96  NOTE: I (Isaac Saito) suspect that this function might have same/similar
97  functionality with _recursive_create_items.
98 
99  @summary: Evaluate current node and the previous node on the same depth.
100  If the name of both nodes at the same depth is the same,
101  current name is added to the previous node.
102  If not, the current name gets added to the parent node.
103  At the end, this function calls itself recursively going down
104  1 level deeper.
105  @param stditem_parent: QStandardItem.
106  @param names_on_branch: List of strings each of which
107  represents the name of the node.
108  Ex. If you have a hierarchy that looks like:
109 
110  /top_node/sub_node/subsub_node
111 
112  then this list would look like:
113 
114  [ top_node, sub_node, subsub_node ]
115  @author: Isaac Saito
116  '''
117  @staticmethod
118  def _build_tree_recursive(stditem_parent, names_on_branch):
119  name_curr = names_on_branch.pop(0)
120  stditem_curr = ReadonlyItem(name_curr)
121 
122  row_index_parent = stditem_parent.rowCount() - 1
123  # item at the bottom is your most recent node.
124 
125  name_prev = ''
126  stditem_prev = None
127  if not stditem_parent.child(row_index_parent) == None:
128  stditem_prev = stditem_parent.child(row_index_parent)
129  name_prev = stditem_prev.text()
130 
131  stditem = None
132  if not name_prev == name_curr:
133  stditem_parent.appendRow(stditem_curr)
134  stditem = stditem_curr
135  else:
136  stditem = stditem_prev
137 
138  rospy.logdebug('add_tree_node 1 name_curr=%s ' +
139  '\n\t\t\t\t\tname_prev=%s row_index_parent=%d', name_curr, name_prev, row_index_parent)
140  if (0 < len(names_on_branch)):
141  MessageTreeModel._build_tree_recursive(stditem, names_on_branch)
def _get_data_items_for_path(self, slot_name, slot_type_name, slot_path, kwargs)
def add_message(self, message_instance, message_name='', message_type='', message_path='')
def _recursive_create_items(self, parent, slot, slot_name, slot_type_name, slot_path, kwargs)
def _build_tree_recursive(stditem_parent, names_on_branch)


rqt_py_common
Author(s): Dorian Scholz, Isaac Saito, Dirk Thomas
autogenerated on Mon Mar 22 2021 02:13:26