Much ado about nothing.
[dorian] / library.cpp
index 873b343..f749c35 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "library.h"
 #include "book.h"
+#include "trace.h"
 
 Library *Library::mInstance = 0;
 
@@ -43,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();
     }
@@ -54,7 +57,7 @@ Book *Library::book(const QModelIndex &index)
         if ((index.row() >= 0) && (index.row() < mBooks.size())) {
             return mBooks[index.row()];
         } else {
-            qWarning() << "*** Library::book: Bad index" << index.row();
+            qCritical() << "*** Library::book: Bad index" << index.row();
         }
     }
     return 0;
@@ -75,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);
     }
@@ -97,12 +102,13 @@ void Library::save()
 
 bool Library::add(QString path)
 {
+    Trace t("Library::add " + path);
     if (path == "") {
-        qWarning() << "*** Library::add: Empty path";
+        qCritical() << "*** Library::add: Empty path";
         return false;
     }
     if (find(path).isValid()) {
-        qDebug() << "Library::add: Book already exists in library";
+        t.trace("Book already exists in library");
         return false;
     }
     int size = mBooks.size();
@@ -134,7 +140,6 @@ void Library::remove(const QModelIndex &index)
 
 QModelIndex Library::nowReading() const
 {
-    qDebug() << "Library::nowReading" << mNowReading.row();
     return mNowReading;
 }
 
@@ -178,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);
+    }
+}