Updated library dialog works.
authorAkos Polster <akos@pipacs.com>
Thu, 18 Nov 2010 23:10:10 +0000 (00:10 +0100)
committerAkos Polster <akos@pipacs.com>
Thu, 18 Nov 2010 23:10:10 +0000 (00:10 +0100)
13 files changed:
bookmarksdialog.cpp
chaptersdialog.cpp
chaptersdialog.h
librarydialog.cpp
librarydialog.h
mainwindow.cpp
model/library.cpp
pkg/changelog
searchdialog.cpp
searchresultsdialog.cpp
searchresultsdialog.h
widgets/listwindow.cpp
widgets/listwindow.h

index 2186671..8de9113 100644 (file)
@@ -6,15 +6,13 @@
 #include "trace.h"
 
 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
-    ListWindow(parent), book(book_)
+    ListWindow(tr("(No bookmarks)"), parent), book(book_)
 {
     setWindowTitle(tr("Bookmarks"));
     if (!book) {
         return;
     }
 
-    addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
-
     // Build and set bookmark model
     // FIXME: Localize me
     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
@@ -35,6 +33,8 @@ BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
     QStringListModel *model = new QStringListModel(data, this);
     setModel(model);
 
+    addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
+
     // FIXME
     // connect(list, SIGNAL(activated(const QModelIndex &)),
     //         this, SLOT(onItemActivated(const QModelIndex &)));
@@ -78,6 +78,8 @@ void BookmarksDialog::onAdd()
 
 void BookmarksDialog::onDelete(bool really)
 {
+#if 0
+    // FIXME
     QModelIndex current = list->currentIndex();
     if (!current.isValid()) {
         return;
@@ -92,4 +94,5 @@ void BookmarksDialog::onDelete(bool really)
     int row = current.row();
     list->model()->removeRow(row);
     book->deleteBookmark(row);
+#endif
 }
index 41aaf59..378c17f 100644 (file)
@@ -2,9 +2,9 @@
 
 #include "chaptersdialog.h"
 #include "book.h"
-#include "listview.h"
 
-ChaptersDialog::ChaptersDialog(Book *book, QWidget *parent): ListWindow(parent)
+ChaptersDialog::ChaptersDialog(Book *book, QWidget *parent):
+        ListWindow(tr("(No chapters)"), parent)
 {
     setWindowTitle(tr("Chapters"));
     if (book) {
index 7ec300c..a29d887 100644 (file)
@@ -7,7 +7,6 @@
 
 class QWidget;
 class QModelIndex;
-class ListView;
 class Book;
 
 /** Display book chapters. */
@@ -25,7 +24,6 @@ public slots:
     void onItemActivated(const QModelIndex &index);
 
 protected:
-    ListView *list;
     QStringList data;
 };
 
index 0b3df65..e45fe8f 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"
@@ -17,7 +16,7 @@
 #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"));
@@ -40,22 +39,21 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     // Set selected item
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
-    // FIXME: setSelected(sortedLibrary->mapFromSource(current));
+    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(rowsInserted(const QModelIndex &, int, int)),
-            this,
-            SLOT(onBookAdded()));
+    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:
@@ -108,24 +106,18 @@ 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:
         ;
@@ -148,21 +140,9 @@ 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);
-    }
-}
-
-QModelIndex LibraryDialog::selected() const
-{
-    QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
-    if (selectedItems.size()) {
-        return sortedLibrary->mapToSource(selectedItems[0]);
+        setCurrentItem(sortedIndex);
     }
-    return QModelIndex();
 }
 
 void LibraryDialog::onAddFolder()
@@ -218,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;
     }
index 480a66d..a29a093 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "listwindow.h"
 
-class ListView;
 class QPushButton;
 class QModelIndex;
 class QCloseEvent;
@@ -15,8 +14,8 @@ class ProgressDialog;
 class Book;
 class InfoWindow;
 class SortedLibrary;
-class SearchDialog;
 class QAction;
