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);
23 #if defined(Q_OS_SYMBIAN)
24 list->setFixedWidth(QApplication::desktop()->availableGeometry().width());
25 list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
28 setCentralWidget(list);
31 charm = new FlickCharm(this);
32 // charm->activateOn(list);
33 QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
34 closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
35 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
36 QMainWindow::addAction(closeAction);
37 #endif // Q_OS_SYMBIAN
39 connect(list, SIGNAL(activated(const QModelIndex &)),
40 this, SLOT(onItemActivated(const QModelIndex &)));
43 void ListWindow::populateList()
48 list->setIconSize(QSize(48, 48)); // FIXME
49 list->setUniformItemSizes(true);
50 if (mModel && mModel->rowCount()) {
51 for (int i = 0; i < mModel->rowCount(); i++) {
52 QModelIndex index = mModel->index(i, 0);
53 QString text = mModel->data(index, Qt::DisplayRole).toString();
54 QVariant imageData = mModel->data(index, Qt::DecorationRole);
55 QIcon icon(QPixmap::fromImage(imageData.value<QImage>()));
56 (void)new QListWidgetItem(icon, text, list);
59 QListWidgetItem *item = new QListWidgetItem(noItems);
60 item->setFlags(Qt::NoItemFlags);
63 for (int i = 0; i < buttons.count(); i++) {
64 insertButton(i, buttons[i]);
68 void ListWindow::insertButton(int row, const Button &b)
70 QPushButton *pushButton = new QPushButton(
71 QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
72 pushButton->setFixedWidth(list->width());
73 connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot);
74 QListWidgetItem *item = new QListWidgetItem();
75 item->setFlags(Qt::NoItemFlags);
76 list->insertItem(row, item);
77 list->setItemWidget(item, pushButton);
80 void ListWindow::setModel(QAbstractItemModel *aModel)
86 connect(mModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
87 this, SLOT(populateList()));
88 connect(mModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
89 this, SLOT(populateList()));
90 connect(mModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
91 this, SLOT(populateList()));
92 connect(mModel, SIGNAL(layoutChanged()), this, SLOT(populateList()));
96 QAbstractItemModel *ListWindow::model() const
101 void ListWindow::addButton(const QString &title, QObject *receiver,
102 const char *slot, const QString &iconName)
108 addMenuAction(title, receiver, slot);
112 b.receiver = receiver;
114 b.iconName = iconName;
115 insertButton(buttons.length(), b);
120 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
125 #if defined(Q_WS_MAEMO_5)
126 action = menuBar()->addAction(title);
127 connect(action, SIGNAL(triggered()), receiver, slot);
128 #elif defined(Q_OS_SYMBIAN)
129 action = new QAction(title, this);
130 connect(action, SIGNAL(triggered()), receiver, slot);
131 action->setSoftKeyRole(QAction::PositiveSoftKey);
132 menuBar()->addAction(action);
137 action = new QAction(this);
139 action->setCheckable(true);
143 void ListWindow::onItemActivated(const QModelIndex &index)
147 // Work around Qt/Symbian^3 bug: Disabled list items still can be selected
151 if (!mModel->rowCount()) {
155 int row = index.row() - buttons.count();
156 qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
157 emit activated(mModel->index(row, 0));
160 void ListWindow::setCurrentItem(const QModelIndex &item)
162 int index = item.row();
163 list->setCurrentItem(list->item(index + buttons.count()));
166 QModelIndex ListWindow::currentItem() const
169 QListWidgetItem *currentItem = list->currentItem();
171 int row = list->row(currentItem) - buttons.count();
172 qDebug() << "Current row is" << row;
173 return mModel->index(row, 0);
175 return QModelIndex();
180 void ListWindow::closeEvent(QCloseEvent *event)
182 // Work around Maemo/Qt bug: Menu items are not removed on close
185 QMainWindow::closeEvent(event);
188 #endif // Q_WS_MAEMO_5
192 void ListWindow::show()
194 foreach (QWidget *w, QApplication::allWidgets()) {
195 w->setContextMenuPolicy(Qt::NoContextMenu);
200 #endif // Q_OS_SYMBIAN