From 7ea6da0610dbf20814af8ec98cb17c4296183c97 Mon Sep 17 00:00:00 2001 From: Flavio Date: Fri, 18 Jul 2014 23:04:37 +0200 Subject: [PATCH] Subscriptions context menu --- src/channelaggregator.h | 2 +- src/channelmodel.cpp | 18 +++++++++++ src/channelmodel.h | 1 + src/channelview.cpp | 67 +++++++++++++++++++++++++++++++---------- src/channelview.h | 3 +- src/ytuser.cpp | 7 +++++ src/ytuser.h | 6 +++- 7 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/channelaggregator.h b/src/channelaggregator.h index 966fa70..1ef224f 100644 --- a/src/channelaggregator.h +++ b/src/channelaggregator.h @@ -41,6 +41,7 @@ public slots: void start(); void stop(); void run(); + void updateUnwatchedCount(); signals: void channelChanged(YTUser*); @@ -54,7 +55,6 @@ private: YTUser* getChannelToCheck(); void processNextChannel(); void addVideo(Video* video); - void updateUnwatchedCount(); void finish(); uint checkInterval; diff --git a/src/channelmodel.cpp b/src/channelmodel.cpp index 2aa6c43..fb8cbdb 100644 --- a/src/channelmodel.cpp +++ b/src/channelmodel.cpp @@ -56,6 +56,8 @@ QVariant ChannelModel::data(const QModelIndex &index, int role) const { } YTUser* ChannelModel::userForIndex(const QModelIndex &index) const { + const int row = index.row(); + if (row < channelOffset) return 0; return channels.at(index.row() - channelOffset); } @@ -84,6 +86,8 @@ void ChannelModel::setQuery(const QString &query, const QSqlDatabase &db) { while (q.next()) { YTUser *user = YTUser::forId(q.value(0).toString()); connect(user, SIGNAL(thumbnailLoaded()), SLOT(updateSender()), Qt::UniqueConnection); + connect(user, SIGNAL(notifyCountChanged()), SLOT(updateSender()), Qt::UniqueConnection); + connect(user, SIGNAL(destroyed(QObject *)), SLOT(removeChannel(QObject *)), Qt::UniqueConnection); channels << user; } @@ -116,6 +120,20 @@ void ChannelModel::updateUnwatched() { emit dataChanged(i, i); } +void ChannelModel::removeChannel(QObject *obj) { + YTUser *user = static_cast(obj); + qWarning() << "user is" << user << obj << obj->metaObject()->className(); + if (!user) return; + + int row = channels.indexOf(user); + if (row == -1) return; + + int position = row + channelOffset; + beginRemoveRows(QModelIndex(), position, position+1); + channels.removeAt(row); + endRemoveRows(); +} + void ChannelModel::setHoveredRow(int row) { int oldRow = hoveredRow; hoveredRow = row; diff --git a/src/channelmodel.h b/src/channelmodel.h index 3d89d4d..6ec5cf1 100644 --- a/src/channelmodel.h +++ b/src/channelmodel.h @@ -59,6 +59,7 @@ public slots: void updateSender(); void updateChannel(YTUser *user); void updateUnwatched(); + void removeChannel(QObject *obj); private: QList channels; diff --git a/src/channelview.cpp b/src/channelview.cpp index eba0591..eef1a72 100644 --- a/src/channelview.cpp +++ b/src/channelview.cpp @@ -72,6 +72,10 @@ ChannelView::ChannelView(QWidget *parent) : QListView(parent), setMouseTracking(true); + setContextMenuPolicy(Qt::CustomContextMenu); + connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), + SLOT(showContextMenu(const QPoint &))); + connect(this, SIGNAL(clicked(const QModelIndex &)), SLOT(itemActivated(const QModelIndex &))); connect(this, SIGNAL(entered(const QModelIndex &)), @@ -95,22 +99,6 @@ ChannelView::ChannelView(QWidget *parent) : QListView(parent), void ChannelView::setupActions() { QSettings settings; - markAsWatchedAction = new QAction( - Utils::icon("mark-watched"), tr("Mark all as watched"), this); - markAsWatchedAction->setEnabled(false); - markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W)); - connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched())); - statusActions << markAsWatchedAction; - - showUpdated = settings.value(showUpdatedKey, false).toBool(); - QAction *showUpdatedAction = new QAction( - Utils::icon("show-updated"), tr("Show Updated"), this); - showUpdatedAction->setCheckable(true); - showUpdatedAction->setChecked(showUpdated); - showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U)); - connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool))); - statusActions << showUpdatedAction; - SortBy sortBy = static_cast(settings.value(sortByKey, SortByName).toInt()); QMenu *sortMenu = new QMenu(this); @@ -163,6 +151,22 @@ void ChannelView::setupActions() { widgetAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O)); statusActions << widgetAction; + markAsWatchedAction = new QAction( + Utils::icon("mark-watched"), tr("Mark all as watched"), this); + markAsWatchedAction->setEnabled(false); + markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W)); + connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched())); + statusActions << markAsWatchedAction; + + showUpdated = settings.value(showUpdatedKey, false).toBool(); + QAction *showUpdatedAction = new QAction( + Utils::icon("show-updated"), tr("Show Updated"), this); + showUpdatedAction->setCheckable(true); + showUpdatedAction->setChecked(showUpdated); + showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U)); + connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool))); + statusActions << showUpdatedAction; + foreach (QAction *action, statusActions) { window()->addAction(action); Utils::setupAction(action); @@ -221,6 +225,37 @@ void ChannelView::itemActivated(const QModelIndex &index) { } } +void ChannelView::showContextMenu(const QPoint &point) { + const QModelIndex index = indexAt(point); + if (!index.isValid()) return; + + YTUser *user = channelsModel->userForIndex(index); + if (!user) return; + + unsetCursor(); + + QMenu menu; + + QAction *markAsWatchedAction = menu.addAction(tr("Mark as Watched"), user, SLOT(updateWatched())); + connect(markAsWatchedAction, SIGNAL(triggered()), + ChannelAggregator::instance(), SLOT(updateUnwatchedCount())); + + menu.addSeparator(); + + /* + // TODO + QAction *notificationsAction = menu.addAction(tr("Receive Notifications"), user, SLOT(unsubscribe())); + notificationsAction->setCheckable(true); + notificationsAction->setChecked(true); + */ + + QAction *unsubscribeAction = menu.addAction(tr("Unsubscribe"), user, 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; diff --git a/src/channelview.h b/src/channelview.h index a5013c9..1a6fd5a 100644 --- a/src/channelview.h +++ b/src/channelview.h @@ -58,6 +58,7 @@ private: private slots: void itemEntered(const QModelIndex &index); void itemActivated(const QModelIndex &index); + void showContextMenu(const QPoint &point); void toggleShowUpdated(bool enable); void setSortBy(SortBy sortBy); void setSortByName() { setSortBy(SortByName); } @@ -67,9 +68,9 @@ private slots: void setSortByMostWatched() { setSortBy(SortByMostWatched); } void markAllAsWatched(); void unwatchedCountChanged(int count); + void updateQuery(bool transition = false); private: - void updateQuery(bool transition = false); void setupActions(); ChannelModel *channelsModel; diff --git a/src/ytuser.cpp b/src/ytuser.cpp index 9dc633a..5098f3f 100644 --- a/src/ytuser.cpp +++ b/src/ytuser.cpp @@ -142,6 +142,10 @@ QString YTUser::getThumbnailLocation() { return getThumbnailDir() + userId; } +void YTUser::unsubscribe() { + YTUser::unsubscribe(userId); +} + void YTUser::storeThumbnail(QByteArray bytes) { thumbnail.loadFromData(bytes); static const int maxWidth = 88; @@ -265,6 +269,7 @@ void YTUser::updateWatched() { uint now = QDateTime::currentDateTime().toTime_t(); watched = now; notifyCount = 0; + emit notifyCountChanged(); QSqlDatabase db = Database::instance().getConnection(); QSqlQuery query(db); @@ -276,6 +281,8 @@ void YTUser::updateWatched() { } void YTUser::storeNotifyCount(int count) { + if (notifyCount != count) + emit notifyCountChanged(); notifyCount = count; QSqlDatabase db = Database::instance().getConnection(); diff --git a/src/ytuser.h b/src/ytuser.h index d7131cd..631a366 100644 --- a/src/ytuser.h +++ b/src/ytuser.h @@ -42,7 +42,6 @@ public: uint getWatched() const { return watched; } void setWatched(uint watched) { this->watched = watched; } - void updateWatched(); int getNotifyCount() const { return notifyCount; } void setNotifyCount(int count) { notifyCount = count; } @@ -62,10 +61,15 @@ public: static QList getCachedUsers() { return cache.values(); } +public slots: + void updateWatched(); + void unsubscribe(); + signals: void infoLoaded(); void thumbnailLoaded(); void error(QString message); + void notifyCountChanged(); private slots: void parseResponse(QByteArray bytes); -- 2.39.5