More on model.
authorAkos Polster <polster@nolove.pipacs.com>
Mon, 19 Jul 2010 13:59:55 +0000 (15:59 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Mon, 19 Jul 2010 13:59:55 +0000 (15:59 +0200)
bookview.cpp
bookview.h
infodialog.cpp
library.cpp
library.h
librarydialog.cpp
mainwindow.cpp
mainwindow.h
sortedlibrary.cpp

index a490f8c..933bb4f 100644 (file)
 #endif
 
 BookView::BookView(QWidget *parent):
-        QWebView(parent), contentIndex(-1), mBook(0), restore(true), restorePos(0),
-        loadFinished(false)
+    QWebView(parent), contentIndex(-1), mBook(0),
+    restore(true), restorePos(0), loadFinished(false)
 {
     settings()->setAttribute(QWebSettings::AutoLoadImages, true);
     settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
     settings()->setAttribute(QWebSettings::PluginsEnabled, false);
     settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
-    settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, false);
+    settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
+                             false);
     page()->setContentEditable(false);
 
 #if defined(Q_WS_MAEMO_5)
index f67d1f2..fc42344 100644 (file)
@@ -9,6 +9,8 @@
 
 #include "book.h"
 
+class QModelIndex;
+
 /** Visual representation of a book. */
 class BookView: public QWebView
 {
index c76828f..07e05c5 100644 (file)
@@ -28,13 +28,13 @@ InfoDialog::InfoDialog(Book *book_, QWidget *parent):
 
 void InfoDialog::onReadBook()
 {
-    Library::instance()->setCurrent(Library::instance()->find(book));
+    Library::instance()->setNowReading(Library::instance()->find(book));
     close();
 }
 
 void InfoDialog::onRemoveBook()
 {
-    QString title = book->title;
+    QString title = book->name();
     if (QMessageBox::Yes ==
         QMessageBox::question(this, "Delete book", "Delete book " + title,
                               QMessageBox::Yes
index ca21725..e5f2b0c 100644 (file)
@@ -7,7 +7,7 @@
 
 Library *Library::mInstance = 0;
 
-Library::Library(QObject *parent): QAbstractListModel(parent), mCurrent(0)
+Library::Library(QObject *parent): QAbstractListModel(parent), mNowReading(0)
 {
     load();
 }
@@ -45,13 +45,20 @@ QVariant Library::data(const QModelIndex &index, int role) const
     switch (role) {
     case Qt::DisplayRole:
         return mBooks[index.row()]->name();
-    case BookRole:
-        return QVariant::fromValue<Book>(*mBooks[index.row()]);
     default:
         return QVariant();
     }
 }
 
+Book *Library::book(const QModelIndex &index)
+{
+    if (index.isValid()) {
+        return mBooks[index.row()];
+    } else {
+        return 0;
+    }
+}
+
 void Library::close()
 {
     delete mInstance;
@@ -71,11 +78,11 @@ void Library::load()
         qDebug() << "Library::load: Add" << book->title << "from" << path;
         mBooks.append(book);
     }
-    QString currentPath = settings.value("lib/current").toString();
+    QString currentPath = settings.value("lib/nowreading").toString();
     QModelIndex index = find(currentPath);
     if (index.isValid()) {
-        mCurrent = mBooks[index.row()];
-        qDebug() << "Library::load: Current book is" << mCurrent->path();
+        mNowReading = mBooks[index.row()];
+        qDebug() << "Library::load: Now reading" << mNowReading->path();
     }
 }
 
@@ -89,7 +96,8 @@ void Library::save()
         QString key = "lib/book" + QString::number(i);
         settings.setValue(key, mBooks[i]->path());
     }
-    settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
+    settings.setValue("lib/nowreading",
+                      mNowReading? mNowReading->path(): QString());
 }
 
 bool Library::add(QString path)
@@ -126,27 +134,30 @@ void Library::remove(const QModelIndex &index)
     mBooks.removeAt(row);
     save();
     endRemoveRows();
