Sort library by author or title. Sort books by two levels.
authorAkos Polster <akos@pipacs.com>
Sat, 13 Nov 2010 21:20:58 +0000 (22:20 +0100)
committerAkos Polster <akos@pipacs.com>
Sat, 13 Nov 2010 21:20:58 +0000 (22:20 +0100)
12 files changed:
librarydialog.cpp
librarydialog.h
mainwindow.cpp
model/book.cpp
model/sortedlibrary.cpp
model/sortedlibrary.h
pkg/changelog
pkg/version.txt
searchresultinfodialog.cpp
searchresultsdialog.cpp
widgets/listwindow.cpp
widgets/listwindow.h

index 796fad6..a3a94f5 100644 (file)
 #include "platform.h"
 #include "searchresultsdialog.h"
 #include "progressdialog.h"
+#include "settings.h"
 
 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
+    TRACE;
     setWindowTitle(tr("Library"));
+    setAttribute(Qt::WA_DeleteOnClose, true);
 
     // Add 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()));
@@ -40,7 +47,7 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     list->setSpacing(1);
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
-    setSelected(current);
+    setSelected(sortedLibrary->mapFromSource(current));
     addList(list);
 
     progress = new ProgressDialog(tr("Adding books"), this);
@@ -58,6 +65,15 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     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 +101,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,7 +111,8 @@ void LibraryDialog::onAdd()
 void LibraryDialog::onBookAdded()
 {
     Library *library = Library::instance();
-    setSelected(library->index(library->rowCount() - 1));
+    setSelected(sortedLibrary->
+                mapFromSource(library->index(library->rowCount() - 1)));
 }
 
 #ifndef Q_WS_MAEMO_5
@@ -139,9 +156,10 @@ void LibraryDialog::onItemActivated(const QModelIndex &index)
     (new InfoDialog(book, this))->exec();
 }
 
-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++) {
@@ -242,7 +260,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 +271,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);
+}
index 52b0deb..7b368a4 100644 (file)
@@ -16,6 +16,7 @@ class Book;
 class InfoWindow;
 class SortedLibrary;
 class SearchDialog;
+class QAction;
 
 /** Manage library. */
 class LibraryDialog: public ListWindow
@@ -40,15 +41,19 @@ public slots:
     void onAddFromFolderDone(int added);
     void onSearch();
     void showSearchResults();
+    void onSortByAuthor();
+    void onSortByTitle();
 
 private:
-    QString createItemText(const Book *book);
+    QString createItemText(Book *book);
     void setSelected(const QModelIndex &index);
     QModelIndex selected() const;
     ListView *list;
     SortedLibrary *sortedLibrary;
     ProgressDialog *progress;
     SearchDialog *searchDialog;
+    QAction *sortByAuthor;
+    QAction *sortByTitle;
 };
 
 #endif // LIBRARYDIALOG_H
index ba148b0..4a5f266 100755 (executable)
@@ -58,12 +58,9 @@ MainWindow::MainWindow(QWidget *parent):
     view->show();
     layout->addWidget(view);
 
-    // Progress
+    // Dialogs
     progress = new Progress(this);
 
-    // Settings dialog
-    settings = new QDialog(this);
-
     // Tool bar actions
 
 #ifdef Q_OS_SYMBIAN
index 2715769..3cced2a 100644 (file)
@@ -505,7 +505,7 @@ bool Book::extractMetaData()
 {
     QStringList excludedExtensions;
     excludedExtensions << ".html" << ".xhtml" << ".xht" << ".htm" << ".gif"
-            << ".png" << ".css";
+            << ".png" << ".css" << "*.ttf" << "mimetype";
     return extract(excludedExtensions);
 }
 
index 3a0fb54..241dfe7 100644 (file)
@@ -1,4 +1,5 @@
 #include "sortedlibrary.h"
+#include "library.h"
 #include "book.h"
 #include "trace.h"
 
@@ -6,12 +7,23 @@ SortedLibrary::SortedLibrary(QObject *parent):
         QSortFilterProxyModel(parent), mSortBy(SortByTitle)
 {
     setSourceModel(Library::instance());
+    setDynamicSortFilter(true);
     sort(0);
 }
 
