]> git.sur5r.net Git - minitube/blob - src/standardfeedsview.cpp
3e83208640c18b54e8496a67838196fe4793fc9f
[minitube] / src / standardfeedsview.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 "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"
28
29 StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
30     QPalette p = palette();
31     p.setBrush(QPalette::Window, Qt::black);
32     setPalette(p);
33     setAutoFillBackground(true);
34
35     connect(MainWindow::instance()->getAction("worldwideRegion"), SIGNAL(triggered()),
36             SLOT(selectWorldwideRegion()));
37
38     connect(MainWindow::instance()->getAction("localRegion"), SIGNAL(triggered()),
39             SLOT(selectLocalRegion()));
40 }
41
42 void StandardFeedsView::load() {
43     setUpdatesEnabled(false);
44     YTCategories *youTubeCategories = new YTCategories(this);
45     connect(youTubeCategories, SIGNAL(categoriesLoaded(const QVector<YTCategory> &)),
46             SLOT(layoutCategories(const QVector<YTCategory> &)));
47     youTubeCategories->loadCategories();
48
49     resetLayout();
50
51     addVideoSourceWidget(buildStandardFeed("most_popular", tr("Most Popular")));
52
53     YTRegion region = YTRegions::currentRegion();
54     QAction *regionAction = MainWindow::instance()->getRegionAction();
55     regionAction->setText(region.name);
56     regionAction->setIcon(YTRegions::iconForRegionId(region.id));
57 }
58
59 void StandardFeedsView::layoutCategories(const QVector<YTCategory> &categories) {
60     QString regionId = YTRegions::currentRegionId();
61     for (const YTCategory &category : categories) {
62         // assign a parent to this VideoSource  so it won't be deleted by MediaView
63         YTStandardFeed *feed = new YTStandardFeed(this);
64         feed->setCategory(category.term);
65         feed->setLabel(category.label);
66         feed->setRegionId(regionId);
67         feed->setFeedId("most_popular");
68         addVideoSourceWidget(feed);
69     }
70     if (categories.size() > 1) setUpdatesEnabled(true);
71 }
72
73 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
74     VideoSourceWidget *w = new VideoSourceWidget(videoSource);
75     connect(w, SIGNAL(activated(VideoSource *)), SIGNAL(activated(VideoSource *)));
76     connect(w, SIGNAL(unavailable(VideoSourceWidget *)),
77             SLOT(removeVideoSourceWidget(VideoSourceWidget *)));
78     int i = layout->count();
79     const int cols = 5;
80     layout->addWidget(w, i / cols, i % cols);
81 }
82
83 void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWidget) {
84     qDebug() << videoSourceWidget->getVideoSource()->getName();
85     layout->removeWidget(videoSourceWidget);
86     videoSourceWidget->deleteLater();
87
88     const int layoutCount = layout->count();
89     QVector<QLayoutItem *> items;
90     items.reserve(layoutCount);
91
92     for (int i = layoutCount - 1; i >= 0; i--) {
93         QLayoutItem *item = layout->takeAt(i);
94         if (item && item->widget()) items.append(item);
95     }
96
97     const int itemCount = items.size();
98     const int cols = itemCount / 3;
99     for (int i = itemCount - 1; i >= 0; i--) {
100         QLayoutItem *item = items.at(i);
101         int index = itemCount - 1 - i;
102         layout->addItem(item, index / cols, index % cols);
103     }
104 }
105
106 void StandardFeedsView::resetLayout() {
107     if (layout) {
108         while (QLayoutItem *item = layout->takeAt(0)) {
109             delete item->widget();
110             delete item;
111         }
112         delete layout;
113     }
114
115     layout = new QGridLayout(this);
116     layout->setMargin(0);
117     layout->setSpacing(1);
118 }
119
120 YTStandardFeed *
121 StandardFeedsView::buildStandardFeed(const QString &feedId, const QString &label, QString time) {
122     YTStandardFeed *feed = new YTStandardFeed(this);
123     feed->setFeedId(feedId);
124     feed->setLabel(label);
125     if (!time.isEmpty()) feed->setTime(time);
126     feed->setRegionId(YTRegions::currentRegionId());
127     return feed;
128 }
129
130 void StandardFeedsView::appear() {
131     if (!layout) {
132         update();
133         qApp->processEvents();
134         load();
135     }
136     QAction *regionAction = MainWindow::instance()->getRegionAction();
137     MainWindow::instance()->showActionInStatusBar(regionAction, true);
138 }
139
140 void StandardFeedsView::disappear() {
141     QAction *regionAction = MainWindow::instance()->getRegionAction();
142     MainWindow::instance()->showActionInStatusBar(regionAction, false);
143 }
144
145 void StandardFeedsView::selectWorldwideRegion() {
146     YTRegions::setRegion(YTRegions::worldwideRegion().id);
147     load();
148 }
149
150 void StandardFeedsView::selectLocalRegion() {
151     YTRegions::setRegion(YTRegions::localRegion().id);
152     load();
153 }
154
155 void StandardFeedsView::paintEvent(QPaintEvent *event) {
156     QWidget::paintEvent(event);
157 }