]> git.sur5r.net Git - minitube/blob - src/channelitemdelegate.cpp
Imported Upstream version 2.1.3
[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 "ytuser.h"
24 #include "fontutils.h"
25 #include "channelaggregator.h"
26 #include "painterutils.h"
27
28 static const int ITEM_WIDTH = 128;
29 static const int ITEM_HEIGHT = 128;
30 static const int THUMB_WIDTH = 88;
31 static const int THUMB_HEIGHT = 88;
32
33 ChannelItemDelegate::ChannelItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
34
35 }
36
37 QSize ChannelItemDelegate::sizeHint(const QStyleOptionViewItem& /*option*/,
38                                      const QModelIndex& /*index*/ ) const {
39     return QSize(ITEM_WIDTH, ITEM_HEIGHT);
40 }
41
42 void ChannelItemDelegate::paint( QPainter* painter,
43                                   const QStyleOptionViewItem& option,
44                                   const QModelIndex& index ) const {
45     const int itemType = index.data(ChannelModel::ItemTypeRole).toInt();
46     if (itemType == ChannelModel::ItemChannel)
47         paintChannel(painter, option, index);
48     else if (itemType == ChannelModel::ItemAggregate)
49         paintAggregate(painter, option, index);
50     else if (itemType == ChannelModel::ItemUnwatched)
51         paintUnwatched(painter, option, index);
52     else
53         QStyledItemDelegate::paint(painter, option, index);
54 }
55
56 void ChannelItemDelegate::paintAggregate(QPainter* painter,
57                                           const QStyleOptionViewItem& option,
58                                           const QModelIndex& index) const {
59     painter->save();
60
61     painter->translate(option.rect.topLeft());
62     const QRect line(0, 0, option.rect.width(), option.rect.height());
63
64     static const QPixmap thumbnail = QPixmap(":/images/channels.png");
65
66     QString name = tr("All Videos");
67
68     drawItem(painter, line, thumbnail, name);
69
70     painter->restore();
71 }
72
73 void ChannelItemDelegate::paintUnwatched(QPainter* painter,
74                                           const QStyleOptionViewItem& option,
75                                           const QModelIndex& index) const {
76     painter->save();
77
78     painter->translate(option.rect.topLeft());
79     const QRect line(0, 0, option.rect.width(), option.rect.height());
80
81     static const QPixmap thumbnail = QPixmap(":/images/unwatched.png");
82
83     QString name = tr("Unwatched Videos");
84
85     drawItem(painter, line, thumbnail, name);
86
87     int notifyCount = ChannelAggregator::instance()->getUnwatchedCount();
88     QString notifyText = QString::number(notifyCount);
89     if (notifyCount > 0) paintBadge(painter, line, notifyText);
90
91     painter->restore();
92 }
93
94 void ChannelItemDelegate::paintChannel(QPainter* painter,
95                                         const QStyleOptionViewItem& option,
96                                         const QModelIndex& index) const {
97     const QVariant dataObject = index.data(ChannelModel::DataObjectRole);
98     const YTUserPointer channelPointer = dataObject.value<YTUserPointer>();
99     YTUser *user = channelPointer.data();
100     if (!user) return;
101
102     painter->save();
103
104     painter->translate(option.rect.topLeft());
105     const QRect line(0, 0, option.rect.width(), option.rect.height());
106
107     // const bool isActive = index.data( ActiveItemRole ).toBool();
108     // const bool isHovered = index.data(ChannelsModel::HoveredItemRole ).toBool();
109     // const bool isSelected = option.state & QStyle::State_Selected;
110
111     QPixmap thumbnail = user->getThumbnail();
112     if (thumbnail.isNull()) {
113         user->loadThumbnail();
114         painter->restore();
115         return;
116     }
117
118     QString name = user->getDisplayName();
119     drawItem(painter, line, thumbnail, name);
120
121     int notifyCount = user->getNotifyCount();
122     if (notifyCount > 0)
123         paintBadge(painter, line, QString::number(notifyCount));
124
125     painter->restore();
126 }
127
128 void ChannelItemDelegate::drawItem(QPainter *painter,
129                                     const QRect &line,
130                                     const QPixmap &thumbnail,
131                                     const QString &name) const {
132     painter->drawPixmap((line.width() - THUMB_WIDTH) / 2, 8, thumbnail);
133
134     QRect nameBox = line;
135     nameBox.adjust(0, 0, 0, -THUMB_HEIGHT - 16);
136     nameBox.translate(0, line.height() - nameBox.height());
137     bool tooBig = false;
138
139     QRect textBox = painter->boundingRect(nameBox,
140                                           Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
141                                           name);
142     if (textBox.height() > nameBox.height() || textBox.width() > nameBox.width()) {
143         painter->setFont(FontUtils::small());
144         textBox = painter->boundingRect(nameBox,
145                                         Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
146                                         name);
147         if (textBox.height() > nameBox.height()) {
148             painter->setClipRect(nameBox);
149             tooBig = true;
150         }
151     }
152     if (tooBig)
153         painter->drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
154     else
155         painter->drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
156 }
157
158 void ChannelItemDelegate::paintBadge(QPainter *painter,
159                                               const QRect &line,
160                                               const QString &text) const {
161     const int topLeft = (line.width() + THUMB_WIDTH) / 2;
162     painter->save();
163     painter->translate(topLeft, 0);
164     painter->setClipping(false);
165     PainterUtils::paintBadge(painter, text, true);
166     painter->restore();
167 }