1ebd5c8d95363f1d8714b80720c8efe41d54af0a
[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;
60     list = listView;
61 #if defined(Q_WS_MAEMO_5)
62     // FIXME: list->installEventFilter(this);
63     list->setProperty("FingerScrollable", false);
64     int height = list->sizeHintForRow(0) * list->model()->rowCount();
65     qDebug() << "Minimum height" << height;
66     list->setMinimumHeight(height);
67     contentLayout->addWidget(list);
68     connect(list->model(),
69             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
70             this, SLOT(onModelChanged()));
71     connect(list->model(),
72             SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
73             this, SLOT(onModelChanged()));
74 #else
75     contentLayout->insertWidget(0, list);
76 #endif // Q_WS_MAEMO5
77
78 #ifdef Q_OS_SYMBIAN
79     if (!charm) {
80         charm = new FlickCharm(this);
81     }
82     // FIXME: Charms need more work...: charm->activateOn(list);
83 #endif // Q_OS_SYMBIAN
84
85     connect(list->selectionModel(),
86       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
87       this,
88       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
89 }
90
91 void ListWindow::addAction(const QString &title, QObject *receiver,
92                            const char *slot, const QString &iconName,
93                            QDialogButtonBox::ButtonRole role)
94 {
95     TRACE;
96 #ifdef Q_WS_MAEMO_5
97     Q_UNUSED(role);
98     QPushButton *button = new QPushButton(QIcon(Platform::instance()->
99                                                 icon(iconName)), title, this);
100     contentLayout->addWidget(button);
101     connect(button, SIGNAL(clicked()), receiver, slot);
102 #elif defined(Q_OS_SYMBIAN)
103     Q_UNUSED(role);
104     Q_UNUSED(iconName);
105     QAction *action = new QAction(title, this);
106     connect(action, SIGNAL(triggered()), receiver, slot);
107     action->setSoftKeyRole(QAction::PositiveSoftKey);
108     menuBar()->addAction(action);
109 #else
110     Q_UNUSED(iconName);
111     QPushButton *button = buttonBox->addButton(title, role);
112     connect(button, SIGNAL(clicked()), receiver, slot);
113 #endif // Q_WS_MAEMO_5
114 }
115
116 void ListWindow::addItemAction(const QString &title, QObject *receiver,
117                                const char *slot)
118 {
119     TRACE;
120 #ifdef Q_WS_MAEMO_5
121     popup->addAction(title, receiver, slot);
122 #elif defined Q_OS_SYMBIAN
123     QAction *action = new QAction(title, this);
124     connect(action, SIGNAL(triggered()), receiver, slot);
125     action->setSoftKeyRole(QAction::PositiveSoftKey);
126     menuBar()->addAction(action);
127     // FIXME: Add action to the list of item specific actions
128 #else
129     QPushButton *button =
130             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
131     connect(button, SIGNAL(clicked()), receiver, slot);
132     itemButtons.append(button);
133     activateItemButtons();
134 #endif // Q_WS_MAEMO_5
135 }
136
137 QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
138                                    const char *slot)
139 {
140     TRACE;
141     QAction *action = 0;
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);
150 #else
151     Q_UNUSED(title);
152     Q_UNUSED(receiver);
153     Q_UNUSED(slot);
154     action = new QAction(this);
155 #endif
156     action->setCheckable(true);
157     return action;
158 }
159
160 #ifdef Q_WS_MAEMO_5
161
162 void ListWindow::closeEvent(QCloseEvent *event)
163 {
164     // Work around Maemo/Qt but: Menu items are not removed on close
165     menuBar()->clear();
166     event->accept();
167 }
168
169 #endif // Q_WS_MAEMO_5
170
171 void ListWindow::onSelectionChanged(const QItemSelection &selected,
172                                     const QItemSelection &deselected)
173 {
174     Q_UNUSED(selected);
175     Q_UNUSED(deselected);
176 #ifndef Q_WS_MAEMO_5
177     activateItemButtons();
178 #endif
179 }
180
181 #ifndef Q_WS_MAEMO_5
182
183 void ListWindow::activateItemButtons()
184 {
185     bool enable = false;
186     if (list) {
187         enable = list->selectionModel()->hasSelection();
188     }
189     foreach (QPushButton *button, itemButtons) {
190         button->setEnabled(enable);
191     }
192 }
193
194 #endif // ! Q_WS_MAEMO_5
195
196 #ifdef Q_WS_MAEMO_5
197
198 bool ListWindow::eventFilter(QObject *obj, QEvent *event)
199 {
200     if (event->type() == QEvent::ContextMenu) {
201         qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu";
202         if (popup->actions().size()) {
203             QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
204             QPoint pos = mouseEvent->globalPos();
205             pos.setX(pos.x() - 150);
206             if (pos.x() < 0) {
207                 pos.setX(0);
208             }
209             popup->exec(pos);
210         }
211         return true;
212     } else {
213         return QObject::eventFilter(obj, event);
214     }
215 }
216
217 void ListWindow::onModelChanged()
218 {
219     list->setMinimumHeight(list->contentsHeight());
220 }
221
222 #endif // Q_WS_MAEMO_5
223
224 #ifdef Q_OS_SYMBIAN
225
226 void ListWindow::show()
227 {
228     foreach (QWidget *w, QApplication::allWidgets()) {
229         w->setContextMenuPolicy(Qt::NoContextMenu);
230     }
231     showMaximized();
232 }
233
234 #endif // Q_OS_SYMBIAN