Fix naming: book parts vs. chapters.
[dorian] / model / book.cpp
index b4a5022..a11d3d3 100644 (file)
@@ -27,7 +27,8 @@ Book::Book(const QString &p, QObject *parent): QObject(parent)
         mPath = info.absoluteFilePath();
         title = info.baseName();
         cover = QImage(":/icons/book.png").scaled(COVER_WIDTH, COVER_HEIGHT,
-            Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+            Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
+            scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
     }
 }
 
@@ -135,14 +136,19 @@ bool Book::parse()
     delete opsHandler;
     delete source;
 
+    // Initially, put all content items in the chapter list.
+    // This will be refined by parsing the NCX file later
+    chapters = toc;
+
     // Load cover image
     QStringList coverKeys;
     coverKeys << "cover-image" << "img-cover-jpeg" << "cover";
     foreach (QString key, coverKeys) {
         if (content.contains(key)) {
             t.trace("Loading cover image from " + content[key].href);
-            cover = QImage(content[key].href).scaled(COVER_WIDTH,
-                COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+            cover = QImage(content[key].href).scaled(COVER_WIDTH, COVER_HEIGHT,
+                Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
+                scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
             break;
         }
     }
@@ -228,25 +234,26 @@ void Book::load()
     subject = settings.value(key + "subject").toString();
     source = settings.value(key + "source").toString();
     rights = settings.value(key + "rights").toString();
-    mLastBookmark.chapter = settings.value(key + "lastchapter").toInt();
+    mLastBookmark.part = settings.value(key + "lastpart").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);
+            Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
+            scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
     }
 
     // Load bookmarks
     int size = settings.value(key + "bookmarks").toInt();
     for (int i = 0; i < size; i++) {
-        int chapter = settings.value(key + "bookmark" + QString::number(i) +
-                                     "/chapter").toInt();
+        int part = settings.value(key + "bookmark" + QString::number(i) +
+                                     "/part").toInt();
         qreal pos = settings.value(key + "bookmark" + QString::number(i) +
                                    "/pos").toReal();
-        t.trace(QString("Bookmark %1 at chapter %2, %3").
-                arg(i).arg(chapter).arg(pos));
-        mBookmarks.append(Bookmark(chapter, pos));
+        t.trace(QString("Bookmark %1 at part %2, %3").
+                arg(i).arg(part).arg(pos));
+        mBookmarks.append(Bookmark(part, pos));
     }
 }
 
@@ -267,7 +274,7 @@ void Book::save()
     settings.setValue(key + "subject", subject);
     settings.setValue(key + "source", source);
     settings.setValue(key + "rights", rights);
-    settings.setValue(key + "lastchapter", mLastBookmark.chapter);
+    settings.setValue(key + "lastpart", mLastBookmark.part);
     settings.setValue(key + "lastpos", mLastBookmark.pos);
     settings.setValue(key + "cover", cover);
 
@@ -275,17 +282,17 @@ void Book::save()
     settings.setValue(key + "bookmarks", mBookmarks.size());
     for (int i = 0; i < mBookmarks.size(); i++) {
         t.trace(QString("Bookmark %1 at %2, %3").
-                arg(i).arg(mBookmarks[i].chapter).arg(mBookmarks[i].pos));
-        settings.setValue(key + "bookmark" + QString::number(i) + "/chapter",
-                          mBookmarks[i].chapter);
+                arg(i).arg(mBookmarks[i].part).arg(mBookmarks[i].pos));
+        settings.setValue(key + "bookmark" + QString::number(i) + "/part",
+                          mBookmarks[i].part);
         settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
                           mBookmarks[i].pos);
     }
 }
 
-void Book::setLastBookmark(int chapter, qreal position)
+void Book::setLastBookmark(int part, qreal position)
 {
-    mLastBookmark.chapter = chapter;
+    mLastBookmark.part = part;
     mLastBookmark.pos = position;
     save();
 }
@@ -295,9 +302,9 @@ Book::Bookmark Book::lastBookmark() const
     return Book::Bookmark(mLastBookmark);
 }
 
-void Book::addBookmark(int chapter, qreal position)
+void Book::addBookmark(int part, qreal position)
 {
-    mBookmarks.append(Bookmark(chapter, position));
+    mBookmarks.append(Bookmark(part, position));
     qSort(mBookmarks.begin(), mBookmarks.end());
     save();
 }
@@ -367,3 +374,47 @@ QString Book::shortName() const
         return title;
     }
 }
+
+int Book::chapterFromToc(int index)
+{
+    int ret = -1;
+    return ret;
+}
+
+int Book::tocFromChapter(int index)
+{
+    Trace t("Book::tocFromChapter");
+    QString id = chapters[index];
+    QString href = content[id].href;
+    QString baseRef(href);
+    QUrl url(QString("file://") + href);
+    if (url.hasFragment()) {
+        QString fragment = url.fragment();
+        baseRef.chop(fragment.length() + 1);
+    }
+
+    // Swipe through all content items to find the one matching the chapter href
+    // FIXME: Do we need to index content items by href, too?
+    QString contentKey;
+    bool found = false;
+    foreach (contentKey, content.keys()) {
+        if (contentKey == id) {
+            continue;
+        }
+        if (content[contentKey].href == baseRef) {
+            found = true;
+            t.trace(QString("Key for %1 is %2").arg(baseRef).arg(contentKey));
+            break;
+        }
+    }
+    if (!found) {
+        t.trace("Could not find key for " + baseRef);
+        return -1;
+    }
+    int tocIndex = toc.indexOf(contentKey);
+    if (tocIndex == -1) {
+        qCritical() << "Book::tocFromChapter: Could not find toc index of chapter"
+                << id;
+    }
+    return tocIndex;
+}