]> Some of my projects - localmylist.git/commitdiff
Expose more data through MyListModel
authorAPTX <marek321@gmail.com>
Sat, 2 Jun 2012 21:41:36 +0000 (23:41 +0200)
committerAPTX <marek321@gmail.com>
Sat, 2 Jun 2012 21:41:36 +0000 (23:41 +0200)
localmylist/mylistmodel.cpp
localmylist/mylistmodel.h
localmylist/mylistnode.cpp
localmylist/mylistnode.h

index 61a8952b03754aea4bc42eb447b1bdfa1d3202b0..8730d6ba27a5af0b21f27f3e0a36b04675725879 100644 (file)
@@ -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<MyListNode *>(idx.internalPointer())->type());
+}
+
+int MyListModel::id(const QModelIndex &idx) const
+{
+       if (!idx.isValid())
+               return 0;
+       return static_cast<MyListNode *>(idx.internalPointer())->id();
+}
+
 } // namespace LocalMyList
index 9e334669524f7c5c9fe5d89ec684c415969adbee..4c599dbe30f54978cc29a7f1e8c1ec11120beb50 100644 (file)
@@ -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;
 };
 
index fea4954ddc9f46721772df9601dec89be1230090..9f6b6bff2f587f3cffa09d6d5e55de700a2d5ae0 100644 (file)
@@ -11,15 +11,15 @@ namespace LocalMyList {
 MyListNode::MyListNode(NodeType type, int id, const QList<QVariant> &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<QVariant> &data, MyListNode *parent) :
@@ -135,23 +148,27 @@ MyListAnimeNode::MyListAnimeNode(int id, const QList<QVariant> &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<QVariant> &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);
        }
 
index 7ef91a0a6327ca17fea12767c17eacd3b4ce96aa..1e23e823a1132c227dd309f596b3c0666ed27973 100644 (file)
@@ -15,7 +15,7 @@ public:
                File
        };
 
-       MyListNode(NodeType type = Root, int id = 0, const QList<QVariant> &data = QList<QVariant>(), MyListNode *parent = 0);
+       MyListNode(NodeType type = Root, int m_id = 0, const QList<QVariant> &data = QList<QVariant>(), 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;