Advertisement
Bruce314

RichTextDelegate.cpp

Jun 5th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include "RichTextDelegate.hpp"
  2.  
  3. #include <QPainter>
  4. #include <QTextDocument>
  5.  
  6. RichTextDelegate::RichTextDelegate(QObject *parent)
  7.     : QStyledItemDelegate(parent)
  8. {
  9.     m_pFormatter = new QTextDocument(this);
  10. }
  11.  
  12. RichTextDelegate::~RichTextDelegate()
  13. {
  14.  
  15. }
  16.  
  17. void RichTextDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
  18. {
  19.     if (option.state & QStyle::State_Selected)
  20.     {
  21.         painter->fillRect(option.rect, option.palette.highlight());
  22.     }
  23.  
  24.     painter->save();
  25.  
  26.     int alignemtn=index.data(Qt::TextAlignmentRole).toInt();
  27.  
  28.     m_pFormatter->setHtml(index.data().toString());
  29.     QFont f(index.data(Qt::FontRole).value<QFont>());
  30.    
  31.     m_pFormatter->setDefaultFont(f);
  32.     QSizeF actualSize (m_pFormatter->size());
  33.  
  34.     if ((alignemtn & Qt::AlignVCenter) != 0)
  35.     {
  36.         painter->translate(0,(option.rect.height()-actualSize.height())/2);
  37.     } else if ((alignemtn & Qt::AlignBottom) != 0)
  38.     {
  39.         painter->translate(0,(option.rect.height()-actualSize.height()));
  40.     }
  41.     if ((alignemtn & Qt::AlignRight) !=0)
  42.     {
  43.         painter->translate(option.rect.width()-actualSize.width(),0);
  44.     } else if ((alignemtn & Qt::AlignHCenter) !=0)
  45.     {
  46.         painter->translate((option.rect.width()-actualSize.width())/2,0);
  47.     }
  48.  
  49.  
  50.     QPixmap pixmap(option.rect.size());
  51.     pixmap.fill(Qt::transparent);
  52.     QPainter p(&pixmap);
  53.  
  54.     m_pFormatter->drawContents(&p);
  55.  
  56.     painter->drawPixmap(option.rect, pixmap);
  57.  
  58.     painter->restore();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement