Re-design ListWindow.
authorAkos Polster <polster@conmp2-42a162100.europe.nokia.com>
Thu, 18 Nov 2010 16:23:32 +0000 (17:23 +0100)
committerAkos Polster <polster@conmp2-42a162100.europe.nokia.com>
Thu, 18 Nov 2010 16:23:32 +0000 (17:23 +0100)
bookmarksdialog.cpp
bookmarksdialog.h
chaptersdialog.cpp
librarydialog.cpp
searchresultsdialog.cpp
searchresultsdialog.h
widgets/listwindow.cpp
widgets/listwindow.h

index f92e30a..2186671 100644 (file)
@@ -3,7 +3,7 @@
 #include "bookmarksdialog.h"
 #include "book.h"
 #include "bookmarkinfodialog.h"
-#include "listview.h"
+#include "trace.h"
 
 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
     ListWindow(parent), book(book_)
@@ -13,9 +13,9 @@ BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
         return;
     }
 
-    addAction(tr("Add bookmark"), this, SLOT(onAdd()), "add");
+    addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
 
-    // Build bookmark list
+    // Build and set bookmark model
     // FIXME: Localize me
     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
         QString label("At ");
@@ -32,25 +32,23 @@ BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
         }
         data.append(label);
     }
-
-    // Create bookmark list view
     QStringListModel *model = new QStringListModel(data, this);
-    list = new ListView;
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-    list->setModel(model);
-    addList(list);
-    connect(list, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(onItemActivated(const QModelIndex &)));
-    addList(list);
+    setModel(model);
+
+    // FIXME
+    // connect(list, SIGNAL(activated(const QModelIndex &)),
+    //         this, SLOT(onItemActivated(const QModelIndex &)));
 }
 
 void BookmarksDialog::onGo()
 {
-    QModelIndex current = list->currentIndex();
-    if (current.isValid()) {
-        emit goToBookmark(current.row());
-        close();
-    }
+    TRACE;
+    // FIXME
+    // QModelIndex current = list->currentIndex();
+    // if (current.isValid()) {
+    //     emit goToBookmark(current.row());
+    //     close();
+    // }
 }
 
 void BookmarksDialog::onItemActivated(const QModelIndex &index)
index 581d764..018822d 100644 (file)
@@ -7,7 +7,6 @@
 
 class QCloseEvent;
 class Book;
-class ListView;
 
 /** Dialog box managing bookmarks. */
 class BookmarksDialog: public ListWindow
@@ -29,7 +28,6 @@ public slots:
 
 protected:
     Book *book;
-    ListView *list;
     QStringList data;
 };
 
index 24fe3dd..41aaf59 100644 (file)
@@ -7,7 +7,6 @@
 ChaptersDialog::ChaptersDialog(Book *book, QWidget *parent): ListWindow(parent)
 {
     setWindowTitle(tr("Chapters"));
-
     if (book) {
         foreach (QString id, book->chapters) {
             QString contentTitle = book->content[id].name;
@@ -15,12 +14,10 @@ ChaptersDialog::ChaptersDialog(Book *book, QWidget *parent): ListWindow(parent)
         }
     }
     QStringListModel *model = new QStringListModel(data, this);
-    list = new ListView;
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-    list->setModel(model);
-    addList(list);
-    connect(list, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(onItemActivated(const QModelIndex &)));
+    setModel(model);
+    // FIXME
+    // connect(list, SIGNAL(activated(const QModelIndex &)),
+    //         this, SLOT(onItemActivated(const QModelIndex &)));
 }
 
 void ChaptersDialog::onItemActivated(const QModelIndex &index)
index 4312cd4..0b3df65 100644 (file)
@@ -23,26 +23,24 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     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()));
 
-    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);
+    setModel(sortedLibrary);
+
+    // 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");
+
+    // Set selected item
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
-    setSelected(sortedLibrary->mapFromSource(current));
-    addList(list);
+    // FIXME: setSelected(sortedLibrary->mapFromSource(current));
 
     progress = new ProgressDialog(tr("Adding books"), this);
 
@@ -50,7 +48,7 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
             this,
             SLOT(onBookAdded()));
