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