]> git.sur5r.net Git - minitube/blob - src/channelsitemdelegate.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / channelsitemdelegate.cpp
1 #include "channelsitemdelegate.h"
2 #include "channelmodel.h"
3 #include "ytuser.h"
4 #include "fontutils.h"
5 #include "channelaggregator.h"
6 #include "painterutils.h"
7
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;
12
13 ChannelsItemDelegate::ChannelsItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
14
15 }
16
17 QSize ChannelsItemDelegate::sizeHint(const QStyleOptionViewItem& /*option*/,
18                                      const QModelIndex& /*index*/ ) const {
19     return QSize(ITEM_WIDTH, ITEM_HEIGHT);
20 }
21
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);
32     else
33         QStyledItemDelegate::paint(painter, option, index);
34 }
35
36 void ChannelsItemDelegate::paintAggregate(QPainter* painter,
37                                           const QStyleOptionViewItem& option,
38                                           const QModelIndex& index) const {
39     painter->save();
40
41     painter->translate(option.rect.topLeft());
42     const QRect line(0, 0, option.rect.width(), option.rect.height());
43
44     static const QPixmap thumbnail = QPixmap(":/images/channels.png");
45
46     QString name = tr("All Videos");
47
48     drawItem(painter, line, thumbnail, name);
49
50     painter->restore();
51 }
52
53 void ChannelsItemDelegate::paintUnwatched(QPainter* painter,
54                                           const QStyleOptionViewItem& option,
55                                           const QModelIndex& index) const {
56     painter->save();
57
58     painter->translate(option.rect.topLeft());
59     const QRect line(0, 0, option.rect.width(), option.rect.height());
60
61     static const QPixmap thumbnail = QPixmap(":/images/unwatched.png");
62
63     QString name = tr("Unwatched Videos");
64
65     drawItem(painter, line, thumbnail, name);
66
67     int notifyCount = ChannelAggregator::instance()->getUnwatchedCount();
68     QString notifyText = QString::number(notifyCount);
69     if (notifyCount > 0) paintBadge(painter, line, notifyText);
70
71     painter->restore();
72 }
73
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();
80     if (!user) return;
81
82     painter->save();
83
84     painter->translate(option.rect.topLeft());
85     const QRect line(0, 0, option.rect.width(), option.rect.height());
86
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;
90
91     QPixmap thumbnail = user->getThumbnail();
92     if (thumbnail.isNull()) {
93         user->loadThumbnail();
94         painter->restore();
95         return;
96     }
97
98     QString name = user->getDisplayName();
99     drawItem(painter, line, thumbnail, name);
100
101     int notifyCount = user->getNotifyCount();
102     if (notifyCount > 0)
103         paintBadge(painter, line, QString::number(notifyCount));
104
105     painter->restore();
106 }
107
108 void ChannelsItemDelegate::drawItem(QPainter *painter,
109                                     const QRect &line,
110                                     const QPixmap &thumbnail,
111                                     const QString &name) const {
112     painter->drawPixmap((line.width() - THUMB_WIDTH) / 2, 8, thumbnail);
113
114     QRect nameBox = line;
115     nameBox.adjust(0, 0, 0, -THUMB_HEIGHT - 16);
116     nameBox.translate(0, line.height() - nameBox.height());
117     bool tooBig = false;
118
119 #ifdef APP_MAC_NO
120     QFont f = painter->font();
121     f.setFamily("Helvetica");
122     painter->setFont(f);
123 #endif
124
125     QRect textBox = painter->boundingRect(nameBox,
126                                           Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap,
127                                           name);
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,
132                                         name);
133         if (textBox.height() > nameBox.height()) {
134             painter->setClipRect(nameBox);
135             tooBig = true;
136         }
137     }
138     if (tooBig)
139         painter->drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
140     else
141         painter->drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
142 }
143
144 void ChannelsItemDelegate::paintBadge(QPainter *painter,
145                                               const QRect &line,
146                                               const QString &text) const {
147     const int topLeft = (line.width() + THUMB_WIDTH) / 2;
148     painter->save();
149     painter->translate(topLeft, 0);
150     PainterUtils::paintBadge(painter, text, true);
151     painter->restore();
152 }