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