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