Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

custom delegate

By: a guest on Jul 5th, 2010  |  syntax: C++  |  size: 3.62 KB  |  views: 229  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "rasplistdeleagte.h"
  2. #include <QTime>
  3. #include <QPainter>
  4. #include <QDebug>
  5.  
  6. RaspListDeleagte::RaspListDeleagte(QObject *parent, short redLine , short yellowLine ) :
  7.     QStyledItemDelegate(parent)
  8.     ,m_redLine(redLine)
  9.     ,m_yellowLine(yellowLine)
  10. {
  11. }
  12.  
  13.  
  14. inline QFont getTimeFont(const QStyleOptionViewItem &option)
  15. {
  16.     QFont timeFont = (option.font.pointSize()>0)?option.font:QFont();
  17.     timeFont.setPointSize(timeFont.pointSize()+3);
  18.     timeFont.setBold(true);
  19.     return timeFont;
  20. }
  21.  
  22.  
  23. inline QFont getNoteFont(const QStyleOptionViewItem &option)
  24. {
  25.     QFont noteFont = (option.font.pointSize()>0)?option.font:QFont();
  26.     noteFont.setPointSize(noteFont.pointSize()-3);
  27.     noteFont.setItalic(true);
  28.     return noteFont;
  29. }
  30.  
  31.  
  32. void RaspListDeleagte::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const {
  33.     if (!index.isValid()) {
  34.         QStyledItemDelegate::paint(painter, option, index);
  35.     } else {
  36.         //Draw background regarding current style;
  37.         if (option.state & QStyle::State_Selected)
  38.         {
  39.             painter->fillRect(option.rect, option.palette.highlight());
  40.         } else {
  41.             //odd and even items should be painted different
  42.             QBrush brush = (index.row()%2)?option.palette.base():option.palette.alternateBase();
  43.             painter->fillRect(option.rect, brush);
  44.         }
  45.  
  46.         //got time and note from model;
  47.         QTime _time = index.data().toTime();
  48.         QString time = _time.toString("hh:mm");
  49.         int delta = QTime::currentTime().secsTo(_time);
  50.         QString timeLeft = (delta>0) ?
  51.                        (QTime(0,0).addSecs(delta)).toString("hh:mm"):
  52.                        QString("--:--");
  53.         QString note = index.data(Qt::UserRole+1).toString();
  54.         //Draw the time with big letters, and small note under it.
  55.         painter->save();
  56.  
  57.         //setup fonts
  58.         QFont timeFont = getTimeFont(option);
  59.         QFont noteFont = getNoteFont(option);
  60.  
  61.         QFontMetrics fmTF(timeFont);
  62.         QFontMetrics fmNF(noteFont);
  63.  
  64.         //Draw the time left
  65.         if (delta<0)
  66.             painter->setPen(Qt::lightGray);
  67.         else if (delta<(60*m_redLine))
  68.             painter->setPen(Qt::red);
  69.         else if (delta<(60*m_yellowLine))
  70.             painter->setPen(Qt::darkYellow);
  71.         else
  72.             painter->setPen(Qt::green);
  73.  
  74.  
  75.         painter->setFont(timeFont);
  76.         painter->drawText(option.rect.x()+2,option.rect.y()+fmTF.height(),timeLeft);
  77.  
  78.         //Draw the time
  79.         painter->setPen(Qt::blue);
  80.         painter->setFont(timeFont);
  81.         painter->drawText(option.rect.x()+2+fmTF.width(timeLeft+" "),option.rect.y()+fmTF.height(),time);
  82.  
  83.         //Draw the note
  84.         painter->setPen(option.palette.text().color());
  85.         painter->setFont(noteFont);
  86.         //        painter->drawText(option.rect.x()+2, option.rect.y()+fmTF.lineSpacing()+fmNF.lineSpacing(), fmNF.elidedText(note,Qt::ElideRight,option.rect.width()));
  87.         painter->drawText(option.rect.adjusted(fmTF.width(timeLeft+" ")+fmTF.width(time)+4,0,0,0), note);
  88.  
  89.         painter->restore();
  90.     }
  91. }
  92.  
  93. QSize RaspListDeleagte::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
  94. {
  95.     if (!index.isValid()) return QStyledItemDelegate::sizeHint(option,index);
  96.  
  97.     QSize result;
  98.     //setup fonts
  99.     QFontMetrics fmTF(getTimeFont(option));
  100.     result.setHeight(fmTF.lineSpacing()+fmTF.leading()*2+4);
  101.     result.setWidth(option.rect.width());
  102.     return(QSize(option.rect.width(),20));
  103.  
  104.     qDebug()<<option.decorationSize;
  105.     return result;
  106. }