status_item.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2014, Austin Hendrix
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Austin Hendrix. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Author: Austin Hendrix
00034 
00035 from python_qt_binding.QtGui import QTreeWidgetItem
00036 import util_robot_monitor as util
00037 
00038 class _StatusItem(QTreeWidgetItem):
00039     """
00040     Internal subclass of QTreeWidgetItem which adds a 'name' member to make
00041     it easier to extract the item name and create an inspector when an item
00042     is clicked
00043     """
00044     def __init__(self, name):
00045         super(_StatusItem, self).__init__()
00046         self.name = name
00047 
00048 class StatusItem(object):
00049     """
00050     A class that wraps the default QTreeWidgetItem, so that we can manipulate
00051     all of the nodes in the tree in the same way (even the invisible root node)
00052     """
00053     def __init__(self, item=None):
00054         self._children = {}
00055         self.updated = False
00056         if item is not None:
00057             self._item = item
00058         else:
00059             self._item = _StatusItem("NONAME")
00060 
00061     def update(self, status, displayname):
00062         self.updated = True
00063         self.displayname = displayname
00064         self._item.name = status.name
00065         self._item.setText(0, self.displayname)
00066         self._item.setIcon(0, util.level_to_icon(status.level))
00067         self._item.setText(1, status.message)
00068 
00069     def prune(self):
00070         stale = []
00071         for child in self._children:
00072             if not self._children[child].updated:
00073                 stale.append(child)
00074             else:
00075                 self._children[child].prune()
00076         if len(stale) > 0:
00077             for child in stale:
00078                 self._item.removeChild(self._children[child]._item)
00079                 del self._children[child]
00080         self.updated = False
00081 
00082     # functions that make this object behave like a dict
00083     def __getitem__(self, key):
00084         # if item doesn't exist, create a sane default for it
00085         if not key in self._children:
00086             self._children[key] = StatusItem()
00087             self._item.addChild(self._children[key]._item)
00088         return self._children[key]
00089 
00090     def __setitem__(self, key, value):
00091         # if item exists, remove it from the tree in addition to overwriting
00092         # it in the 'children' dict
00093         if key in self._children:
00094             self._item.removeChild(self._children[key]._item)
00095         self._children[key] = value
00096         self._item.addChild(value._item)
00097 
00098     def __contains__(self, key):
00099         return key in self._children
00100 
00101     def __iter__(self):
00102         for key in self._children:
00103             yield key


rqt_robot_monitor
Author(s): Austin Hendrix, Isaac Saito, Ze'ev Klapow, Kevin Watts, Josh Faust
autogenerated on Fri Aug 28 2015 12:50:47