-    if (book == mCurrent) {
-        mCurrent = 0;
-        emit currentBookChanged();
+    if (book == mNowReading) {
+        mNowReading = 0;
+        emit nowReadingChanged();
     }
     delete book;
 }
 
-Book *Library::current() const
+QModelIndex Library::nowReading() const
 {
-    return mCurrent;
+    return find(mNowReading);
 }
 
-void Library::setCurrent(const QModelIndex index)
+void Library::setNowReading(const QModelIndex index)
 {
-    int row = index.row();
-    qDebug() << "Library::setCurrent" << row;
-    if ((row >= 0) && (row < mBooks.size())) {
-        mCurrent = mBooks[row];
-        save();
-        emit currentBookChanged();
+    if (index.isValid()) {
+        int row = index.row();
+        if ((row >= 0) && (row < mBooks.size())) {
+            mNowReading = mBooks[row];
+        }
+    } else {
+        mNowReading = 0;
     }
+    save();
+    emit nowReadingChanged();
 }
 
 void Library::clear()
@@ -155,7 +166,7 @@ void Library::clear()
         delete mBooks[i];
     }
     mBooks.clear();
-    mCurrent = 0;
+    mNowReading = 0;
 }
 
 QModelIndex Library::find(QString path) const
index 8f2f2e8..0f8f1e5 100644 (file)
--- a/library.h
+++ b/library.h
@@ -16,10 +16,6 @@ class Library: public QAbstractListModel
     Q_OBJECT
 
 public:
-    enum {
-        BookRole = Qt::UserRole + 1,
-    };
-
     static Library *instance();
     static void close();
     int rowCount(const QModelIndex &parent = QModelIndex()) const;
@@ -27,13 +23,14 @@ public:
     void save();
     QModelIndex find(QString path) const;
     QModelIndex find(const Book *book) const;
-    Book *current() const;
     bool add(QString path);
     void remove(const QModelIndex &index);
-    void setCurrent(const QModelIndex index);
+    void setNowReading(const QModelIndex index);
+    QModelIndex nowReading() const;
+    Book *book(const QModelIndex &index);
 
 signals:
-    void currentBookChanged();
+    void nowReadingChanged();
 
 private:
     explicit Library(QObject *parent = 0);
@@ -42,7 +39,7 @@ private:
     void clear();
     static Library *mInstance;
     QList<Book *> mBooks;
-    Book *mCurrent;
+    Book *mNowReading;
 };
 
 #endif // LIBRARY_H
index dfae909..a14c67e 100644 (file)
@@ -83,9 +83,8 @@ void LibraryDialog::onAdd()
     if (lastDir == "") {
         if (library->rowCount()) {
             QModelIndex lastIndex = library->index(library->rowCount() - 1);
-            Book lastBook = library->data(lastIndex,
-                                          Library::BookRole).value<Book>();
-            QFileInfo info(lastBook.path());
+            Book *lastBook = library->book(lastIndex);
+            QFileInfo info(lastBook->path());
             lastDir = info.absolutePath();
         }
     }
