]> git.sur5r.net Git - minitube/blobdiff - src/standardfeedsview.cpp
New upstream version 3.1
[minitube] / src / standardfeedsview.cpp
index 4bfeee628e6a58aebdb6191c32171fbe49238867..9080f1ce0be2beef85219a91bca750f1465f06e7 100644 (file)
@@ -19,67 +19,44 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "standardfeedsview.h"
+#include "mainwindow.h"
+#include "painterutils.h"
 #include "videosourcewidget.h"
 #include "ytcategories.h"
-#include "ytstandardfeed.h"
 #include "ytregions.h"
-#include "mainwindow.h"
-#include "painterutils.h"
-
-namespace The {
-QHash<QString, QAction*>* globalActions();
-}
+#include "ytstandardfeed.h"
 
-StandardFeedsView::StandardFeedsView(QWidget *parent) : QWidget(parent),
-    layout(0) {
-    QPalette p = palette();
-    p.setBrush(QPalette::Window, Qt::black);
-    setPalette(p);
+StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
+    setBackgroundRole(QPalette::Base);
     setAutoFillBackground(true);
 
-    connect(The::globalActions()->value("worldwide-region"), SIGNAL(triggered()),
+    connect(MainWindow::instance()->getAction("worldwideRegion"), SIGNAL(triggered()),
             SLOT(selectWorldwideRegion()));
 
-    connect(The::globalActions()->value("local-region"), SIGNAL(triggered()),
+    connect(MainWindow::instance()->getAction("localRegion"), SIGNAL(triggered()),
             SLOT(selectLocalRegion()));
-
-    /*
-    QAction *regionAction = MainWindow::instance()->getRegionAction();
-    connect(regionAction, SIGNAL(changed()), SLOT(load()));
-    */
 }
 
 void StandardFeedsView::load() {
+    setUpdatesEnabled(false);
     YTCategories *youTubeCategories = new YTCategories(this);
-    connect(youTubeCategories, SIGNAL(categoriesLoaded(const QList<YTCategory> &)),
-            SLOT(layoutCategories(const QList<YTCategory> &)));
+    connect(youTubeCategories, SIGNAL(categoriesLoaded(const QVector<YTCategory> &)),
+            SLOT(layoutCategories(const QVector<YTCategory> &)));
     youTubeCategories->loadCategories();
 
-    if (layout) {
-        while (QLayoutItem *item = layout->takeAt(0)) {
-            delete item->widget();
-            delete item;
-        }
-        delete layout;
-    }
+    resetLayout();
 
-    layout = new QGridLayout(this);
-    layout->setMargin(0);
-    layout->setSpacing(1);
-
-    QList<YTStandardFeed*> feeds = getMainFeeds();
-    foreach(YTStandardFeed *feed, feeds)
-        addVideoSourceWidget(feed);
+    addVideoSourceWidget(buildStandardFeed("most_popular", tr("Most Popular")));
 
     YTRegion region = YTRegions::currentRegion();
-    QToolButton *regionButton = MainWindow::instance()->getRegionButton();
-    regionButton->setText(region.name);
-    regionButton->setIcon(YTRegions::iconForRegionId(region.id));
+    QAction *regionAction = MainWindow::instance()->getRegionAction();
+    regionAction->setText(region.name);
+    regionAction->setIcon(YTRegions::iconForRegionId(region.id));
 }
 
-void StandardFeedsView::layoutCategories(const QList<YTCategory> &categories) {
+void StandardFeedsView::layoutCategories(const QVector<YTCategory> &categories) {
     QString regionId = YTRegions::currentRegionId();
-    foreach(YTCategory category, categories) {
+    for (const YTCategory &category : categories) {
         // assign a parent to this VideoSource  so it won't be deleted by MediaView
         YTStandardFeed *feed = new YTStandardFeed(this);
         feed->setCategory(category.term);
@@ -88,32 +65,58 @@ void StandardFeedsView::layoutCategories(const QList<YTCategory> &categories) {
         feed->setFeedId("most_popular");
         addVideoSourceWidget(feed);
     }
+    if (categories.size() > 1) setUpdatesEnabled(true);
 }
 
 void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
     VideoSourceWidget *w = new VideoSourceWidget(videoSource);
-    connect(w, SIGNAL(activated(VideoSource*)),
-            SIGNAL(activated(VideoSource*)));
+    connect(w, SIGNAL(activated(VideoSource *)), SIGNAL(activated(VideoSource *)));
+    connect(w, SIGNAL(unavailable(VideoSourceWidget *)),
+            SLOT(removeVideoSourceWidget(VideoSourceWidget *)));
     int i = layout->count();
-    static const int cols = 5;
+    const int cols = 5;
     layout->addWidget(w, i / cols, i % cols);
 }
 
-QList<YTStandardFeed*> StandardFeedsView::getMainFeeds() {
-    QList<YTStandardFeed*> feeds;
+void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWidget) {
+    qDebug() << videoSourceWidget->getVideoSource()->getName();
+    layout->removeWidget(videoSourceWidget);
+    videoSourceWidget->deleteLater();
 
-    feeds << buildStardardFeed("most_popular", tr("Most Popular"))
-          // << buildStardardFeed("recently_featured", tr("Featured"))
-          << buildStardardFeed("most_shared", tr("Most Shared"))
-          << buildStardardFeed("most_discussed", tr("Most Discussed"))
-          << buildStardardFeed("top_rated", tr("Top Rated"))
-          << buildStardardFeed("most_popular", tr("All Time Popular"), "all_time");
+    const int layoutCount = layout->count();
+    QVector<QLayoutItem *> items;
+    items.reserve(layoutCount);
 
-    return feeds;
+    for (int i = layoutCount - 1; i >= 0; i--) {
+        QLayoutItem *item = layout->takeAt(i);
+        if (item && item->widget()) items.append(item);
+    }
+
+    const int itemCount = items.size();
+    const int cols = 4; // itemCount / 3;
+    for (int i = itemCount - 1; i >= 0; i--) {
+        QLayoutItem *item = items.at(i);
+        int index = itemCount - 1 - i;
+        layout->addItem(item, index / cols, index % cols);
+    }
 }
 
-YTStandardFeed* StandardFeedsView::buildStardardFeed(
-        QString feedId, QString label, QString time) {
+void StandardFeedsView::resetLayout() {
+    if (layout) {
+        while (QLayoutItem *item = layout->takeAt(0)) {
+            delete item->widget();
+            delete item;
+        }
+        delete layout;
+    }
+
+    layout = new QGridLayout(this);
+    layout->setMargin(0);
+    layout->setSpacing(0);
+}
+
+YTStandardFeed *
+StandardFeedsView::buildStandardFeed(const QString &feedId, const QString &label, QString time) {
     YTStandardFeed *feed = new YTStandardFeed(this);
     feed->setFeedId(feedId);
     feed->setLabel(label);
@@ -123,15 +126,18 @@ YTStandardFeed* StandardFeedsView::buildStardardFeed(
 }
 
 void StandardFeedsView::appear() {
-    setFocus();
-    if (!layout) load();
+    if (!layout) {
+        update();
+        qApp->processEvents();
+        load();
+    }
     QAction *regionAction = MainWindow::instance()->getRegionAction();
-    regionAction->setVisible(true);
+    MainWindow::instance()->showActionsInStatusBar({regionAction}, true);
 }
 
 void StandardFeedsView::disappear() {
     QAction *regionAction = MainWindow::instance()->getRegionAction();
-    regionAction->setVisible(false);
+    MainWindow::instance()->showActionsInStatusBar({regionAction}, false);
 }
 
 void StandardFeedsView::selectWorldwideRegion() {
@@ -146,6 +152,4 @@ void StandardFeedsView::selectLocalRegion() {
 
 void StandardFeedsView::paintEvent(QPaintEvent *event) {
     QWidget::paintEvent(event);
-    PainterUtils::topShadow(this);
 }
-