3 #include <QAbstractItemModel>
5 #include "listwindow.h"
10 #include "flickcharm.h"
13 ListWindow::ListWindow(const QString &noItems_, QWidget *parent):
14 QMainWindow(parent), mModel(0), noItems(noItems_)
16 #if defined(Q_WS_MAEMO_5)
17 setAttribute(Qt::WA_Maemo5StackedWindow, true);
19 setAttribute(Qt::WA_DeleteOnClose);
21 list = new QListWidget(this);
22 list->setSelectionMode(QAbstractItemView::SingleSelection);
24 setCentralWidget(list);
27 charm = new FlickCharm(this);
28 charm->activateOn(list);
29 QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
30 closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
31 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
32 QMainWindow::addAction(closeAction);
33 #endif // Q_OS_SYMBIAN
35 connect(list, SIGNAL(activated(const QModelIndex &)),
36 this, SLOT(onItemActivated(const QModelIndex &)));
39 void ListWindow::populateList()
44 list->setIconSize(QSize(48, 48)); // FIXME
45 list->setUniformItemSizes(true);
46 if (mModel && mModel->rowCount()) {
47 for (int i = 0; i < mModel->rowCount(); i++) {
48 QModelIndex index = mModel->index(i, 0);
49 QString text = mModel->data(index, Qt::DisplayRole).toString();
50 QVariant imageData = mModel->data(index, Qt::DecorationRole);
51 QIcon icon(QPixmap::fromImage(imageData.value<QImage>()));
52 (void)new QListWidgetItem(icon, text, list);
55 QListWidgetItem *item = new QListWidgetItem(noItems);
56 item->setFlags(Qt::NoItemFlags);
59 for (int i = 0; i < buttons.count(); i++) {
60 insertButton(i, buttons[i]);
64 void ListWindow::insertButton(int row, const Button &b)
66 QPushButton *pushButton = new QPushButton(
67 QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
68 connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot);
69 QListWidgetItem *item = new QListWidgetItem();
70 item->setFlags(Qt::NoItemFlags);
71 list->insertItem(row, item);
72 list->setItemWidget(item, pushButton);
75 void ListWindow::setModel(QAbstractItemModel *aModel)
81 connect(mModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
82 this, SLOT(populateList()));
83 connect(mModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
84 this, SLOT(populateList()));
85 connect(mModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
86 this, SLOT(populateList()));
90 QAbstractItemModel *ListWindow::model() const
95 void ListWindow::addButton(const QString &title, QObject *receiver,
96 const char *slot, const QString &iconName)
102 b.receiver = receiver;
104 b.iconName = iconName;
106 insertButton(buttons.length(), b);
110 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
115 #if defined(Q_WS_MAEMO_5)
116 action = menuBar()->addAction(title);
117 connect(action, SIGNAL(triggered()), receiver, slot);
118 #elif defined(Q_OS_SYMBIAN)
119 action = new QAction(title, this);
120 connect(action, SIGNAL(triggered()), receiver, slot);
121 action->setSoftKeyRole(QAction::PositiveSoftKey);
122 menuBar()->addAction(action);
127 action = new QAction(this);
129 action->setCheckable(true);
133 void ListWindow::onItemActivated(const QModelIndex &index)
136 int row = index.row() - buttons.count();
137 qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
138 emit activated(mModel->index(row, 0));
141 void ListWindow::setCurrentItem(const QModelIndex &item)
143 int index = item.row();
144 list->setCurrentItem(list->item(index + buttons.count()));
149 void ListWindow::closeEvent(QCloseEvent *event)
151 // Work around Maemo/Qt bug: Menu items are not removed on close
153 // FIXME: Is this needed? event->accept();
154 QMainWindow::closeEvent(event);
157 #endif // Q_WS_MAEMO_5
161 void ListWindow::show()
163 foreach (QWidget *w, QApplication::allWidgets()) {
164 w->setContextMenuPolicy(Qt::NoContextMenu);
169 #endif // Q_OS_SYMBIAN