@@ -118,7 +117,7 @@ void LibraryDialog::onAdd()
 
 void LibraryDialog::onBookAdded()
 {
-#if 0 // FIXME
+#if 0
     Library *library = Library::instance();
     int index = library->size() - 1;
     Book *book = library->at(index);
@@ -136,9 +135,8 @@ void LibraryDialog::onRemove()
     qDebug() << "LibraryDialog::onRemove";
     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
     if (current.isValid()) {
-        Book currentBook =
-                Library::instance()->data(current, Library::BookRole).value<Book>();
-        QString title = currentBook.name();
+        Book *currentBook = Library::instance()->book(current);
+        QString title = currentBook->name();
         if (QMessageBox::Yes ==
             QMessageBox::question(this, "Delete book",
                                   "Delete book \"" + title + "\"?",
@@ -157,7 +155,7 @@ void LibraryDialog::onRead()
     qDebug() << "LibraryDialog::onRead";
     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
     if (current.isValid()) {
-        Library::instance()->setCurrent(current);
+        Library::instance()->setNowReading(current);
     }
 }
 
index 5c5aeaf..59550c5 100755 (executable)
@@ -80,7 +80,7 @@ MainWindow::MainWindow(QWidget *parent):
                                         "view-fullscreen");
 
     // Handle model changes
-    connect(Library::instance(), SIGNAL(currentBookChanged()),
+    connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
 
     normalFlags = windowFlags();
@@ -93,19 +93,19 @@ MainWindow::MainWindow(QWidget *parent):
         library->add(path);
         QModelIndex index = library->find(path);
         if (index.isValid()) {
-            library->setCurrent(index);
+            library->setNowReading(index);
         }
     }
     else {
-        Book *current = library->current();
-        if (current) {
-            setCurrentBook(current);
+        QModelIndex index = library->nowReading();
+        if (index.isValid()) {
+            library->setNowReading(index);
         }
         else {
             if (!library->rowCount()) {
                 library->add(":/books/2 B R 0 2 B.epub");
             }
-            library->setCurrent(library->index(0));
+            library->setNowReading(library->index(0));
         }
     }
 
@@ -125,7 +125,7 @@ MainWindow::MainWindow(QWidget *parent):
 
 void MainWindow::onCurrentBookChanged()
 {
-    setCurrentBook(Library::instance()->current());
+    setCurrentBook(Library::instance()->nowReading());
 }
 
 void MainWindow::showNormal()
@@ -151,14 +151,20 @@ void MainWindow::showFullScreen()
     restoreButton->flash();
 }
 
-void MainWindow::setCurrentBook(Book *current)
+void MainWindow::setCurrentBook(const QModelIndex &current)
 {
-    book = current;
-    view->setBook(current);
-    setWindowTitle(current? current->title: "Dorian");
+    if (current.isValid()) {
+        Book *book = Library::instance()->book(current);
+        view->setBook(book);
+        setWindowTitle(book->name());
+    } else {
+        view->setBook(0);
+        setWindowTitle("Dorian");
+    }
 }
 
-QAction *MainWindow::addToolBarAction(const QObject *receiver, const char *member,
+QAction *MainWindow::addToolBarAction(const QObject *receiver,
+                                      const char *member,
                                       const QString &name)
 {
     return toolBar->
index 8698e8c..6ac81d0 100755 (executable)
@@ -4,6 +4,7 @@
 #include <QtGui>
 
 class QString;
+class QModelIndex;
 class DevTools;
 class BookView;
 class Book;
@@ -40,7 +41,7 @@ protected:
     virtual void closeEvent(QCloseEvent *event);
 
 private:
-    void setCurrentBook(Book *book);
+    void setCurrentBook(const QModelIndex &current);
     QAction *addToolBarAction(const QObject *receiver, const char *member,
                               const QString &name);
     QRect fullScreenZone() const;
index ff87f68..1764a23 100644 (file)
@@ -14,20 +14,20 @@ void SortedLibrary::sortBy(SortBy key)
 bool SortedLibrary::lessThan(const QModelIndex &left,
                              const QModelIndex &right) const
 {
-    Book leftBook = sourceModel()->data(left, Library::BookRole).value<Book>();
-    Book rightBook = sourceModel()->data(right, Library::BookRole).value<Book>();
+    Book *leftBook = Library::instance()->book(left);
+    Book *rightBook = Library::instance()->book(right);
 
     QString leftString;
     QString rightString;
 
     switch (mSortBy) {
     case SortByTitle:
-        leftString = leftBook.title;
-        rightString = rightBook.title;
+        leftString = leftBook->name();
+        rightString = rightBook->name();
         break;
     default:
-        leftString = leftBook.creators[0];
-        rightString = rightBook.creators[0];
+        leftString = leftBook->creators[0];
+        rightString = rightBook->creators[0];
         break;
     }