-    connect(list, SIGNAL(activated(const QModelIndex &)),
+    connect(this, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
 
     // Create search dialog
index 940c438..41c5b18 100644 (file)
@@ -27,14 +27,10 @@ SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
     }
 
     QStringListModel *model = new QStringListModel(data, this);
-    list = new ListView;
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-    list->setModel(model);
-    list->setUniformItemSizes(true);
-    addList(list);
-    addItemAction(tr("Download book"), this, SLOT(onDownload()));
-    connect(list, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(onItemActivated(const QModelIndex &)));
+    setModel(model);
+    // FIXME
+    // connect(list, SIGNAL(activated(const QModelIndex &)),
+    //         this, SLOT(onItemActivated(const QModelIndex &)));
     Search *search = Search::instance();
     connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
     connect(search,
@@ -61,11 +57,6 @@ void SearchResultsDialog::onItemActivated(const QModelIndex &index)
     }
 }
 
-void SearchResultsDialog::onDownload()
-{
-    onItemActivated(list->currentIndex());
-}
-
 QString SearchResultsDialog::downloadName() const
 {
     TRACE;
index 29427bf..a3b97e7 100644 (file)
@@ -21,7 +21,6 @@ public:
                                  QWidget *parent = 0);
 
 protected slots:
-    void onDownload();
     void onItemActivated(const QModelIndex &index);
     void onBeginDownload(int size);
     void onEndDownload(int status, const Search::Result &result,
index 1ebd5c8..db92196 100644 (file)
@@ -1,47 +1,28 @@
 #include <QtGui>
+#include <QListWidget>
+#include <QAbstractItemModel>
 
 #include "listwindow.h"
 #include "trace.h"
-#include "listview.h"
 #include "platform.h"
 
 #ifdef Q_OS_SYMBIAN
 #include "flickcharm.h"
 #endif
 
-ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
+ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), model(0)
 {
 #if defined(Q_WS_MAEMO_5)
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
-    popup = new QMenu(this);
-
-    QScrollArea *scroller = new QScrollArea(this);
-    setCentralWidget(scroller);
-    scroller->setProperty("FingerScrollable", true);
-    // scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-    // scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-    scroller->setFrameStyle(QFrame::NoFrame);
-    scroller->show();
-
-    QWidget *content = new QWidget(scroller);
-    contentLayout = new QVBoxLayout(content);
-    contentLayout->setMargin(0);
-    content->setLayout(contentLayout);
-    content->show();
-
-    scroller->setWidget(content);
-    scroller->setWidgetResizable(true);
-#else
-    QFrame *frame = new QFrame(this);
-    setCentralWidget(frame);
-    contentLayout = new QHBoxLayout();
-    frame->setLayout(contentLayout);
-    buttonBox = new QDialogButtonBox(Qt::Vertical, this);
-    contentLayout->addWidget(buttonBox);
-#endif // Q_WS_MAEMO_5
+#endif
+
+    list = new QListWidget(this);
+    populateList();
+    setCentralWidget(list);
 
 #ifdef Q_OS_SYMBIAN
-    charm = 0;
+    charm = new FlickCharm(this);
+    charm->activateOn(list);
     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
@@ -49,89 +30,64 @@ ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
 #endif // Q_OS_SYMBIAN
 
 #ifdef Q_WS_MAC
-    addAction(tr("Close"), this, SLOT(close()), QString(),
-              QDialogButtonBox::RejectRole);
+    // 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 &)));
 }
 
