Show cover images in library.
authorAkos Polster <polster@marzipan.pipacs.com>
Fri, 30 Jul 2010 22:09:30 +0000 (00:09 +0200)
committerAkos Polster <polster@marzipan.pipacs.com>
Fri, 30 Jul 2010 22:09:30 +0000 (00:09 +0200)
book.cpp
book.h
fullscreenwindow.h
library.cpp
library.h
librarydialog.cpp
pkg/changelog

index f5109f4..471b991 100644 (file)
--- a/book.cpp
+++ b/book.cpp
 #include "ncxhandler.h"
 #include "trace.h"
 
-Book::Book()
-{
-}
+const int COVER_WIDTH = 50;
+const int COVER_HEIGHT = 55;
 
-Book::Book(const QString &path_)
+Book::Book(const QString &p, QObject *parent): QObject(parent)
 {
     mPath = "";
-    if (path_ != "") {
-        QFileInfo info(path_);
+    if (p != "") {
+        QFileInfo info(p);
         mPath = info.absoluteFilePath();
         title = info.baseName();
-        cover = QIcon(":/icons/book.png");
+        cover = QImage(":/icons/book.png").scaled(COVER_WIDTH, COVER_HEIGHT,
+            Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
     }
 }
 
@@ -54,6 +54,7 @@ void Book::open()
     }
     else {
         save();
+        emit opened(path());
     }
 }
 
