3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "channelitemdelegate.h"
22 #include "channelmodel.h"
23 #include "ytchannel.h"
24 #include "fontutils.h"
25 #include "channelaggregator.h"
26 #include "painterutils.h"
27 #include "iconutils.h"
29 static const int ITEM_WIDTH = 150;
30 static const int ITEM_HEIGHT = 150;
31 static const int THUMB_WIDTH = 88;
32 static const int THUMB_HEIGHT = 88;
34 ChannelItemDelegate::ChannelItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
38 QSize ChannelItemDelegate::sizeHint(const QStyleOptionViewItem& /*option*/,
39 const QModelIndex& /*index*/ ) const {
40 return QSize(ITEM_WIDTH, ITEM_HEIGHT);
43 void ChannelItemDelegate::paint( QPainter* painter,
44 const QStyleOptionViewItem& option,
45 const QModelIndex& index ) const {
46 const int itemType = index.data(ChannelModel::ItemTypeRole).toInt();
47 if (itemType == ChannelModel::ItemChannel)
48 paintChannel(painter, option, index);
49 else if (itemType == ChannelModel::ItemAggregate)
50 paintAggregate(painter, option, index);
51 else if (itemType == ChannelModel::ItemUnwatched)
52 paintUnwatched(painter, option, index);
54 QStyledItemDelegate::paint(painter, option, index);
57 void ChannelItemDelegate::paintAggregate(QPainter* painter,
58 const QStyleOptionViewItem& option,
59 const QModelIndex& index) const {
63 painter->translate(option.rect.topLeft());
64 const QRect line(0, 0, option.rect.width(), option.rect.height());
66 static const QPixmap thumbnail = IconUtils::pixmap(":/images/channels.png");
68 QString name = tr("All Videos");
70 drawItem(painter, line, thumbnail, name);
75 void ChannelItemDelegate::paintUnwatched(QPainter* painter,
76 const QStyleOptionViewItem& option,
77 const QModelIndex& index) const {
81 painter->translate(option.rect.topLeft());
82 const QRect line(0, 0, option.rect.width(), option.rect.height());
84 static const QPixmap thumbnail = IconUtils::pixmap(":/images/unwatched.png");
86 QString name = tr("Unwatched Videos");
88 drawItem(painter, line, thumbnail, name);
90 int notifyCount = ChannelAggregator::instance()->getUnwatchedCount();
91 QString notifyText = QString::number(notifyCount);
92 if (notifyCount > 0) paintBadge(painter, line, notifyText);
97 void ChannelItemDelegate::paintChannel(QPainter* painter,
98 const QStyleOptionViewItem& option,
99 const QModelIndex& index) const {
100 YTChannel *channel = index.data(ChannelModel::DataObjectRole).value<YTChannelPointer>().data();
101 if (!channel) return;
105 painter->translate(option.rect.topLeft());
106 const QRect line(0, 0, option.rect.width(), option.rect.height());
108 // const bool isActive = index.data( ActiveItemRole ).toBool();
109 // const bool isHovered = index.data(ChannelsModel::HoveredItemRole ).toBool();
110 // const bool isSelected = option.state & QStyle::State_Selected;
112 QPixmap thumbnail = channel->getThumbnail();
113 if (thumbnail.isNull()) {
114 channel->loadThumbnail();
119 QString name = channel->getDisplayName();
120 drawItem(painter, line, thumbnail, name);
122 int notifyCount = channel->getNotifyCount();
124 paintBadge(painter, line, QString::number(notifyCount));
129 void ChannelItemDelegate::drawItem(QPainter *painter,
131 const QPixmap &thumbnail,
132 const QString &name) const {
133 painter->drawPixmap((line.width() - THUMB_WIDTH) / 2, 8, thumbnail);
135 QRect nameBox = line;
136 nameBox.adjust(0, 0, 0, -THUMB_HEIGHT - 16);
137 nameBox.translate(0, line.height() - nameBox.height());
140 QRect textBox = painter->boundingRect(nameBox,
141 Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
143 if (textBox.height() > nameBox.height() || textBox.width() > nameBox.width()) {
144 painter->setFont(FontUtils::small());
145 textBox = painter->boundingRect(nameBox,
146 Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
148 if (textBox.height() > nameBox.height()) {
149 painter->setClipRect(nameBox);
154 painter->drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
156 painter->drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
159 void ChannelItemDelegate::paintBadge(QPainter *painter,
161 const QString &text) const {
162 const int topLeft = (line.width() + THUMB_WIDTH) / 2;
164 painter->translate(topLeft, 0);
165 painter->setClipping(false);
166 PainterUtils::paintBadge(painter, text, true);