Package Camelot :: Package camelot :: Package view :: Package controls :: Package delegates :: Module richtextdelegate
[frames] | no frames]

Source Code for Module Camelot.camelot.view.controls.delegates.richtextdelegate

 1  from PyQt4 import QtGui, QtCore 
 2  from PyQt4.QtCore import Qt 
 3  
 
 4  from customdelegate import CustomDelegate, DocumentationMetaclass 
 5  from camelot.view.controls import editors 
 6  
 
7 -class RichTextDelegate(CustomDelegate):
8 """ 9 """ 10 11 __metaclass__ = DocumentationMetaclass 12 13 editor = editors.RichTextEditor 14
15 - def __init__(self, parent=None, editable=True, **kwargs):
16 CustomDelegate.__init__(self, parent, editable) 17 self.editable = editable 18 self._height = self._height * 10 19 self._width = self._width * 3
20
21 - def paint(self, painter, option, index):
22 painter.save() 23 self.drawBackground(painter, option, index) 24 unstrippedText = unicode(index.model().data(index, Qt.EditRole).toString()) 25 26 background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole)) 27 28 if not unstrippedText: 29 text = '' 30 else: 31 32 from HTMLParser import HTMLParser 33 34 string = [] 35 36 class HtmlToTextParser(HTMLParser): 37 def handle_data(self, data): 38 string.append(data.replace('\n',''))
39 40 parser = HtmlToTextParser() 41 parser.feed(unstrippedText) 42 43 text = (' '.join(string))[:256] 44 45 rect = option.rect 46 rect = QtCore.QRect(rect.left(), rect.top(), rect.width(), rect.height()) 47 48 if( option.state & QtGui.QStyle.State_Selected ): 49 painter.fillRect(option.rect, option.palette.highlight()) 50 fontColor = QtGui.QColor() 51 if self.editable: 52 Color = option.palette.highlightedText().color() 53 fontColor.setRgb(Color.red(), Color.green(), Color.blue()) 54 else: 55 fontColor.setRgb(130,130,130) 56 else: 57 if self.editable: 58 painter.fillRect(option.rect, background_color) 59 fontColor = QtGui.QColor() 60 fontColor.setRgb(0,0,0) 61 else: 62 painter.fillRect(option.rect, option.palette.window()) 63 fontColor = QtGui.QColor() 64 fontColor.setRgb(130,130,130) 65 66 67 painter.setPen(fontColor.toRgb()) 68 rect = QtCore.QRect(option.rect.left(), 69 option.rect.top(), 70 option.rect.width(), 71 option.rect.height()) 72 painter.drawText(rect.x() + 2, 73 rect.y(), 74 rect.width() - 4, 75 rect.height(), 76 Qt.AlignVCenter | Qt.AlignLeft, 77 text) 78 painter.restore() 79