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 StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
30 QPalette p = palette();
31 p.setBrush(QPalette::Window, Qt::black);
33 setAutoFillBackground(true);
35 connect(MainWindow::instance()->getAction("worldwideRegion"), SIGNAL(triggered()),
36 SLOT(selectWorldwideRegion()));
38 connect(MainWindow::instance()->getAction("localRegion"), SIGNAL(triggered()),
39 SLOT(selectLocalRegion()));
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();
51 addVideoSourceWidget(buildStandardFeed("most_popular", tr("Most Popular")));
53 YTRegion region = YTRegions::currentRegion();
54 QAction *regionAction = MainWindow::instance()->getRegionAction();
55 regionAction->setText(region.name);
56 regionAction->setIcon(YTRegions::iconForRegionId(region.id));
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);
70 if (categories.size() > 1) setUpdatesEnabled(true);
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();
80 layout->addWidget(w, i / cols, i % cols);
83 void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWidget) {
84 qDebug() << videoSourceWidget->getVideoSource()->getName();
85 layout->removeWidget(videoSourceWidget);
86 videoSourceWidget->deleteLater();
88 const int layoutCount = layout->count();
89 QVector<QLayoutItem *> items;
90 items.reserve(layoutCount);
92 for (int i = layoutCount - 1; i >= 0; i--) {
93 QLayoutItem *item = layout->takeAt(i);
94 if (item && item->widget()) items.append(item);
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);
106 void StandardFeedsView::resetLayout() {
108 while (QLayoutItem *item = layout->takeAt(0)) {
109 delete item->widget();
115 layout = new QGridLayout(this);
116 layout->setMargin(0);
117 layout->setSpacing(1);
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());
130 void StandardFeedsView::appear() {
133 qApp->processEvents();
136 QAction *regionAction = MainWindow::instance()->getRegionAction();
137 MainWindow::instance()->showActionInStatusBar(regionAction, true);
140 void StandardFeedsView::disappear() {
141 QAction *regionAction = MainWindow::instance()->getRegionAction();
142 MainWindow::instance()->showActionInStatusBar(regionAction, false);
145 void StandardFeedsView::selectWorldwideRegion() {
146 YTRegions::setRegion(YTRegions::worldwideRegion().id);
150 void StandardFeedsView::selectLocalRegion() {
151 YTRegions::setRegion(YTRegions::localRegion().id);
155 void StandardFeedsView::paintEvent(QPaintEvent *event) {
156 QWidget::paintEvent(event);