1
#include "playlistsuggest.h"
3
#include "networkaccess.h"
17
PlaylistSuggest::PlaylistSuggest(QObject *parent) : Suggester() {
21
void PlaylistSuggest::suggest(QString query) {
22
QUrl url("http://gdata.youtube.com/feeds/api/playlists/snippets");
23
url.addQueryItem("v", "2");
24
url.addQueryItem("q", query);
25
QObject *reply = The::http()->get(url);
26
connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
29
void PlaylistSuggest::handleNetworkData(QByteArray data) {
30
QList<Playlist> playlists;
32
QXmlStreamReader xml(data);
33
while (!xml.atEnd()) {
35
if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "entry") {
37
Playlist playlist = {};
39
while (xml.readNextStartElement()) {
40
if (xml.name() == "title") {
41
playlist.title = xml.readElementText();
43
else if (xml.name() == "summary") {
44
playlist.summary = xml.readElementText();
46
else if (xml.name() == "author") {
47
while (xml.readNextStartElement()) {
48
if (xml.name() == "name") {
49
playlist.author = xml.readElementText();
54
else if (xml.name() == "playlistId") {
55
playlist.id = xml.readElementText();
57
else if (xml.name() == "countHint") {
58
playlist.videoCount = xml.readElementText().toInt();
62
playlists << playlist;
67
// emit ready(choices);
72
class PlaylistSuggestModel : public QAbstractListModel {
77
PlaylistSuggestModel(QWidget *parent) : QAbstractListModel(parent) { }
78
int rowCount(const QModelIndex &parent = QModelIndex()) const {
82
int columnCount( const QModelIndex& parent = QModelIndex() ) const {
86
QVariant data(const QModelIndex &index, int role) const {
97
class PlaylistSuggestDelegate : public QStyledItemDelegate {
102
PlaylistSuggestDelegate(QObject* parent);
103
QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const {
104
return QSize(0, 100);
106
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
107
QStyleOptionViewItemV4 opt = QStyleOptionViewItemV4(option);
108
initStyleOption(&opt, index);
110
opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
113
painter->translate(option.rect.topLeft());
115
QRect line(0, 0, option.rect.width(), option.rect.height());