2 #include "videomimedata.h"
6 ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
15 ListModel::~ListModel() {
19 int ListModel::rowCount(const QModelIndex &/*parent*/) const {
20 int count = videos.size();
22 // add the message item
23 if (videos.isEmpty() || !searching)
29 QVariant ListModel::data(const QModelIndex &index, int role) const {
31 int row = index.row();
33 if (row == videos.size()) {
37 boldFont.setBold(true);
41 return ItemTypeShowMore;
43 case Qt::StatusTipRole:
44 if (searching) return tr("Searching...");
45 if (canSearchMore) return tr("Show %1 More").arg(MAX_ITEMS);
46 if (videos.isEmpty()) return tr("No videos");
47 else return tr("No more videos");
48 case Qt::TextAlignmentRole:
49 return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
50 case Qt::ForegroundRole:
51 return palette.color(QPalette::Dark);
58 } else if (row < 0 || row >= videos.size())
61 Video *video = videos.at(row);
67 return QVariant::fromValue(QPointer<Video>(video));
69 return video == m_activeVideo;
71 case Qt::StatusTipRole:
72 return video->title();
77 if (!element.firstChildElement().text().isEmpty()) {
78 tooltip.append(QString("<b>").append(element.firstChildElement().text()).append("</b><br/>"));
80 if (!fromDate.isEmpty()) {
81 tooltip.append("<i>Pubblicato il</i> ").append(fromDate);
83 if (!toDate.isEmpty()) {
84 tooltip.append("<br/><i>Scadenza</i>: ").append(toDate);
86 tooltip.append("<br/><i>Tipo</i>: ").append(typeName)
87 .append("<br/><i>Id</i>: ").appen QFont boldFont;
88 boldFont.setBold(true);d(id);
92 // case StreamUrlRole:
93 // return video->streamUrl();
99 void ListModel::setActiveRow( int row) {
100 if ( rowExists( row ) ) {
103 m_activeVideo = videoAt(row);
105 // setStateOfRow( row, Item::Played );
107 int oldactiverow = m_activeRow;
109 if ( rowExists( oldactiverow ) )
110 emit dataChanged( createIndex( oldactiverow, 0 ), createIndex( oldactiverow, columnCount() - 1 ) );
112 emit dataChanged( createIndex( m_activeRow, 0 ), createIndex( m_activeRow, columnCount() - 1 ) );
113 emit activeRowChanged(row);
122 int ListModel::nextRow() const {
123 int nextRow = m_activeRow + 1;
124 if (rowExists(nextRow))
129 Video* ListModel::videoAt( int row ) const {
130 if ( rowExists( row ) )
131 return videos.at( row );
135 Video* ListModel::activeVideo() const {
136 return m_activeVideo;
139 void ListModel::search(SearchParams *searchParams) {
141 // delete current videos
142 while (!videos.isEmpty())
143 delete videos.takeFirst();
149 // (re)initialize the YouTubeSearch
150 if (youtubeSearch) delete youtubeSearch;
151 youtubeSearch = new YouTubeSearch();
152 connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
153 connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
155 this->searchParams = searchParams;
157 youtubeSearch->search(searchParams, MAX_ITEMS, skip);
161 void ListModel::searchMore(int max) {
162 if (searching) return;
164 youtubeSearch->search(searchParams, max, skip);
168 void ListModel::searchMore() {
169 searchMore(MAX_ITEMS);
172 void ListModel::searchNeeded() {
173 int remainingRows = videos.size() - m_activeRow;
174 int rowsNeeded = MAX_ITEMS - remainingRows;
176 searchMore(rowsNeeded);
179 void ListModel::abortSearch() {
180 while (!videos.isEmpty())
181 delete videos.takeFirst();
183 youtubeSearch->abort();
187 void ListModel::searchFinished(int total) {
189 canSearchMore = total > 0;
192 void ListModel::addVideo(Video* video) {
194 connect(video, SIGNAL(gotThumbnail()), this, SLOT(updateThumbnail()));
196 beginInsertRows(QModelIndex(), videos.size(), videos.size());
201 if (videos.size() == 1) {
207 void ListModel::updateThumbnail() {
209 Video *video = static_cast<Video *>(sender());
211 qDebug() << "Cannot get sender";
215 int row = rowForVideo(video);
216 emit dataChanged( createIndex( row, 0 ), createIndex( row, columnCount() - 1 ) );
223 * This function does not free memory
225 bool ListModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
226 beginRemoveRows(QModelIndex(), position, position+rows-1);
227 for (int row = 0; row < rows; ++row) {
228 videos.removeAt(position);
234 void ListModel::removeIndexes(QModelIndexList &indexes) {
235 QList<Video*> originalList(videos);
236 QList<Video*> delitems;
237 foreach (QModelIndex index, indexes) {
238 Video* video = originalList.at(index.row());
239 int idx = videos.indexOf(video);
241 beginRemoveRows(QModelIndex(), idx, idx);
242 delitems.append(video);
243 videos.removeAll(video);
248 qDeleteAll(delitems);
252 // --- Sturm und drang ---
256 Qt::DropActions ListModel::supportedDropActions() const {
257 return Qt::MoveAction;
260 Qt::ItemFlags ListModel::flags(const QModelIndex &index) const {
261 Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
263 if (index.isValid()) {
264 if (index.row() == videos.size()) {
265 // don't drag the "show 10 more" item
268 return ( defaultFlags | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled );
270 return Qt::ItemIsDropEnabled | defaultFlags;
273 QStringList ListModel::mimeTypes() const {
275 types << "application/x-minitube-video";
279 QMimeData* ListModel::mimeData( const QModelIndexList &indexes ) const {
280 VideoMimeData* mime = new VideoMimeData();
282 foreach( const QModelIndex &it, indexes ) {
284 if (row >= 0 && row < videos.size())
285 mime->addVideo( videos.at( it.row() ) );
291 bool ListModel::dropMimeData(const QMimeData *data,
292 Qt::DropAction action, int row, int column,
293 const QModelIndex &parent) {
294 if (action == Qt::IgnoreAction)
297 if (!data->hasFormat("application/x-minitube-video"))
306 else if (parent.isValid())
307 beginRow = parent.row();
309 beginRow = rowCount(QModelIndex());
311 const VideoMimeData* videoMimeData = dynamic_cast<const VideoMimeData*>( data );
312 if(!videoMimeData ) return false;
314 QList<Video*> droppedVideos = videoMimeData->videos();
315 foreach( Video *video, droppedVideos) {
318 int videoRow = videos.indexOf(video);
319 removeRows(videoRow, 1, QModelIndex());
321 // and then add them again at the new position
322 beginInsertRows(QModelIndex(), beginRow, beginRow);
323 videos.insert(beginRow, video);
328 // fix m_activeRow after all this
329 m_activeRow = videos.indexOf(m_activeVideo);
331 // let the MediaView restore the selection
332 emit needSelectionFor(droppedVideos);
338 int ListModel::rowForVideo(Video* video) {
339 return videos.indexOf(video);
342 QModelIndex ListModel::indexForVideo(Video* video) {
343 return createIndex(videos.indexOf(video), 0);
346 void ListModel::move(QModelIndexList &indexes, bool up) {
348 QList<Video*> movedVideos;
350 foreach (QModelIndex index, indexes) {
351 int row = index.row();
352 qDebug() << "index row" << row;
353 Video *video = videoAt(row);
354 movedVideos << video;
358 foreach (Video *video, movedVideos) {
360 int row = rowForVideo(video);
361 qDebug() << "video row" << row;
362 removeRows(row, 1, QModelIndex());
367 beginInsertRows(QModelIndex(), row, row);
368 videos.insert(row, video);
374 emit needSelectionFor(movedVideos);