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);
73 pushButton->setFixedWidth(list->width());
75 connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot);
76 QListWidgetItem *item = new QListWidgetItem();
77 item->setFlags(Qt::NoItemFlags);
78 list->insertItem(row, item);
79 list->setItemWidget(item, pushButton);
82 void ListWindow::setModel(QAbstractItemModel *aModel)
88 connect(mModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
89 this, SLOT(populateList()));
90 connect(mModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
91 this, SLOT(populateList()));
92 connect(mModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
93 this, SLOT(populateList()));
94 connect(mModel, SIGNAL(layoutChanged()), this, SLOT(populateList()));
98 QAbstractItemModel *ListWindow::model() const
103 void ListWindow::addButton(const QString &title, QObject *receiver,
104 const char *slot, const QString &iconName)
110 addMenuAction(title, receiver, slot);
114 b.receiver = receiver;
116 b.iconName = iconName;
117 insertButton(buttons.length(), b);
122 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
127 #if defined(Q_WS_MAEMO_5)
128 action = menuBar()->addAction(title);
129 connect(action, SIGNAL(triggered()), receiver, slot);
130 #elif defined(Q_OS_SYMBIAN)
131 action = new QAction(title, this);
132 connect(action, SIGNAL(triggered()), receiver, slot);
133 action->setSoftKeyRole(QAction::PositiveSoftKey);
134 menuBar()->addAction(action);
139 action = new QAction(this);
141 action->setCheckable(true);
145 void ListWindow::onItemActivated(const QModelIndex &index)
149 // Work around Qt/Symbian^3 bug: Disabled list items still can be selected
153 if (!mModel->rowCount()) {
157 int row = index.row() - buttons.count();
158 qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
159 emit activated(mModel->index(row, 0));
162 void ListWindow::setCurrentItem(const QModelIndex &item)
164 int index = item.row();
165 list->setCurrentItem(list->item(index + buttons.count()));
168 QModelIndex ListWindow::currentItem() const
171 QListWidgetItem *currentItem = list->currentItem();
173 int row = list->row(currentItem) - buttons.count();
174 qDebug() << "Current row is" << row;
175 return mModel->index(row, 0);
177 return QModelIndex();
182 void ListWindow::closeEvent(QCloseEvent *event)
184 // Work around Maemo/Qt bug: Menu items are not removed on close
187 QMainWindow::closeEvent(event);
190 #endif // Q_WS_MAEMO_5
194 void ListWindow::show()
196 foreach (QWidget *w, QApplication::allWidgets()) {
197 w->setContextMenuPolicy(Qt::NoContextMenu);
202 #endif // Q_OS_SYMBIAN