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 "playlistmodel.h"
22 #include "videomimedata.h"
23 #include "videosource.h"
26 #include "searchparams.h"
27 #include "mediaview.h"
29 static const int maxItems = 10;
30 static const QString recentKeywordsKey = "recentKeywords";
31 static const QString recentChannelsKey = "recentChannels";
33 PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) {
43 authorHovered = false;
44 authorPressed = false;
47 int PlaylistModel::rowCount(const QModelIndex &/*parent*/) const {
48 int count = videos.size();
50 // add the message item
51 if (videos.isEmpty() || !searching)
57 QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
59 int row = index.row();
61 if (row == videos.size()) {
65 boldFont.setBold(true);
69 return ItemTypeShowMore;
71 if (!errorMessage.isEmpty()) return errorMessage;
72 if (searching) return tr("Searching...");
73 if (canSearchMore) return tr("Show %1 More").arg(maxItems);
74 if (videos.isEmpty()) return tr("No videos");
75 else return tr("No more videos");
76 case Qt::TextAlignmentRole:
77 return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
78 case Qt::ForegroundRole:
79 if (!errorMessage.isEmpty())
80 return palette.color(QPalette::ToolTipText);
82 return palette.color(QPalette::Dark);
83 case Qt::BackgroundColorRole:
84 if (!errorMessage.isEmpty())
85 return palette.color(QPalette::ToolTipBase);
94 } else if (row < 0 || row >= videos.size())
97 Video *video = videos.at(row);
101 return ItemTypeVideo;
103 return QVariant::fromValue(QPointer<Video>(video));
104 case ActiveTrackRole:
105 return video == m_activeVideo;
106 case Qt::DisplayRole:
107 return video->title();
108 case HoveredItemRole:
109 return hoveredRow == index.row();
110 case AuthorHoveredRole:
111 return authorHovered;
112 case AuthorPressedRole:
113 return authorPressed;
114 case Qt::StatusTipRole:
115 return video->description();
121 void PlaylistModel::setActiveRow(int row, bool notify) {
122 if ( rowExists( row ) ) {
125 m_activeVideo = videoAt(row);
127 int oldactiverow = m_activeRow;
129 if ( rowExists( oldactiverow ) )
130 emit dataChanged( createIndex( oldactiverow, 0 ), createIndex( oldactiverow, columnCount() - 1 ) );
132 emit dataChanged( createIndex( m_activeRow, 0 ), createIndex( m_activeRow, columnCount() - 1 ) );
133 if (notify) emit activeRowChanged(row);
142 int PlaylistModel::nextRow() const {
143 int nextRow = m_activeRow + 1;
144 if (rowExists(nextRow))
149 int PlaylistModel::previousRow() const {
150 int prevRow = m_activeRow - 1;
151 if (rowExists(prevRow))
156 Video* PlaylistModel::videoAt( int row ) const {
157 if ( rowExists( row ) )
158 return videos.at( row );
162 Video* PlaylistModel::activeVideo() const {
163 return m_activeVideo;
166 void PlaylistModel::setVideoSource(VideoSource *videoSource) {
168 while (!videos.isEmpty())
169 delete videos.takeFirst();
175 this->videoSource = videoSource;
176 connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
177 SLOT(addVideos(QList<Video*>)), Qt::UniqueConnection);
178 connect(videoSource, SIGNAL(finished(int)),
179 SLOT(searchFinished(int)), Qt::UniqueConnection);
180 connect(videoSource, SIGNAL(error(QString)),
181 SLOT(searchError(QString)), Qt::UniqueConnection);
186 void PlaylistModel::searchMore(int max) {
187 if (searching) return;
189 firstSearch = skip == 1;
191 errorMessage.clear();
192 videoSource->loadVideos(max, skip);
196 void PlaylistModel::searchMore() {
197 searchMore(maxItems);
200 void PlaylistModel::searchNeeded() {
201 int remainingRows = videos.size() - m_activeRow;
202 int rowsNeeded = maxItems - remainingRows;
204 searchMore(rowsNeeded);
207 void PlaylistModel::abortSearch() {
209 while (!videos.isEmpty())
210 delete videos.takeFirst();
211 // if (videoSource) videoSource->abort();
219 void PlaylistModel::searchFinished(int total) {
221 canSearchMore = total >= max;
223 // update the message item
224 emit dataChanged( createIndex( maxItems, 0 ), createIndex( maxItems, columnCount() - 1 ) );
226 if (!videoSource->getSuggestions().isEmpty())
227 emit haveSuggestions(videoSource->getSuggestions());
229 if (firstSearch && !videos.isEmpty())
230 handleFirstVideo(videos.first());
233 void PlaylistModel::searchError(QString message) {
234 errorMessage = message;
235 // update the message item
236 emit dataChanged( createIndex( maxItems, 0 ), createIndex( maxItems, columnCount() - 1 ) );
239 void PlaylistModel::addVideos(QList<Video*> newVideos) {
240 if (newVideos.isEmpty()) return;
241 beginInsertRows(QModelIndex(), videos.size(), videos.size() + newVideos.size() - 2);
242 videos.append(newVideos);
244 foreach (Video* video, newVideos) {
245 connect(video, SIGNAL(gotThumbnail()),
246 SLOT(updateThumbnail()), Qt::UniqueConnection);
247 video->loadThumbnail();
251 void PlaylistModel::handleFirstVideo(Video *video) {
253 int currentVideoRow = rowForCloneVideo(MediaView::instance()->getCurrentVideoId());
254 if (currentVideoRow != -1) setActiveRow(currentVideoRow, false);
257 if (!settings.value("manualplay", false).toBool())
262 if (!settings.value("manualplay", false).toBool()) {
263 int newActiveRow = rowForCloneVideo(MediaView::instance()->getCurrentVideoId());
264 if (newActiveRow != -1) setActiveRow(newActiveRow, false);
265 else setActiveRow(0);
268 if (videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
270 static const int maxRecentElements = 10;
272 YTSearch *search = dynamic_cast<YTSearch *>(videoSource);
273 SearchParams *searchParams = search->getSearchParams();
276 QString query = searchParams->keywords();
277 if (!query.isEmpty() && !searchParams->isTransient()) {
278 if (query.startsWith("http://")) {
279 // Save the video title
280 query += "|" + videos.first()->title();
282 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
283 keywords.removeAll(query);
284 keywords.prepend(query);
285 while (keywords.size() > maxRecentElements)
286 keywords.removeLast();
287 settings.setValue(recentKeywordsKey, keywords);
291 QString channel = searchParams->author();
292 if (!channel.isEmpty() && !searchParams->isTransient()) {
294 if (!video->userId().isEmpty() && video->userId() != video->author())
295 value = video->userId() + "|" + video->author();
296 else value = video->author();
297 QStringList channels = settings.value(recentChannelsKey).toStringList();
298 channels.removeAll(value);
299 channels.removeAll(channel);
300 channels.prepend(value);
301 while (channels.size() > maxRecentElements)
302 channels.removeLast();
303 settings.setValue(recentChannelsKey, channels);
308 void PlaylistModel::updateThumbnail() {
310 Video *video = static_cast<Video *>(sender());
312 qDebug() << "Cannot get sender";
316 int row = rowForVideo(video);
317 emit dataChanged( createIndex( row, 0 ), createIndex( row, columnCount() - 1 ) );
324 * This function does not free memory
326 bool PlaylistModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
327 beginRemoveRows(QModelIndex(), position, position+rows-1);
328 for (int row = 0; row < rows; ++row) {
329 videos.removeAt(position);
335 void PlaylistModel::removeIndexes(QModelIndexList &indexes) {
336 QList<Video*> originalList(videos);
337 QList<Video*> delitems;
338 foreach (QModelIndex index, indexes) {
339 if (index.row() >= originalList.size()) continue;
340 Video* video = originalList.at(index.row());
341 int idx = videos.indexOf(video);
343 beginRemoveRows(QModelIndex(), idx, idx);
344 delitems.append(video);
345 videos.removeAll(video);
350 qDeleteAll(delitems);
354 // --- Sturm und drang ---
358 Qt::DropActions PlaylistModel::supportedDropActions() const {
359 return Qt::CopyAction;
362 Qt::DropActions PlaylistModel::supportedDragActions() const {
363 return Qt::CopyAction;
366 Qt::ItemFlags PlaylistModel::flags(const QModelIndex &index) const {
367 if (index.isValid()) {
368 if (index.row() == videos.size()) {
369 // don't drag the "show 10 more" item
370 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
371 } else return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
373 return Qt::ItemIsDropEnabled;
376 QStringList PlaylistModel::mimeTypes() const {
378 types << "application/x-minitube-video";
382 QMimeData* PlaylistModel::mimeData( const QModelIndexList &indexes ) const {
383 VideoMimeData* mime = new VideoMimeData();
385 foreach( const QModelIndex &it, indexes ) {
387 if (row >= 0 && row < videos.size())
388 mime->addVideo( videos.at( it.row() ) );
394 bool PlaylistModel::dropMimeData(const QMimeData *data,
395 Qt::DropAction action, int row, int column,
396 const QModelIndex &parent) {
397 if (action == Qt::IgnoreAction)
400 if (!data->hasFormat("application/x-minitube-video"))
409 else if (parent.isValid())
410 beginRow = parent.row();
412 beginRow = rowCount(QModelIndex());
414 const VideoMimeData* videoMimeData = dynamic_cast<const VideoMimeData*>( data );
415 if(!videoMimeData ) return false;
417 QList<Video*> droppedVideos = videoMimeData->videos();
418 foreach( Video *video, droppedVideos) {
421 int videoRow = videos.indexOf(video);
422 removeRows(videoRow, 1, QModelIndex());
424 // and then add them again at the new position
425 beginInsertRows(QModelIndex(), beginRow, beginRow);
426 videos.insert(beginRow, video);
431 // fix m_activeRow after all this
432 m_activeRow = videos.indexOf(m_activeVideo);
434 // let the MediaView restore the selection
435 emit needSelectionFor(droppedVideos);
441 int PlaylistModel::rowForCloneVideo(const QString &videoId) const {
442 for (int i = 0; i < videos.size(); ++i) {
443 Video *v = videos.at(i);
444 if (v->id() == videoId) return i;
449 int PlaylistModel::rowForVideo(Video* video) {
450 return videos.indexOf(video);
453 QModelIndex PlaylistModel::indexForVideo(Video* video) {
454 return createIndex(videos.indexOf(video), 0);
457 void PlaylistModel::move(QModelIndexList &indexes, bool up) {
458 QList<Video*> movedVideos;
460 foreach (QModelIndex index, indexes) {
461 int row = index.row();
462 if (row >= videos.size()) continue;
463 // qDebug() << "index row" << row;
464 Video *video = videoAt(row);
465 movedVideos << video;
468 int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1;
469 foreach (Video *video, movedVideos) {
471 int row = rowForVideo(video);
472 if (row+mod==end) { end=row; continue; }
473 // qDebug() << "video row" << row;
474 removeRows(row, 1, QModelIndex());
479 beginInsertRows(QModelIndex(), row, row);
480 videos.insert(row, video);
485 emit needSelectionFor(movedVideos);
491 void PlaylistModel::setHoveredRow(int row) {
492 int oldRow = hoveredRow;
494 emit dataChanged( createIndex( oldRow, 0 ), createIndex( oldRow, columnCount() - 1 ) );
495 emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
498 void PlaylistModel::clearHover() {
499 emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
503 /* clickable author */
505 void PlaylistModel::enterAuthorHover() {
506 if (authorHovered) return;
507 authorHovered = true;
511 void PlaylistModel::exitAuthorHover() {
512 if (!authorHovered) return;
513 authorHovered = false;
515 setHoveredRow(hoveredRow);
518 void PlaylistModel::enterAuthorPressed() {
519 if (authorPressed) return;
520 authorPressed = true;
524 void PlaylistModel::exitAuthorPressed() {
525 if (!authorPressed) return;
526 authorPressed = false;
530 void PlaylistModel::updateAuthor() {
531 emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );