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