From: APTX Date: Sat, 2 Jun 2012 21:41:36 +0000 (+0200) Subject: Expose more data through MyListModel X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=d55e72ba7d9f05b5e173ca46647ea0bd0484a267;p=localmylist.git Expose more data through MyListModel --- diff --git a/localmylist/mylistmodel.cpp b/localmylist/mylistmodel.cpp index 61a8952..8730d6b 100644 --- a/localmylist/mylistmodel.cpp +++ b/localmylist/mylistmodel.cpp @@ -135,4 +135,18 @@ bool MyListModel::hasChildren(const QModelIndex &parent) const return rootItem->hasChildren(); } +MyListModel::NodeType MyListModel::type(const QModelIndex &idx) const +{ + if (!idx.isValid()) + return None; + return NodeType(static_cast(idx.internalPointer())->type()); +} + +int MyListModel::id(const QModelIndex &idx) const +{ + if (!idx.isValid()) + return 0; + return static_cast(idx.internalPointer())->id(); +} + } // namespace LocalMyList diff --git a/localmylist/mylistmodel.h b/localmylist/mylistmodel.h index 9e33466..4c599db 100644 --- a/localmylist/mylistmodel.h +++ b/localmylist/mylistmodel.h @@ -13,6 +13,13 @@ class LOCALMYLISTSHARED_EXPORT MyListModel : public QAbstractItemModel Q_OBJECT public: + enum NodeType { + None, + Anime, + Episode, + File + }; + explicit MyListModel(QObject *parent = 0); ~MyListModel(); @@ -29,13 +36,13 @@ public: bool canFetchMore(const QModelIndex &parent) const; void fetchMore(const QModelIndex &parent); bool hasChildren(const QModelIndex &parent = QModelIndex()) const; - signals: public slots: -private: - void setupModelData(const QStringList &lines, MyListNode *parent); + NodeType type(const QModelIndex &idx) const; + int id(const QModelIndex &idx) const; +private: MyListNode *rootItem; }; diff --git a/localmylist/mylistnode.cpp b/localmylist/mylistnode.cpp index fea4954..9f6b6bf 100644 --- a/localmylist/mylistnode.cpp +++ b/localmylist/mylistnode.cpp @@ -11,15 +11,15 @@ namespace LocalMyList { MyListNode::MyListNode(NodeType type, int id, const QList &data, MyListNode *parent) : m_totalRowCount(-1), fetchedRowCount(0) { - this->type = type; - this->id = id; + m_type = type; + m_id = id; parentItem = parent; itemData = data; - if (type != Root) + if (m_type != Root) return; - itemData << "Title" << "Episode / Version"; + itemData << "Title" << "Episode / Version" << "Rating / Quality" << "Vote"; } @@ -66,7 +66,7 @@ int MyListNode::totalRowCount() const if (m_totalRowCount != -1) return m_totalRowCount; - if (type == File) + if (m_type == File) { m_totalRowCount = 0; return m_totalRowCount; @@ -88,17 +88,17 @@ int MyListNode::totalRowCount() const bool MyListNode::canFetchMore() const { - if (type != File && childCount() < totalRowCount()) + if (m_type != File && childCount() < totalRowCount()) return true; return false; } int MyListNode::fetchMore() { - qDebug() << "fetching some more for" << id; + qDebug() << "fetching some more for" << m_id; QSqlQuery q(LocalMyList::instance()->database()->connection()); - if (!q.exec("SELECT aid, title_romaji AS title, (SELECT COUNT(e.eid) FROM episode e WHERE e.aid = a.aid) FROM anime a ORDER BY title ASC " + if (!q.exec("SELECT aid, title_romaji AS title, (SELECT COUNT(e.eid) FROM episode e WHERE e.aid = a.aid), rating, my_vote FROM anime a ORDER BY title ASC " "LIMIT " + QString::number(LIMIT) + " " "OFFSET " + QString::number(childCount()))) return 0; @@ -107,7 +107,10 @@ int MyListNode::fetchMore() { int id = q.value(0).toInt(); QVariantList data; - data << q.value(1) << q.value(2); + + data << q.value(1) << q.value(2) + << (q.value(3).toDouble() < 1 ? "n/a" : QString::number(q.value(3).toDouble(), 'f', 2)) + << (q.value(4).toDouble() < 1 ? "n/a" : QString::number(q.value(4).toDouble(), 'f', 2)); childItems << new MyListAnimeNode(id, data, this); } return q.size(); @@ -115,7 +118,7 @@ int MyListNode::fetchMore() bool MyListNode::hasChildren() const { - if (type == File) + if (m_type == File) return false; return true; @@ -126,6 +129,16 @@ QString MyListNode::totalRowCountSql() const return "SELECT COUNT(aid) FROM anime"; } +MyListNode::NodeType MyListNode::type() const +{ + return m_type; +} + +int MyListNode::id() const +{ + return m_id; +} + // ------ MyListAnimeNode::MyListAnimeNode(int id, const QList &data, MyListNode *parent) : @@ -135,23 +148,27 @@ MyListAnimeNode::MyListAnimeNode(int id, const QList &data, MyListNode QString MyListAnimeNode::totalRowCountSql() const { - return "SELECT COUNT(eid) FROM episode WHERE aid = " + QString::number(id); + return "SELECT COUNT(eid) FROM episode WHERE aid = " + QString::number(m_id); } int MyListAnimeNode::fetchMore() { - qDebug() << "fetching some more for" << id; + qDebug() << "fetching some more for aid" << m_id; QSqlQuery q(LocalMyList::instance()->database()->connection()); - if (!q.exec("SELECT eid, title_english, epno FROM episode WHERE aid = " + QString::number(id) - + " ORDER BY epno ASC")) + if (!q.exec("SELECT eid, title_english, epno, rating, my_vote FROM episode WHERE aid = " + QString::number(m_id) + + " ORDER BY epno ASC " + "LIMIT " + QString::number(LIMIT) + " " + "OFFSET " + QString::number(childCount()))) return 0; while (q.next()) { int id = q.value(0).toInt(); QVariantList data; - data << q.value(1) << q.value(2); + data << q.value(1) << q.value(2) + << (q.value(3).toDouble() < 1 ? "n/a" : QString::number(q.value(3).toDouble(), 'f', 2)) + << (q.value(4).toDouble() < 1 ? "n/a" : QString::number(q.value(4).toDouble(), 'f', 2)); childItems << new MyListEpisodeNode(id, data, this); } @@ -167,22 +184,22 @@ MyListEpisodeNode::MyListEpisodeNode(int id, const QList &data, MyList QString MyListEpisodeNode::totalRowCountSql() const { - return "SELECT COUNT(fid) FROM file WHERE eid = " + QString::number(id); + return "SELECT COUNT(fid) FROM file WHERE eid = " + QString::number(m_id); } int MyListEpisodeNode::fetchMore() { - qDebug() << "fetching some more for" << id; + qDebug() << "fetching some more for eid" << m_id; QSqlQuery q(LocalMyList::instance()->database()->connection()); - if (!q.exec("SELECT fid, group_name, version FROM file WHERE eid = " + QString::number(id))) + if (!q.exec("SELECT fid, group_name, version, quality FROM file WHERE eid = " + QString::number(m_id))) return 0; while (q.next()) { int id = q.value(0).toInt(); QVariantList data; - data << q.value(1) << q.value(2); + data << q.value(1) << "v" + q.value(2).toString() << q.value(3); childItems << new MyListFileNode(id, data, this); } diff --git a/localmylist/mylistnode.h b/localmylist/mylistnode.h index 7ef91a0..1e23e82 100644 --- a/localmylist/mylistnode.h +++ b/localmylist/mylistnode.h @@ -15,7 +15,7 @@ public: File }; - MyListNode(NodeType type = Root, int id = 0, const QList &data = QList(), MyListNode *parent = 0); + MyListNode(NodeType type = Root, int m_id = 0, const QList &data = QList(), MyListNode *parent = 0); virtual ~MyListNode(); void appendChild(MyListNode *child); @@ -33,11 +33,14 @@ public: virtual int fetchMore(); bool hasChildren() const; + NodeType type() const; + int id() const; + protected: virtual QString totalRowCountSql() const; - NodeType type; - int id; + NodeType m_type; + int m_id; mutable int m_totalRowCount; int fetchedRowCount;