PageRenderTime 52ms CodeModel.GetById 47ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/hgqt/htmldelegate.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 64 lines | 47 code | 8 blank | 9 comment | 10 complexity | 5bc09b7b058b9d17f6b913763f183149 MD5 | raw file
Possible License(s): GPL-2.0
  1. # htmldelegate.py - HTML QStyledItemDelegate
  2. #
  3. # Copyright 2010 Steve Borho <steve@borho.org>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2, incorporated herein by reference.
  7. from PyQt4.QtCore import *
  8. from PyQt4.QtGui import *
  9. from mercurial import error
  10. from tortoisehg.hgqt import qtlib
  11. class HTMLDelegate(QStyledItemDelegate):
  12. def __init__(self, parent=0, cols=None):
  13. QStyledItemDelegate.__init__(self, parent)
  14. self.cols = cols
  15. def paint(self, painter, option, index):
  16. if self.cols and index.column() not in self.cols:
  17. return QStyledItemDelegate.paint(self, painter, option, index)
  18. try:
  19. text = index.model().data(index, Qt.DisplayRole).toString()
  20. except error.RevlogError, e:
  21. # this can happen if revlog is being truncated while we read it
  22. text = _('?? Error: %s ??') % hglib.tounicode(str(e))
  23. # draw selection
  24. option = QStyleOptionViewItemV4(option)
  25. self.parent().style().drawControl(QStyle.CE_ItemViewItem, option, painter)
  26. # draw text
  27. doc = QTextDocument(defaultFont=option.font)
  28. painter.save()
  29. doc.setHtml(text)
  30. painter.setClipRect(option.rect)
  31. painter.translate(QPointF(
  32. option.rect.left(),
  33. option.rect.top() + (option.rect.height() - doc.size().height()) / 2))
  34. ctx = QAbstractTextDocumentLayout.PaintContext()
  35. ctx.palette = option.palette
  36. if option.state & QStyle.State_Selected:
  37. if option.state & QStyle.State_Active:
  38. ctx.palette.setCurrentColorGroup(QPalette.Active)
  39. else:
  40. ctx.palette.setCurrentColorGroup(QPalette.Inactive)
  41. ctx.palette.setBrush(QPalette.Text, ctx.palette.highlightedText())
  42. elif not option.state & QStyle.State_Enabled:
  43. ctx.palette.setCurrentColorGroup(QPalette.Disabled)
  44. doc.documentLayout().draw(painter, ctx)
  45. painter.restore()
  46. def sizeHint(self, option, index):
  47. try:
  48. text = index.model().data(index, Qt.DisplayRole).toString()
  49. except error.RevlogError, e:
  50. text = _('?? Error: %s ??') % hglib.tounicode(str(e))
  51. doc = QTextDocument()
  52. doc.setDefaultStyleSheet(qtlib.thgstylesheet)
  53. doc.setDefaultFont(option.font)
  54. doc.setHtml(text)
  55. doc.setTextWidth(option.rect.width())
  56. return QSize(doc.idealWidth() + 5, doc.size().height())