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 #ifndef Q_OS_SYMBIAN
35     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
36     contentLayout->addWidget(buttonBox);
37 #endif // Q_OS_SYMBIAN
38 #endif // Q_WS_MAEMO_5
39 }
40
41 void ListWindow::addList(ListView *listView)
42 {
43     Trace t("ListWindow::addList");
44     list = listView;
45 #ifdef Q_WS_MAEMO_5
46     list->installEventFilter(this);
47     list->setMinimumHeight(list->contentsHeight());
48     contentLayout->addWidget(list);
49     connect(list->model(),
50             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
51             this, SLOT(onModelChanged()));
52     connect(list->model(),
53             SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
54             this, SLOT(onModelChanged()));
55 #else
56     contentLayout->insertWidget(0, list);
57 #endif
58     connect(list->selectionModel(),
59       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
60       this,
61       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
62 }
63
64 void ListWindow::addAction(const QString &title, QObject *receiver,
65                            const char *slot, const QString &iconPath,
66                            QDialogButtonBox::ButtonRole role)
67 {
68     Trace t("ListWindow::addAction");
69 #ifdef Q_WS_MAEMO_5
70     Q_UNUSED(role);
71     QPushButton *button = new QPushButton(QIcon(iconPath), title, this);
72     contentLayout->addWidget(button);
73     connect(button, SIGNAL(clicked()), receiver, slot);
74 #elif defined(Q_OS_SYMBIAN)
75     Q_UNUSED(role);
76     QAction *action = new QAction(title, this);
77     connect(action, SIGNAL(triggered()), receiver, slot);
78     action->setSoftKeyRole(QAction::PositiveSoftKey);
79     menuBar()->addAction(action);
80 #else
81     Q_UNUSED(iconPath);
82     QPushButton *button = buttonBox->addButton(title, role);
83     connect(button, SIGNAL(clicked()), receiver, slot);
84 #endif // Q_WS_MAEMO_5
85 }
86
87 void ListWindow::addItemAction(const QString &title, QObject *receiver,
88                                const char *slot)
89 {
90     Trace t("ListWindow::addItemAction");
91 #ifdef Q_WS_MAEMO
92     popup->addAction(title, receiver, slot);
93 #elif defined Q_OS_SYMBIAN
94     QAction *action = new QAction(title, this);
95     connect(action, SIGNAL(triggered()), receiver, slot);
96     action->setSoftKeyRole(QAction::PositiveSoftKey);
97     menuBar()->addAction(action);
98     // FIXME: Add action to the list of item specific actions
99 #else
100     QPushButton *button =
101             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
102     connect(button, SIGNAL(clicked()), receiver, slot);
103     itemButtons.append(button);
104     activateItemButtons();
105 #endif // Q_WS_MAEMO_5
106 }
107
108 #ifdef Q_WS_MAEMO_5
109
110 void ListWindow::closeEvent(QCloseEvent *event)
111 {
112     // Work around Maemo/Qt but: Menu items are not removed on close
113     menuBar()->clear();
114     event->accept();
115 }
116
117 #endif // Q_WS_MAEMO_5
118
119 void ListWindow::onSelectionChanged(const QItemSelection &selected,
120                                     const QItemSelection &deselected)
121 {
122     Q_UNUSED(selected);
123     Q_UNUSED(deselected);
124 #ifndef Q_WS_MAEMO_5
125     activateItemButtons();
126 #endif
127 }
128
129 #ifndef Q_WS_MAEMO_5
130
131 void ListWindow::activateItemButtons()
132 {
133     bool enable = false;
134     if (list) {
135         enable = list->selectionModel()->hasSelection();
136     }
137     foreach (QPushButton *button, itemButtons) {
138         button->setEnabled(enable);
139     }
140 }
141
142 #endif // ! Q_WS_MAEMO_5
143
144 #ifdef Q_WS_MAEMO_5
145
146 bool ListWindow::eventFilter(QObject *obj, QEvent *event)
147 {
148     if (event->type() == QEvent::ContextMenu) {
149         qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu";
150         if (popup->actions().size()) {
151             QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
152             QPoint pos = mouseEvent->globalPos();
153             pos.setX(pos.x() - 150);
154             if (pos.x() < 0) {
155                 pos.setX(0);
156             }
157             popup->exec(pos);
158         }
159         return true;
160     } else {
161         return QObject::eventFilter(obj, event);
162     }
163 }
164
165 void ListWindow::onModelChanged()
166 {
167     qDebug() << "ListWindow::onModelChanged";
168     list->setMinimumHeight(list->contentsHeight());
169 }
170
171 #endif // Q_WS_MAEMO_5
172
173 #ifdef Q_OS_SYMBIAN
174
175 void ListWindow::show()
176 {
177     showMaximized();
178 }
179
180 #endif // Q_OS_SYMBIAN