Turn library into proper model.
authorAkos Polster <polster@marzipan.pipacs.com>
Sun, 18 Jul 2010 13:49:55 +0000 (15:49 +0200)
committerAkos Polster <polster@marzipan.pipacs.com>
Sun, 18 Jul 2010 13:49:55 +0000 (15:49 +0200)
book.cpp
book.h
library.cpp
library.h
pkg/changelog

index 497ec75..2aff091 100644 (file)
--- a/book.cpp
+++ b/book.cpp
 #include "library.h"
 #include "containerhandler.h"
 
+Book::Book()
+{
+}
+
 Book::Book(const QString &path_)
 {
     mPath = "";
diff --git a/book.h b/book.h
index 5d3c393..cdfd60f 100644 (file)
--- a/book.h
+++ b/book.h
@@ -5,6 +5,7 @@
 #include <QStringList>
 #include <QHash>
 #include <QIcon>
+#include <QMetaType>
 
 /** A book. */
 class Book
@@ -35,7 +36,10 @@ public:
     };
 
     /** Construct a book from an EPUB file in path. */
-    explicit Book(const QString &path);
+    Book(const QString &path);
+
+    /** Default constructor. */
+    Book();
 
     /** Load book from persistent storage. */
     void load();
@@ -111,4 +115,6 @@ protected:
     QString mRootPath;                      //< Path to root item in EPUB dir.
 };
 
+Q_DECLARE_METATYPE(Book)
+
 #endif // BOOK_H
index 3d65ce2..4f64109 100644 (file)
@@ -1,13 +1,13 @@
 #include <QSettings>
-#include <QString>
 #include <QDebug>
 #include <QFileInfo>
 
 #include "library.h"
+#include "book.h"
 
 Library *Library::mInstance = 0;
 
-Library::Library(): mCurrent(0)
+Library::Library(QObject *parent): QAbstractListModel(parent), mCurrent(0)
 {
     load();
 }
@@ -25,6 +25,19 @@ Library *Library::instance()
     return mInstance;
 }
 
+int Library::rowCount(const QModelIndex &parent) const
+{
+    Q_UNUSED(parent);
+    return mBooks.size();
+}
+
+QVariant Library::data(const QModelIndex &index, int role) const
+{
+    Q_UNUSED(role);
+    Book *book = mBooks[index.row()];
+    return QVariant::fromValue<Book>(*book);
+}
+
 void Library::close()
 {
     delete mInstance;
index b111dae..103c3d4 100644 (file)
--- a/library.h
+++ b/library.h
@@ -1,20 +1,24 @@
 #ifndef LIBRARY_H
 #define LIBRARY_H
 
-#include <QObject>
+#include <QAbstractListModel>
+#include <QVariant>
 #include <QString>
 #include <QList>
 
-#include "book.h"
+class QObject;
+class Book;
 
 /** Library of books. */
-class Library: public QObject
+class Library: public QAbstractListModel
 {
     Q_OBJECT
 
 public:
     static Library *instance();
     static void close();
+    int rowCount(const QModelIndex & parent = QModelIndex()) const;
+    QVariant data(const QModelIndex &index, int role) const;
     void save();
     int find(QString path) const;
     int find(const Book *book) const;
@@ -31,7 +35,7 @@ signals:
     void currentBookChanged();
 
 private:
-    Library();
+    explicit Library(QObject *parent = 0);
     ~Library();
     void load();
     void clear();
index 5f0710f..ed564a3 100644 (file)
@@ -1,3 +1,9 @@
+dorian (0.0.10-1) unstable; urgency=low
+
+  * Turn library into proper model
+
+ -- Akos Polster <akos@pipacs.com>  Fri, 16 Jul 2010 20:00:00 +0200
+
 dorian (0.0.9-1) unstable; urgency=low
 
   * Optify Maemo package