status_item.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2014, Austin Hendrix
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Austin Hendrix. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Author: Austin Hendrix
34 
35 from python_qt_binding.QtWidgets import QTreeWidgetItem
37 
38 class _StatusItem(QTreeWidgetItem):
39  """
40  Internal subclass of QTreeWidgetItem which adds a 'name' member to make
41  it easier to extract the item name and create an inspector when an item
42  is clicked
43  """
44  def __init__(self, name):
45  super(_StatusItem, self).__init__()
46  self.name = name
47 
48 class StatusItem(object):
49  """
50  A class that wraps the default QTreeWidgetItem, so that we can manipulate
51  all of the nodes in the tree in the same way (even the invisible root node)
52  """
53  def __init__(self, item=None):
54  self._children = {}
55  self.updated = False
56  if item is not None:
57  self._item = item
58  else:
59  self._item = _StatusItem("NONAME")
60 
61  def update(self, status, displayname):
62  self.updated = True
63  self.displayname = displayname
64  self._item.name = status.name
65  self._item.setText(0, self.displayname)
66  self._item.setIcon(0, util.level_to_icon(status.level))
67  self._item.setText(1, status.message)
68 
69  def prune(self):
70  stale = []
71  for child in self._children:
72  if not self._children[child].updated:
73  stale.append(child)
74  else:
75  self._children[child].prune()
76  if len(stale) > 0:
77  for child in stale:
78  self._item.removeChild(self._children[child]._item)
79  del self._children[child]
80  self.updated = False
81 
82  # functions that make this object behave like a dict
83  def __getitem__(self, key):
84  # if item doesn't exist, create a sane default for it
85  if not key in self._children:
86  self._children[key] = StatusItem()
87  self._item.addChild(self._children[key]._item)
88  return self._children[key]
89 
90  def __setitem__(self, key, value):
91  # if item exists, remove it from the tree in addition to overwriting
92  # it in the 'children' dict
93  if key in self._children:
94  self._item.removeChild(self._children[key]._item)
95  self._children[key] = value
96  self._item.addChild(value._item)
97 
98  def __contains__(self, key):
99  return key in self._children
100 
101  def __iter__(self):
102  for key in self._children:
103  yield key
def update(self, status, displayname)
Definition: status_item.py:61


rqt_robot_monitor
Author(s): Austin Hendrix, Isaac Saito, Ze'ev Klapow, Kevin Watts, Josh Faust
autogenerated on Thu May 16 2019 02:14:30