-void ListWindow::addList(ListView *listView)
+void ListWindow::populateList()
 {
     TRACE;
-    list = listView;
-#if defined(Q_WS_MAEMO_5)
-    // FIXME: list->installEventFilter(this);
-    list->setProperty("FingerScrollable", false);
-    int height = list->sizeHintForRow(0) * list->model()->rowCount();
-    qDebug() << "Minimum height" << height;
-    list->setMinimumHeight(height);
-    contentLayout->addWidget(list);
-    connect(list->model(),
-            SIGNAL(rowsInserted(const QModelIndex &, int, int)),
-            this, SLOT(onModelChanged()));
-    connect(list->model(),
-            SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
-            this, SLOT(onModelChanged()));
-#else
-    contentLayout->insertWidget(0, list);
-#endif // Q_WS_MAEMO5
 
-#ifdef Q_OS_SYMBIAN
-    if (!charm) {
-        charm = new FlickCharm(this);
+    list->clear();
+    if (model) {
+        for (int i = 0; i < model->rowCount(); i++) {
+            QModelIndex index = model->index(i, 0);
+            QString text = model->data(index, Qt::DisplayRole).toString();
+            QIcon icon;
+            QVariant iconData = model->data(index, Qt::DecorationRole);
+            if (iconData.canConvert<QIcon>()) {
+                icon = iconData.value<QIcon>();
+            }
+            (void)new QListWidgetItem(icon, text, list);
+        }
     }
-    // FIXME: Charms need more work...: charm->activateOn(list);
-#endif // Q_OS_SYMBIAN
+    for (int i = 0; i < buttons.count(); i++) {
+        QListWidgetItem *item = new QListWidgetItem();
+        list->insertItem(i, item);
+        list->setItemWidget(item, buttons[i]);
+    }
+}
 
-    connect(list->selectionModel(),
-      SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
-      this,
-      SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
+void ListWindow::setModel(QAbstractItemModel *model_)
+{
+    model = model_;
+    populateList();
+    if (model) {
+        connect(model,
+                SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
+                this, SLOT(populateList()));
+    }
 }
 
-void ListWindow::addAction(const QString &title, QObject *receiver,
-                           const char *slot, const QString &iconName,
-                           QDialogButtonBox::ButtonRole role)
+void ListWindow::addButton(const QString &title, QObject *receiver,
+                           const char *slot, const QString &iconName)
 {
     TRACE;
-#ifdef Q_WS_MAEMO_5
-    Q_UNUSED(role);
+
     QPushButton *button = new QPushButton(QIcon(Platform::instance()->
                                                 icon(iconName)), title, this);
-    contentLayout->addWidget(button);
-    connect(button, SIGNAL(clicked()), receiver, slot);
-#elif defined(Q_OS_SYMBIAN)
-    Q_UNUSED(role);
-    Q_UNUSED(iconName);
-    QAction *action = new QAction(title, this);
-    connect(action, SIGNAL(triggered()), receiver, slot);
-    action->setSoftKeyRole(QAction::PositiveSoftKey);
-    menuBar()->addAction(action);
-#else
-    Q_UNUSED(iconName);
-    QPushButton *button = buttonBox->addButton(title, role);
     connect(button, SIGNAL(clicked()), receiver, slot);
-#endif // Q_WS_MAEMO_5
-}
+    buttons.append(button);
 
-void ListWindow::addItemAction(const QString &title, QObject *receiver,
-                               const char *slot)
-{
-    TRACE;
-#ifdef Q_WS_MAEMO_5
-    popup->addAction(title, receiver, slot);
-#elif defined Q_OS_SYMBIAN
-    QAction *action = new QAction(title, this);
-    connect(action, SIGNAL(triggered()), receiver, slot);
-    action->setSoftKeyRole(QAction::PositiveSoftKey);
-    menuBar()->addAction(action);
-    // FIXME: Add action to the list of item specific actions
-#else
-    QPushButton *button =
-            buttonBox->addButton(title, QDialogButtonBox::ActionRole);
-    connect(button, SIGNAL(clicked()), receiver, slot);
-    itemButtons.append(button);
-    activateItemButtons();
-#endif // Q_WS_MAEMO_5
+    int pos = buttons.length() - 1;
+    QListWidgetItem *item = new QListWidgetItem();
+    list->insertItem(pos, item);
+    list->setItemWidget(item, button);
 }
 
 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
@@ -157,66 +113,20 @@ QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
     return action;
 }
 
-#ifdef Q_WS_MAEMO_5
-
-void ListWindow::closeEvent(QCloseEvent *event)
-{
-    // Work around Maemo/Qt but: Menu items are not removed on close
-    menuBar()->clear();
-    event->accept();
-}
-
-#endif // Q_WS_MAEMO_5
-
-void ListWindow::onSelectionChanged(const QItemSelection &selected,
-                                    const QItemSelection &deselected)
-{
-    Q_UNUSED(selected);
-    Q_UNUSED(deselected);
-#ifndef Q_WS_MAEMO_5
-    activateItemButtons();
-#endif
-}
-
-#ifndef Q_WS_MAEMO_5
-
-void ListWindow::activateItemButtons()
+void ListWindow::onItemActivated(const QModelIndex &)
 {
-    bool enable = false;
-    if (list) {
-        enable = list->selectionModel()->hasSelection();
-    }
-    foreach (QPushButton *button, itemButtons) {
-        button->setEnabled(enable);
-    }
+    TRACE;
+    // FIXME
 }
 
-#endif // ! Q_WS_MAEMO_5
-
 #ifdef Q_WS_MAEMO_5
 
-bool ListWindow::eventFilter(QObject *obj, QEvent *event)
-{
-    if (event->type() == QEvent::ContextMenu) {
-        qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu";
-        if (popup->actions().size()) {
-            QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
-            QPoint pos = mouseEvent->globalPos();
-            pos.setX(pos.x() - 150);
-            if (pos.x() < 0) {
-                pos.setX(0);
-            }
-            popup->exec(pos);
-        }
-        return true;
-    } else {
-        return QObject::eventFilter(obj, event);
-    }
-}
-
-void ListWindow::onModelChanged()
+void ListWindow::closeEvent(QCloseEvent *event)
 {
-    list->setMinimumHeight(list->contentsHeight());
+    // Work around Maemo/Qt bug: Menu items are not removed on close
+    menuBar()->clear();
+    // FIXME: Is this needed? event->accept();
+    QMainWindow::closeEvent(event);
 }
 
 #endif // Q_WS_MAEMO_5
index 4e66301..36545ee 100644 (file)
@@ -5,15 +5,12 @@
 #include <QDialogButtonBox>
 #include <QList>
 
-class QListView;
 class QString;
-class QBoxLayout;
 class QPushButton;
-class QModelIndex;
-class QItemSelection;
-class QEvent;
-class ListView;
 class FlickCharm;
+class QAbstractItemModel;
+class QListWidget;
+class QModelIndex;
 
 /** A window with a list and menu actions (Maemo) or buttons (non-Maemo). */
 class ListWindow: public QMainWindow
@@ -21,28 +18,23 @@ class ListWindow: public QMainWindow
     Q_OBJECT
 
 public:
+    /**
+     * Constructor.
+     * @param   model   Model for the list widget contents.
+     * @param   parent  Parent widget.
+     */
     explicit ListWindow(QWidget *parent = 0);
 
-    /** Add a list view to the window. */
-    void addList(ListView *list);
-
     /**
-     * Add an action to the window: either a button, or, on Maemo, a top
-     * level menu item.
-     * Activating the action invokes the slot with no parameters.
+     * Set the model for the list.
      */
-    void addAction(const QString &title, QObject *receiver, const char *slot,
-        const QString &iconPath = QString(),
-        QDialogButtonBox::ButtonRole role = QDialogButtonBox::ActionRole);
+    void setModel(QAbstractItemModel *model);
 
     /**
-     * Add an action to the selected item in the list: either a button which is
-     * enabled when a list item is selected, or, on Maemo, a pop-up menu item
-     * which is displayed when a list item is long-pressed.
-     * Activating the action invokes the slot with no parameters.
+     * Add an action button to the beginning of the list.
      */
-    void addItemAction(const QString &title, QObject *receiver,
-                       const char *slot);
+    void addButton(const QString &title, QObject *receiver, const char *slot,
+                   const QString &iconPath = QString());
 
     /**
       * Add an action to the menu.
@@ -56,28 +48,16 @@ public slots:
 #endif
 
 protected slots:
-    void onSelectionChanged(const QItemSelection &selected,
-                            const QItemSelection &deselected);
-#ifdef Q_WS_MAEMO_5
-    void onModelChanged();
-#else
-    void activateItemButtons();
-#endif
+    void onItemActivated(const QModelIndex &);
+    void populateList();
 
 protected:
-#ifdef Q_WS_MAEMO_5
-    bool eventFilter(QObject *obj, QEvent *event);
-    void closeEvent(QCloseEvent *event);
-    QMenu *popup;
-#else
-    QDialogButtonBox *buttonBox;
-    QList<QPushButton *> itemButtons;
-#endif // Q_WS_MAEMO_5
+    QListWidget *list;
+    QAbstractItemModel *model;
+    QList<QPushButton *> buttons;
 #ifdef Q_OS_SYMBIAN
     FlickCharm *charm;
 #endif
-    QBoxLayout *contentLayout;
-    ListView *list;
 };
 
 #endif // LISTWINDOW_H