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()));
87 connect(mModel, SIGNAL(layoutChanged()), this, SLOT(populateList()));
91 QAbstractItemModel *ListWindow::model() const
96 void ListWindow::addButton(const QString &title, QObject *receiver,
97 const char *slot, const QString &iconName)
103 b.receiver = receiver;
105 b.iconName = iconName;
107 insertButton(buttons.length(), b);
111 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
116 #if defined(Q_WS_MAEMO_5)
117 action = menuBar()->addAction(title);
118 connect(action, SIGNAL(triggered()), receiver, slot);
119 #elif defined(Q_OS_SYMBIAN)
120 action = new QAction(title, this);
121 connect(action, SIGNAL(triggered()), receiver, slot);
122 action->setSoftKeyRole(QAction::PositiveSoftKey);
123 menuBar()->addAction(action);
128 action = new QAction(this);
130 action->setCheckable(true);
134 void ListWindow::onItemActivated(const QModelIndex &index)
137 int row = index.row() - buttons.count();
138 qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
139 emit activated(mModel->index(row, 0));
142 void ListWindow::setCurrentItem(const QModelIndex &item)
144 int index = item.row();
145 list->setCurrentItem(list->item(index + buttons.count()));
148 QModelIndex ListWindow::currentItem() const
151 QListWidgetItem *currentItem = list->currentItem();
153 int row = list->row(currentItem) - buttons.count();
154 qDebug() << "Current row is" << row;
155 return mModel->index(row, 0);
157 return QModelIndex();
162 void ListWindow::closeEvent(QCloseEvent *event)
164 // Work around Maemo/Qt bug: Menu items are not removed on close
167 QMainWindow::closeEvent(event);
170 #endif // Q_WS_MAEMO_5
174 void ListWindow::show()
176 foreach (QWidget *w, QApplication::allWidgets()) {
177 w->setContextMenuPolicy(Qt::NoContextMenu);
182 #endif // Q_OS_SYMBIAN