More modelification.
authorAkos Polster <polster@nolove.pipacs.com>
Mon, 19 Jul 2010 11:25:54 +0000 (13:25 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Mon, 19 Jul 2010 11:25:54 +0000 (13:25 +0200)
library.cpp
library.h
librarydialog.cpp
mainwindow.cpp

index c204233..96a2344 100644 (file)
@@ -63,9 +63,9 @@ void Library::load()
         mBooks.append(book);
     }
     QString currentPath = settings.value("lib/current").toString();
-    int index = find(currentPath);
-    if (-1 != index) {
-        mCurrent = mBooks[index];
+    QModelIndex index = find(currentPath);
+    if (index.isValid()) {
+        mCurrent = mBooks[index.row()];
         qDebug() << "Library::load: Current book is" << mCurrent->path();
     }
 }
@@ -85,10 +85,13 @@ void Library::save()
 
 bool Library::add(QString path)
 {
+    qDebug() << "Library::add" << path;
     if (path == "") {
+        qWarning() << "Library::add: Empty path";
         return false;
     }
-    if (find(path) != -1) {
+    if (find(path).isValid()) {
+        qDebug() << " Book already exists in library";
         return false;
     }
     int size = mBooks.size();
@@ -100,14 +103,15 @@ bool Library::add(QString path)
     return true;
 }
 
-void Library::remove(int index)
+void Library::remove(const QModelIndex &index)
 {
-    if ((index < 0) || (index >= mBooks.size())) {
+    int row = index.row();
+    if ((row < 0) || (row >= mBooks.size())) {
         return;
     }
-    beginRemoveRows(QModelIndex(), index, index + 1);
-    Book *book = mBooks[index];
-    mBooks.removeAt(index);
+    beginRemoveRows(QModelIndex(), row, row + 1);
+    Book *book = mBooks[row];
+    mBooks.removeAt(row);
     save();
     endRemoveRows();
     if (book == mCurrent) {
@@ -117,29 +121,18 @@ void Library::remove(int index)
     delete book;
 }
 
-int Library::size() const
-{
-    return mBooks.size();
-}
-
-Book *Library::at(int index) const
-{
-    return mBooks[index];
-}
-
 Book *Library::current() const
 {
     return mCurrent;
 }
 
-void Library::setCurrent(int index)
+void Library::setCurrent(const QModelIndex index)
 {
-    qDebug() << "Library::setCurrent" << index;
-    if ((index >= 0) && (index < mBooks.size()))
-    {
-        mCurrent = mBooks[index];
+    int row = index.row();
+    qDebug() << "Library::setCurrent" << row;
+    if ((row >= 0) && (row < mBooks.size())) {
+        mCurrent = mBooks[row];
         save();
-        qDebug() << " Emit currentBookChanged()";
         emit currentBookChanged();
     }
 }
@@ -153,25 +146,25 @@ void Library::clear()
     mCurrent = 0;
 }
 
-int Library::find(QString path) const
+QModelIndex Library::find(QString path) const
 {
     if (path != "") {
         QString absolutePath = QFileInfo(path).absoluteFilePath();
         for (int i = 0; i < mBooks.size(); i++) {
             if (absolutePath == mBooks[i]->path()) {
-                return i;
+                return index(i);
             }
         }
     }
-    return -1;
+    return QModelIndex();
 }
 
-int Library::find(const Book *book) const
+QModelIndex Library::find(const Book *book) const
 {
     for (int i = 0; i < mBooks.size(); i++) {
         if (book == mBooks[i]) {
-            return i;
+            return index(i);
         }
     }
-    return -1;
+    return QModelIndex();
 }
index e8142e1..8f2f2e8 100644 (file)
--- a/library.h
+++ b/library.h
@@ -7,6 +7,7 @@
 #include <QList>
 
 class QObject;
+class QModelIndex;
 class Book;
 
 /** Library of books. */
@@ -21,17 +22,15 @@ public:
 
     static Library *instance();
     static void close();
-    int rowCount(const QModelIndex & parent = QModelIndex()) const;
+    int rowCount(const QModelIndex &parent = QModelIndex()) const;
     QVariant data(const QModelIndex &index, int role) const;
     void save();
-    int find(QString path) const;
-    int find(const Book *book) const;
-    Book *at(int index) const;
-    int size() const;
+    QModelIndex find(QString path) const;
+    QModelIndex find(const Book *book) const;
     Book *current() const;
     bool add(QString path);
-    void remove(int index);
-    void setCurrent(int index);
+    void remove(const QModelIndex &index);
+    void setCurrent(const QModelIndex index);
 
 signals:
     void currentBookChanged();
index d90151a..9d2cc03 100644 (file)
@@ -23,6 +23,7 @@ LibraryDialog::LibraryDialog(QWidget *parent):
     sortedLibrary = new SortedLibrary(this);
     list->setModel(sortedLibrary);
     list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setUniformItemSizes(true);
 #ifndef Q_WS_MAEMO_5
     setSizeGripEnabled(true);
 #endif
@@ -54,11 +55,8 @@ LibraryDialog::LibraryDialog(QWidget *parent):
 
     horizontalLayout->addWidget(buttonBox);
 
-#if 0 // FIXME
-    connect(library, SIGNAL(currentBookChanged()),
+    connect(Library::instance(), SIGNAL(currentBookChanged()),
             this, SLOT(onCurrentBookChanged()));
-#endif
-
 #ifndef Q_WS_MAEMO_5
     connect(list, SIGNAL(itemSelectionChanged()),
             this, SLOT(onItemSelectionChanged()));
@@ -81,15 +79,21 @@ void LibraryDialog::onAdd()
 {
     Library *library = Library::instance();
 
+    // Figure out directory to show
     if (lastDir == "") {
-        if (library->size()) {
-            QFileInfo info(library->at(library->size() - 1)->path());
+        if (library->rowCount()) {
+            QModelIndex lastIndex = library->index(library->rowCount() - 1);
+            Book lastBook = library->data(lastIndex,
+                                          Library::BookRole).value<Book>();
+            QFileInfo info(lastBook.path());
             lastDir = info.absolutePath();
         }
     }
     if (lastDir == "") {
         lastDir = QDir::homePath();
     }
+
+    // Get book file name
     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
                                                 lastDir, "Books (*.epub)");
     qDebug() << "LibraryDialog::onAdd" << path;
@@ -97,8 +101,9 @@ void LibraryDialog::onAdd()
         return;
     }
 
+    // Add book to library
     lastDir = QFileInfo(path).absolutePath();
-    if (library->find(path) != -1) {
+    if (library->find(path).isValid()) {
 #ifdef Q_WS_MAEMO_5
         QMaemo5InformationBox::information(this,
             tr("This book is already in the library"),
@@ -113,7 +118,7 @@ void LibraryDialog::onAdd()
 
 void LibraryDialog::onBookAdded()
 {
-#if 0
+#if 0 // FIXME
     Library *library = Library::instance();
     int index = library->size() - 1;
     Book *book = library->at(index);
@@ -128,38 +133,32 @@ void LibraryDialog::onBookAdded()
 
 void LibraryDialog::onRemove()
 {
-#if 0
     qDebug() << "LibraryDialog::onRemove";
-    if (list->selectedItems().isEmpty()) {
-        return;
-    }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int row = list->row(item);
-    QString title = Library::instance()->at(row)->title;
-    if (QMessageBox::Yes ==
-        QMessageBox::question(this, "Delete book",
-                              "Delete book \"" + title + "\"?", QMessageBox::Yes
+    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+    if (current.isValid()) {
+        Book currentBook =
+                Library::instance()->data(current, Library::BookRole).value<Book>();
+        QString title = currentBook.title;
+        if (QMessageBox::Yes ==
+            QMessageBox::question(this, "Delete book",
+                                  "Delete book \"" + title + "\"?",
+                                  QMessageBox::Yes
 #ifndef Q_WS_MAEMO_5
-                              , QMessageBox::No
+                                  , QMessageBox::No
 #endif
-                              )) {
-        list->takeItem(row);
-        Library::instance()->remove(row);
+                                  )) {
+            Library::instance()->remove(current);
+        }
     }
-#endif
 }
 
 void LibraryDialog::onRead()
 {
     qDebug() << "LibraryDialog::onRead";
-#if 0 // FIXME
-    if (list->selectedItems().isEmpty()) {
-        return;
+    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+    if (current.isValid()) {
+        Library::instance()->setCurrent(current);
     }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int row = list->row(item);
-    Library::instance()->setCurrent(row);
-#endif
 }
 
 void LibraryDialog::onDetails()
index 36a88fd..e1fbf6b 100755 (executable)
@@ -87,8 +87,8 @@ MainWindow::MainWindow(QWidget *parent):
     if (QCoreApplication::arguments().size() == 2) {
         QString path = QCoreApplication::arguments()[1];
         library->add(path);
-        int index = library->find(path);
-        if (index != -1) {
+        QModelIndex index = library->find(path);
+        if (index.isValid()) {
             library->setCurrent(index);
         }
     }
@@ -98,10 +98,10 @@ MainWindow::MainWindow(QWidget *parent):
             setCurrentBook(current);
         }
         else {
-            if (!library->size()) {
+            if (!library->rowCount()) {
                 library->add(":/books/2 B R 0 2 B.epub");
             }
-            library->setCurrent(0);
+            library->setCurrent(library->index(0));
         }
     }