Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 import os
00034 
00035 import roslib;roslib.load_manifest('rqt_robot_dashboard')
00036 import rospy
00037 
00038 from python_qt_binding.QtGui import QIcon, QImage, QMessageBox, QPainter, QPixmap
00039 from python_qt_binding.QtSvg import QSvgRenderer
00040 
00041 
00042 def dashinfo(msg, obj, title='Info'):
00043     """
00044     Logs a message with ``rospy.loginfo`` and displays a ``QMessageBox`` to the user
00045 
00046     :param msg: Message to display.
00047     :type msg: str
00048     :param obj: Parent object for the ``QMessageBox``
00049     :type obj: QObject
00050     :param title: An optional title for the `QMessageBox``
00051     :type title: str
00052     """
00053     rospy.loginfo(msg)
00054 
00055     box = QMessageBox()
00056     box.setText(msg)
00057     box.setWindowTitle(title)
00058     box.show()
00059 
00060     obj._message_box = box
00061 
00062 
00063 def dashwarn(msg, obj, title='Warning'):
00064     """
00065     Logs a message with ``rospy.logwarn`` and displays a ``QMessageBox`` to the user
00066 
00067     :param msg: Message to display.
00068     :type msg: str
00069     :param obj: Parent object for the ``QMessageBox``
00070     :type obj: QObject
00071     :param title: An optional title for the `QMessageBox``
00072     :type title: str
00073     """
00074     rospy.logwarn(msg)
00075 
00076     box = QMessageBox()
00077     box.setText(msg)
00078     box.setWindowTitle(title)
00079     box.show()
00080 
00081     obj._message_box = box
00082 
00083 
00084 def dasherr(msg, obj, title='Error'):
00085     """
00086     Logs a message with ``rospy.logerr`` and displays a ``QMessageBox`` to the user
00087 
00088     :param msg: Message to display.
00089     :type msg: str
00090     :param obj: Parent object for the ``QMessageBox``
00091     :type obj: QObject
00092     :param title: An optional title for the `QMessageBox``
00093     :type title: str
00094     """
00095     rospy.logerr(msg)
00096 
00097     box = QMessageBox()
00098     box.setText(msg)
00099     box.setWindowTitle(title)
00100     box.show()
00101 
00102     obj._message_box = box
00103 
00104 
00105 class IconHelper(object):
00106     """
00107     Helper class to easily access images and build QIcons out of lists of file names
00108     """
00109     def __init__(self, paths=None):
00110         self._image_paths = paths if paths else []
00111 
00112     def add_image_path(self, path):
00113         """
00114         Paths added will be searched for images by the _find_image function
00115         Paths will be searched in revearse order by add time
00116         The last path to be searched is always rqt_robot_dashboard/images
00117         Subdirectories are not recursively searched
00118 
00119         :param path: The path to add to the image paths list
00120         :type path: str
00121         """
00122         self._image_paths = [path] + self._image_paths
00123 
00124     def make_icon(self, image_list, mode=QIcon.Normal, state=QIcon.On):
00125         """
00126         Helper function to create QIcons from lists of image files
00127         Warning: svg files interleaved with other files will not render correctly
00128 
00129         :param image_list: list of image paths to layer into an icon.
00130         :type image: list of str
00131         :param mode: The mode of the QIcon to be created.
00132         :type mode: int
00133         :param state: the state of the QIcon to be created.
00134         :type state: int
00135         """
00136         if type(image_list) is not list:
00137             image_list = [image_list]
00138         if len(image_list) <= 0:
00139             raise TypeError('The list of images is empty.')
00140 
00141         num_svg = 0
00142         for item in image_list:
00143             if item[-4:].lower() == '.svg':
00144                 num_svg = num_svg + 1
00145 
00146         if num_svg != len(image_list):
00147             
00148             icon_pixmap = QPixmap()
00149             icon_pixmap.load(image_list[0])
00150             painter = QPainter(icon_pixmap)
00151             for item in image_list[1:]:
00152                 painter.drawPixmap(0, 0, QPixmap(item))
00153             icon = QIcon()
00154             icon.addPixmap(icon_pixmap, mode, state)
00155             painter.end()
00156             return icon
00157         else:
00158             
00159             renderer = QSvgRenderer(image_list[0])
00160             icon_image = QImage(renderer.defaultSize(), QImage.Format_ARGB32)
00161             icon_image.fill(0)
00162             painter = QPainter(icon_image)
00163             renderer.render(painter)
00164             if len(image_list) > 1:
00165                 for item in image_list[1:]:
00166                     renderer.load(item)
00167                     renderer.render(painter)
00168             painter.end()
00169             
00170             icon_pixmap = QPixmap()
00171             icon_pixmap.convertFromImage(icon_image)
00172             icon = QIcon(icon_pixmap)
00173             return icon
00174 
00175     def find_image(self, path):
00176         """
00177         Convenience function to help with finding images.
00178         Path can either be specified as absolute paths or relative to any path in ``_image_paths``
00179 
00180         :param path: The path or name of the image.
00181         :type path: str
00182         """
00183         if os.path.exists(path):
00184             return path
00185         for image_path in self._image_paths:
00186             if os.path.exists(os.path.join(image_path, path)):
00187                 return os.path.join(image_path, path)
00188             elif '.' in path and os.path.exists(os.path.join(image_path, 'nonsvg', path)):
00189                 return os.path.join(image_path, 'nonsvg', path)
00190         return os.path.join(self._image_paths[-1], 'ic-missing-icon.svg')
00191 
00192     def build_icon(self, image_name_list, mode=QIcon.Normal, state=QIcon.On):
00193         """
00194         Convenience function to create an icon from a list of file names
00195 
00196         :param image_name_list: List of file image names to make into an icon
00197         :type image_name_list: list of str
00198         :param mode: The mode of the QIcon to be created.
00199         :type mode: int
00200         :param state: the state of the QIcon to be created.
00201         :type state: int
00202         """
00203         found_list = []
00204         for name in image_name_list:
00205             found_list.append(self.find_image(name))
00206         return self.make_icon(found_list, mode, state)