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 "standardfeedsview.h"
22 #include "mainwindow.h"
23 #include "painterutils.h"
24 #include "videosourcewidget.h"
25 #include "ytcategories.h"
26 #include "ytregions.h"
27 #include "ytstandardfeed.h"
29 #include "ivvideolist.h"
32 StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
33 setBackgroundRole(QPalette::Base);
34 setAutoFillBackground(true);
36 connect(MainWindow::instance()->getAction("worldwideRegion"), SIGNAL(triggered()),
37 SLOT(selectWorldwideRegion()));
39 connect(MainWindow::instance()->getAction("localRegion"), SIGNAL(triggered()),
40 SLOT(selectLocalRegion()));
43 void StandardFeedsView::load() {
44 setUpdatesEnabled(false);
47 YTRegion region = YTRegions::currentRegion();
49 if (VideoAPI::impl() == VideoAPI::YT3) {
50 YTCategories *youTubeCategories = new YTCategories(this);
51 connect(youTubeCategories, SIGNAL(categoriesLoaded(const QVector<YTCategory> &)),
52 SLOT(layoutCategories(const QVector<YTCategory> &)));
53 youTubeCategories->loadCategories();
54 addVideoSourceWidget(buildStandardFeed("most_popular", tr("Most Popular")));
56 QString regionParam = "region=" + region.id;
57 addVideoSourceWidget(new IVVideoList("popular?" + regionParam, tr("Most Popular")));
58 addVideoSourceWidget(new IVVideoList("trending?" + regionParam, tr("Trending")));
59 addVideoSourceWidget(new IVVideoList("trending?type=music&" + regionParam, tr("Music")));
60 addVideoSourceWidget(new IVVideoList("trending?type=news&" + regionParam, tr("News")));
61 addVideoSourceWidget(new IVVideoList("trending?type=movies&" + regionParam, tr("Movies")));
62 addVideoSourceWidget(new IVVideoList("trending?type=gaming&" + regionParam, tr("Gaming")));
63 setUpdatesEnabled(true);
66 QAction *regionAction = MainWindow::instance()->getRegionAction();
67 regionAction->setText(region.name);
68 regionAction->setIcon(YTRegions::iconForRegionId(region.id));
71 void StandardFeedsView::layoutCategories(const QVector<YTCategory> &categories) {
72 QString regionId = YTRegions::currentRegionId();
73 for (const YTCategory &category : categories) {
74 // assign a parent to this VideoSource so it won't be deleted by MediaView
75 YTStandardFeed *feed = new YTStandardFeed(this);
76 feed->setCategory(category.term);
77 feed->setLabel(category.label);
78 feed->setRegionId(regionId);
79 feed->setFeedId("most_popular");
80 addVideoSourceWidget(feed);
82 if (categories.size() > 1) setUpdatesEnabled(true);
85 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
86 VideoSourceWidget *w = new VideoSourceWidget(videoSource);
87 connect(w, SIGNAL(activated(VideoSource *)), SIGNAL(activated(VideoSource *)));
88 connect(w, SIGNAL(unavailable(VideoSourceWidget *)),
89 SLOT(removeVideoSourceWidget(VideoSourceWidget *)));
90 int i = layout->count();
91 const int cols = VideoAPI::impl() == VideoAPI::YT3 ? 5 : 3;
92 layout->addWidget(w, i / cols, i % cols);
95 void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWidget) {
96 qDebug() << videoSourceWidget->getVideoSource()->getName();
97 layout->removeWidget(videoSourceWidget);
98 videoSourceWidget->deleteLater();
100 const int layoutCount = layout->count();
101 QVector<QLayoutItem *> items;
102 items.reserve(layoutCount);
104 for (int i = layoutCount - 1; i >= 0; i--) {
105 QLayoutItem *item = layout->takeAt(i);
106 if (item && item->widget()) items.append(item);
109 const int itemCount = items.size();
110 const int cols = 4; // itemCount / 3;
111 for (int i = itemCount - 1; i >= 0; i--) {
112 QLayoutItem *item = items.at(i);
113 int index = itemCount - 1 - i;
114 layout->addItem(item, index / cols, index % cols);
118 void StandardFeedsView::resetLayout() {
120 while (QLayoutItem *item = layout->takeAt(0)) {
121 delete item->widget();
127 layout = new QGridLayout(this);
128 layout->setMargin(0);
129 layout->setSpacing(0);
133 StandardFeedsView::buildStandardFeed(const QString &feedId, const QString &label, QString time) {
134 YTStandardFeed *feed = new YTStandardFeed(this);
135 feed->setFeedId(feedId);
136 feed->setLabel(label);
137 if (!time.isEmpty()) feed->setTime(time);
138 feed->setRegionId(YTRegions::currentRegionId());
142 void StandardFeedsView::appear() {
145 qApp->processEvents();
148 QAction *regionAction = MainWindow::instance()->getRegionAction();
149 MainWindow::instance()->showActionsInStatusBar({regionAction}, true);
152 void StandardFeedsView::disappear() {
153 QAction *regionAction = MainWindow::instance()->getRegionAction();
154 MainWindow::instance()->showActionsInStatusBar({regionAction}, false);
157 void StandardFeedsView::selectWorldwideRegion() {
158 YTRegions::setRegion(YTRegions::worldwideRegion().id);
162 void StandardFeedsView::selectLocalRegion() {
163 YTRegions::setRegion(YTRegions::localRegion().id);
167 void StandardFeedsView::paintEvent(QPaintEvent *event) {
168 QWidget::paintEvent(event);