Fix Symbian bugs.
[dorian] / widgets / listwindow.cpp
index c15fa30..3443024 100644 (file)
 #include <QtGui>
+#include <QListWidget>
+#include <QAbstractItemModel>
 
 #include "listwindow.h"
 #include "trace.h"
-#include "listview.h"
+#include "platform.h"
 
-ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
+#ifdef Q_OS_SYMBIAN
+#include "flickcharm.h"
+#endif
+
+ListWindow::ListWindow(const QString &noItems_, QWidget *parent):
+        QMainWindow(parent), mModel(0), noItems(noItems_)
 {
-#ifdef Q_WS_MAEMO_5
+#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
-}
+    setAttribute(Qt::WA_DeleteOnClose);
 
-void ListWindow::addList(ListView *listView)
-{
-    Trace t("ListWindow::addList");
-    list = listView;
-#ifdef Q_WS_MAEMO_5
-    list->installEventFilter(this);
-    list->setMinimumHeight(list->contentsHeight());
-    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);
+    list = new QListWidget(this);
+    list->setSelectionMode(QAbstractItemView::SingleSelection);
+#if defined(Q_OS_SYMBIAN)
+    list->setFixedWidth(QApplication::desktop()->availableGeometry().width());
+    list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 #endif
-    connect(list->selectionModel(),
-      SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
-      this,
-      SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
+    populateList();
+    setCentralWidget(list);
+
+#ifdef Q_OS_SYMBIAN
+    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()));
+    QMainWindow::addAction(closeAction);
+#endif // Q_OS_SYMBIAN
+
+    connect(list, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
 }
 
-void ListWindow::addAction(const QString &title, QObject *receiver,
-                           const char *slot, QDialogButtonBox::ButtonRole role)
+void ListWindow::populateList()
 {
-    Trace t("ListWindow::addAction");
-#ifndef Q_WS_MAEMO_5
-    QPushButton *button = buttonBox->addButton(title, role);
-    connect(button, SIGNAL(clicked()), receiver, slot);
-#else
-    Q_UNUSED(role);
-    // QAction *action = menuBar()->addAction(title);
-    // connect(action, SIGNAL(triggered()), receiver, slot);
-    QPushButton *button = new QPushButton(title, this);
-    contentLayout->addWidget(button);
-    connect(button, SIGNAL(clicked()), receiver, slot);
-#endif // ! Q_WS_MAEMO_5
+    TRACE;
+
+    list->clear();
+    list->setIconSize(QSize(48, 48)); // FIXME
+    list->setUniformItemSizes(true);
+    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++) {
+        insertButton(i, buttons[i]);
+    }
 }
 
-void ListWindow::addItemAction(const QString &title, QObject *receiver,
-                               const char *slot)
+void ListWindow::insertButton(int row, const Button &b)
 {
-    Trace t("ListWindow::addItemAction");
-#ifndef Q_WS_MAEMO_5
-    QPushButton *button =
-            buttonBox->addButton(title, QDialogButtonBox::ActionRole);
-    connect(button, SIGNAL(clicked()), receiver, slot);
-    itemButtons.append(button);
-    activateItemButtons();
-#else
-    popup->addAction(title, receiver, slot);
-#endif // ! Q_WS_MAEMO_5
+    QPushButton *pushButton = new QPushButton(
+        QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
+    pushButton->setFixedWidth(list->width());
+    connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot);
+    QListWidgetItem *item = new QListWidgetItem();
+    item->setFlags(Qt::NoItemFlags);
+    list->insertItem(row, item);
+    list->setItemWidget(item, pushButton);
 }
 
-#ifdef Q_WS_MAEMO_5
-
-void ListWindow::closeEvent(QCloseEvent *event)
+void ListWindow::setModel(QAbstractItemModel *aModel)
 {
-    // Work around Maemo/Qt but: Menu items are not removed on close
-    menuBar()->clear();
-    event->accept();
+    TRACE;
+    mModel = aModel;
+    populateList();
+    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()));
+        connect(mModel, SIGNAL(layoutChanged()), this, SLOT(populateList()));
+    }
 }
 
-#endif // Q_WS_MAEMO_5
+QAbstractItemModel *ListWindow::model() const
+{
+    return mModel;
+}
 
-void ListWindow::onSelectionChanged(const QItemSelection &selected,
-                                    const QItemSelection &deselected)
+void ListWindow::addButton(const QString &title, QObject *receiver,
+                           const char *slot, const QString &iconName)
 {
-    Q_UNUSED(selected);
-    Q_UNUSED(deselected);
-#ifndef Q_WS_MAEMO_5
-    activateItemButtons();
+    TRACE;
+
+#ifdef Q_OS_SYMBIAN
+    Q_UNUSED(iconName);
+    addMenuAction(title, receiver, slot);
+#else
+    Button b;
+    b.title = title;
+    b.receiver = receiver;
+    b.slot = slot;
+    b.iconName = iconName;
+    insertButton(buttons.length(), b);
+    buttons.append(b);
 #endif
 }
 
-#ifndef 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);
+    action = new QAction(this);
+#endif
+    action->setCheckable(true);
+    return action;
+}
 
-void ListWindow::activateItemButtons()
+void ListWindow::onItemActivated(const QModelIndex &index)
 {
-    bool enable = false;
-    if (list) {
-        enable = list->selectionModel()->hasSelection();
+    TRACE;
+
+    // Work around Qt/Symbian^3 bug: Disabled list items still can be selected
+    if (!mModel) {
+        return;
     }
-    foreach (QPushButton *button, itemButtons) {
-        button->setEnabled(enable);
+    if (!mModel->rowCount()) {
+        return;
     }
-}
 
-#endif // ! Q_WS_MAEMO_5
+    int row = index.row() - buttons.count();
+    qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
+    emit activated(mModel->index(row, 0));
+}
 
-#ifdef Q_WS_MAEMO_5
+void ListWindow::setCurrentItem(const QModelIndex &item)
+{
+    int index = item.row();
+    list->setCurrentItem(list->item(index + buttons.count()));
+}
 
-bool ListWindow::eventFilter(QObject *obj, QEvent *event)
+QModelIndex ListWindow::currentItem() const
 {
-    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);
+    TRACE;
+    QListWidgetItem *currentItem = list->currentItem();
+    if (currentItem) {
+        int row = list->row(currentItem) - buttons.count();
+        qDebug() << "Current row is" << row;
+        return mModel->index(row, 0);
     }
+    return QModelIndex();
 }
 
-void ListWindow::onModelChanged()
+#ifdef Q_WS_MAEMO_5
+
+void ListWindow::closeEvent(QCloseEvent *event)
 {
-    qDebug() << "ListWindow::onModelChanged";
-    list->setMinimumHeight(list->contentsHeight());
+    // Work around Maemo/Qt bug: Menu items are not removed on close
+    menuBar()->clear();
+    event->accept();
+    QMainWindow::closeEvent(event);
 }
 
 #endif // Q_WS_MAEMO_5
+
+#ifdef Q_OS_SYMBIAN
+
+void ListWindow::show()
+{
+    foreach (QWidget *w, QApplication::allWidgets()) {
+        w->setContextMenuPolicy(Qt::NoContextMenu);
+    }
+    showMaximized();
+}
+
+#endif // Q_OS_SYMBIAN