3 #include <QAbstractItemModel>
5 #include "listwindow.h"
10 #include "flickcharm.h"
13 ListWindow::ListWindow(const QString &noItems_, QWidget *parent):
14 MainBase(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->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
27 setCentralWidget(list);
30 charm = new FlickCharm(this);
31 // charm->activateOn(list);
32 QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
33 closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
34 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
35 MainBase::addAction(closeAction);
36 #endif // Q_OS_SYMBIAN
38 connect(list, SIGNAL(activated(const QModelIndex &)),
39 this, SLOT(onItemActivated(const QModelIndex &)));
42 void ListWindow::populateList()
47 list->setIconSize(QSize(48, 48)); // FIXME
48 list->setUniformItemSizes(true);
49 if (mModel && mModel->rowCount()) {
50 for (int i = 0; i < mModel->rowCount(); i++) {
51 QModelIndex index = mModel->index(i, 0);
52 QString text = mModel->data(index, Qt::DisplayRole).toString();
53 QVariant imageData = mModel->data(index, Qt::DecorationRole);
54 QIcon icon(QPixmap::fromImage(imageData.value<QImage>()));
55 (void)new QListWidgetItem(icon, text, list);
58 QListWidgetItem *item = new QListWidgetItem(noItems);
59 item->setFlags(Qt::NoItemFlags);
62 for (int i = 0; i < buttons.count(); i++) {
63 insertButton(i, buttons[i]);
67 void ListWindow::insertButton(int row, const Button &b)
69 QPushButton *pushButton = new QPushButton(
70 QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
72 pushButton->setFixedWidth(list->width());
74 connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot);
75 QListWidgetItem *item = new QListWidgetItem();
76 item->setFlags(Qt::NoItemFlags);
77 list->insertItem(row, item);
78 list->setItemWidget(item, pushButton);
81 void ListWindow::setModel(QAbstractItemModel *aModel)
87 connect(mModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
88 this, SLOT(populateList()));
89 connect(mModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
90 this, SLOT(populateList()));
91 connect(mModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
92 this, SLOT(populateList()));
93 connect(mModel, SIGNAL(layoutChanged()), this, SLOT(populateList()));
97 QAbstractItemModel *ListWindow::model() const
102 void ListWindow::addButton(const QString &title, QObject *receiver,
103 const char *slot, const QString &iconName)
107 #if defined(Q_WS_MAEMO_5)
110 b.receiver = receiver;
112 b.iconName = iconName;
113 insertButton(buttons.length(), b);
116 (void)addToolBarAction(receiver, slot, iconName, title, true);
120 void ListWindow::addItemButton(const QString &title, QObject *receiver,
121 const char *slot, const QString &iconName)
124 #if defined(Q_WS_MAEMO_5)
130 QAction *toolBarAction =
131 addToolBarAction(receiver, slot, iconName, title, true);
132 toolBarAction->setEnabled(false);
133 itemActions.append(toolBarAction);
137 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
142 #if defined(Q_WS_MAEMO_5)
143 action = menuBar()->addAction(title);
144 connect(action, SIGNAL(triggered()), receiver, slot);
145 #elif defined(Q_OS_SYMBIAN)
146 action = new QAction(title, this);
147 connect(action, SIGNAL(triggered()), receiver, slot);
148 action->setSoftKeyRole(QAction::PositiveSoftKey);
149 menuBar()->addAction(action);
154 action = new QAction(this);
159 void ListWindow::onItemActivated(const QModelIndex &index)
163 // Work around Qt/Symbian^3 bug: Disabled list items still can be selected
167 if (!mModel->rowCount()) {
171 int row = index.row() - buttons.count();
172 qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
173 emit activated(mModel->index(row, 0));
176 void ListWindow::setCurrentItem(const QModelIndex &item)
178 int index = item.row();
179 list->setCurrentItem(list->item(index + buttons.count()));
182 QModelIndex ListWindow::currentItem() const
185 QListWidgetItem *currentItem = list->currentItem();
187 int row = list->row(currentItem) - buttons.count();
188 qDebug() << "Current row is" << row;
189 return mModel->index(row, 0);
191 return QModelIndex();
196 void ListWindow::closeEvent(QCloseEvent *event)
198 // Work around Maemo/Qt bug: Menu items are not removed on close
201 QMainWindow::closeEvent(event);
204 #endif // Q_WS_MAEMO_5