]> git.sur5r.net Git - minitube/blobdiff - src/channelview.cpp
New upstream version 2.9
[minitube] / src / channelview.cpp
index af5cb0184f8b57c4caf1f739424209473acbef62..d6dcd46556a34cffa93d44c3eeb486e08aecb56d 100644 (file)
@@ -19,73 +19,50 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "channelview.h"
-#include "ytuser.h"
+#include "ytchannel.h"
 #include "ytsearch.h"
 #include "searchparams.h"
 #include "channelmodel.h"
 #include "channelitemdelegate.h"
 #include "database.h"
-#include "ytsearch.h"
 #include "channelaggregator.h"
 #include "aggregatevideosource.h"
-#include "painterutils.h"
 #include "mainwindow.h"
 #include "iconutils.h"
 #ifdef APP_EXTRA
 #include "extra.h"
 #endif
+#include "channellistview.h"
 
-static const char *sortByKey = "subscriptionsSortBy";
-static const char *showUpdatedKey = "subscriptionsShowUpdated";
+namespace {
+static const QString sortByKey = "subscriptionsSortBy";
+static const QString showUpdatedKey = "subscriptionsShowUpdated";
+}
 
-ChannelView::ChannelView(QWidget *parent) : QListView(parent),
+ChannelView::ChannelView(QWidget *parent) : View(parent),
     showUpdated(false),
     sortBy(SortByName) {
 
-    setItemDelegate(new ChannelItemDelegate(this));
-    setSelectionMode(QAbstractItemView::NoSelection);
-
-    // layout
-    setSpacing(15);
-    setFlow(QListView::LeftToRight);
-    setWrapping(true);
-    setResizeMode(QListView::Adjust);
-    setMovement(QListView::Static);
-    setUniformItemSizes(true);
-
-    // cosmetics
-    setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
-    setFrameShape(QFrame::NoFrame);
-    setAttribute(Qt::WA_MacShowFocusRect, false);
+    QBoxLayout *layout = new QVBoxLayout(this);
+    layout->setMargin(0);
+    layout->setSpacing(0);
 
-    QPalette p = palette();
-    /*
-    p.setColor(QPalette::Base, p.window().color());
-    p.setColor(QPalette::Text, p.windowText().color());
-    */
-    p.setColor(QPalette::Disabled, QPalette::Base, p.base().color());
-    p.setColor(QPalette::Disabled, QPalette::Text, p.text().color());
-    setPalette(p);
+    listView = new ChannelListView();
+    listView->setItemDelegate(new ChannelItemDelegate(this));
 
-    verticalScrollBar()->setPageStep(3);
-    verticalScrollBar()->setSingleStep(1);
+    channelsModel = new ChannelModel(this);
+    listView->setModel(channelsModel);
 
-    setMouseTracking(true);
+    connect(listView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemActivated(const QModelIndex &)));
+    connect(listView, SIGNAL(contextMenu(QPoint)), SLOT(showContextMenu(QPoint)));
+    connect(listView, SIGNAL(viewportEntered()), channelsModel, SLOT(clearHover()));
 
-    connect(this, SIGNAL(clicked(const QModelIndex &)),
-            SLOT(itemActivated(const QModelIndex &)));
-    connect(this, SIGNAL(entered(const QModelIndex &)),
-            SLOT(itemEntered(const QModelIndex &)));
-
-    channelsModel = new ChannelModel(this);
-    setModel(channelsModel);
-    connect(this, SIGNAL(viewportEntered()),
-            channelsModel, SLOT(clearHover()));
+    layout->addWidget(listView);
 
     setupActions();
 
-    connect(ChannelAggregator::instance(), SIGNAL(channelChanged(YTUser*)),
-            channelsModel, SLOT(updateChannel(YTUser*)));
+    connect(ChannelAggregator::instance(), SIGNAL(channelChanged(YTChannel*)),
+            channelsModel, SLOT(updateChannel(YTChannel*)));
     connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
             SLOT(unwatchedCountChanged(int)));
 
