Add tool bars to list windows on Symbian.
[dorian] / widgets / listwindow.cpp
1 #include <QtGui>
2 #include <QListWidget>
3 #include <QAbstractItemModel>
4
5 #include "listwindow.h"
6 #include "trace.h"
7 #include "platform.h"
8
9 #ifdef Q_OS_SYMBIAN
10 #include "flickcharm.h"
11 #endif
12
13 ListWindow::ListWindow(const QString &noItems_, QWidget *parent):
14         MainBase(parent), mModel(0), noItems(noItems_)
15 {
16 #if defined(Q_WS_MAEMO_5)
17     setAttribute(Qt::WA_Maemo5StackedWindow, true);
18 #endif
19     setAttribute(Qt::WA_DeleteOnClose);
20
21     list = new QListWidget(this);
22     list->setSelectionMode(QAbstractItemView::SingleSelection);
23 #if defined(Q_OS_SYMBIAN)
24     list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
25 #endif
26     populateList();
27     setCentralWidget(list);
28
29 #ifdef Q_OS_SYMBIAN
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
37
38     connect(list, SIGNAL(activated(const QModelIndex &)),
39             this, SLOT(onItemActivated(const QModelIndex &)));
40 }
41
42 void ListWindow::populateList()
43 {
44     TRACE;
45
46     list->clear();
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);
56         }
57     } else {
58         QListWidgetItem *item = new QListWidgetItem(noItems);
59         item->setFlags(Qt::NoItemFlags);
60         list->addItem(item);
61     }
62     for (int i = 0; i < buttons.count(); i++) {
63         insertButton(i, buttons[i]);
64     }
65 }
66
67 void ListWindow::insertButton(int row, const Button &b)
68 {
69     QPushButton *pushButton = new QPushButton(
70         QIcon(Platform::instance()->icon(b.iconName)), b.title, this);
71 #ifdef Q_OS_SYMBIAN
72     pushButton->setFixedWidth(list->width());
73 #endif
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);
79 }
80
81 void ListWindow::setModel(QAbstractItemModel *aModel)
82 {
83     TRACE;
84     mModel = aModel;
85     populateList();
86     if (mModel) {
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()));
94     }
95 }
96
97 QAbstractItemModel *ListWindow::model() const
98 {
99     return mModel;
100 }
101
102 void ListWindow::addButton(const QString &title, QObject *receiver,
103                            const char *slot, const QString &iconName)
104 {
105     TRACE;
106
107 #if defined(Q_WS_MAEMO_5)
108     Button b;
109     b.title = title;
110     b.receiver = receiver;
111     b.slot = slot;
112     b.iconName = iconName;
113     insertButton(buttons.length(), b);
114     buttons.append(b);
115 #else
116     (void)addToolBarAction(receiver, slot, iconName, title, true);
117     (void)addMenuAction(title, receiver, slot);
118 #endif
119 }
120
121 void ListWindow::addItemButton(const QString &title, QObject *receiver,
122                                const char *slot, const QString &iconName)
123 {
124     TRACE;
125 #if defined(Q_WS_MAEMO_5)
126     Q_UNUSED(title);
127     Q_UNUSED(receiver);
128     Q_UNUSED(slot);
129     Q_UNUSED(iconPath);
130 #else
131     QAction *toolBarAction =
132             addToolBarAction(receiver, slot, iconName, title, true);
133     // QAction *menuAction = addMenuAction(title, receiver, slot);
134     // toolBarAction->setEnabled(false);
135     // menuAction->setEnabled(false);
136     itemActions.append(toolBarAction);
137     // itemActions.append(menuAction);
138 #endif
139 }
140
141 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
142                                    const char *slot)
143 {
144     TRACE;
145     QAction *action = 0;
146 #if defined(Q_WS_MAEMO_5)
147     action = menuBar()->addAction(title);
148     connect(action, SIGNAL(triggered()), receiver, slot);
149 #elif defined(Q_OS_SYMBIAN)
150     action = new QAction(title, this);
151     connect(action, SIGNAL(triggered()), receiver, slot);
152     action->setSoftKeyRole(QAction::PositiveSoftKey);
153     menuBar()->addAction(action);
154 #else
155     Q_UNUSED(title);
156     Q_UNUSED(receiver);
157     Q_UNUSED(slot);
158     action = new QAction(this);
159 #endif
160     return action;
161 }
162
163 void ListWindow::onItemActivated(const QModelIndex &index)
164 {
165     TRACE;
166
167     // Work around Qt/Symbian^3 bug: Disabled list items still can be selected
168     if (!mModel) {
169         return;
170     }
171     if (!mModel->rowCount()) {
172         return;
173     }
174
175     int row = index.row() - buttons.count();
176     qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
177     emit activated(mModel->index(row, 0));
178 }
179
180 void ListWindow::setCurrentItem(const QModelIndex &item)
181 {
182     int index = item.row();
183     list->setCurrentItem(list->item(index + buttons.count()));
184 }
185
186 QModelIndex ListWindow::currentItem() const
187 {
188     TRACE;
189     QListWidgetItem *currentItem = list->currentItem();
190     if (currentItem) {
191         int row = list->row(currentItem) - buttons.count();
192         qDebug() << "Current row is" << row;
193         return mModel->index(row, 0);
194     }
195     return QModelIndex();
196 }
197
198 #ifdef Q_WS_MAEMO_5
199
200 void ListWindow::closeEvent(QCloseEvent *event)
201 {
202     // Work around Maemo/Qt bug: Menu items are not removed on close
203     menuBar()->clear();
204     event->accept();
205     QMainWindow::closeEvent(event);
206 }
207
208 #endif // Q_WS_MAEMO_5