Symbian fixes.
[dorian] / widgets / listwindow.cpp
1 #include <QtGui>
2
3 #include "listwindow.h"
4 #include "trace.h"
5 #include "listview.h"
6 #include "platform.h"
7
8 #ifdef Q_OS_SYMBIAN
9 #include "flickcharm.h"
10 #endif
11
12 ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
13 {
14 #if defined(Q_WS_MAEMO_5)
15     setAttribute(Qt::WA_Maemo5StackedWindow, true);
16     popup = new QMenu(this);
17
18     QScrollArea *scroller = new QScrollArea(this);
19     setCentralWidget(scroller);
20     scroller->setProperty("FingerScrollable", true);
21     // scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
22     // scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
23     scroller->setFrameStyle(QFrame::NoFrame);
24     scroller->show();
25
26     QWidget *content = new QWidget(scroller);
27     contentLayout = new QVBoxLayout(content);
28     contentLayout->setMargin(0);
29     content->setLayout(contentLayout);
30     content->show();
31
32     scroller->setWidget(content);
33     scroller->setWidgetResizable(true);
34 #else
35     QFrame *frame = new QFrame(this);
36     setCentralWidget(frame);
37     contentLayout = new QHBoxLayout();
38     frame->setLayout(contentLayout);
39     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
40     contentLayout->addWidget(buttonBox);
41 #endif // Q_WS_MAEMO_5
42
43 #ifdef Q_OS_SYMBIAN
44     charm = 0;
45     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
46     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
47     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
48     QMainWindow::addAction(closeAction);
49 #endif // Q_OS_SYMBIAN
50
51 #ifdef Q_WS_MAC
52     addAction(tr("Close"), this, SLOT(close()), QString(),
53               QDialogButtonBox::RejectRole);
54 #endif // Q_WS_MAC
55 }
56
57 void ListWindow::addList(ListView *listView)
58 {
59     Trace t("ListWindow::addList");
60     list = listView;
61 #if defined(Q_WS_MAEMO_5)
62     list->installEventFilter(this);
63     list->setMinimumHeight(list->contentsHeight());
64     contentLayout->addWidget(list);
65     connect(list->model(),
66             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
67             this, SLOT(onModelChanged()));
68     connect(list->model(),
69             SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
70             this, SLOT(onModelChanged()));
71 #else
72     contentLayout->insertWidget(0, list);
73 #endif // Q_WS_MAEMO5
74
75 #ifdef Q_OS_SYMBIAN
76     if (!charm) {
77         charm = new FlickCharm(this);
78     }
79     charm->activateOn(list);
80 #endif // Q_OS_SYMBIAN
81
82     connect(list->selectionModel(),
83       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
84       this,
85       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
86 }
87
88 void ListWindow::addAction(const QString &title, QObject *receiver,
89                            const char *slot, const QString &iconName,
90                            QDialogButtonBox::ButtonRole role)
91 {
92     Trace t("ListWindow::addAction");
93 #ifdef Q_WS_MAEMO_5
94     Q_UNUSED(role);
95     QPushButton *button =
96             new QPushButton(QIcon(Platform::icon(iconName)), title, this);
97     contentLayout->addWidget(button);
98     connect(button, SIGNAL(clicked()), receiver, slot);
99 #elif defined(Q_OS_SYMBIAN)
100     Q_UNUSED(role);
101     Q_UNUSED(iconName);
102     QAction *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(iconName);
108     QPushButton *button = buttonBox->addButton(title, role);
109     connect(button, SIGNAL(clicked()), receiver, slot);
110 #endif // Q_WS_MAEMO_5
111 }
112
113 void ListWindow::addItemAction(const QString &title, QObject *receiver,
114                                const char *slot)
115 {
116     Trace t("ListWindow::addItemAction");
117 #ifdef Q_WS_MAEMO_5
118     popup->addAction(title, receiver, slot);
119 #elif defined Q_OS_SYMBIAN
120     QAction *action = new QAction(title, this);
121     connect(action, SIGNAL(triggered()), receiver, slot);
122     action->setSoftKeyRole(QAction::PositiveSoftKey);
123     menuBar()->addAction(action);
124     // FIXME: Add action to the list of item specific actions
125 #else
126     QPushButton *button =
127             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
128     connect(button, SIGNAL(clicked()), receiver, slot);
129     itemButtons.append(button);
130     activateItemButtons();
131 #endif // Q_WS_MAEMO_5
132 }
133
134 #ifdef Q_WS_MAEMO_5
135
136 void ListWindow::closeEvent(QCloseEvent *event)
137 {
138     // Work around Maemo/Qt but: Menu items are not removed on close
139     menuBar()->clear();
140     event->accept();
141 }
142
143 #endif // Q_WS_MAEMO_5
144
145 void ListWindow::onSelectionChanged(const QItemSelection &selected,
146                                     const QItemSelection &deselected)
147 {
148     Q_UNUSED(selected);
149     Q_UNUSED(deselected);
150 #ifndef Q_WS_MAEMO_5
151     activateItemButtons();
152 #endif
153 }
154
155 #ifndef Q_WS_MAEMO_5
156
157 void ListWindow::activateItemButtons()
158 {
159     bool enable = false;
160     if (list) {
161         enable = list->selectionModel()->hasSelection();
162     }
163     foreach (QPushButton *button, itemButtons) {
164         button->setEnabled(enable);
165     }
166 }
167
168 #endif // ! Q_WS_MAEMO_5
169
170 #ifdef Q_WS_MAEMO_5
171
172 bool ListWindow::eventFilter(QObject *obj, QEvent *event)
173 {
174     if (event->type() == QEvent::ContextMenu) {
175         qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu";
176         if (popup->actions().size()) {
177             QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
178             QPoint pos = mouseEvent->globalPos();
179             pos.setX(pos.x() - 150);
180             if (pos.x() < 0) {
181                 pos.setX(0);
182             }
183             popup->exec(pos);
184         }
185         return true;
186     } else {
187         return QObject::eventFilter(obj, event);
188     }
189 }
190
191 void ListWindow::onModelChanged()
192 {
193     qDebug() << "ListWindow::onModelChanged";
194     list->setMinimumHeight(list->contentsHeight());
195 }
196
197 #endif // Q_WS_MAEMO_5
198
199 #ifdef Q_OS_SYMBIAN
200
201 void ListWindow::show()
202 {
203     foreach (QWidget *w, QApplication::allWidgets()) {
204         w->setContextMenuPolicy(Qt::NoContextMenu);
205     }
206     showMaximized();
207 }
208
209 #endif // Q_OS_SYMBIAN