@@ -131,6 +132,7 @@ bool Book::parse()
 {
     Trace t("Book::parse");
 
+    // Parse OPS file
     bool ret = false;
     QString opsFileName = opsPath();
     t.trace("Parsing OPS file" + opsFileName);
@@ -146,6 +148,17 @@ bool Book::parse()
     delete opsHandler;
     delete source;
 
+    // Load cover image
+    if (content.contains("cover-image")) {
+        t.trace("Loading cover image from " + content["cover-image"].href);
+        cover = QImage(content["cover-image"].href).scaled(COVER_WIDTH,
+            COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+    } else if (content.contains("img-cover-jpeg")) {
+        t.trace("Loading cover image from " + content["img-cover-jpeg"].href);
+        cover = QImage(content["img-cover-jpeg"].href).scaled(COVER_WIDTH,
+            COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+    }
+
     // If there is an "ncx" item in content, parse it: That's the real table of
     // contents
     if (content.contains("ncx")) {
@@ -229,6 +242,12 @@ void Book::load()
     rights = settings.value(key + "rights").toString();
     mLastBookmark.chapter = settings.value(key + "lastchapter").toInt();
     mLastBookmark.pos = settings.value(key + "lastpos").toReal();
+    cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
+        COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+    if (cover.isNull()) {
+        cover = QImage(":/icons/book.png").scaled(COVER_WIDTH, COVER_HEIGHT,
+            Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+    }
 
     // Load bookmarks
     int size = settings.value(key + "bookmarks").toInt();
@@ -262,6 +281,7 @@ void Book::save()
     settings.setValue(key + "rights", rights);
     settings.setValue(key + "lastchapter", mLastBookmark.chapter);
     settings.setValue(key + "lastpos", mLastBookmark.pos);
+    settings.setValue(key + "cover", cover);
 
     // Save bookmarks
     settings.setValue(key + "bookmarks", mBookmarks.size());
@@ -338,7 +358,14 @@ QString Book::rootPath() const
 QString Book::name() const
 {
     if (title != "") {
-        return title;
+        QString ret = title;
+        if (creators.length()) {
+            ret += "\nBy " + creators[0];
+            for (int i = 1; i < creators.length(); i++) {
+                ret += ", " + creators[i];
+            }
+        }
+        return ret;
     } else {
         return path();
     }
diff --git a/book.h b/book.h
index bf8e1c8..72754ac 100644 (file)
--- a/book.h
+++ b/book.h
@@ -6,10 +6,13 @@
 #include <QHash>
 #include <QIcon>
 #include <QMetaType>
+#include <QObject>
 
 /** A book. */
-class Book
+class Book: public QObject
 {
+    Q_OBJECT
+
 public:
 
     /** Content item in the table of contents. */
@@ -36,7 +39,7 @@ public:
     };
 
     /** Construct a book from an EPUB file in path. */
-    Book(const QString &path);
+    Book(const QString &path, QObject *parent = 0);
 
     /** Default constructor. */
     Book();
@@ -92,7 +95,7 @@ public:
     QString title;                          //< Book title from EPUB.
     QStringList toc;                        //< Table of contents from EPUB.
     QHash<QString, ContentItem> content;    //< Content items from EPUB.
-    QIcon cover;                            //< Cover image.
+    QImage cover;                           //< Cover image.
     QStringList creators;                   //< Creators.
     QString date;                           //< Date of creation.
     QString publisher;                      //< Publisher.
@@ -102,7 +105,10 @@ public:
     QString rights;                         //< Rights.
     QString tocPath;                        //< Path to toc ncx.
     QString coverPath;                      //< Path to cover html.
-    QString coverImagePath;                 //< Path to cover image.
+
+signals:
+    /** Emitted if @see open() succeeds. */
+    void opened(const QString &bookPath);
 
 protected:
     /** Indicate failure by creating a single "error" content item. */
@@ -127,6 +133,4 @@ protected:
     QString mRootPath;                      //< Path to root item in EPUB dir.
 };
 
-Q_DECLARE_METATYPE(Book)
-
 #endif // BOOK_H
index 5c8a15f..3d06086 100644 (file)
@@ -17,8 +17,6 @@ public:
 signals:
     void restore();
 
-public slots:
-
 protected:
 #ifdef Q_WS_MAEMO_5
 #   define MOUSE_ACTIVATE_EVENT mouseReleaseEvent
index c40fb0f..f749c35 100644 (file)
@@ -44,6 +44,8 @@ QVariant Library::data(const QModelIndex &index, int role) const
     switch (role) {
     case Qt::DisplayRole:
         return mBooks[index.row()]->name();
+    case Qt::DecorationRole:
+        return QPixmap::fromImage(mBooks[index.row()]->cover);
     default:
         return QVariant();
     }
@@ -76,6 +78,8 @@ void Library::load()
         QString key = "lib/book" + QString::number(i);
         QString path = settings.value(key).toString();
         Book *book = new Book(path);
+        connect(book, SIGNAL(opened(const QString &)),
+                this, SLOT(onBookOpened(const QString &)));
         book->load();
         mBooks.append(book);
     }
@@ -179,3 +183,12 @@ QModelIndex Library::find(const Book *book) const
     }
     return QModelIndex();
 }
+
+void Library::onBookOpened(const QString &path)
+{
+    Trace t("Library::onBookOpened " + path);
+    QModelIndex index = find(path);
+    if (index.isValid()) {
+        emit dataChanged(index, index);
+    }
+}
index c4eb0f0..a1ac176 100644 (file)
--- a/library.h
+++ b/library.h
@@ -32,6 +32,9 @@ public:
 signals:
     void nowReadingChanged();
 
+public slots:
+    void onBookOpened(const QString &path);
+
 private:
     explicit Library(QObject *parent = 0);
     ~Library();
index 32b4a04..4b89042 100644 (file)
@@ -24,13 +24,14 @@ LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
 
     QFrame *frame = new QFrame(this);
     setCentralWidget(frame);
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
+    QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
     frame->setLayout(horizontalLayout);
 
     list = new QListView(this);
     sortedLibrary = new SortedLibrary(this);
     list->setModel(sortedLibrary);
     list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setSpacing(1);
     list->setUniformItemSizes(true);
 
     Library *library = Library::instance();
@@ -63,8 +64,6 @@ LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
     connect(list, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
 #ifndef Q_WS_MAEMO_5
-    connect(list, SIGNAL(itemSelectionChanged()),
-            this, SLOT(onItemSelectionChanged()));
     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
index 69522ec..4e8fb89 100644 (file)
@@ -2,6 +2,7 @@ dorian (0.0.13-1) unstable; urgency=low
 
   * Fix multiple navigation arrows [#6041]
   * Make full screen switching more robust
+  * Show cover images in library
 
  -- Akos Polster <akos@pipacs.com>  Thu, 29 Jul 2010 20:00:00 +0200