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 "channelview.h"
24 #include "searchparams.h"
25 #include "channelmodel.h"
26 #include "channelitemdelegate.h"
29 #include "channelaggregator.h"
30 #include "aggregatevideosource.h"
31 #include "painterutils.h"
32 #include "mainwindow.h"
38 static const char *sortByKey = "subscriptionsSortBy";
39 static const char *showUpdatedKey = "subscriptionsShowUpdated";
41 ChannelView::ChannelView(QWidget *parent) : QListView(parent),
45 setItemDelegate(new ChannelItemDelegate(this));
46 setSelectionMode(QAbstractItemView::NoSelection);
50 setFlow(QListView::LeftToRight);
52 setResizeMode(QListView::Adjust);
53 setMovement(QListView::Static);
54 setUniformItemSizes(true);
57 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
58 setFrameShape(QFrame::NoFrame);
59 setAttribute(Qt::WA_MacShowFocusRect, false);
61 QPalette p = palette();
63 p.setColor(QPalette::Base, p.window().color());
64 p.setColor(QPalette::Text, p.windowText().color());
66 p.setColor(QPalette::Disabled, QPalette::Base, p.base().color());
67 p.setColor(QPalette::Disabled, QPalette::Text, p.text().color());
70 verticalScrollBar()->setPageStep(3);
71 verticalScrollBar()->setSingleStep(1);
73 setMouseTracking(true);
75 connect(this, SIGNAL(clicked(const QModelIndex &)),
76 SLOT(itemActivated(const QModelIndex &)));
77 connect(this, SIGNAL(entered(const QModelIndex &)),
78 SLOT(itemEntered(const QModelIndex &)));
80 channelsModel = new ChannelModel(this);
81 setModel(channelsModel);
82 connect(this, SIGNAL(viewportEntered()),
83 channelsModel, SLOT(clearHover()));
87 connect(ChannelAggregator::instance(), SIGNAL(channelChanged(YTUser*)),
88 channelsModel, SLOT(updateChannel(YTUser*)));
89 connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
90 SLOT(unwatchedCountChanged(int)));
92 unwatchedCountChanged(ChannelAggregator::instance()->getUnwatchedCount());
95 void ChannelView::setupActions() {
98 markAsWatchedAction = new QAction(
99 Utils::icon("mark-watched"), tr("Mark all as watched"), this);
100 markAsWatchedAction->setEnabled(false);
101 markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W));
102 connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched()));
103 statusActions << markAsWatchedAction;
105 showUpdated = settings.value(showUpdatedKey, false).toBool();
106 QAction *showUpdatedAction = new QAction(
107 Utils::icon("show-updated"), tr("Show Updated"), this);
108 showUpdatedAction->setCheckable(true);
109 showUpdatedAction->setChecked(showUpdated);
110 showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U));
111 connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
112 statusActions << showUpdatedAction;
114 SortBy sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
116 QMenu *sortMenu = new QMenu(this);
117 QActionGroup *sortGroup = new QActionGroup(this);
119 QAction *sortByNameAction = new QAction(tr("Name"), this);
120 sortByNameAction->setActionGroup(sortGroup);
121 sortByNameAction->setCheckable(true);
122 if (sortBy == SortByName) sortByNameAction->setChecked(true);
123 connect(sortByNameAction, SIGNAL(triggered()), SLOT(setSortByName()));
124 sortMenu->addAction(sortByNameAction);
126 QAction *sortByUpdatedAction = new QAction(tr("Last Updated"), this);
127 sortByUpdatedAction->setActionGroup(sortGroup);
128 sortByUpdatedAction->setCheckable(true);
129 if (sortBy == SortByUpdated) sortByUpdatedAction->setChecked(true);
130 connect(sortByUpdatedAction, SIGNAL(triggered()), SLOT(setSortByUpdated()));
131 sortMenu->addAction(sortByUpdatedAction);
133 QAction *sortByAddedAction = new QAction(tr("Last Added"), this);
134 sortByAddedAction->setActionGroup(sortGroup);
135 sortByAddedAction->setCheckable(true);
136 if (sortBy == SortByAdded) sortByAddedAction->setChecked(true);
137 connect(sortByAddedAction, SIGNAL(triggered()), SLOT(setSortByAdded()));
138 sortMenu->addAction(sortByAddedAction);
140 QAction *sortByLastWatched = new QAction(tr("Last Watched"), this);
141 sortByLastWatched->setActionGroup(sortGroup);
142 sortByLastWatched->setCheckable(true);
143 if (sortBy == SortByLastWatched) sortByLastWatched->setChecked(true);
144 connect(sortByLastWatched, SIGNAL(triggered()), SLOT(setSortByLastWatched()));
145 sortMenu->addAction(sortByLastWatched);
147 QAction *sortByMostWatched = new QAction(tr("Most Watched"), this);
148 sortByMostWatched->setActionGroup(sortGroup);
149 sortByMostWatched->setCheckable(true);
150 if (sortBy == SortByMostWatched) sortByMostWatched->setChecked(true);
151 connect(sortByMostWatched, SIGNAL(triggered()), SLOT(setSortByMostWatched()));
152 sortMenu->addAction(sortByMostWatched);
154 QToolButton *sortButton = new QToolButton(this);
155 sortButton->setText(tr("Sort by"));
156 sortButton->setIcon(Utils::icon("sort"));
157 sortButton->setIconSize(QSize(16, 16));
158 sortButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
159 sortButton->setPopupMode(QToolButton::InstantPopup);
160 sortButton->setMenu(sortMenu);
161 QWidgetAction *widgetAction = new QWidgetAction(this);
162 widgetAction->setDefaultWidget(sortButton);
163 widgetAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
164 statusActions << widgetAction;
166 foreach (QAction *action, statusActions) {
167 window()->addAction(action);
168 Utils::setupAction(action);
172 void ChannelView::appear() {
174 foreach (QAction* action, statusActions)
175 MainWindow::instance()->showActionInStatusBar(action, true);
177 ChannelAggregator::instance()->run();
180 void ChannelView::disappear() {
181 foreach (QAction* action, statusActions)
182 MainWindow::instance()->showActionInStatusBar(action, false);
185 void ChannelView::mouseMoveEvent(QMouseEvent *event) {
186 QListView::mouseMoveEvent(event);
187 const QModelIndex index = indexAt(event->pos());
188 if (index.isValid()) setCursor(Qt::PointingHandCursor);
192 void ChannelView::leaveEvent(QEvent *event) {
193 QListView::leaveEvent(event);
194 // channelsModel->clearHover();
197 void ChannelView::itemEntered(const QModelIndex &index) {
198 // channelsModel->setHoveredRow(index.row());
201 void ChannelView::itemActivated(const QModelIndex &index) {
202 ChannelModel::ItemTypes itemType = channelsModel->typeForIndex(index);
203 if (itemType == ChannelModel::ItemChannel) {
204 YTUser *user = channelsModel->userForIndex(index);
205 SearchParams *params = new SearchParams();
206 params->setAuthor(user->getUserId());
207 params->setSortBy(SearchParams::SortByNewest);
208 params->setTransient(true);
209 YTSearch *videoSource = new YTSearch(params, this);
210 emit activated(videoSource);
211 user->updateWatched();
212 } else if (itemType == ChannelModel::ItemAggregate) {
213 AggregateVideoSource *videoSource = new AggregateVideoSource(this);
214 videoSource->setName(tr("All Videos"));
215 emit activated(videoSource);
216 } else if (itemType == ChannelModel::ItemUnwatched) {
217 AggregateVideoSource *videoSource = new AggregateVideoSource(this);
218 videoSource->setName(tr("Unwatched Videos"));
219 videoSource->setUnwatched(true);
220 emit activated(videoSource);
224 void ChannelView::paintEvent(QPaintEvent *event) {
225 if (model()->rowCount() < 3) {
227 if (!errorMessage.isEmpty())
229 else if (showUpdated)
230 msg = tr("There are no updated subscriptions at this time.");
232 msg = tr("You have no subscriptions. "
233 "Use the star symbol to subscribe to channels.");
234 PainterUtils::centeredMessage(msg, viewport());
235 } else QListView::paintEvent(event);
236 PainterUtils::topShadow(viewport());
239 void ChannelView::toggleShowUpdated(bool enable) {
240 showUpdated = enable;
243 settings.setValue(showUpdatedKey, showUpdated);
246 void ChannelView::updateQuery(bool transition) {
247 errorMessage.clear();
248 if (!Database::exists()) return;
250 QString sql = "select user_id from subscriptions";
252 sql += " where notify_count>0";
256 sql += " order by updated desc";
259 sql += " order by added desc";
261 case SortByLastWatched:
262 sql += " order by watched desc";
264 case SortByMostWatched:
265 sql += " order by views desc";
268 sql += " order by name collate nocase";
274 Extra::fadeInWidget(this, this);
277 channelsModel->setQuery(sql, Database::instance().getConnection());
278 if (channelsModel->lastError().isValid()) {
279 qWarning() << channelsModel->lastError().text();
280 errorMessage = channelsModel->lastError().text();
284 void ChannelView::setSortBy(SortBy sortBy) {
285 this->sortBy = sortBy;
288 settings.setValue(sortByKey, (int)sortBy);
291 void ChannelView::markAllAsWatched() {
292 ChannelAggregator::instance()->markAllAsWatched();
294 markAsWatchedAction->setEnabled(false);
297 void ChannelView::unwatchedCountChanged(int count) {
298 markAsWatchedAction->setEnabled(count > 0);
299 channelsModel->updateUnwatched();