From 678671f16bae6bf6276d9a56bf9f4bd17119c239 Mon Sep 17 00:00:00 2001 From: Akos Polster Date: Sat, 13 Nov 2010 22:20:58 +0100 Subject: [PATCH] Sort library by author or title. Sort books by two levels. --- librarydialog.cpp | 48 ++++++++++++++++++++++++++++++++++++------ librarydialog.h | 7 ++++++- mainwindow.cpp | 5 +---- model/book.cpp | 2 +- model/sortedlibrary.cpp | 50 +++++++++++++++++++++++++++++++++----------- model/sortedlibrary.h | 9 ++++++-- pkg/changelog | 8 ++++++- pkg/version.txt | 2 +- searchresultinfodialog.cpp | 2 +- searchresultsdialog.cpp | 4 ++-- widgets/listwindow.cpp | 23 ++++++++++++++++++++ widgets/listwindow.h | 6 ++++++ 12 files changed, 135 insertions(+), 31 deletions(-) diff --git a/librarydialog.cpp b/librarydialog.cpp index 796fad6..a3a94f5 100644 --- a/librarydialog.cpp +++ b/librarydialog.cpp @@ -15,13 +15,20 @@ #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 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); +} diff --git a/librarydialog.h b/librarydialog.h index 52b0deb..7b368a4 100644 --- a/librarydialog.h +++ b/librarydialog.h @@ -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 diff --git a/mainwindow.cpp b/mainwindow.cpp index ba148b0..4a5f266 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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 diff --git a/model/book.cpp b/model/book.cpp index 2715769..3cced2a 100644 --- a/model/book.cpp +++ b/model/book.cpp @@ -505,7 +505,7 @@ bool Book::extractMetaData() { QStringList excludedExtensions; excludedExtensions << ".html" << ".xhtml" << ".xht" << ".htm" << ".gif" - << ".png" << ".css"; + << ".png" << ".css" << "*.ttf" << "mimetype"; return extract(excludedExtensions); } diff --git a/model/sortedlibrary.cpp b/model/sortedlibrary.cpp index 3a0fb54..241dfe7 100644 --- a/model/sortedlibrary.cpp +++ b/model/sortedlibrary.cpp @@ -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()); + } } diff --git a/model/sortedlibrary.h b/model/sortedlibrary.h index f677684..3720fb0 100644 --- a/model/sortedlibrary.h +++ b/model/sortedlibrary.h @@ -2,7 +2,8 @@ #define SORTEDLIBRARY_H #include -#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; }; diff --git a/pkg/changelog b/pkg/changelog index f53c5cc..bf269d6 100644 --- a/pkg/changelog +++ b/pkg/changelog @@ -1,10 +1,16 @@ +dorian (0.3.7-1) unstable; urgency=low + + * Sort library by title or author + + -- Akos Polster 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 Sun, 7 Nov 2010 02:00:00 +0100 + -- Akos Polster Fri, 12 Nov 2010 02:00:00 +0100 dorian (0.3.5-1) unstable; urgency=low diff --git a/pkg/version.txt b/pkg/version.txt index 856a74e..fa19453 100644 --- a/pkg/version.txt +++ b/pkg/version.txt @@ -1 +1 @@ -"0.3.6" +"0.3.7" diff --git a/searchresultinfodialog.cpp b/searchresultinfodialog.cpp index 2ba2350..1f2837d 100644 --- a/searchresultinfodialog.cpp +++ b/searchresultinfodialog.cpp @@ -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); diff --git a/searchresultsdialog.cpp b/searchresultsdialog.cpp index 9535938..940c438 100644 --- a/searchresultsdialog.cpp +++ b/searchresultsdialog.cpp @@ -16,7 +16,7 @@ SearchResultsDialog::SearchResultsDialog(const QList 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 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) diff --git a/widgets/listwindow.cpp b/widgets/listwindow.cpp index 65e26b5..219f208 100644 --- a/widgets/listwindow.cpp +++ b/widgets/listwindow.cpp @@ -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) diff --git a/widgets/listwindow.h b/widgets/listwindow.h index c9c958e..4e66301 100644 --- a/widgets/listwindow.h +++ b/widgets/listwindow.h @@ -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(); -- 1.7.9.5