New version. Have and overlap between current/next page.
[dorian] / librarydialog.cpp
index 796fad6..e45fe8f 100644 (file)
@@ -8,56 +8,60 @@
 #include "infodialog.h"
 #include "settings.h"
 #include "listwindow.h"
-#include "listview.h"
 #include "trace.h"
 #include "bookfinder.h"
 #include "searchdialog.h"
 #include "platform.h"
 #include "searchresultsdialog.h"
 #include "progressdialog.h"
+#include "settings.h"
 
-LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
+LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(tr("(No books)"), parent)
 {
+    TRACE;
     setWindowTitle(tr("Library"));
+    setAttribute(Qt::WA_DeleteOnClose, true);
 
-    // Add actions
+    // Add menu actions
+    sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
+    sortByAuthor =
+            addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
 
-#ifndef Q_WS_MAEMO_5
-    addItemAction(tr("Details"), this, SLOT(onDetails()));
-    addItemAction(tr("Read"), this, SLOT(onRead()));
-    addItemAction(tr("Delete"), this, SLOT(onRemove()));
-#endif // ! Q_WS_MAEMO_5
+    // Set model
+    sortedLibrary = new SortedLibrary(this);
+    setModel(sortedLibrary);
 
-    addAction(tr("Add book"), this, SLOT(onAdd()), "add");
-    addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
-    addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
+    // Add action buttons
+    addButton(tr("Add book"), this, SLOT(onAdd()), "add");
+    addButton(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
+    addButton(tr("Search the Web"), this, SLOT(onSearch()), "search");
 
-    // Create and add list view
-    list = new ListView(this);
-    sortedLibrary = new SortedLibrary(this);
-    list->setModel(sortedLibrary);
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-    list->setSpacing(1);
+    // Set selected item
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
-    setSelected(current);
-    addList(list);
+    setCurrentItem(sortedLibrary->mapFromSource(current));
 
+    // Search dialog box
+    searchDialog = new SearchDialog(this);
+    connect(Search::instance(), SIGNAL(endSearch()),
+            this, SLOT(showSearchResults()));
+
+    // Progress bar
     progress = new ProgressDialog(tr("Adding books"), this);
 
-    connect(Library::instance(), SIGNAL(nowReadingChanged()),
-            this, SLOT(onCurrentBookChanged()));
-    connect(Library::instance(),
-            SIGNAL(rowsInserted(const QModelIndex &, int, int)),
-            this,
-            SLOT(onBookAdded()));
-    connect(list, SIGNAL(activated(const QModelIndex &)),
+    connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
+            this, SLOT(onBookAdded()));
+    connect(this, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
 
-    // Create search dialog
-    searchDialog = new SearchDialog(this);
-    connect(Search::instance(), SIGNAL(endSearch()),
-            this, SLOT(showSearchResults()));
+    // Retrieve default sort criteria
+    switch (Settings::instance()->value("lib/sortby").toInt()) {
+    case SortedLibrary::SortByAuthor:
+        onSortByAuthor();
+        break;
+    default:
+        onSortByTitle();
+    }
 }
 
 void LibraryDialog::onAdd()
@@ -85,7 +89,7 @@ void LibraryDialog::onAdd()
     if (index.isValid()) {
         Platform::instance()->information(
                 tr("This book is already in the library"), this);
-        setSelected(index);
+        setSelected(sortedLibrary->mapFromSource(index));
     }
     else {
         library->add(path);
@@ -95,53 +99,35 @@ void LibraryDialog::onAdd()
 void LibraryDialog::onBookAdded()
 {
     Library *library = Library::instance();
-    setSelected(library->index(library->rowCount() - 1));
-}
-
-#ifndef Q_WS_MAEMO_5
-
-void LibraryDialog::onRemove()
-{
-    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
-    if (current.isValid()) {
-        Book *currentBook = Library::instance()->book(current);
-        QString title = currentBook->name();
-        if (QMessageBox::Yes ==
-            QMessageBox::question(this, tr("Delete book"),
-                tr("Delete book \"%1\" from library?").
-                    arg(currentBook->shortName()),
-                QMessageBox::Yes | QMessageBox::No)) {
-            Library::instance()->remove(current);
-        }
-    }
-}
-
-void LibraryDialog::onRead()
-{
-    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
-    if (current.isValid()) {
-        Library::instance()->setNowReading(current);
-    }
+    setSelected(sortedLibrary->
+                mapFromSource(library->index(library->rowCount() - 1)));
 }
 
-void LibraryDialog::onDetails()
-{
-    onItemActivated(list->currentIndex());
-}
-
-#endif // Q_WS_MAEMO_5
-
 void LibraryDialog::onItemActivated(const QModelIndex &index)
 {
     TRACE;
+
     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
     Book *book = Library::instance()->book(libraryIndex);
-    (new InfoDialog(book, this))->exec();
+    int ret = (new InfoDialog(book, this))->exec();
+
+    switch (ret) {
+    case InfoDialog::Read:
+        Library::instance()->setNowReading(libraryIndex);
+        close();
+        break;
+    case InfoDialog::Delete:
+        Library::instance()->remove(libraryIndex);
+        break;
+    default:
+        ;
+    }
 }
 
-QString LibraryDialog::createItemText(const Book *book)
+QString LibraryDialog::createItemText(Book *book)
 {
-    QString text = book->title + "\n";
+    Q_ASSERT(book);
+    QString text = book->shortName() + "\n";
     if (book->creators.size()) {
         text += book->creators[0];
         for (int i = 1; i < book->creators.size(); i++) {
@@ -151,29 +137,12 @@ QString LibraryDialog::createItemText(const Book *book)
     return text;
 }
 
-void LibraryDialog::onCurrentBookChanged()
-{
-    close();
-}
-
 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
 {
     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
-    list->selectionModel()->clearSelection();
     if (sortedIndex.isValid()) {
-        list->selectionModel()->select(sortedIndex,
-                                       QItemSelectionModel::Select);
-        list->setCurrentIndex(sortedIndex);
-    }
-}
-
-QModelIndex LibraryDialog::selected() const
-{
-    QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
-    if (selectedItems.size()) {
-        return sortedLibrary->mapToSource(selectedItems[0]);
+        setCurrentItem(sortedIndex);
     }
-    return QModelIndex();
 }
 
 void LibraryDialog::onAddFolder()
@@ -229,7 +198,9 @@ void LibraryDialog::onAddFromFolder(const QString &path)
 
 void LibraryDialog::onSearch()
 {
+    TRACE;
     int ret = searchDialog->exec();
+    qDebug() << "Search dialog returned" << ret;
     if (ret != QDialog::Accepted) {
         return;
     }
@@ -242,7 +213,7 @@ void LibraryDialog::showSearchResults()
 {
     progress->reset();
     QList<Search::Result> results = Search::instance()->results();
-    if (results.count() == 0) {
+    if (results.isEmpty()) {
         QMessageBox::information(this, tr("Search results"),
                                  tr("No books found"));
         return;
@@ -253,3 +224,21 @@ void LibraryDialog::showSearchResults()
             this, SLOT(onAddSearchResult(const Search::Result &)));
     dialog->show();
 }
+
+void LibraryDialog::onSortByAuthor()
+{
+    TRACE;
+    sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
+    Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
+    sortByAuthor->setChecked(true);
+    sortByTitle->setChecked(false);
+}
+
+void LibraryDialog::onSortByTitle()
+{
+    TRACE;
+    sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
+    Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
+    sortByAuthor->setChecked(false);
+    sortByTitle->setChecked(true);
+}