#include <qxtscheduleitemdelegate.h>
Public Types | |
enum | ItemPart { Top, Middle, Bottom, Single } |
Public Member Functions | |
QxtScheduleItemDelegate (QObject *parent=0) | |
~QxtScheduleItemDelegate () | |
virtual void | paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
virtual void | paintItemBody (QPainter *painter, const QRect rect, const QxtStyleOptionScheduleViewItem &option, const ItemPart part, const QModelIndex &index) const |
paints the items body reimplement this to paint a custom body | |
virtual void | paintItemHeader (QPainter *painter, const QRect rect, const QxtStyleOptionScheduleViewItem &option, const QModelIndex &index) const |
paints the items header reimplement this to paint a custom header | |
virtual void | paintSubItem (QPainter *painter, const QRect rect, const QxtStyleOptionScheduleViewItem &option, const QModelIndex &index) const |
paints a subitem, if you want custom subitem painting reimplement this member function | |
virtual QSize | sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const |
returns the sizeHint for subitems. | |
virtual void | createPainterPath (QPainterPath &emptyPath, const QRect &fullItemRect, const int iRoundTop, const int iRoundBottom) const |
Definition at line 38 of file qxtscheduleitemdelegate.h.
QxtScheduleItemDelegate::QxtScheduleItemDelegate | ( | QObject * | parent = 0 ) |
Definition at line 37 of file qxtscheduleitemdelegate.cpp.
: QAbstractItemDelegate(parent) { }
QxtScheduleItemDelegate::~QxtScheduleItemDelegate | ( | ) |
Definition at line 43 of file qxtscheduleitemdelegate.cpp.
{ }
void QxtScheduleItemDelegate::createPainterPath | ( | QPainterPath & | emptyPath, |
const QRect & | fullItemRect, | ||
const int | iRoundTop, | ||
const int | iRoundBottom | ||
) | const [virtual] |
Definition at line 250 of file qxtscheduleitemdelegate.cpp.
Referenced by paintItemBody().
{ emptyPath = QPainterPath(); bool bRoundTop = iRoundTop > 0; bool bRountBottom = iRoundBottom > 0; if (bRoundTop) { emptyPath.moveTo(fullItemRect.topLeft() + QPoint(0, iRoundTop)); emptyPath.quadTo(fullItemRect.topLeft(), fullItemRect.topLeft() + QPoint(iRoundTop, 0)); } else emptyPath.moveTo(fullItemRect.topLeft()); emptyPath.lineTo(fullItemRect.topRight() - QPoint(iRoundTop, 0)); if (bRoundTop) emptyPath.quadTo(fullItemRect.topRight(), fullItemRect.topRight() + QPoint(0, iRoundTop)); emptyPath.lineTo(fullItemRect.bottomRight() - QPoint(0, iRoundBottom)); if (bRountBottom) emptyPath.quadTo(fullItemRect.bottomRight(), fullItemRect.bottomRight() - QPoint(iRoundBottom, 0)); emptyPath.lineTo(fullItemRect.bottomLeft() + QPoint(iRoundBottom, 0)); if (bRountBottom) emptyPath.quadTo(fullItemRect.bottomLeft(), fullItemRect.bottomLeft() - QPoint(0, iRoundBottom)); emptyPath.closeSubpath(); }
void QxtScheduleItemDelegate::paint | ( | QPainter * | painter, |
const QStyleOptionViewItem & | option, | ||
const QModelIndex & | index | ||
) | const [virtual] |
reimplemented for item painting You should not reimplement this to change the item painting, use paintItemBody, paintItemHeader and paintSubItem instead because this function uses caches to speed up painting. If you want to change the item shape only you could also reimplement the createPainterPath function.
Definition at line 56 of file qxtscheduleitemdelegate.cpp.
References Bottom, QxtStyleOptionScheduleViewItem::itemGeometries, QxtStyleOptionScheduleViewItem::itemHeaderHeight, QxtStyleOptionScheduleViewItem::itemPaintCache, Middle, paintItemBody(), paintItemHeader(), paintSubItem(), Single, sizeHint(), Top, and QxtStyleOptionScheduleViewItem::translate.
{ const QxtStyleOptionScheduleViewItem *agendaOption = qstyleoption_cast<const QxtStyleOptionScheduleViewItem *>(&option); if (!agendaOption) return; QStringList rowsData = index.data(Qt::EditRole).toStringList(); QRect currRect; painter->save(); if (agendaOption->itemPaintCache->size() != agendaOption->itemGeometries.size()) (*agendaOption->itemPaintCache) = QVector<QPixmap>(agendaOption->itemGeometries.size(), QPixmap()); int lastPart = agendaOption->itemGeometries.size() - 1; int paintedSubItems = 0; for (int iLoop = 0; iLoop < agendaOption->itemGeometries.size();iLoop++) { if ((*agendaOption->itemPaintCache)[iLoop].width() != agendaOption->itemGeometries[iLoop].width() || (*agendaOption->itemPaintCache)[iLoop].height() != agendaOption->itemGeometries[iLoop].height()) { //If we enter this codepath we have to rebuild the pixmap cache //so first we create a empty pixmap (*agendaOption->itemPaintCache)[iLoop] = QPixmap(agendaOption->itemGeometries[iLoop].size()); (*agendaOption->itemPaintCache)[iLoop].fill(Qt::transparent); QPainter cachePainter(&(*agendaOption->itemPaintCache)[iLoop]); QRect rect = QRect(QPoint(0, 0), agendaOption->itemGeometries[iLoop].size()); //what kind of itempart do we need to paint? ItemPart part = iLoop == 0 ? Top : (iLoop == lastPart ? Bottom : Middle); //if the item has only one part if (lastPart == 0) part = Single; //paint the item body cachePainter.save(); paintItemBody(&cachePainter, rect, *agendaOption, part, index); cachePainter.restore(); int remainingHeight = rect.height(); //paint item header if (iLoop == 0 && agendaOption->itemHeaderHeight > 0 && agendaOption->itemHeaderHeight < remainingHeight) { QRect headerRect(0, 0, rect.width(), agendaOption->itemHeaderHeight); paintItemHeader(&cachePainter, headerRect, *agendaOption, index); remainingHeight -= agendaOption->itemHeaderHeight; } //paint subitems if there are any int subItems = index.model()->rowCount(index); for (int items = paintedSubItems; items < subItems; items++) { QModelIndex currSubItem = index.model()->index(items, 0, index); QSize size = sizeHint(option, currSubItem); if (currSubItem.isValid()){ paintSubItem(&cachePainter, QRect(), *agendaOption, currSubItem); } paintedSubItems++; } cachePainter.end(); } currRect = agendaOption->itemGeometries[iLoop]; currRect.translate(agendaOption->translate); painter->drawPixmap(currRect, (*agendaOption->itemPaintCache)[iLoop]); } painter->restore(); }
void QxtScheduleItemDelegate::paintItemBody | ( | QPainter * | painter, |
const QRect | rect, | ||
const QxtStyleOptionScheduleViewItem & | option, | ||
const ItemPart | part, | ||
const QModelIndex & | index | ||
) | const [virtual] |
paints the items body reimplement this to paint a custom body
QPainter | *painter the initialized painter |
const | QRect rect the ItemPart rect |
const | QxtStyleOptionScheduleViewItem & option |
const | ItemPart part this indicates what part of the item gets painted, remember items can be splitted in parts |
const | QModelIndex &index the items model index |
Definition at line 142 of file qxtscheduleitemdelegate.cpp.
References Bottom, createPainterPath(), QxtStyleOptionScheduleViewItem::roundCornersRadius, Single, and Top.
Referenced by paint().
{ int iCurrRoundTop, iCurrRoundBottom; iCurrRoundTop = iCurrRoundBottom = 0; QColor fillColor = index.data(Qt::BackgroundRole).value<QColor>(); fillColor.setAlpha(120); QColor outLineColor = index.data(Qt::ForegroundRole).value<QColor>(); painter->setFont(option.font); painter->setRenderHint(QPainter::Antialiasing); if (part == Top || part == Single) iCurrRoundTop = option.roundCornersRadius; if (part == Bottom || part == Single) iCurrRoundBottom = option.roundCornersRadius; QPainterPath cachePath; QRect cacheRect = QRect(QPoint(1, 1), rect.size() - QSize(1, 1)); painter->setBrush(fillColor); painter->setPen(outLineColor); createPainterPath(cachePath, cacheRect, iCurrRoundTop, iCurrRoundBottom); painter->drawPath(cachePath); }
void QxtScheduleItemDelegate::paintItemHeader | ( | QPainter * | painter, |
const QRect | rect, | ||
const QxtStyleOptionScheduleViewItem & | option, | ||
const QModelIndex & | index | ||
) | const [virtual] |
paints the items header reimplement this to paint a custom header
QPainter | *painter the initialized painter |
const | QRect rect the header rect |
const | QxtStyleOptionScheduleViewItem & option |
const | QModelIndex &index the items model index |
Definition at line 176 of file qxtscheduleitemdelegate.cpp.
References Qxt::ItemDurationRole, and Qxt::ItemStartTimeRole.
Referenced by paint().
{ bool converted = false; int startUnixTime = index.data(Qxt::ItemStartTimeRole).toInt(&converted); if (!converted) return; int duration = index.data(Qxt::ItemDurationRole).toInt(&converted); if (!converted) return; QDateTime startTime = QDateTime::fromTime_t(startUnixTime); QDateTime endTime = QDateTime::fromTime_t(startUnixTime + duration); if (!startTime.isValid() || !endTime.isValid()) return; QFont font; QVariant vfont = index.data(Qt::FontRole); if (vfont.isValid()) font = vfont.value<QFont>(); else font = option.font; QString text = startTime.toString("hh:mm") + ' ' + endTime.toString("hh:mm"); QFontMetrics metr(font); text = metr.elidedText(text, Qt::ElideRight, rect.width()); painter->drawText(rect, Qt::AlignCenter, text); }
void QxtScheduleItemDelegate::paintSubItem | ( | QPainter * | painter, |
const QRect | rect, | ||
const QxtStyleOptionScheduleViewItem & | option, | ||
const QModelIndex & | index | ||
) | const [virtual] |
paints a subitem, if you want custom subitem painting reimplement this member function
QPainter | *painter the initialized painter |
const | QRect rect the subitem rect |
const | QxtStyleOptionScheduleViewItem & option |
const | QModelIndex &index the items model index |
Definition at line 214 of file qxtscheduleitemdelegate.cpp.
Referenced by paint().
{ }
QSize QxtScheduleItemDelegate::sizeHint | ( | const QStyleOptionViewItem & | option, |
const QModelIndex & | index | ||
) | const [virtual] |
returns the sizeHint for subitems.
Definition at line 222 of file qxtscheduleitemdelegate.cpp.
Referenced by paint().
{ //we return the size only for subitems and only the height if (index.parent().isValid()) { QSize size = index.data(Qt::SizeHintRole).toSize(); if (!size.isValid()) { QFont font; QVariant vfont = index.data(Qt::FontRole); if (vfont.isValid()) font = vfont.value<QFont>(); else font = option.font; int height = 0; QFontMetrics metr(font); height = metr.height() + 2; return QSize(0, height); } } return QSize(); }