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 case Qt::StatusTipRole:
82 return video->title();
87 if (!element.firstChildElement().text().isEmpty()) {
88 tooltip.append(QString("<b>").append(element.firstChildElement().text()).append("</b><br/>"));
90 if (!fromDate.isEmpty()) {
91 tooltip.append("<i>Pubblicato il</i> ").append(fromDate);
93 if (!toDate.isEmpty()) {
94 tooltip.append("<br/><i>Scadenza</i>: ").append(toDate);
96 tooltip.append("<br/><i>Tipo</i>: ").append(typeName)
97 .append("<br/><i>Id</i>: ").appen QFont boldFont;
98 boldFont.setBold(true);d(id);
102 // case StreamUrlRole:
103 // return video->streamUrl();
109 void ListModel::setActiveRow( int row) {
110 if ( rowExists( row ) ) {
113 m_activeVideo = videoAt(row);
115 // setStateOfRow( row, Item::Played );
117 int oldactiverow = m_activeRow;
119 if ( rowExists( oldactiverow ) )
120 emit dataChanged( createIndex( oldactiverow, 0 ), createIndex( oldactiverow, columnCount() - 1 ) );
122 emit dataChanged( createIndex( m_activeRow, 0 ), createIndex( m_activeRow, columnCount() - 1 ) );
123 emit activeRowChanged(row);
132 int ListModel::nextRow() const {
133 int nextRow = m_activeRow + 1;
134 if (rowExists(nextRow))
139 Video* ListModel::videoAt( int row ) const {
140 if ( rowExists( row ) )
141 return videos.at( row );
145 Video* ListModel::activeVideo() const {
146 return m_activeVideo;
149 void ListModel::search(SearchParams *searchParams) {
151 // delete current videos
152 while (!videos.isEmpty())
153 delete videos.takeFirst();
157 errorMessage.clear();
160 // (re)initialize the YouTubeSearch
161 if (youtubeSearch) delete youtubeSearch;
162 youtubeSearch = new YouTubeSearch();
163 connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
164 connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
165 connect(youtubeSearch, SIGNAL(error(QString)), this, SLOT(searchError(QString)));
167 this->searchParams = searchParams;
169 youtubeSearch->search(searchParams, MAX_ITEMS, skip);
173 void ListModel::searchMore(int max) {
174 if (searching) return;
176 errorMessage.clear();
177 youtubeSearch->search(searchParams, max, skip);
181 void ListModel::searchMore() {
182 searchMore(MAX_ITEMS);
185 void ListModel::searchNeeded() {
186 int remainingRows = videos.size() - m_activeRow;
187 int rowsNeeded = MAX_ITEMS - remainingRows;
189 searchMore(rowsNeeded);
192 void ListModel::abortSearch() {
193 while (!videos.isEmpty())
194 delete videos.takeFirst();
196 youtubeSearch->abort();
200 void ListModel::searchFinished(int total) {
202 canSearchMore = total > 0;
204 // update the message item
205 emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
208 void ListModel::searchError(QString message) {
209 errorMessage = message;
210 // update the message item
211 emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
214 void ListModel::addVideo(Video* video) {
216 connect(video, SIGNAL(gotThumbnail()), this, SLOT(updateThumbnail()));
218 beginInsertRows(QModelIndex(), videos.size(), videos.size());
223 if (videos.size() == 1) {
228 QString query = searchParams->keywords();
230 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
231 keywords.removeAll(query);
232 keywords.prepend(query);
233 while (keywords.size() > 10)
234 keywords.removeLast();
235 settings.setValue(recentKeywordsKey, keywords);
240 void ListModel::updateThumbnail() {
242 Video *video = static_cast<Video *>(sender());
244 qDebug() << "Cannot get sender";
248 int row = rowForVideo(video);
249 emit dataChanged( createIndex( row, 0 ), createIndex( row, columnCount() - 1 ) );
256 * This function does not free memory
258 bool ListModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
259 beginRemoveRows(QModelIndex(), position, position+rows-1);
260 for (int row = 0; row < rows; ++row) {
261 videos.removeAt(position);
267 void ListModel::removeIndexes(QModelIndexList &indexes) {
268 QList<Video*> originalList(videos);
269 QList<Video*> delitems;
270 foreach (QModelIndex index, indexes) {
271 Video* video = originalList.at(index.row());
272 int idx = videos.indexOf(video);
274 beginRemoveRows(QModelIndex(), idx, idx);
275 delitems.append(video);
276 videos.removeAll(video);
281 qDeleteAll(delitems);
285 // --- Sturm und drang ---
289 Qt::DropActions ListModel::supportedDropActions() const {
290 return Qt::MoveAction;
293 Qt::ItemFlags ListModel::flags(const QModelIndex &index) const {
294 Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
296 if (index.isValid()) {
297 if (index.row() == videos.size()) {
298 // don't drag the "show 10 more" item
301 return ( defaultFlags | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled );
303 return Qt::ItemIsDropEnabled | defaultFlags;
306 QStringList ListModel::mimeTypes() const {
308 types << "application/x-minitube-video";
312 QMimeData* ListModel::mimeData( const QModelIndexList &indexes ) const {
313 VideoMimeData* mime = new VideoMimeData();
315 foreach( const QModelIndex &it, indexes ) {
317 if (row >= 0 && row < videos.size())
318 mime->addVideo( videos.at( it.row() ) );
324 bool ListModel::dropMimeData(const QMimeData *data,
325 Qt::DropAction action, int row, int column,
326 const QModelIndex &parent) {
327 if (action == Qt::IgnoreAction)
330 if (!data->hasFormat("application/x-minitube-video"))
339 else if (parent.isValid())
340 beginRow = parent.row();
342 beginRow = rowCount(QModelIndex());
344 const VideoMimeData* videoMimeData = dynamic_cast<const VideoMimeData*>( data );
345 if(!videoMimeData ) return false;
347 QList<Video*> droppedVideos = videoMimeData->videos();
348 foreach( Video *video, droppedVideos) {
351 int videoRow = videos.indexOf(video);
352 removeRows(videoRow, 1, QModelIndex());
354 // and then add them again at the new position
355 beginInsertRows(QModelIndex(), beginRow, beginRow);
356 videos.insert(beginRow, video);
361 // fix m_activeRow after all this
362 m_activeRow = videos.indexOf(m_activeVideo);
364 // let the MediaView restore the selection
365 emit needSelectionFor(droppedVideos);
371 int ListModel::rowForVideo(Video* video) {
372 return videos.indexOf(video);
375 QModelIndex ListModel::indexForVideo(Video* video) {
376 return createIndex(videos.indexOf(video), 0);
379 void ListModel::move(QModelIndexList &indexes, bool up) {
381 QList<Video*> movedVideos;
383 foreach (QModelIndex index, indexes) {
384 int row = index.row();
385 qDebug() << "index row" << row;
386 Video *video = videoAt(row);
387 movedVideos << video;
391 foreach (Video *video, movedVideos) {
393 int row = rowForVideo(video);
394 qDebug() << "video row" << row;
395 removeRows(row, 1, QModelIndex());
400 beginInsertRows(QModelIndex(), row, row);
401 videos.insert(row, video);
407 emit needSelectionFor(movedVideos);