#include "videomimedata.h"
#define MAX_ITEMS 10
+static const QString recentKeywordsKey = "recentKeywords";
ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
youtubeSearch = 0;
return ItemTypeShowMore;
case Qt::DisplayRole:
case Qt::StatusTipRole:
+ if (!errorMessage.isEmpty()) return errorMessage;
if (searching) return tr("Searching...");
if (canSearchMore) return tr("Show %1 More").arg(MAX_ITEMS);
if (videos.isEmpty()) return tr("No videos");
case Qt::TextAlignmentRole:
return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
case Qt::ForegroundRole:
- return palette.color(QPalette::Dark);
+ if (!errorMessage.isEmpty())
+ return palette.color(QPalette::ToolTipText);
+ else
+ return palette.color(QPalette::Dark);
+ case Qt::BackgroundColorRole:
+ if (!errorMessage.isEmpty())
+ return palette.color(QPalette::ToolTipBase);
+ else
+ return QVariant();
case Qt::FontRole:
return boldFont;
default:
case ActiveTrackRole:
return video == m_activeVideo;
case Qt::DisplayRole:
- case Qt::StatusTipRole:
return video->title();
- /*
- case Qt::ToolTipRole:
-
- QString tooltip;
- if (!element.firstChildElement().text().isEmpty()) {
- tooltip.append(QString("<b>").append(element.firstChildElement().text()).append("</b><br/>"));
- }
- if (!fromDate.isEmpty()) {
- tooltip.append("<i>Pubblicato il</i> ").append(fromDate);
- }
- if (!toDate.isEmpty()) {
- tooltip.append("<br/><i>Scadenza</i>: ").append(toDate);
- }
- tooltip.append("<br/><i>Tipo</i>: ").append(typeName)
- .append("<br/><i>Id</i>: ").appen QFont boldFont;
- boldFont.setBold(true);d(id);
- return tooltip;
- */
-
- // case StreamUrlRole:
- // return video->streamUrl();
}
return QVariant();
m_activeRow = row;
m_activeVideo = videoAt(row);
- // setStateOfRow( row, Item::Played );
-
int oldactiverow = m_activeRow;
if ( rowExists( oldactiverow ) )
m_activeVideo = 0;
m_activeRow = -1;
skip = 1;
+ errorMessage.clear();
reset();
// (re)initialize the YouTubeSearch
youtubeSearch = new YouTubeSearch();
connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
+ connect(youtubeSearch, SIGNAL(error(QString)), this, SLOT(searchError(QString)));
this->searchParams = searchParams;
searching = true;
void ListModel::searchMore(int max) {
if (searching) return;
searching = true;
+ errorMessage.clear();
youtubeSearch->search(searchParams, max, skip);
skip += max;
}
void ListModel::searchFinished(int total) {
searching = false;
canSearchMore = total > 0;
+
+ // update the message item
+ emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
+}
+
+void ListModel::searchError(QString message) {
+ errorMessage = message;
+ // update the message item
+ emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
}
void ListModel::addVideo(Video* video) {
videos << video;
endInsertRows();
- // autoplay
+ // first result!
if (videos.size() == 1) {
+ // autoplay
setActiveRow(0);
+
+ // save keyword
+ QString query = searchParams->keywords();
+ QSettings settings;
+ QStringList keywords = settings.value(recentKeywordsKey).toStringList();
+ keywords.removeAll(query);
+ keywords.prepend(query);
+ while (keywords.size() > 10)
+ keywords.removeLast();
+ settings.setValue(recentKeywordsKey, keywords);
}
}
QList<Video*> originalList(videos);
QList<Video*> delitems;
foreach (QModelIndex index, indexes) {
+ if (index.row() >= originalList.size()) continue;
Video* video = originalList.at(index.row());
int idx = videos.indexOf(video);
if (idx != -1) {
}
void ListModel::move(QModelIndexList &indexes, bool up) {
-
QList<Video*> movedVideos;
foreach (QModelIndex index, indexes) {
int row = index.row();
- qDebug() << "index row" << row;
+ if (row >= videos.size()) continue;
+ // qDebug() << "index row" << row;
Video *video = videoAt(row);
movedVideos << video;
}
- int counter = 1;
+ int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1;
foreach (Video *video, movedVideos) {
int row = rowForVideo(video);
- qDebug() << "video row" << row;
+ if (row+mod==end) { end=row; continue; }
+ // qDebug() << "video row" << row;
removeRows(row, 1, QModelIndex());
if (up) row--;
videos.insert(row, video);
endInsertRows();
- counter++;
}
emit needSelectionFor(movedVideos);