2 #include "videomimedata.h"
5 static const QString recentKeywordsKey = "recentKeywords";
7 ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
16 ListModel::~ListModel() {
20 int ListModel::rowCount(const QModelIndex &/*parent*/) const {
21 int count = videos.size();
23 // add the message item
24 if (videos.isEmpty() || !searching)
30 QVariant ListModel::data(const QModelIndex &index, int role) const {
32 int row = index.row();
34 if (row == videos.size()) {
38 boldFont.setBold(true);
42 return ItemTypeShowMore;
44 case Qt::StatusTipRole:
45 if (!errorMessage.isEmpty()) return errorMessage;
46 if (searching) return tr("Searching...");
47 if (canSearchMore) return tr("Show %1 More").arg(MAX_ITEMS);
48 if (videos.isEmpty()) return tr("No videos");
49 else return tr("No more videos");
50 case Qt::TextAlignmentRole:
51 return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
52 case Qt::ForegroundRole:
53 if (!errorMessage.isEmpty())
54 return palette.color(QPalette::ToolTipText);
56 return palette.color(QPalette::Dark);
57 case Qt::BackgroundColorRole:
58 if (!errorMessage.isEmpty())
59 return palette.color(QPalette::ToolTipBase);
68 } else if (row < 0 || row >= videos.size())
71 Video *video = videos.at(row);
77 return QVariant::fromValue(QPointer<Video>(video));
79 return video == m_activeVideo;
81 return video->title();
87 void ListModel::setActiveRow( int row) {
88 if ( rowExists( row ) ) {
91 m_activeVideo = videoAt(row);
93 int oldactiverow = m_activeRow;
95 if ( rowExists( oldactiverow ) )
96 emit dataChanged( createIndex( oldactiverow, 0 ), createIndex( oldactiverow, columnCount() - 1 ) );
98 emit dataChanged( createIndex( m_activeRow, 0 ), createIndex( m_activeRow, columnCount() - 1 ) );
99 emit activeRowChanged(row);
108 int ListModel::nextRow() const {
109 int nextRow = m_activeRow + 1;
110 if (rowExists(nextRow))
115 Video* ListModel::videoAt( int row ) const {
116 if ( rowExists( row ) )
117 return videos.at( row );
121 Video* ListModel::activeVideo() const {
122 return m_activeVideo;
125 void ListModel::search(SearchParams *searchParams) {
127 // delete current videos
128 while (!videos.isEmpty())
129 delete videos.takeFirst();
133 errorMessage.clear();
136 // (re)initialize the YouTubeSearch
137 if (youtubeSearch) delete youtubeSearch;
138 youtubeSearch = new YouTubeSearch();
139 connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
140 connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
141 connect(youtubeSearch, SIGNAL(error(QString)), this, SLOT(searchError(QString)));
143 this->searchParams = searchParams;
145 youtubeSearch->search(searchParams, MAX_ITEMS, skip);
149 void ListModel::searchMore(int max) {
150 if (searching) return;
152 errorMessage.clear();
153 youtubeSearch->search(searchParams, max, skip);
157 void ListModel::searchMore() {
158 searchMore(MAX_ITEMS);
161 void ListModel::searchNeeded() {
162 int remainingRows = videos.size() - m_activeRow;
163 int rowsNeeded = MAX_ITEMS - remainingRows;
165 searchMore(rowsNeeded);
168 void ListModel::abortSearch() {
169 while (!videos.isEmpty())
170 delete videos.takeFirst();
172 youtubeSearch->abort();
176 void ListModel::searchFinished(int total) {
178 canSearchMore = total > 0;
180 // update the message item
181 emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
184 void ListModel::searchError(QString message) {
185 errorMessage = message;
186 // update the message item
187 emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
190 void ListModel::addVideo(Video* video) {
192 connect(video, SIGNAL(gotThumbnail()), this, SLOT(updateThumbnail()));
194 beginInsertRows(QModelIndex(), videos.size(), videos.size());
199 if (videos.size() == 1) {
204 QString query = searchParams->keywords();
206 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
207 keywords.removeAll(query);
208 keywords.prepend(query);
209 while (keywords.size() > 10)
210 keywords.removeLast();
211 settings.setValue(recentKeywordsKey, keywords);
216 void ListModel::updateThumbnail() {
218 Video *video = static_cast<Video *>(sender());
220 qDebug() << "Cannot get sender";
224 int row = rowForVideo(video);
225 emit dataChanged( createIndex( row, 0 ), createIndex( row, columnCount() - 1 ) );
232 * This function does not free memory
234 bool ListModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
235 beginRemoveRows(QModelIndex(), position, position+rows-1);
236 for (int row = 0; row < rows; ++row) {
237 videos.removeAt(position);
243 void ListModel::removeIndexes(QModelIndexList &indexes) {
244 QList<Video*> originalList(videos);
245 QList<Video*> delitems;
246 foreach (QModelIndex index, indexes) {
247 if (index.row() >= originalList.size()) continue;
248 Video* video = originalList.at(index.row());
249 int idx = videos.indexOf(video);
251 beginRemoveRows(QModelIndex(), idx, idx);
252 delitems.append(video);
253 videos.removeAll(video);
258 qDeleteAll(delitems);
262 // --- Sturm und drang ---
266 Qt::DropActions ListModel::supportedDropActions() const {
267 return Qt::MoveAction;
270 Qt::ItemFlags ListModel::flags(const QModelIndex &index) const {
271 Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
273 if (index.isValid()) {
274 if (index.row() == videos.size()) {
275 // don't drag the "show 10 more" item
278 return ( defaultFlags | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled );
280 return Qt::ItemIsDropEnabled | defaultFlags;
283 QStringList ListModel::mimeTypes() const {
285 types << "application/x-minitube-video";
289 QMimeData* ListModel::mimeData( const QModelIndexList &indexes ) const {
290 VideoMimeData* mime = new VideoMimeData();
292 foreach( const QModelIndex &it, indexes ) {
294 if (row >= 0 && row < videos.size())
295 mime->addVideo( videos.at( it.row() ) );
301 bool ListModel::dropMimeData(const QMimeData *data,
302 Qt::DropAction action, int row, int column,
303 const QModelIndex &parent) {
304 if (action == Qt::IgnoreAction)
307 if (!data->hasFormat("application/x-minitube-video"))
316 else if (parent.isValid())
317 beginRow = parent.row();
319 beginRow = rowCount(QModelIndex());
321 const VideoMimeData* videoMimeData = dynamic_cast<const VideoMimeData*>( data );
322 if(!videoMimeData ) return false;
324 QList<Video*> droppedVideos = videoMimeData->videos();
325 foreach( Video *video, droppedVideos) {
328 int videoRow = videos.indexOf(video);
329 removeRows(videoRow, 1, QModelIndex());
331 // and then add them again at the new position
332 beginInsertRows(QModelIndex(), beginRow, beginRow);
333 videos.insert(beginRow, video);
338 // fix m_activeRow after all this
339 m_activeRow = videos.indexOf(m_activeVideo);
341 // let the MediaView restore the selection
342 emit needSelectionFor(droppedVideos);
348 int ListModel::rowForVideo(Video* video) {
349 return videos.indexOf(video);
352 QModelIndex ListModel::indexForVideo(Video* video) {
353 return createIndex(videos.indexOf(video), 0);
356 void ListModel::move(QModelIndexList &indexes, bool up) {
357 QList<Video*> movedVideos;
359 foreach (QModelIndex index, indexes) {
360 int row = index.row();
361 if (row >= videos.size()) continue;
362 // qDebug() << "index row" << row;
363 Video *video = videoAt(row);
364 movedVideos << video;
367 int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1;
368 foreach (Video *video, movedVideos) {
370 int row = rowForVideo(video);
371 if (row+mod==end) { end=row; continue; }
372 // qDebug() << "video row" << row;
373 removeRows(row, 1, QModelIndex());
378 beginInsertRows(QModelIndex(), row, row);
379 videos.insert(row, video);
384 emit needSelectionFor(movedVideos);