Package node_manager_fkie :: Module html_delegate
[frames] | no frames]

Source Code for Module node_manager_fkie.html_delegate

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  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 Fraunhofer 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  from python_qt_binding import QtGui 
 34  from python_qt_binding import QtCore 
 35   
 36  import rospy 
 37  from rosgraph.names import is_legal_name 
38 39 40 -class HTMLDelegate(QtGui.QStyledItemDelegate):
41 ''' 42 A class to display the HTML text in QTreeView. 43 ''' 44
45 - def paint(self, painter, option, index):
46 ''' 47 Use the QTextDokument to represent the HTML text. 48 @see: U{http://www.pyside.org/docs/pyside/PySide/QtGui/QAbstractItemDelegate.html#PySide.QtGui.QAbstractItemDelegate} 49 ''' 50 options = QtGui.QStyleOptionViewItemV4(option) 51 self.initStyleOption(options, index) 52 53 style = QtGui.QApplication.style() if options.widget is None else options.widget.style() 54 55 doc = QtGui.QTextDocument() 56 doc.setHtml(self.toHTML(options.text)) 57 doc.setTextWidth(option.rect.width()) 58 59 options.text = '' 60 style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter); 61 62 ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() 63 64 #Highlighting text if item is selected 65 #if (optionV4.state and QStyle::State_Selected): 66 # ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)); 67 68 textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options, options.widget) 69 painter.save() 70 painter.translate(QtCore.QPoint(textRect.topLeft().x(), textRect.topLeft().y()-3)) 71 painter.setClipRect(textRect.translated(-textRect.topLeft())) 72 doc.documentLayout().draw(painter, ctx) 73 74 painter.restore()
75
76 - def sizeHint(self, option, index):
77 ''' 78 Determines and returns the size of the text after the format. 79 @see: U{http://www.pyside.org/docs/pyside/PySide/QtGui/QAbstractItemDelegate.html#PySide.QtGui.QAbstractItemDelegate} 80 ''' 81 options = QtGui.QStyleOptionViewItemV4(option) 82 self.initStyleOption(options,index) 83 84 doc = QtGui.QTextDocument() 85 doc.setHtml(options.text) 86 doc.setTextWidth(options.rect.width()) 87 metric = QtGui.QFontMetrics(doc.defaultFont()) 88 return QtCore.QSize(doc.idealWidth(), metric.height()+4)
89 90 @classmethod
91 - def toHTML(cls, text):
92 ''' 93 Creates a HTML representation of the topic name. 94 @param topic_name: the topic name 95 @type topic_name: C{str} 96 @return: the HTML representation of the topic name 97 @rtype: C{str} 98 ''' 99 if text.rfind('@') > 0: # handle host names 100 name, sep, host = text.rpartition('@') 101 result = '' 102 if sep: 103 result = ''.join(['<div>', name, '<span style="color:gray;">', sep, host, '</span></div>']) 104 else: 105 result = group_name 106 elif text.find('{') > -1: # handle group names 107 text = text.strip('{}') 108 ns, sep, name = text.rpartition('/') 109 result = '' 110 if sep: 111 result = ''.join(['<div>', '<b>{</b><span style="color:gray;">', ns, sep, '</span><b>', name, '}</b></div>']) 112 else: 113 result = ''.join(['<div>', '<b>{', name, '}</b></div>']) 114 elif not is_legal_name(text): # handle all invalid names (used space in the name) 115 ns, sep, name = text.rpartition('/') 116 result = '' 117 if sep: 118 result = ''.join(['<div>', '<span style="color:#CC0000;">', str(ns), sep, '<b>', name, '</b></span></div>']) 119 else: 120 result = ''.join(['<div>', '<span style="color:#CC0000;">', name, '</span></div>']) 121 else: # handle all ROS names 122 ns, sep, name = text.rpartition('/') 123 result = '' 124 if sep: 125 result = ''.join(['<div>', '<span style="color:gray;">', str(ns), sep, '</span><b>', name, '</b></div>']) 126 else: 127 result = name 128 return result
129