-void SortedLibrary::sortBy(SortBy key)
+void SortedLibrary::setSortBy(SortBy key)
 {
-    mSortBy = key;
+    TRACE;
+    if (mSortBy != key) {
+        mSortBy = key;
+        invalidate();
+        sort(0);
+    }
+}
+
+SortedLibrary::SortBy SortedLibrary::sortBy()
+{
+    return mSortBy;
 }
 
 bool SortedLibrary::lessThan(const QModelIndex &left,
@@ -19,20 +31,34 @@ bool SortedLibrary::lessThan(const QModelIndex &left,
 {
     Book *leftBook = Library::instance()->book(left);
     Book *rightBook = Library::instance()->book(right);
-
-    QString leftString;
-    QString rightString;
+    int ret = 0;
 
     switch (mSortBy) {
-    case SortByTitle:
-        leftString = leftBook->shortName();
-        rightString = rightBook->shortName();
+    case SortByAuthor:
+        ret = compareBy(SortByAuthor, leftBook, rightBook);
+        if (ret == 0) {
+            ret = compareBy(SortByTitle, leftBook, rightBook);
+        }
         break;
     default:
-        leftString = leftBook->creators[0];
-        rightString = rightBook->creators[0];
-        break;
+        ret = compareBy(SortByTitle, leftBook, rightBook);
+        if (ret == 0) {
+            ret = compareBy(SortByAuthor, leftBook, rightBook);
+        }
     }
 
-    return QString::localeAwareCompare(leftString, rightString) < 0;
+    return ret < 0;
+}
+
+int SortedLibrary::compareBy(SortBy key, Book *left, Book *right) const
+{
+    Q_ASSERT(left);
+    Q_ASSERT(right);
+    switch (key) {
+    case SortByAuthor:
+        return QString::localeAwareCompare(left->creators.join(" "),
+                                           right->creators.join(" "));
+    default:
+        return QString::localeAwareCompare(left->shortName(), right->shortName());
+    }
 }
index f677684..3720fb0 100644 (file)
@@ -2,7 +2,8 @@
 #define SORTEDLIBRARY_H
 
 #include <QSortFilterProxyModel>
-#include "library.h"
+
+class Book;
 
 /** Sorted library model. */
 class SortedLibrary: public QSortFilterProxyModel
@@ -13,13 +14,17 @@ public:
     enum SortBy {SortByTitle, SortByAuthor};
     explicit SortedLibrary(QObject *parent = 0);
     bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
-    void sortBy(SortBy key);
+    void setSortBy(SortBy key);
+    SortBy sortBy();
 
 signals:
 
 public slots:
 
 protected:
+    int compareBy(SortBy key, Book *left, Book *right) const;
+
+private:
     SortBy mSortBy;
 };
 
index f53c5cc..bf269d6 100644 (file)
@@ -1,10 +1,16 @@
+dorian (0.3.7-1) unstable; urgency=low
+
+  * Sort library by title or author
+
+ -- Akos Polster <akos@pipacs.com>  Sat, 13 Nov 2010 02:00:00 +0100
+
 dorian (0.3.6-1) unstable; urgency=low
 
   * Show Qt versions in Developer dialog box
   * Delete temporary files
   * Don't require Qt 4.7 on Symbian
 
- -- Akos Polster <akos@pipacs.com>  Sun,  7 Nov 2010 02:00:00 +0100
+ -- Akos Polster <akos@pipacs.com>  Fri, 12 Nov 2010 02:00:00 +0100
 
 dorian (0.3.5-1) unstable; urgency=low
 
index 856a74e..fa19453 100644 (file)
@@ -1 +1 @@
-"0.3.6"
+"0.3.7"
index 2ba2350..1f2837d 100644 (file)
@@ -8,7 +8,7 @@ SearchResultInfoDialog::SearchResultInfoDialog(const Search::Result &result_,
                                                QWidget *parent):
     Dyalog(parent), result(result_)
 {
-    setWindowTitle(tr("Download Book?"));
+    setWindowTitle(tr("Download book?"));
 
     QLabel *title = new QLabel(result.title, this);
     addWidget(title);
index 9535938..940c438 100644 (file)
@@ -16,7 +16,7 @@
 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
     QWidget *parent): ListWindow(parent), results(results_)
 {
-    setWindowTitle(tr("Search Results"));
+    setWindowTitle(tr("Search results"));
 
     foreach (Search::Result result, results) {
         QString author;
@@ -42,7 +42,7 @@ SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
             this,
             SLOT(onEndDownload(int, const Search::Result &, const QString &)));
 
-    progress = new ProgressDialog(tr("Downloading Book"), this);
+    progress = new ProgressDialog(tr("Downloading book"), this);
 }
 
 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
index 65e26b5..219f208 100644 (file)
@@ -131,6 +131,29 @@ void ListWindow::addItemAction(const QString &title, QObject *receiver,
 #endif // Q_WS_MAEMO_5
 }
 
+QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
+                                   const char *slot)
+{
+    TRACE;
+    QAction *action = 0;
+#if defined(Q_WS_MAEMO_5)
+    action = menuBar()->addAction(title);
+    connect(action, SIGNAL(triggered()), receiver, slot);
+#elif defined(Q_OS_SYMBIAN)
+    action = new QAction(title, this);
+    connect(action, SIGNAL(triggered()), receiver, slot);
+    action->setSoftKeyRole(QAction::PositiveSoftKey);
+    menuBar()->addAction(action);
+#else
+    Q_UNUSED(title);
+    Q_UNUSED(receiver);
+    Q_UNUSED(slot);
+    QAction = new QAction(this);
+#endif
+    action->setCheckable(true);
+    return action;
+}
+
 #ifdef Q_WS_MAEMO_5
 
 void ListWindow::closeEvent(QCloseEvent *event)
index c9c958e..4e66301 100644 (file)
@@ -44,6 +44,12 @@ public:
     void addItemAction(const QString &title, QObject *receiver,
                        const char *slot);
 
+    /**
+      * Add an action to the menu.
+      */
+    QAction *addMenuAction(const QString &title, QObject *receiver,
+                           const char *slot);
+
 public slots:
 #ifdef Q_OS_SYMBIAN
     void show();