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