@@ -95,7 +72,7 @@ ChannelView::ChannelView(QWidget *parent) : QListView(parent),
 void ChannelView::setupActions() {
     QSettings settings;
 
-    SortBy sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
+    sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
 
     QMenu *sortMenu = new QMenu(this);
     QActionGroup *sortGroup = new QActionGroup(this);
@@ -163,65 +140,48 @@ void ChannelView::setupActions() {
     connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
     statusActions << showUpdatedAction;
 
-    foreach (QAction *action, statusActions) {
+    for (QAction *action : statusActions) {
         window()->addAction(action);
         IconUtils::setupAction(action);
     }
 }
 
+QString ChannelView::noSubscriptionsMessage() {
+    return tr("You have no subscriptions. "
+                    "Use the star symbol to subscribe to channels.");
+}
+
 void ChannelView::appear() {
     updateQuery();
-    foreach (QAction* action, statusActions)
+    for (QAction* action : statusActions)
         MainWindow::instance()->showActionInStatusBar(action, true);
     setFocus();
-    ChannelAggregator::instance()->run();
+    ChannelAggregator::instance()->start();
 }
 
 void ChannelView::disappear() {
-    foreach (QAction* action, statusActions)
+    for (QAction* action : statusActions)
         MainWindow::instance()->showActionInStatusBar(action, false);
 }
 
-void ChannelView::mousePressEvent(QMouseEvent *event) {
-    if (event->button() == Qt::RightButton)
-        showContextMenu(event->pos());
-    else
-        QListView::mousePressEvent(event);
-}
-
-void ChannelView::mouseMoveEvent(QMouseEvent *event) {
-    QListView::mouseMoveEvent(event);
-    const QModelIndex index = indexAt(event->pos());
-    if (index.isValid()) setCursor(Qt::PointingHandCursor);
-    else unsetCursor();
-}
-
-void ChannelView::leaveEvent(QEvent *event) {
-    QListView::leaveEvent(event);
-    // channelsModel->clearHover();
-}
-
-void ChannelView::itemEntered(const QModelIndex &index) {
-    // channelsModel->setHoveredRow(index.row());
-}
-
 void ChannelView::itemActivated(const QModelIndex &index) {
     ChannelModel::ItemTypes itemType = channelsModel->typeForIndex(index);
     if (itemType == ChannelModel::ItemChannel) {
-        YTUser *user = channelsModel->userForIndex(index);
+        YTChannel *channel = channelsModel->channelForIndex(index);
         SearchParams *params = new SearchParams();
-        params->setAuthor(user->getUserId());
+        params->setChannelId(channel->getChannelId());
         params->setSortBy(SearchParams::SortByNewest);
         params->setTransient(true);
-        YTSearch *videoSource = new YTSearch(params, this);
+        YTSearch *videoSource = new YTSearch(params);
+        videoSource->setAsyncDetails(true);
         emit activated(videoSource);
-        user->updateWatched();
+        channel->updateWatched();
     } else if (itemType == ChannelModel::ItemAggregate) {
-        AggregateVideoSource *videoSource = new AggregateVideoSource(this);
+        AggregateVideoSource *videoSource = new AggregateVideoSource();
         videoSource->setName(tr("All Videos"));
         emit activated(videoSource);
     } else if (itemType == ChannelModel::ItemUnwatched) {
-        AggregateVideoSource *videoSource = new AggregateVideoSource(this);
+        AggregateVideoSource *videoSource = new AggregateVideoSource();
         videoSource->setName(tr("Unwatched Videos"));
         videoSource->setUnwatched(true);
         emit activated(videoSource);
@@ -229,18 +189,18 @@ void ChannelView::itemActivated(const QModelIndex &index) {
 }
 
 void ChannelView::showContextMenu(const QPoint &point) {
-    const QModelIndex index = indexAt(point);
+    const QModelIndex index = listView->indexAt(point);
     if (!index.isValid()) return;
 
-    YTUser *user = channelsModel->userForIndex(index);
-    if (!user) return;
+    YTChannel *channel = channelsModel->channelForIndex(index);
+    if (!channel) return;
 
     unsetCursor();
 
     QMenu menu;
 
-    if (user->getNotifyCount() > 0) {
-        QAction *markAsWatchedAction = menu.addAction(tr("Mark as Watched"), user, SLOT(updateWatched()));
+    if (channel->getNotifyCount() > 0) {
+        QAction *markAsWatchedAction = menu.addAction(tr("Mark as Watched"), channel, SLOT(updateWatched()));
         connect(markAsWatchedAction, SIGNAL(triggered()),
                 ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
         menu.addSeparator();
@@ -253,28 +213,13 @@ void ChannelView::showContextMenu(const QPoint &point) {
     notificationsAction->setChecked(true);
     */
 
-    QAction *unsubscribeAction = menu.addAction(tr("Unsubscribe"), user, SLOT(unsubscribe()));
+    QAction *unsubscribeAction = menu.addAction(tr("Unsubscribe"), channel, SLOT(unsubscribe()));
     connect(unsubscribeAction, SIGNAL(triggered()),
             ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
 
     menu.exec(mapToGlobal(point));
 }
 
-void ChannelView::paintEvent(QPaintEvent *event) {
-    if (model()->rowCount() < 3) {
-        QString msg;
-        if (!errorMessage.isEmpty())
-            msg = errorMessage;
-        else if (showUpdated)
-            msg = tr("There are no updated subscriptions at this time.");
-        else
-            msg = tr("You have no subscriptions. "
-                     "Use the star symbol to subscribe to channels.");
-        PainterUtils::centeredMessage(msg, viewport());
-    } else QListView::paintEvent(event);
-    PainterUtils::topShadow(viewport());
-}
-
 void ChannelView::toggleShowUpdated(bool enable) {
     showUpdated = enable;
     updateQuery(true);
@@ -283,8 +228,13 @@ void ChannelView::toggleShowUpdated(bool enable) {
 }
 
 void ChannelView::updateQuery(bool transition) {
-    errorMessage.clear();
-    if (!Database::exists()) return;
+    Q_UNUSED(transition);
+    listView->clearErrorMessage();
+
+    if (!Database::exists()) {
+        listView->setErrorMessage(noSubscriptionsMessage());
+        return;
+    }
 
     QString sql = "select user_id from subscriptions";
     if (showUpdated)
@@ -316,7 +266,14 @@ void ChannelView::updateQuery(bool transition) {
     channelsModel->setQuery(sql, Database::instance().getConnection());
     if (channelsModel->lastError().isValid()) {
         qWarning() << channelsModel->lastError().text();
-        errorMessage = channelsModel->lastError().text();
+        listView->setErrorMessage(channelsModel->lastError().text());
+    } else if (channelsModel->rowCount() < 3) {
+        QString msg;
+        if (showUpdated)
+            msg = tr("There are no updated subscriptions at this time.");
+        else
+            msg = noSubscriptionsMessage();
+        listView->setErrorMessage(msg);
     }
 }