#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):
+ MainBase(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);
-# ifdef Q_OS_SYMBIAN
+#endif
+ setAttribute(Qt::WA_DeleteOnClose);
+
+ list = new QListWidget(this);
+ list->setSelectionMode(QAbstractItemView::SingleSelection);
+#if defined(Q_OS_SYMBIAN)
+ list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+#endif
+ 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);
-# else
- buttonBox = new QDialogButtonBox(Qt::Vertical, this);
- contentLayout->addWidget(buttonBox);
-# endif // Q_OS_SYMBIAN
-#endif // Q_WS_MAEMO_5
+ MainBase::addAction(closeAction);
+#endif // Q_OS_SYMBIAN
-#ifdef Q_WS_MAC
- addAction(tr("Close"), this, SLOT(close()), QString(),
- QDialogButtonBox::RejectRole);
-#endif // Q_WS_MAC
+ connect(list, SIGNAL(activated(const QModelIndex &)),
+ this, SLOT(onItemActivated(const QModelIndex &)));
}
-void ListWindow::addList(ListView *listView)
+void ListWindow::populateList()
{
- 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);
-#endif
- connect(list->selectionModel(),
- SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
- this,
- SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
+ 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::addAction(const QString &title, QObject *receiver,
- const char *slot, const QString &iconPath,
- QDialogButtonBox::ButtonRole role)
+void ListWindow::insertButton(int row, const Button &b)
{
- Trace t("ListWindow::addAction");
-#ifdef Q_WS_MAEMO_5
- Q_UNUSED(role);
- QPushButton *button = new QPushButton(QIcon(iconPath), title, this);
- contentLayout->addWidget(button);
- connect(button, SIGNAL(clicked()), receiver, slot);
-#elif defined(Q_OS_SYMBIAN)
- Q_UNUSED(role);
- QAction *action = new QAction(title, this);
- connect(action, SIGNAL(triggered()), receiver, slot);
- action->setSoftKeyRole(QAction::PositiveSoftKey);
- menuBar()->addAction(action);
-#else
- Q_UNUSED(iconPath);
- QPushButton *button = buttonBox->addButton(title, role);
- connect(button, SIGNAL(clicked()), receiver, slot);
-#endif // Q_WS_MAEMO_5
+ QPushButton *pushButton = new QPushButton(
+ QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
+#ifdef Q_OS_SYMBIAN
+ pushButton->setFixedWidth(list->width());
+#endif
+ 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::addItemAction(const QString &title, QObject *receiver,
- const char *slot)
+void ListWindow::setModel(QAbstractItemModel *aModel)
{
- Trace t("ListWindow::addItemAction");
-#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
+ 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()));
+ }
}
-#ifdef Q_WS_MAEMO_5
-
-void ListWindow::closeEvent(QCloseEvent *event)
+QAbstractItemModel *ListWindow::model() const
{
- // Work around Maemo/Qt but: Menu items are not removed on close
- menuBar()->clear();
- event->accept();
+ return mModel;
}
-#endif // Q_WS_MAEMO_5
+void ListWindow::addButton(const QString &title, QObject *receiver,
+ const char *slot, const QString &iconName)
+{
+ TRACE;
+
+#if defined(Q_WS_MAEMO_5)
+ Button b;
+ b.title = title;
+ b.receiver = receiver;
+ b.slot = slot;
+ b.iconName = iconName;
+ insertButton(buttons.length(), b);
+ buttons.append(b);
+#else
+ (void)addToolBarAction(receiver, slot, iconName, title, true);
+ (void)addMenuAction(title, receiver, slot);
+#endif
+}
-void ListWindow::onSelectionChanged(const QItemSelection &selected,
- const QItemSelection &deselected)
+void ListWindow::addItemButton(const QString &title, QObject *receiver,
+ const char *slot, const QString &iconName)
{
- Q_UNUSED(selected);
- Q_UNUSED(deselected);
-#ifndef Q_WS_MAEMO_5
- activateItemButtons();
+ TRACE;
+#if defined(Q_WS_MAEMO_5)
+ Q_UNUSED(title);
+ Q_UNUSED(receiver);
+ Q_UNUSED(slot);
+ Q_UNUSED(iconPath);
+#else
+ QAction *toolBarAction =
+ addToolBarAction(receiver, slot, iconName, title, true);
+ // QAction *menuAction = addMenuAction(title, receiver, slot);
+ // toolBarAction->setEnabled(false);
+ // menuAction->setEnabled(false);
+ itemActions.append(toolBarAction);
+ // itemActions.append(menuAction);
#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
+ 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
-#ifdef Q_WS_MAEMO_5
+ int row = index.row() - buttons.count();
+ qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
+ emit activated(mModel->index(row, 0));
+}
-bool ListWindow::eventFilter(QObject *obj, QEvent *event)
+void ListWindow::setCurrentItem(const QModelIndex &item)
{
- 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);
- }
+ int index = item.row();
+ list->setCurrentItem(list->item(index + buttons.count()));
}
-void ListWindow::onModelChanged()
+QModelIndex ListWindow::currentItem() const
{
- qDebug() << "ListWindow::onModelChanged";
- list->setMinimumHeight(list->contentsHeight());
+ 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();
}
-#endif // Q_WS_MAEMO_5
-
-#ifdef Q_OS_SYMBIAN
+#ifdef Q_WS_MAEMO_5
-void ListWindow::show()
+void ListWindow::closeEvent(QCloseEvent *event)
{
- foreach (QWidget *w, QApplication::allWidgets()) {
- w->setContextMenuPolicy(Qt::NoContextMenu);
- }
- showMaximized();
+ // Work around Maemo/Qt bug: Menu items are not removed on close
+ menuBar()->clear();
+ event->accept();
+ QMainWindow::closeEvent(event);
}
-#endif // Q_OS_SYMBIAN
+#endif // Q_WS_MAEMO_5