+class SearchDialog;
 
 /** Manage library. */
 class LibraryDialog: public ListWindow
@@ -41,13 +40,11 @@ public slots:
 private:
     QString createItemText(Book *book);
     void setSelected(const QModelIndex &index);
-    QModelIndex selected() const;
-    ListView *list;
     SortedLibrary *sortedLibrary;
     ProgressDialog *progress;
-    SearchDialog *searchDialog;
     QAction *sortByAuthor;
     QAction *sortByTitle;
+    SearchDialog *searchDialog;
 };
 
 #endif // LIBRARYDIALOG_H
index 4c635cd..eadf5e3 100755 (executable)
@@ -300,7 +300,6 @@ void MainWindow::showBookmarks()
     Book *book = Library::instance()->book(mCurrent);
     if (book) {
         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
-        bookmarks->setWindowModality(Qt::WindowModal);
         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
                 this, SLOT(onAddBookmark(const QString &)));
         connect(bookmarks, SIGNAL(goToBookmark(int)),
@@ -392,7 +391,6 @@ void MainWindow::showChapters()
     Book *book = Library::instance()->book(mCurrent);
     if (book) {
         ChaptersDialog *chapters = new ChaptersDialog(book, this);
-        chapters->setWindowModality(Qt::WindowModal);
         connect(chapters, SIGNAL(goToChapter(int)),
                 this, SLOT(onGoToChapter(int)));
         chapters->show();
index 144d9e5..6d66a91 100644 (file)
@@ -37,8 +37,6 @@ int Library::rowCount(const QModelIndex &parent) const
 
 QVariant Library::data(const QModelIndex &index, int role) const
 {
-    TRACE;
-
     QVariant ret;
     if (!index.isValid()) {
         return ret;
@@ -46,11 +44,9 @@ QVariant Library::data(const QModelIndex &index, int role) const
 
     switch (role) {
     case Qt::DisplayRole:
-        qDebug() << mBooks[index.row()]->name();
         ret = mBooks[index.row()]->name();
         break;
     case Qt::DecorationRole:
-        qDebug() << "(cover)";
         ret.setValue(mBooks[index.row()]->cover);
         break;
     default:
index d690533..4eeedd1 100644 (file)
@@ -3,6 +3,7 @@ dorian (0.3.9-1) unstable; urgency=low
   * Don't close library dialog when deleting current book
   * Really delete current book [#6614]
   * Remove inactive items from Options menu on Symbian [#6606]
+  * Fix Library window content scrolling [#6622]
 
  -- Akos Polster <akos@pipacs.com>  Wed, 17 Nov 2010 02:00:00 +0100
 
index bc5256c..e2f2b99 100644 (file)
@@ -6,7 +6,9 @@
 
 SearchDialog::SearchDialog(QWidget *parent): Dyalog(parent)
 {
+    TRACE;
     setWindowTitle(tr("Search the Web"));
+    setAttribute(Qt::WA_DeleteOnClose, false);
 
     QLabel *titleLabel = new QLabel(tr("Title:"), this);
     title = new QLineEdit(this);
index 41c5b18..15d6977 100644 (file)
@@ -5,7 +5,6 @@
 #include <QDir>
 #include <QFile>
 
-#include "listview.h"
 #include "searchresultsdialog.h"
 #include "searchresultinfodialog.h"
 #include "trace.h"
@@ -14,7 +13,7 @@
 #include "platform.h"
 
 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
-    QWidget *parent): ListWindow(parent), results(results_)
+    QWidget *parent): ListWindow(tr("(No results)"), parent), results(results_)
 {
     setWindowTitle(tr("Search results"));
 
@@ -28,9 +27,8 @@ SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
 
     QStringListModel *model = new QStringListModel(data, this);
     setModel(model);
-    // FIXME
-    // connect(list, SIGNAL(activated(const QModelIndex &)),
-    //         this, SLOT(onItemActivated(const QModelIndex &)));
+    connect(this, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
     Search *search = Search::instance();
     connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
     connect(search,
@@ -91,7 +89,7 @@ void SearchResultsDialog::onEndDownload(int status, const Search::Result &result
         Library::instance()->add(fileName);
         int row = results.indexOf(result);
         if (-1 != row) {
-            list->model()->removeRow(row);
+            model()->removeRow(row);
         }
         Platform::instance()->information(tr("Downloaded \"%1\"\nand added to the "
                                              "library").arg(result.title), this);
index a3b97e7..880a002 100644 (file)
@@ -7,7 +7,6 @@
 #include "listwindow.h"
 #include "search.h"
 
-class ListView;
 class QModelIndex;
 class ProgressDialog;
 
@@ -31,7 +30,6 @@ protected:
 
 private:
     const QList<Search::Result> results;
-    ListView *list;
     QStringList data;
     ProgressDialog *progress;
 };
index a1c9be3..748be7b 100644 (file)
 #include "flickcharm.h"
 #endif
 
-ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), model(0)
+ListWindow::ListWindow(const QString &noItems_, QWidget *parent):
+        QMainWindow(parent), mModel(0), noItems(noItems_)
 {
 #if defined(Q_WS_MAEMO_5)
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
 #endif
+    setAttribute(Qt::WA_DeleteOnClose);
 
     list = new QListWidget(this);
+    list->setSelectionMode(QAbstractItemView::SingleSelection);
     populateList();
     setCentralWidget(list);
 
@@ -29,14 +32,8 @@ ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), model(0)
     QMainWindow::addAction(closeAction);
 #endif // Q_OS_SYMBIAN
 
-#ifdef Q_WS_MAC
-    // FIXME
-    // addAction(tr("Close"), this, SLOT(close()), QString(),
-    //           QDialogButtonBox::RejectRole);
-#endif // Q_WS_MAC
-
     connect(list, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(onItemActicvated(const QModelIndex &)));
+            this, SLOT(onItemActivated(const QModelIndex &)));
 }
 
 void ListWindow::populateList()
@@ -46,48 +43,68 @@ void ListWindow::populateList()
     list->clear();
     list->setIconSize(QSize(48, 48)); // FIXME
     list->setUniformItemSizes(true);
-    if (model) {
-        for (int i = 0; i < model->rowCount(); i++) {
-            QModelIndex index = model->index(i, 0);
-            QString text = model->data(index, Qt::DisplayRole).toString();
-            QVariant imageData = model->data(index, Qt::DecorationRole);
+    if (mModel && mModel->rowCount()) {
+        for (int i = 0; i < mModel->rowCount(); i++) {
+            QModelIndex index = mModel->index(i, 0);
+            QString text = mModel->data(index, Qt::DisplayRole).toString();
+            QVariant imageData = mModel->data(index, Qt::DecorationRole);
             QIcon icon(QPixmap::fromImage(imageData.value<QImage>()));
             (void)new QListWidgetItem(icon, text, list);
         }
+    } else {
+        QListWidgetItem *item = new QListWidgetItem(noItems);
+        item->setFlags(Qt::NoItemFlags);
+        list->addItem(item);
     }
     for (int i = 0; i < buttons.count(); i++) {
-        QListWidgetItem *item = new QListWidgetItem();
-        item->setFlags(Qt::NoItemFlags);
-        list->insertItem(i, item);
-        list->setItemWidget(item, buttons[i]);
+        insertButton(i, buttons[i]);
     }
 }
 
-void ListWindow::setModel(QAbstractItemModel *model_)
+void ListWindow::insertButton(int row, const Button &b)
 {
-    model = model_;
+    QPushButton *pushButton = new QPushButton(
+        QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
+    connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot);
+    QListWidgetItem *item = new QListWidgetItem();
+    item->setFlags(Qt::NoItemFlags);
+    list->insertItem(row, item);
+    list->setItemWidget(item, pushButton);
+}
+
+void ListWindow::setModel(QAbstractItemModel *aModel)
+{
+    TRACE;
+    mModel = aModel;
     populateList();
-    if (model) {
-        connect(model,
-                SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
+    if (mModel) {
+        connect(mModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
+                this, SLOT(populateList()));
+        connect(mModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
+                this, SLOT(populateList()));
+        connect(mModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
                 this, SLOT(populateList()));
     }
 }
 
+QAbstractItemModel *ListWindow::model() const
+{
+    return mModel;
+}
+
 void ListWindow::addButton(const QString &title, QObject *receiver,
                            const char *slot, const QString &iconName)
 {
     TRACE;
 
-    QPushButton *button = new QPushButton(QIcon(Platform::instance()->
-                                                icon(iconName)), title, this);
-    connect(button, SIGNAL(clicked()), receiver, slot);
-    buttons.append(button);
+    Button b;
+    b.title = title;
+    b.receiver = receiver;
+    b.slot = slot;
+    b.iconName = iconName;
 
-    int pos = buttons.length() - 1;
-    QListWidgetItem *item = new QListWidgetItem();
-    list->insertItem(pos, item);
-    list->setItemWidget(item, button);
+    insertButton(buttons.length(), b);
+    buttons.append(b);
 }
 
 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
@@ -113,10 +130,18 @@ QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
     return action;
 }
 
-void ListWindow::onItemActivated(const QModelIndex &)
+void ListWindow::onItemActivated(const QModelIndex &index)
 {
     TRACE;
-    // FIXME
+    int row = index.row() - buttons.count();
+    qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
+    emit activated(mModel->index(row, 0));
+}
+
+void ListWindow::setCurrentItem(const QModelIndex &item)
+{
+    int index = item.row();
+    list->setCurrentItem(list->item(index + buttons.count()));
 }
 
 #ifdef Q_WS_MAEMO_5
index ba5d77b..71cef8b 100644 (file)
@@ -20,29 +20,33 @@ class ListWindow: public QMainWindow
 public:
     /**
      * Constructor.
-     * @param   model   Model for the list widget contents.
+     * @param   noItems Text to display when the list has no items.
      * @param   parent  Parent widget.
      */
-    explicit ListWindow(QWidget *parent = 0);
+    explicit ListWindow(const QString &noItems, QWidget *parent = 0);
 
-    /**
-     * Set the model for the list.
-     */
+    /** Set the model for the list. */
     void setModel(QAbstractItemModel *model);
 
-    /**
-     * Add an action button to the beginning of the list.
-     */
+    /** Get model. */
+    QAbstractItemModel *model() const;
+
+    /** Add an action button to the beginning of the list. */
     void addButton(const QString &title, QObject *receiver, const char *slot,
                    const QString &iconPath = QString());
 
-    /**
-      * Add an action to the menu.
-      */
+    /** Add an action to the menu. */
     QAction *addMenuAction(const QString &title, QObject *receiver,
                            const char *slot);
 
+signals:
+    /** Emitted when a list item is activated. */
+    void activated(const QModelIndex &index);
+
 public slots:
+    /** Set the current (selected) item. */
+    void setCurrentItem(const QModelIndex &item);
+
 #ifdef Q_OS_SYMBIAN
     void show();
 #endif
@@ -52,12 +56,22 @@ protected slots:
     void populateList();
 
 protected:
+    struct Button {
+        QString title;
+        QObject *receiver;
+        const char *slot;
+        QString iconName;
+    };
+    void insertButton(int row, const Button &button);
 #ifdef Q_WS_MAEMO_5
     void closeEvent(QCloseEvent *event);
 #endif
+
+private:
     QListWidget *list;
-    QAbstractItemModel *model;
-    QList<QPushButton *> buttons;
+    QAbstractItemModel *mModel;
+    QList<Button> buttons;
+    QString noItems;
 #ifdef Q_OS_SYMBIAN
     FlickCharm *charm;
 #endif