Re-design ListWindow.
[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(QWidget *parent): QMainWindow(parent), model(0)
14 {
15 #if defined(Q_WS_MAEMO_5)
16     setAttribute(Qt::WA_Maemo5StackedWindow, true);
17 #endif
18
19     list = new QListWidget(this);
20     populateList();
21     setCentralWidget(list);
22
23 #ifdef Q_OS_SYMBIAN
24     charm = new FlickCharm(this);
25     charm->activateOn(list);
26     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
27     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
28     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
29     QMainWindow::addAction(closeAction);
30 #endif // Q_OS_SYMBIAN
31
32 #ifdef Q_WS_MAC
33     // FIXME
34     // addAction(tr("Close"), this, SLOT(close()), QString(),
35     //           QDialogButtonBox::RejectRole);
36 #endif // Q_WS_MAC
37
38     connect(list, SIGNAL(activated(const QModelIndex &)),
39             this, SLOT(onItemActicvated(const QModelIndex &)));
40 }
41
42 void ListWindow::populateList()
43 {
44     TRACE;
45
46     list->clear();
47     if (model) {
48         for (int i = 0; i < model->rowCount(); i++) {
49             QModelIndex index = model->index(i, 0);
50             QString text = model->data(index, Qt::DisplayRole).toString();
51             QIcon icon;
52             QVariant iconData = model->data(index, Qt::DecorationRole);
53             if (iconData.canConvert<QIcon>()) {
54                 icon = iconData.value<QIcon>();
55             }
56             (void)new QListWidgetItem(icon, text, list);
57         }
58     }
59     for (int i = 0; i < buttons.count(); i++) {
60         QListWidgetItem *item = new QListWidgetItem();
61         list->insertItem(i, item);
62         list->setItemWidget(item, buttons[i]);
63     }
64 }
65
66 void ListWindow::setModel(QAbstractItemModel *model_)
67 {
68     model = model_;
69     populateList();
70     if (model) {
71         connect(model,
72                 SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
73                 this, SLOT(populateList()));
74     }
75 }
76
77 void ListWindow::addButton(const QString &title, QObject *receiver,
78                            const char *slot, const QString &iconName)
79 {
80     TRACE;
81
82     QPushButton *button = new QPushButton(QIcon(Platform::instance()->
83                                                 icon(iconName)), title, this);
84     connect(button, SIGNAL(clicked()), receiver, slot);
85     buttons.append(button);
86
87     int pos = buttons.length() - 1;
88     QListWidgetItem *item = new QListWidgetItem();
89     list->insertItem(pos, item);
90     list->setItemWidget(item, button);
91 }
92
93 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
94                                    const char *slot)
95 {
96     TRACE;
97     QAction *action = 0;
98 #if defined(Q_WS_MAEMO_5)
99     action = menuBar()->addAction(title);
100     connect(action, SIGNAL(triggered()), receiver, slot);
101 #elif defined(Q_OS_SYMBIAN)
102     action = new QAction(title, this);
103     connect(action, SIGNAL(triggered()), receiver, slot);
104     action->setSoftKeyRole(QAction::PositiveSoftKey);
105     menuBar()->addAction(action);
106 #else
107     Q_UNUSED(title);
108     Q_UNUSED(receiver);
109     Q_UNUSED(slot);
110     action = new QAction(this);
111 #endif
112     action->setCheckable(true);
113     return action;
114 }
115
116 void ListWindow::onItemActivated(const QModelIndex &)
117 {
118     TRACE;
119     // FIXME
120 }
121
122 #ifdef Q_WS_MAEMO_5
123
124 void ListWindow::closeEvent(QCloseEvent *event)
125 {
126     // Work around Maemo/Qt bug: Menu items are not removed on close
127     menuBar()->clear();
128     // FIXME: Is this needed? event->accept();
129     QMainWindow::closeEvent(event);
130 }
131
132 #endif // Q_WS_MAEMO_5
133
134 #ifdef Q_OS_SYMBIAN
135
136 void ListWindow::show()
137 {
138     foreach (QWidget *w, QApplication::allWidgets()) {
139         w->setContextMenuPolicy(Qt::NoContextMenu);
140     }
141     showMaximized();
142 }
143
144 #endif // Q_OS_SYMBIAN