Updated library dialog works.
[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         QMainWindow(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     populateList();
24     setCentralWidget(list);
25
26 #ifdef Q_OS_SYMBIAN
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
34
35     connect(list, SIGNAL(activated(const QModelIndex &)),
36             this, SLOT(onItemActivated(const QModelIndex &)));
37 }
38
39 void ListWindow::populateList()
40 {
41     TRACE;
42
43     list->clear();
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);
53         }
54     } else {
55         QListWidgetItem *item = new QListWidgetItem(noItems);
56         item->setFlags(Qt::NoItemFlags);
57         list->addItem(item);
58     }
59     for (int i = 0; i < buttons.count(); i++) {
60         insertButton(i, buttons[i]);
61     }
62 }
63
64 void ListWindow::insertButton(int row, const Button &b)
65 {
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);
73 }
74
75 void ListWindow::setModel(QAbstractItemModel *aModel)
76 {
77     TRACE;
78     mModel = aModel;
79     populateList();
80     if (mModel) {
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     }
88 }
89
90 QAbstractItemModel *ListWindow::model() const
91 {
92     return mModel;
93 }
94
95 void ListWindow::addButton(const QString &title, QObject *receiver,
96                            const char *slot, const QString &iconName)
97 {
98     TRACE;
99
100     Button b;
101     b.title = title;
102     b.receiver = receiver;
103     b.slot = slot;
104     b.iconName = iconName;
105
106     insertButton(buttons.length(), b);
107     buttons.append(b);
108 }
109
110 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
111                                    const char *slot)
112 {
113     TRACE;
114     QAction *action = 0;
115 #if defined(Q_WS_MAEMO_5)
116     action = menuBar()->addAction(title);
117     connect(action, SIGNAL(triggered()), receiver, slot);
118 #elif defined(Q_OS_SYMBIAN)
119     action = new QAction(title, this);
120     connect(action, SIGNAL(triggered()), receiver, slot);
121     action->setSoftKeyRole(QAction::PositiveSoftKey);
122     menuBar()->addAction(action);
123 #else
124     Q_UNUSED(title);
125     Q_UNUSED(receiver);
126     Q_UNUSED(slot);
127     action = new QAction(this);
128 #endif
129     action->setCheckable(true);
130     return action;
131 }
132
133 void ListWindow::onItemActivated(const QModelIndex &index)
134 {
135     TRACE;
136     int row = index.row() - buttons.count();
137     qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")";
138     emit activated(mModel->index(row, 0));
139 }
140
141 void ListWindow::setCurrentItem(const QModelIndex &item)
142 {
143     int index = item.row();
144     list->setCurrentItem(list->item(index + buttons.count()));
145 }
146
147 #ifdef Q_WS_MAEMO_5
148
149 void ListWindow::closeEvent(QCloseEvent *event)
150 {
151     // Work around Maemo/Qt bug: Menu items are not removed on close
152     menuBar()->clear();
153     // FIXME: Is this needed? event->accept();
154     QMainWindow::closeEvent(event);
155 }
156
157 #endif // Q_WS_MAEMO_5
158
159 #ifdef Q_OS_SYMBIAN
160
161 void ListWindow::show()
162 {
163     foreach (QWidget *w, QApplication::allWidgets()) {
164         w->setContextMenuPolicy(Qt::NoContextMenu);
165     }
166     showMaximized();
167 }
168
169 #endif // Q_OS_SYMBIAN