Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "rasplistdeleagte.h"
- #include <QTime>
- #include <QPainter>
- #include <QDebug>
- RaspListDeleagte::RaspListDeleagte(QObject *parent, short redLine , short yellowLine ) :
- QStyledItemDelegate(parent)
- ,m_redLine(redLine)
- ,m_yellowLine(yellowLine)
- {
- }
- inline QFont getTimeFont(const QStyleOptionViewItem &option)
- {
- QFont timeFont = (option.font.pointSize()>0)?option.font:QFont();
- timeFont.setPointSize(timeFont.pointSize()+3);
- timeFont.setBold(true);
- return timeFont;
- }
- inline QFont getNoteFont(const QStyleOptionViewItem &option)
- {
- QFont noteFont = (option.font.pointSize()>0)?option.font:QFont();
- noteFont.setPointSize(noteFont.pointSize()-3);
- noteFont.setItalic(true);
- return noteFont;
- }
- void RaspListDeleagte::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const {
- if (!index.isValid()) {
- QStyledItemDelegate::paint(painter, option, index);
- } else {
- //Draw background regarding current style;
- if (option.state & QStyle::State_Selected)
- {
- painter->fillRect(option.rect, option.palette.highlight());
- } else {
- //odd and even items should be painted different
- QBrush brush = (index.row()%2)?option.palette.base():option.palette.alternateBase();
- painter->fillRect(option.rect, brush);
- }
- //got time and note from model;
- QTime _time = index.data().toTime();
- QString time = _time.toString("hh:mm");
- int delta = QTime::currentTime().secsTo(_time);
- QString timeLeft = (delta>0) ?
- (QTime(0,0).addSecs(delta)).toString("hh:mm"):
- QString("--:--");
- QString note = index.data(Qt::UserRole+1).toString();
- //Draw the time with big letters, and small note under it.
- painter->save();
- //setup fonts
- QFont timeFont = getTimeFont(option);
- QFont noteFont = getNoteFont(option);
- QFontMetrics fmTF(timeFont);
- QFontMetrics fmNF(noteFont);
- //Draw the time left
- if (delta<0)
- painter->setPen(Qt::lightGray);
- else if (delta<(60*m_redLine))
- painter->setPen(Qt::red);
- else if (delta<(60*m_yellowLine))
- painter->setPen(Qt::darkYellow);
- else
- painter->setPen(Qt::green);
- painter->setFont(timeFont);
- painter->drawText(option.rect.x()+2,option.rect.y()+fmTF.height(),timeLeft);
- //Draw the time
- painter->setPen(Qt::blue);
- painter->setFont(timeFont);
- painter->drawText(option.rect.x()+2+fmTF.width(timeLeft+" "),option.rect.y()+fmTF.height(),time);
- //Draw the note
- painter->setPen(option.palette.text().color());
- painter->setFont(noteFont);
- // painter->drawText(option.rect.x()+2, option.rect.y()+fmTF.lineSpacing()+fmNF.lineSpacing(), fmNF.elidedText(note,Qt::ElideRight,option.rect.width()));
- painter->drawText(option.rect.adjusted(fmTF.width(timeLeft+" ")+fmTF.width(time)+4,0,0,0), note);
- painter->restore();
- }
- }
- QSize RaspListDeleagte::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- if (!index.isValid()) return QStyledItemDelegate::sizeHint(option,index);
- QSize result;
- //setup fonts
- QFontMetrics fmTF(getTimeFont(option));
- result.setHeight(fmTF.lineSpacing()+fmTF.leading()*2+4);
- result.setWidth(option.rect.width());
- return(QSize(option.rect.width(),20));
- qDebug()<<option.decorationSize;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement