1 #include "channelsitemdelegate.h"
2 #include "channelmodel.h"
5 #include "channelaggregator.h"
6 #include "painterutils.h"
8 static const int ITEM_WIDTH = 128;
9 static const int ITEM_HEIGHT = 128;
10 static const int THUMB_WIDTH = 88;
11 static const int THUMB_HEIGHT = 88;
13 ChannelsItemDelegate::ChannelsItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
17 QSize ChannelsItemDelegate::sizeHint(const QStyleOptionViewItem& /*option*/,
18 const QModelIndex& /*index*/ ) const {
19 return QSize(ITEM_WIDTH, ITEM_HEIGHT);
22 void ChannelsItemDelegate::paint( QPainter* painter,
23 const QStyleOptionViewItem& option,
24 const QModelIndex& index ) const {
25 const int itemType = index.data(ChannelModel::ItemTypeRole).toInt();
26 if (itemType == ChannelModel::ItemChannel)
27 paintChannel(painter, option, index);
28 else if (itemType == ChannelModel::ItemAggregate)
29 paintAggregate(painter, option, index);
30 else if (itemType == ChannelModel::ItemUnwatched)
31 paintUnwatched(painter, option, index);
33 QStyledItemDelegate::paint(painter, option, index);
36 void ChannelsItemDelegate::paintAggregate(QPainter* painter,
37 const QStyleOptionViewItem& option,
38 const QModelIndex& index) const {
41 painter->translate(option.rect.topLeft());
42 const QRect line(0, 0, option.rect.width(), option.rect.height());
44 static const QPixmap thumbnail = QPixmap(":/images/channels.png");
46 QString name = tr("All Videos");
48 drawItem(painter, line, thumbnail, name);
53 void ChannelsItemDelegate::paintUnwatched(QPainter* painter,
54 const QStyleOptionViewItem& option,
55 const QModelIndex& index) const {
58 painter->translate(option.rect.topLeft());
59 const QRect line(0, 0, option.rect.width(), option.rect.height());
61 static const QPixmap thumbnail = QPixmap(":/images/unwatched.png");
63 QString name = tr("Unwatched Videos");
65 drawItem(painter, line, thumbnail, name);
67 int notifyCount = ChannelAggregator::instance()->getUnwatchedCount();
68 QString notifyText = QString::number(notifyCount);
69 if (notifyCount > 0) paintBadge(painter, line, notifyText);
74 void ChannelsItemDelegate::paintChannel(QPainter* painter,
75 const QStyleOptionViewItem& option,
76 const QModelIndex& index) const {
77 const QVariant dataObject = index.data(ChannelModel::DataObjectRole);
78 const YTUserPointer channelPointer = dataObject.value<YTUserPointer>();
79 YTUser *user = channelPointer.data();
84 painter->translate(option.rect.topLeft());
85 const QRect line(0, 0, option.rect.width(), option.rect.height());
87 // const bool isActive = index.data( ActiveItemRole ).toBool();
88 // const bool isHovered = index.data(ChannelsModel::HoveredItemRole ).toBool();
89 // const bool isSelected = option.state & QStyle::State_Selected;
91 QPixmap thumbnail = user->getThumbnail();
92 if (thumbnail.isNull()) {
93 user->loadThumbnail();
98 QString name = user->getDisplayName();
99 drawItem(painter, line, thumbnail, name);
101 int notifyCount = user->getNotifyCount();
103 paintBadge(painter, line, QString::number(notifyCount));
108 void ChannelsItemDelegate::drawItem(QPainter *painter,
110 const QPixmap &thumbnail,
111 const QString &name) const {
112 painter->drawPixmap((line.width() - THUMB_WIDTH) / 2, 8, thumbnail);
114 QRect nameBox = line;
115 nameBox.adjust(0, 0, 0, -THUMB_HEIGHT - 16);
116 nameBox.translate(0, line.height() - nameBox.height());
120 QFont f = painter->font();
121 f.setFamily("Helvetica");
125 QRect textBox = painter->boundingRect(nameBox,
126 Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
128 if (textBox.height() > nameBox.height() || textBox.width() > nameBox.width()) {
129 painter->setFont(FontUtils::small());
130 textBox = painter->boundingRect(nameBox,
131 Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
133 if (textBox.height() > nameBox.height()) {
134 painter->setClipRect(nameBox);
139 painter->drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
141 painter->drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
144 void ChannelsItemDelegate::paintBadge(QPainter *painter,
146 const QString &text) const {
147 const int topLeft = (line.width() + THUMB_WIDTH) / 2;
149 painter->translate(topLeft, 0);
150 PainterUtils::paintBadge(painter, text, true);