Pass model test.
authorAkos Polster <polster@nolove.pipacs.com>
Mon, 19 Jul 2010 12:37:21 +0000 (14:37 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Mon, 19 Jul 2010 12:37:21 +0000 (14:37 +0200)
book.cpp
book.h
dorian.pro
library.cpp
librarydialog.cpp
mainwindow.cpp
modeltest/modeltest.pro [deleted file]

index 2aff091..bd96c45 100644 (file)
--- a/book.cpp
+++ b/book.cpp
@@ -304,3 +304,12 @@ QString Book::rootPath() const
 {
     return mRootPath;
 }
+
+QString Book::name() const
+{
+    if (title != "") {
+        return title;
+    } else {
+        return path();
+    }
+}
diff --git a/book.h b/book.h
index cdfd60f..974b7c7 100644 (file)
--- a/book.h
+++ b/book.h
@@ -80,6 +80,12 @@ public:
     /** List bookmarks. */
     QList<Bookmark> bookmarks() const;
 
+    /**
+     * Get friendly name.
+     * @return  @see title or path name if title is not available yet.
+     */
+    QString name() const;
+
     QString title;                          //< Book title from EPUB.
     QStringList toc;                        //< Table of contents from EPUB.
     QHash<QString, ContentItem> content;    //< Content items from EPUB.
index 4db48ad..276d007 100644 (file)
@@ -64,11 +64,13 @@ OTHER_FILES += \
     styles/day.js
 
 DEFINES += \
-    USE_FILE32API
+    USE_FILE32API \
+    DORIAN_TEST_MODEL
+
+include(modeltest/modeltest.pri)
 
 unix {
     LIBS += -lz
-    include(modeltest/modeltest.pri)
 }
 windows {
     # FIXME: Build zlib, too
index 96a2344..ca21725 100644 (file)
@@ -27,15 +27,24 @@ Library *Library::instance()
 
 int Library::rowCount(const QModelIndex &parent) const
 {
-    Q_UNUSED(parent);
-    return mBooks.size();
+    if (parent.isValid()) {
+        return 0;
+    } else {
+        return mBooks.size();
+    }
 }
 
 QVariant Library::data(const QModelIndex &index, int role) const
 {
+    qDebug() << "Library::data, row" << index.row() << "role" << role;
+
+    if (!index.isValid()) {
+        return QVariant();
+    }
+
     switch (role) {
     case Qt::DisplayRole:
-        return mBooks[index.row()]->title;
+        return mBooks[index.row()]->name();
     case BookRole:
         return QVariant::fromValue<Book>(*mBooks[index.row()]);
     default:
@@ -96,7 +105,7 @@ bool Library::add(QString path)
     }
     int size = mBooks.size();
     Book *book = new Book(path);
-    beginInsertRows(QModelIndex(), size - 1, size);
+    beginInsertRows(QModelIndex(), size, size);
     mBooks.append(book);
     save();
     endInsertRows();
@@ -105,11 +114,14 @@ bool Library::add(QString path)
 
 void Library::remove(const QModelIndex &index)
 {
+    if (!index.isValid()) {
+        return;
+    }
     int row = index.row();
     if ((row < 0) || (row >= mBooks.size())) {
         return;
     }
-    beginRemoveRows(QModelIndex(), row, row + 1);
+    beginRemoveRows(QModelIndex(), row, row);
     Book *book = mBooks[row];
     mBooks.removeAt(row);
     save();
index 9d2cc03..dfae909 100644 (file)
@@ -138,7 +138,7 @@ void LibraryDialog::onRemove()
     if (current.isValid()) {
         Book currentBook =
                 Library::instance()->data(current, Library::BookRole).value<Book>();
-        QString title = currentBook.title;
+        QString title = currentBook.name();
         if (QMessageBox::Yes ==
             QMessageBox::question(this, "Delete book",
                                   "Delete book \"" + title + "\"?",
index e1fbf6b..5c5aeaf 100755 (executable)
 #include "bookmarksdialog.h"
 #include "settings.h"
 
+#ifdef DORIAN_TEST_MODEL
+#include "modeltest.h"
+#endif
+
 #ifdef Q_WS_MAC
 #   define ICON_PREFIX ":/icons/mac/"
 #else
@@ -113,6 +117,10 @@ MainWindow::MainWindow(QWidget *parent):
 
     // Handle loading chapters
     connect(view, SIGNAL(chapterLoaded(int)), this, SLOT(onChapterLoaded(int)));
+
+#ifdef DORIAN_TEST_MODEL
+    (void)new ModelTest(Library::instance(), this);
+#endif
 }
 
 void MainWindow::onCurrentBookChanged()
diff --git a/modeltest/modeltest.pro b/modeltest/modeltest.pro
deleted file mode 100644 (file)
index 6f991dd..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-include(modeltest.pri)
-
-OTHER_FILES += \
-    README