Fix forward navigation control on Linux.
[dorian] / librarydialog.cpp
index 4312cd4..9e985b2 100644 (file)
@@ -8,7 +8,6 @@
 #include "infodialog.h"
 #include "settings.h"
 #include "listwindow.h"
-#include "listview.h"
 #include "trace.h"
 #include "bookfinder.h"
 #include "searchdialog.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
-
-    sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
+    // Add menu actions
+    sortByTitle =
+            addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
     sortByAuthor =
             addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
 
-    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");
-
-    // Create and add list view
-    list = new ListView(this);
+    // Set model
     sortedLibrary = new SortedLibrary(this);
-    list->setModel(sortedLibrary);
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-    list->setSpacing(1);
-    Library *library = Library::instance();
-    QModelIndex current = library->nowReading();
-    setSelected(sortedLibrary->mapFromSource(current));
-    addList(list);
+    setModel(sortedLibrary);
 
-    progress = new ProgressDialog(tr("Adding books"), this);
+    // 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");
+    addItemButton(tr("Delete"), this, SLOT(onDelete()), "delete");
 
-    connect(Library::instance(),
-            SIGNAL(rowsInserted(const QModelIndex &, int, int)),
-            this,
-            SLOT(onBookAdded()));
-    connect(list, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(onItemActivated(const QModelIndex &)));
+    // Set selected item
+    Library *library = Library::instance();
+    QModelIndex current = library->nowReading();
+    setCurrentItem(sortedLibrary->mapFromSource(current));
 
-    // Create search dialog
+    // 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, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
+            this, SLOT(onBookAdded()));
+    connect(this, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
+
     // Retrieve default sort criteria
     switch (Settings::instance()->value("lib/sortby").toInt()) {
     case SortedLibrary::SortByAuthor:
@@ -79,7 +79,7 @@ void LibraryDialog::onAdd()
     }
 
     // Get book file name
-    QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
+    QString path = QFileDialog::getOpenFileName(this, tr("Add book"),
                                                 lastDir, "Books (*.epub)");
     if (path == "") {
         return;
@@ -110,30 +110,41 @@ void LibraryDialog::onBookAdded()
 void LibraryDialog::onItemActivated(const QModelIndex &index)
 {
     TRACE;
+
     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
     Book *book = Library::instance()->book(libraryIndex);
     int ret = (new InfoDialog(book, this))->exec();
 
     switch (ret) {
     case InfoDialog::Read:
-        {
-            QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
-            Q_ASSERT(current.isValid());
-            Library::instance()->setNowReading(current);
-            close();
-        }
+        Library::instance()->setNowReading(libraryIndex);
+        close();
         break;
     case InfoDialog::Delete:
-        {
-            QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
-            Library::instance()->remove(current);
-        }
+        Library::instance()->remove(libraryIndex);
         break;
     default:
         ;
     }
 }
 
+void LibraryDialog::onDelete()
+{
+    QModelIndex current = currentItem();
+    if (!current.isValid()) {
+        return;
+    }
+    QModelIndex libraryIndex = sortedLibrary->mapToSource(current);
+    Book *book = Library::instance()->book(libraryIndex);
+    if (QMessageBox::Yes !=
+        QMessageBox::question(this, tr("Delete book"),
+            tr("Delete book \"%1\"?").arg(book->shortName()),
+            QMessageBox::Yes | QMessageBox::No)) {
+        return;
+    }
+    Library::instance()->remove(libraryIndex);
+}
+
 QString LibraryDialog::createItemText(Book *book)
 {
     Q_ASSERT(book);
@@ -150,23 +161,11 @@ QString LibraryDialog::createItemText(Book *book)
 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);
+        setCurrentItem(sortedIndex);
     }
 }
 
-QModelIndex LibraryDialog::selected() const
-{
-    QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
-    if (selectedItems.size()) {
-        return sortedLibrary->mapToSource(selectedItems[0]);
-    }
-    return QModelIndex();
-}
-
 void LibraryDialog::onAddFolder()
 {
     TRACE;
@@ -220,7 +219,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;
     }