]> git.sur5r.net Git - minitube/blob - src/channelitemdelegate.cpp
664efc460072e2d8c6530ed0ee19b2f95f7c4d58
[minitube] / src / channelitemdelegate.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
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"
28
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;
33
34 ChannelItemDelegate::ChannelItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
35
36 }
37
38 QSize ChannelItemDelegate::sizeHint(const QStyleOptionViewItem& /*option*/,
39                                      const QModelIndex& /*index*/ ) const {
40     return QSize(ITEM_WIDTH, ITEM_HEIGHT);
41 }
42
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);
53     else
54         QStyledItemDelegate::paint(painter, option, index);
55 }
56
57 void ChannelItemDelegate::paintAggregate(QPainter* painter,
58                                           const QStyleOptionViewItem& option,
59                                           const QModelIndex& index) const {
60     Q_UNUSED(index);
61     painter->save();
62
63     painter->translate(option.rect.topLeft());
64     const QRect line(0, 0, option.rect.width(), option.rect.height());
65
66     static const QPixmap thumbnail = IconUtils::pixmap(":/images/channels.png");
67
68     QString name = tr("All Videos");
69
70     drawItem(painter, line, thumbnail, name);
71
72     painter->restore();
73 }
74
75 void ChannelItemDelegate::paintUnwatched(QPainter* painter,
76                                           const QStyleOptionViewItem& option,
77                                           const QModelIndex& index) const {
78     Q_UNUSED(index);
79     painter->save();
80
81     painter->translate(option.rect.topLeft());
82     const QRect line(0, 0, option.rect.width(), option.rect.height());
83
84     static const QPixmap thumbnail = IconUtils::pixmap(":/images/unwatched.png");
85
86     QString name = tr("Unwatched Videos");
87
88     drawItem(painter, line, thumbnail, name);
89
90     int notifyCount = ChannelAggregator::instance()->getUnwatchedCount();
91     QString notifyText = QString::number(notifyCount);
92     if (notifyCount > 0) paintBadge(painter, line, notifyText);
93
94     painter->restore();
95 }
96
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;
102
103     painter->save();
104
105     painter->translate(option.rect.topLeft());
106     const QRect line(0, 0, option.rect.width(), option.rect.height());
107
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;
111
112     QPixmap thumbnail = channel->getThumbnail();
113     if (thumbnail.isNull()) {
114         channel->loadThumbnail();
115         painter->restore();
116         return;
117     }
118
119     QString name = channel->getDisplayName();
120     drawItem(painter, line, thumbnail, name);
121
122     int notifyCount = channel->getNotifyCount();
123     if (notifyCount > 0)
124         paintBadge(painter, line, QString::number(notifyCount));
125
126     painter->restore();
127 }
128
129 void ChannelItemDelegate::drawItem(QPainter *painter,
130                                     const QRect &line,
131                                     const QPixmap &thumbnail,
132                                     const QString &name) const {
133     painter->drawPixmap((line.width() - THUMB_WIDTH) / 2, 8, thumbnail);
134
135     QRect nameBox = line;
136     nameBox.adjust(0, 0, 0, -THUMB_HEIGHT - 16);
137     nameBox.translate(0, line.height() - nameBox.height());
138     bool tooBig = false;
139
140     QRect textBox = painter->boundingRect(nameBox,
141                                           Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
142                                           name);
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,
147                                         name);
148         if (textBox.height() > nameBox.height()) {
149             painter->setClipRect(nameBox);
150             tooBig = true;
151         }
152     }
153     if (tooBig)
154         painter->drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
155     else
156         painter->drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
157 }
158
159 void ChannelItemDelegate::paintBadge(QPainter *painter,
160                                               const QRect &line,
161                                               const QString &text) const {
162     const int topLeft = (line.width() + THUMB_WIDTH) / 2;
163     painter->save();
164     painter->translate(topLeft, 0);
165     painter->setClipping(false);
166     PainterUtils::paintBadge(painter, text, true);
167     painter->restore();
168 }